mirror of
https://github.com/soypat/lneto.git
synced 2026-08-07 16:33:40 +00:00
5c54030f19
* add http/httphi * begin adding httphi tests * claude found neat bugs * add low level Handle function and more tests * more tests, run go generate * add Hijacker-like functionality * improve locking and acquisition of Exchanges in reconfiguring * several bugfixes, add internal.IntLen, round up http-linux example with new router API * small nit * add benchmarks * add query handling * remove ForEach pattern, allocates in TinyGo * massive documentation push and code reordering in files * Router.Handle returns error after being torn down * run go fix * rework Mux interface to receive a string request path * add MethodFrom * minor doc nit * fail on incomplete staging * add raw buffer access * add streaming API distinct from Exchange * begin adding multipart form logic * finish rounding up multipart form parsing * remove status type * first Multipart approach * begin adding readMultiPart * add Exchange.ReadMultiparts reimagining of clanker slop * ai insists with backoffs * simplify clanker slop * apply go fix * add a pattern argument to Mux * explicit header key/value alloc and add ExchangeConfig * fix tests after excplicit header alloc change * fix examples * run go fix * expose rawsock as experimental package (will use for external benchmarks) * remove backoff from form parsing * @MDr164 suggestions get potential fixes * apply go fix * add examples * add README.md * fix rawsock tinygo implementation * apply @MDr164 various fixes * update documentation on ContentLength methods and fix bug in Form reset on empty body * fix tests * add fuzz tests * run go fix * io.ErrNoProgress on parsing form spin * run go fix * remove backoff assumption from Router * httphi.Handle rejects unsupported protocols * go format router.go * add kvbuffer * rewrite Cookie with KVBuffer * mid refactor of KVBuffer into Header * work on KVBuffer exhausted semantics * add Go's ServeMux Request.PathValue access semantics to Exchange, Mux and MuxSlice * add PathValue example * document all the things; improve req Query semantics; add Form.EnableBufferGrowth * unexport kvBuffer * add Exchange.PathValueAppend * use stdlib in example instead of rawsock * remove rawsock from http example * add darwin arch rawsock * fix example * rename Router.TeardownGoroutines to Shutdown matching http.Server.Shutdown * rename types and identifiers * @MDr164 Content-Type and Transfer-Encoding bug catches
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package httphi_test
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"log/slog"
|
|
"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() {
|
|
// Chrome tends to send ~700 bytes on a typical landing page request.
|
|
const requestBuffer = 1024
|
|
const numHeaderKV = requestBuffer / 32 //
|
|
var mux httphi.MuxSlice
|
|
mux.Handle("GET /", func(ex *httphi.Exchange) {
|
|
ex.WriteBody([]byte("hello world"))
|
|
})
|
|
var router httphi.Router
|
|
err := router.Configure(httphi.RouterConfig{
|
|
FixedNumGoroutines: -1, // Unbounded goroutines and allocations.
|
|
RequestHeaderBufferSize: requestBuffer,
|
|
ResponseHeaderMinBufferSize: 32, // Shared buffer with Request, not strictly necessary, especially if not sending headers.
|
|
RequestNumHeaderKVCap: numHeaderKV,
|
|
NormalizeOutgoingKeys: true,
|
|
Mux: &mux,
|
|
Logger: slog.Default(),
|
|
})
|
|
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.
|
|
formbuf := make([]byte, 1024)
|
|
var form httpraw.Form
|
|
err := ex.RequestParseForm(&form, formbuf)
|
|
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
|
|
}
|
|
})
|
|
|
|
}
|