Artifact 729795f15380a02471fc2701e7c969a33671e2595ae5138fd3acf470f72176f5:
- File
internal/sync/serve_http.go
— part of check-in
[9594cf3bae]
at
2026-10-29 03:06:23
on branch main
— perf(wasm): add a libfossil_nohttp build tag that leaves out net/http
Under TinyGo, importing net/http at all links its TLS and X.509 code (crypto/x509, encoding/asn1, math/big's integers, AES-GCM, SHA-3, netip) whether or not a request is ever made: a five-line program that calls only http.StatusText grows by about 430 KB raw. The library imports it for its HTTP client transport and its HTTP server, which a program that serves sync through HandleSyncWithOpts and supplies its own transport never calls.
Built with -tags libfossil_nohttp, the library leaves out NewHTTPTransport, WithHTTPClient, Repo.Pull, Repo.XferHandler and Repo.ServeHTTP, and the internal/sync client, server, redirect handling and peer-address trace they use. HandleSync, HandleSyncWithOpts, Sync over a host Transport and everything else are unchanged. Without the tag nothing changes. The retry classifier keeps the two statuses it read from net/http as constants, and DNS fault classification moves to its own file so it stays in the tagged build.
For the html-artifact-publisher engine (TinyGo 0.43.0, Go 1.26.0) the tag takes the gzipped binary from 1,100,251 to 1,003,279 bytes. A test builds the library, db and simio with the tag for the host and js/wasm and checks none links net/http, net/http/httptrace or crypto/tls, and that the untagged library still does. (user: [email protected], size: 5705) [annotate] [blame] [check-ins using] [more...]
//go:build !libfossil_nohttp package sync // This file holds the net/http sync server. A build with the libfossil_nohttp tag // leaves it out, and with it net/http, for a host that supplies its own // transport (docs: Install, TinyGo). import ( "context" "fmt" "io" "log/slog" "net" "net/http" "strconv" "github.com/danmestas/go-libfossil/internal/repo" "github.com/danmestas/go-libfossil/internal/xfer" ) // maxRequestBodyBytes caps the size of incoming xfer request bodies. // Fossil sync payloads are typically under 1MB; 50MB is generous headroom. const maxRequestBodyBytes = 50 * 1024 * 1024 // ServeHTTP starts an HTTP server that accepts Fossil xfer requests. // Blocks until ctx is cancelled. Stock fossil clone/sync can connect. func ServeHTTP(ctx context.Context, addr string, r *repo.Repo, h HandleFunc) error { if addr == "" { panic("sync.ServeHTTP: addr must not be empty") } if r == nil { panic("sync.ServeHTTP: r must not be nil") } if h == nil { panic("sync.ServeHTTP: h must not be nil") } mux := http.NewServeMux() mux.HandleFunc("/", xferHandler(r, h)) srv := &http.Server{ Handler: mux, } ln, err := net.Listen("tcp", addr) if err != nil { return fmt.Errorf("sync.ServeHTTP: listen: %w", err) } go func() { <-ctx.Done() srv.Close() }() err = srv.Serve(ln) if err == http.ErrServerClosed { return nil } return err } // XferHandler returns an http.HandlerFunc that decodes Fossil xfer requests, // dispatches to the HandleFunc, and encodes the response. Use this to compose // a custom mux when you need additional routes (e.g. /healthz) alongside xfer. func XferHandler(r *repo.Repo, h HandleFunc) http.HandlerFunc { if r == nil { panic("sync.XferHandler: r must not be nil") } if h == nil { panic("sync.XferHandler: h must not be nil") } return xferHandler(r, h) } // xferHandler is the unexported implementation. func xferHandler(r *repo.Repo, h HandleFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { // Fossil sends GET as a server probe — respond with a basic page. if req.Method != http.MethodPost { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "<html><body><h1>Fossil Sync Server</h1></body></html>") return } // Bound request body size to prevent resource exhaustion. req.Body = http.MaxBytesReader(w, req.Body, maxRequestBodyBytes) body, err := io.ReadAll(req.Body) if err != nil { http.Error(w, "read body: "+err.Error(), http.StatusBadRequest) return } if len(body) == 0 { // Empty POST — respond with empty xfer message. writeXferResponse(w, &xfer.Message{}) return } // §4: select framing from the request's Content-Type rather than by // trial. A canonical or libfossil client sends the compressed container // as application/x-fossil. A type we do not recognise carries no framing // signal at all, so decoding it either way is a guess -- reject it rather // than feed possibly-compressed bytes to the card parser (issue #104). ct := req.Header.Get("Content-Type") if !xfer.KnownContentType(ct) { slog.Error("serve-http: unsupported content type", "content-type", ct) http.Error(w, fmt.Sprintf("unsupported content type %q: expected %s, %s or %s", ct, xfer.ContentTypeCompressed, xfer.ContentTypeUncompressed, xfer.ContentTypeDebug), http.StatusUnsupportedMediaType) return } msg, err := xfer.DecodeRequest(body, ct, loginCookie(req)) if err != nil { slog.Error("serve-http: decode failed", "bytes", len(body), "err", err) http.Error(w, fmt.Sprintf("decode xfer (%d bytes): %v", len(body), err), http.StatusBadRequest) return } resp, err := h(WithRemoteAddr(req.Context(), req.RemoteAddr), r, msg) if err != nil { http.Error(w, "handler: "+err.Error(), http.StatusInternalServerError) return } writeXferResponse(w, resp) } } // loginCookie returns the value of the request's login cookie (§6.5), or "" // when it has none. func loginCookie(req *http.Request) string { c, err := req.Cookie(xfer.LoginCookieName) if err != nil { return "" } return c.Value } // writeXferResponse encodes an xfer message and writes it as the HTTP response. // // The full response is already materialised by Encode before any write, so the // exact body length is known for free. Setting Content-Length explicitly keeps // net/http from falling back to Transfer-Encoding: chunked once the buffered // body exceeds its internal ~2KB flush threshold. Release-tagged fossil <=2.23 // reads the reply length only from Content-Length (src/http.c) and aborts with // "server did not reply" on a chunked reply above ~256KB (issue #101). // Buffering is not introduced here -- Encode already holds the whole slice, so // #88's per-clone bound (DefaultCloneBatchBytes) still caps the response. func writeXferResponse(w http.ResponseWriter, msg *xfer.Message) { if w == nil { panic("writeXferResponse: w must not be nil") } if msg == nil { panic("writeXferResponse: msg must not be nil") } encode, contentType := msg.Encode, xfer.ContentTypeCompressed if msg.Uncompressed { encode, contentType = msg.EncodeUncompressed, xfer.ContentTypeUncompressed } respBytes, err := encode() if err != nil { http.Error(w, "encode response: "+err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) n, err := w.Write(respBytes) if err != nil { slog.Error("serve-http: write response failed", "err", err) return } if n != len(respBytes) { panic(fmt.Sprintf("writeXferResponse: short write %d of %d bytes", n, len(respBytes))) } }