Documentation
¶
Overview ¶
Package headerorder merges a request's headers with a profile's defaults and puts them in the profile's order. HTTP/1.1, HTTP/2 and HTTP/3 use it; it also builds responses' header maps (response.go), HTTP/2's now.
Index ¶
- Constants
- Variables
- func AppendLower(dst []byte, s string) ([]byte, bool)
- func Crumbs(value string) []string
- func Framing(name string) bool
- func StatusLine(status string, code int) string
- func TableOrder(t *profile.HeaderTable, members, page []string) (order []string, ok bool)
- func WithPlacements(ctx context.Context, ps []Placement) context.Context
- func WithPlan(ctx context.Context, p *Plan) context.Context
- func WithoutFraming(h http.Header) http.Header
- type Canon
- type Field
- type Placement
- type Plan
- type Replacement
- type ResponseHeader
- type Set
Constants ¶
const Key = "Header-Order:"
Key is the request header naming, in order, the headers to send for one request. It replaces the profile's order: unnamed profile defaults are dropped, unnamed request headers go after. The trailing colon makes it an invalid header name, so it is never sent by mistake.
Variables ¶
var ErrNotMeasured = errors.New("mimicry: not measured for this profile")
ErrNotMeasured reports a request the profile cannot describe. The root package exports it as mimicry.ErrNotMeasured; it lives here so the connections can return it too.
var ErrPlacement = errors.New("mimicry: header placement")
ErrPlacement reports a header placement that cannot be applied. The root package exports it as mimicry.ErrPlacement; it lives here so the connections can return it too.
Functions ¶
func AppendLower ¶
AppendLower appends s lowercased to dst, and reports whether s is ASCII: otherwise the result is not strings.ToLower(s) and the caller falls back to it.
func Crumbs ¶
Crumbs splits a Cookie header value into its cookies ("name=value"), in the order given: the jar has already put them in the browser's order, and a caller's own header is sent as written. Empty pieces are dropped.
func StatusLine ¶
StatusLine is http.Response's Status for a :status value and the code strconv.Atoi parsed it to: status, a space and http.StatusText(code). The common ones cost no allocation.
func TableOrder ¶
func TableOrder(t *profile.HeaderTable, members, page []string) (order []string, ok bool)
TableOrder returns the order browser table t sends a request's table headers in. members are the list's table names the request sends, page the page's own names in insertion order (sorted for fetch(), as set for XHR). ok is false when t cannot hold members in the list's order. See decision 0021.
func WithPlacements ¶
WithPlacements returns ctx carrying ps, applied in order to the request's headers once they are in the profile's order.
func WithoutFraming ¶ added in v0.2.0
WithoutFraming returns h without the headers that frame a message (Content-Length, Connection, Transfer-Encoding), in any spelling: the connection builds them from the request, as net/http does. It is h itself when h has none, else a copy.
Types ¶
type Canon ¶
type Canon struct {
// contains filtered or unexported fields
}
Canon is http.CanonicalHeaderKey with a cache for the names one connection's responses repeat, which spares allocating the names net/http does not intern (Alt-Svc, Vary, Access-Control-...). The zero value is ready; it is not safe for concurrent use.
type Field ¶
Field is one header to send. Name is lowercase.
func Build ¶
func Build(dst []Field, prof []profile.Header, h http.Header, sets []Set, skip func(name string) bool, table *profile.HeaderTable, ownRun bool) []Field
Build appends the headers to send for a request with headers h to dst and returns it, using the profile's headers for order and defaults:
- h's value wins over a profile default; a header in h with no values suppresses the default;
- sets win over h;
- order is the profile's, or Key's (dropping unnamed defaults); unlisted headers go at profile.OtherHeaders, else in table order (TableOrder), else after the rest, sorted by name;
- names compare case-insensitively;
- skip names headers never to send (connection-level ones);
- with ownRun, table orders the list's members even when h adds no header of its own (Plan.OwnRun).
h is only read, so a caller need not copy it to change it: sets do.
type Placement ¶
Placement moves Header to directly after (After) or before Anchor. Both names are lowercase.
func PlacementsFrom ¶
PlacementsFrom returns the placements ctx carries, or nil.
type Plan ¶
type Plan struct {
// Kind is the request's kind in the profile (Profile.Kinds); empty
// for the navigation and for Exact requests.
Kind string
// Headers is the HTTP/2 list (lowercase names); HTTP1 the HTTP/1.1 one,
// named as sent, or nil to write Headers after Host and Connection.
// Both empty sends only the request's own headers.
Headers []profile.Header
HTTP1 []profile.Header
// NoHTTP1: the request's lists were measured over HTTP/2 only, so an
// HTTP/1.1 connection must refuse it (ErrNotMeasured) rather than
// guess the order.
NoHTTP1 bool
// HTTP3Only: part of the request (its fetchpriority's priority header)
// was measured over HTTP/3 only, so a TCP connection must refuse it
// (ErrNotMeasured).
HTTP3Only bool
// Weight is the HEADERS priority weight; zero keeps the profile's.
Weight uint8
// Table is the browser's header table for the page's own headers
// (fetch() and XHR); nil places them as the lists say.
Table *profile.HeaderTable
// OwnRun orders the list's table members by Table even when the page
// set no header of its own: a request carrying the client hints its
// page opted in to, which the list holds in the order they enter the
// table (decision 0055).
OwnRun bool
// LowerOwn sends the headers the lists do not name lowercase over
// HTTP/1.1 (fetch()), not as the caller spelled them.
LowerOwn bool
// NoContentLength sends a body over HTTP/2 and HTTP/3 without content-length:
// the request's measured list has none (Safari's redirected bodies
// after a preflight).
NoContentLength bool
// ZeroContentLength sends a request without a body over HTTP/2 and
// HTTP/3 with content-length: 0, in the list's place for it or, with
// ZeroContentLengthLast, after its other headers (before a final te);
// EmptyData then ends its HTTP/2 stream with an empty DATA frame
// rather than on HEADERS (profile.EmptyBody).
ZeroContentLength bool
ZeroContentLengthLast bool
EmptyData bool
}
Plan is how to send one request other than as the profile's navigation: its header lists, with request-dependent slots already filled, and its HEADERS weight. The connections use it in place of the profile's lists.
type Replacement ¶
Replacement is a list header whose value Build takes from the request's headers instead: the list's value, and the request's (none: suppressed). Name is lowercase.
type ResponseHeader ¶
type ResponseHeader struct {
// contains filtered or unexported fields
}
ResponseHeader collects a response's fields into an http.Header, as net/http's HTTP/2 client does: one array holds a value for every field, and each name's slice is capped at its first value, so that a second one (or the caller's append) copies instead of overwriting the next name's.
func NewResponseHeader ¶
func NewResponseHeader(canon *Canon, n int) ResponseHeader
NewResponseHeader starts a header for n fields (an upper bound), their names canonicalized by canon.
func (*ResponseHeader) Header ¶
func (r *ResponseHeader) Header() http.Header
Header returns the header the fields made.