mirror of
https://github.com/soypat/lneto.git
synced 2026-08-05 23:43:40 +00:00
d05cd14018
* httphi: RouterConfig refactor to enable DefaultRouterConfig * RequestHeader not case sensitive anymore * httphi: use DefaultRouterConfig in examples * httphi: remove gated stage complexity Misusing Stage methods by calling them once header has been written is totally harmless as far as I can tell. We simplify the codebase on this occasion by removing the headerWritten check for all stage methods * httphi: improve APIs * httphi: remove status
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package httphi_test
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
|
|
"github.com/soypat/lneto/http/httphi"
|
|
"github.com/soypat/lneto/http/httpraw"
|
|
)
|
|
|
|
// ExampleRouter_linux goes over how to setup a linux server using raw linux connections.
|
|
// See [ExampleMuxSlice_query_forms_multipart] on how to define handlers for common HTTP processing.
|
|
func ExampleRouter() {
|
|
const numWorkers = 8
|
|
const memoryPerConn = 2048
|
|
var mux httphi.MuxSlice
|
|
mux.Handle("GET /", func(ex *httphi.Exchange) {
|
|
ex.WriteBody([]byte("hello world"))
|
|
})
|
|
var router httphi.Router
|
|
cfg := httphi.DefaultRouterConfig(numWorkers, memoryPerConn, mux.MaxPathValues())
|
|
err := router.Configure(&mux, cfg)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
const port = ":8080"
|
|
listener, err := net.Listen("tcp", port)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("server up at http://localhost%s", port)
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
err = router.Handle(conn)
|
|
if err != nil {
|
|
log.Println("httphi.Router failed to handle connection:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ExampleMuxSlice_query_forms_multipart goes over how to define Handlers (HandleFunc).
|
|
// See [ExampleRouter_linux] for how to setup the server.
|
|
func ExampleMuxSlice_query_forms_multipart() {
|
|
|
|
var mux httphi.MuxSlice
|
|
|
|
mux.Handle("/users/{id}", func(ex *httphi.Exchange) {
|
|
userID := ex.PathValue("id")
|
|
fmt.Printf("someone requested data for user %s\n", userID)
|
|
})
|
|
|
|
mux.Handle("/query", func(ex *httphi.Exchange) {
|
|
// query parameter in URL.
|
|
const decodeQuery = true
|
|
const queryKey = "search"
|
|
valueRaw, present := ex.RequestQueryValue(queryKey)
|
|
if !present {
|
|
return
|
|
}
|
|
valueDecoded, present := ex.RequestQueryAppend(nil, queryKey, decodeQuery)
|
|
fmt.Printf("got query=%v %s=%s (raw:%s)\n", present, queryKey, valueDecoded, valueRaw)
|
|
})
|
|
|
|
mux.Handle("GET /form", func(ex *httphi.Exchange) {
|
|
// Request Body Form. The form owns the memory: hand it a buffer and
|
|
// forbid growth to bound what a request may spend.
|
|
var form httpraw.Form
|
|
form.Reset(make([]byte, 0, 1024), 8) // Room for 8 pairs.
|
|
form.EnableBufferGrowth(false)
|
|
err := ex.RequestParseForm(&form, false, false)
|
|
if err != nil {
|
|
ex.WriteHeader(httphi.StatusInternalServerError)
|
|
return
|
|
}
|
|
for i := range form.Len() {
|
|
k, v := form.Pair(i)
|
|
fmt.Printf("received form value %d: %s=%s\n", i, k, v)
|
|
}
|
|
})
|
|
|
|
mux.Handle("GET /file-upload", func(ex *httphi.Exchange) {
|
|
// File upload directly onto server using multipart.
|
|
formbuf := make([]byte, 1024)
|
|
_, err := ex.ReadMultiparts(nil, formbuf, func(hdr *httpraw.MultipartHeader) io.WriteCloser {
|
|
fp, err := os.Create(string(hdr.Filename))
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return fp // Will write full file contents to fp.
|
|
})
|
|
if err != nil {
|
|
ex.WriteHeader(httphi.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
}
|