mirror of
https://github.com/soypat/lneto.git
synced 2026-08-08 08:53: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
107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
//go:build !tinygo && linux
|
|
|
|
package main
|
|
|
|
import (
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/soypat/lneto/http/httphi"
|
|
)
|
|
|
|
const (
|
|
kB = 1 << 10
|
|
listenPort = 8080
|
|
bufferSizes = 2 * kB
|
|
// A browser sends around twenty header fields; a request carrying more
|
|
// than this is answered 431 rather than parsed into memory it was not
|
|
// given. Each field costs 8 bytes of table.
|
|
numHeaderFields = 32
|
|
numGoroutines = 4
|
|
readTimeout = 2 * time.Second
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
println("Error:", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
println("DONE")
|
|
}
|
|
|
|
func run() error {
|
|
ln, err := net.Listen("tcp", ":"+strconv.Itoa(listenPort))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer ln.Close()
|
|
print("listening on http://localhost:", listenPort, "\n")
|
|
|
|
var mux httphi.MuxSlice
|
|
mux.Handle("GET /", homepage)
|
|
|
|
var router httphi.Router
|
|
err = router.Configure(httphi.RouterConfig{
|
|
FixedNumGoroutines: numGoroutines,
|
|
RequestHeaderBufferSize: bufferSizes,
|
|
RequestNumHeaderKVCap: numHeaderFields,
|
|
ResponseHeaderMinBufferSize: bufferSizes,
|
|
MaxAwaitingConns: 256,
|
|
Mux: &mux,
|
|
Logger: slog.Default(),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer router.Shutdown()
|
|
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The connection owns the idle policy: a peer that opens a socket and
|
|
// then stalls fails its read instead of holding a router goroutine.
|
|
conn.SetReadDeadline(time.Now().Add(readTimeout))
|
|
err = router.Handle(conn)
|
|
if err != nil {
|
|
// Every goroutine is busy and the queue is full. Dropping the
|
|
// connection is the backpressure: memory stays bounded.
|
|
slog.Warn("dropped connection", slog.String("remote", conn.RemoteAddr().String()), slog.String("err", err.Error()))
|
|
conn.Close()
|
|
}
|
|
}
|
|
}
|
|
|
|
const (
|
|
htmlHead = `<html><body bgcolor="#000080" text="#00FF00"><center>` +
|
|
`<marquee><font face="Comic Sans MS" size="5" color="#FFFF00">` +
|
|
`*** WELCOME TO MY HOMEPAGE ***</font></marquee>` +
|
|
`<h1><blink>YOU ARE VISITOR #`
|
|
htmlTail = `!</blink></h1>` +
|
|
`<font color="#FF00FF">Sign my guestbook!</font>` +
|
|
`<br><hr>Best viewed in Netscape Navigator</center></body></html>`
|
|
// maxPage bounds the rendered page: both halves plus the visitor number.
|
|
maxPage = len(htmlHead) + 20 + len(htmlTail)
|
|
)
|
|
|
|
// visits counts served requests. Handlers run on the router's goroutines, so
|
|
// every visitor gets their own number.
|
|
var visits atomic.Uint64
|
|
|
|
func homepage(exch *httphi.Exchange) {
|
|
var page [maxPage]byte
|
|
n := copy(page[:], htmlHead)
|
|
n += len(strconv.AppendUint(page[n:n], visits.Add(1), 10))
|
|
n += copy(page[n:], htmlTail)
|
|
|
|
exch.StageHeader("Content-Type", "text/html")
|
|
exch.StageHeaderInt("Content-Length", int64(n), 10)
|
|
exch.WriteHeader(int(httphi.StatusOK))
|
|
exch.WriteBody(page[:n])
|
|
}
|