Artifact 90ae8eef47d7323f8a49cd16139a6aafeb5d55606df916931846651dc4aec447:
- File
transport_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: 3844) [annotate] [blame] [check-ins using] [more...]
//go:build !libfossil_nohttp package libfossil // This file holds the net/http sync client. 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" "errors" "fmt" "net/http" "net/url" internalsync "github.com/danmestas/go-libfossil/internal/sync" "github.com/danmestas/go-libfossil/internal/xfer" ) // NewHTTPTransport creates a Transport that speaks Fossil's HTTP /xfer protocol. func NewHTTPTransport(url string, opts ...HTTPOption) Transport { t := &httpTransport{url: url, client: http.DefaultClient} for _, o := range opts { o(t) } return t } // HTTPOption configures an HTTP transport. type HTTPOption func(*httpTransport) // WithHTTPClient sets a custom http.Client for the transport. func WithHTTPClient(c *http.Client) HTTPOption { return func(t *httpTransport) { t.client = c } } type httpTransport struct { url string client *http.Client peer internalsync.PeerAddr } // SyncOrigin names the peer this transport addresses, for state a sync keeps // per origin (the horizon cursor of draft-fossil-horizon-00 §6.2.6): its URL // with any password removed, as last-sync-url keeps it, so no credential is // written into the repository. func (t *httpTransport) SyncOrigin() string { u, err := url.Parse(t.url) if err != nil { return "" } if u.User != nil { u.User = url.User(u.User.Username()) } return u.String() } // PeerAddr returns the IP address of the server the last round trip reached. func (t *httpTransport) PeerAddr() string { return t.peer.Get() } func (t *httpTransport) RoundTrip(ctx context.Context, payload []byte) ([]byte, error) { body, _, err := t.roundTrip(ctx, payload, "") return body, err } // RoundTripFramed implements FramedTransport: it returns the reply body along // with the reply's Content-Type, which selects the §4 framing for decoding. A // peer that replies uncompressed (e.g. a real Fossil clone reply) is decoded // correctly instead of being force-read as the compressed container. func (t *httpTransport) RoundTripFramed(ctx context.Context, payload []byte) ([]byte, string, error) { return t.roundTrip(ctx, payload, "") } func (t *httpTransport) roundTripLoginCookie(ctx context.Context, payload []byte, cookie string) ([]byte, string, error) { return t.roundTrip(ctx, payload, cookie) } func (t *httpTransport) roundTrip(ctx context.Context, payload []byte, cookie string) ([]byte, string, error) { resp, err := internalsync.PostXfer(t.peer.Trace(ctx), t.client, t.url, "application/x-fossil", cookie, payload) if err != nil { return nil, "", fmt.Errorf("libfossil: http transport: %w", err) } defer resp.Body.Close() // Only 200 is a sync reply (§4-7). Any other status, a 2xx included, is not // the protocol answering. Reporting the status directly keeps a peer's // outage from surfacing as a framing defect, and lets the sync round loop // tell a 5xx worth retrying from a 4xx that never will be. if resp.StatusCode != http.StatusOK { return nil, "", &internalsync.StatusError{Code: resp.StatusCode, Status: resp.Status} } body, err := xfer.ReadBody(resp.Body) if errors.Is(err, xfer.ErrBodyTooLarge) { // The reply is the peer's, and resending gets the same one. return nil, "", err } if err != nil { // The reply was cut short on the wire; nothing has been decoded yet, // so the round loop may safely send this request again. Tagging it // here is what distinguishes a severed connection from a body that // arrived whole and would not decode — both surface as // io.ErrUnexpectedEOF, and only the former is worth retrying. return nil, "", internalsync.RetryableFault( fmt.Errorf("libfossil: http transport read: %w", err)) } return body, resp.Header.Get("Content-Type"), nil }