headerorder

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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.

View Source
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

func AppendLower(dst []byte, s string) ([]byte, bool)

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

func Crumbs(value string) []string

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 Framing added in v0.2.0

func Framing(name string) bool

Framing reports a header name WithoutFraming drops, in any case.

func StatusLine

func StatusLine(status string, code int) string

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

func WithPlacements(ctx context.Context, ps []Placement) context.Context

WithPlacements returns ctx carrying ps, applied in order to the request's headers once they are in the profile's order.

func WithPlan

func WithPlan(ctx context.Context, p *Plan) context.Context

WithPlan returns ctx carrying p.

func WithoutFraming added in v0.2.0

func WithoutFraming(h http.Header) http.Header

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.

func (*Canon) Key

func (c *Canon) Key(name string) string

Key returns http.CanonicalHeaderKey(name).

type Field

type Field struct {
	Name  string
	Value string
}

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) []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).

h is only read, so a caller need not copy it to change it: sets do.

func Place

func Place(fields []Field, ps []Placement) ([]Field, error)

Place applies ps to fields, in their final order, one by one: each field named p.Header moves, keeping its order, right after the last p.Anchor (After) or before the first. A missing header or anchor is an ErrPlacement. fields' array is reused. See decision 0030.

func ToEnd added in v0.3.0

func ToEnd(fields []Field, name string) []Field

ToEnd moves the field named name after the others, before a te that ends fields (Firefox's content-length: 0, profile.EmptyBody.Last). fields' array is reused.

type Placement

type Placement struct {
	Header string
	Anchor string
	After  bool
}

Placement moves Header to directly after (After) or before Anchor. Both names are lowercase.

func PlacementsFrom

func PlacementsFrom(ctx context.Context) []Placement

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
	// 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.

func PlanFrom

func PlanFrom(ctx context.Context) *Plan

PlanFrom returns the plan ctx carries, or nil: the profile's navigation.

type Replacement

type Replacement struct {
	Name   string
	List   string
	Values []string
}

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.

func Replaced

func Replaced(prof []profile.Header, h http.Header) (replaced []Replacement, dropped []string)

Replaced reports, by Build's rules, which of prof's valued headers h replaces or suppresses and, when h sets Key, which it drops. Both in prof's order, names 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) Add

func (r *ResponseHeader) Add(name, value string)

Add adds a field.

func (*ResponseHeader) Header

func (r *ResponseHeader) Header() http.Header

Header returns the header the fields made.

type Set

type Set struct {
	Name  string
	Value string
}

Set gives a header Build sends in place of the request's own, whatever the request's spelling of it: Value, or nothing when Value is empty, which also suppresses the profile's default. Name is lowercase.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL