Artifact 09b069935f28fd4f0ead23380591f35ef1763a5a5999e139552fce909bc0726b:
- File
internal/sync/redirect.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: 3934) [annotate] [blame] [check-ins using] [more...]
//go:build !libfossil_nohttp package sync // This file holds the net/http request path. 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 ( "bytes" "context" "errors" "fmt" "io" "net/http" "net/url" "strings" "github.com/danmestas/go-libfossil/internal/xfer" ) // maxRedirects is fossil's MAX_REDIRECTS (src/xfer.c). http_exchange // decrements it on each redirect and fails when it reaches zero, so 19 // redirects are followed and the twentieth is fatal (draft §4). const maxRedirects = 20 // ErrRedirectDowngrade reports a redirect from HTTPS to plain HTTP. Following // it would resend the request, login card included, in cleartext. var ErrRedirectDowngrade = errors.New("sync: cannot redirect from HTTPS to HTTP") // ErrRedirectLimit reports a twentieth redirect. var ErrRedirectLimit = errors.New("sync: redirect limit exceeded") // PostXfer POSTs body to target and follows redirects as fossil's // http_exchange does (src/http.c), not as net/http does. net/http turns a 301 // or 302 into a GET without the body, follows a downgrade from HTTPS to HTTP, // and stops after 10. Fossil re-POSTs the same body for 301, 302, 307 and // 308, strips a trailing /xfer from the new location, refuses a downgrade, // and fails on the twentieth redirect. // // The client's own redirect policy is replaced for these requests only, so // a caller's client is used as given but cannot loosen these rules. A redirect // without a Location is returned as is, for the caller to report its status. // // loginCookie, when not empty, goes in the x-f-l-c cookie of every request, // as fossil rebuilds a cookie login for each redirect target (§4, §6.5). func PostXfer(ctx context.Context, client *http.Client, target, contentType, loginCookie string, body []byte) (*http.Response, error) { if client == nil { panic("sync.PostXfer: client must not be nil") } c := *client c.CheckRedirect = func(*http.Request, []*http.Request) error { return errStopRedirect } for left := maxRedirects; ; { req, err := http.NewRequestWithContext(ctx, http.MethodPost, target, bytes.NewReader(body)) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) if loginCookie != "" { req.Header.Set("Cookie", xfer.LoginCookieName+"="+loginCookie) } resp, err := c.Do(req) if err != nil { return nil, err } loc := resp.Header.Get("Location") if !isFollowedRedirect(resp.StatusCode) || loc == "" { return resp, nil } // Drain a little so the connection can be reused, then let it go. io.Copy(io.Discard, io.LimitReader(resp.Body, 4096)) resp.Body.Close() left-- if left == 0 { return nil, ErrRedirectLimit } if target, err = redirectTarget(target, loc); err != nil { return nil, err } } } func isFollowedRedirect(code int) bool { switch code { case http.StatusMovedPermanently, http.StatusFound, http.StatusTemporaryRedirect, http.StatusPermanentRedirect: return true } return false } // redirectTarget resolves loc against from and applies fossil's rules to it. // Fossil strips one trailing /xfer, though draft §4 says "repeatedly": its // loop truncates the string at the match, after which the substring it // compares is four characters long and can never equal "/xfer" again. func redirectTarget(from, loc string) (string, error) { base, err := url.Parse(from) if err != nil { return "", fmt.Errorf("sync: redirect from %q: %w", from, err) } next, err := base.Parse(loc) if err != nil { return "", fmt.Errorf("sync: malformed redirect %q: %w", loc, err) } if next.Scheme != "http" && next.Scheme != "https" { return "", fmt.Errorf("sync: cannot redirect to %q", loc) } if base.Scheme == "https" && next.Scheme == "http" { return "", ErrRedirectDowngrade } return strings.TrimSuffix(next.String(), "/xfer"), nil }