headers

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package headers is all about HTTP header names.

Index

Constants

View Source
const (
	// common request headers
	Origin = "Origin"

	// preflight-only request headers
	ACRPN = "Access-Control-Request-Private-Network"
	ACRM  = "Access-Control-Request-Method"
	ACRH  = "Access-Control-Request-Headers"

	// common response headers
	ACAO = "Access-Control-Allow-Origin"
	ACAC = "Access-Control-Allow-Credentials"

	// preflight-only response headers
	ACAPN = "Access-Control-Allow-Private-Network"
	ACAM  = "Access-Control-Allow-Methods"
	ACAH  = "Access-Control-Allow-Headers"
	ACMA  = "Access-Control-Max-Age"

	// actual-only response headers
	ACEH = "Access-Control-Expose-Headers"

	Vary = "Vary"
)

header names in canonical format

View Source
const (
	ValueTrue        = "true"
	ValueWildcard    = "*"
	ValueVaryOptions = ACRH + ", " + ACRM + ", " + ACRPN + ", " + Origin
)
View Source
const Authorization = "authorization" // note: byte-lowercase
View Source
const ValueSep = ","

Variables

View Source
var (
	PreflightVarySgl = []string{ValueVaryOptions}
	TrueSgl          = []string{ValueTrue}
	OriginSgl        = []string{Origin}
	WildcardSgl      = []string{ValueWildcard}
	WildcardAuthSgl  = []string{ValueWildcard + ValueSep + Authorization}
)

Functions

func First

func First(hdrs http.Header, k string) (string, []string, bool)

First, if k is present in hdrs, returns the value associated to k in hdrs, a singleton slice containing that value, and true; otherwise, First returns "", nil, false. Precondition: k is in canonical format (see http.CanonicalHeaderKey).

First is useful because

  • contrary to http.Header.Get, it returns a slice that can be reused, which saves a heap allocation in client code;
  • it returns the value both as a scalar and as a singleton slice, which saves a bounds check in client code.

func IsForbiddenRequestHeaderName

func IsForbiddenRequestHeaderName(name string) bool

IsForbiddenRequestHeaderName reports whether name is a forbidden request-header name per the Fetch standard.

Precondition: name is a valid and byte-lowercase header name.

func IsForbiddenResponseHeaderName

func IsForbiddenResponseHeaderName(name string) bool

IsForbiddenResponseHeaderName reports whether name is a forbidden response-header name per the Fetch standard.

Precondition: name is a valid and byte-lowercase header name.

func IsProhibitedRequestHeaderName

func IsProhibitedRequestHeaderName(name string) bool

IsProhibitedRequestHeaderName reports whether name is a prohibited request-header name. Attempts to allow such request headers almost always stem from some misunderstanding of CORS.

Precondition: name is a valid and byte-lowercase header name.

func IsProhibitedResponseHeaderName

func IsProhibitedResponseHeaderName(name string) bool

IsProhibitedResponseHeaderName reports whether name is a prohibited response-header name. Attempts to expose such response headers almost always stem from some misunderstanding of CORS.

Precondition: name is a valid and byte-lowercase header name.

func IsSafelistedResponseHeaderName

func IsSafelistedResponseHeaderName(name string) bool

IsSafelistedResponseHeaderName reports whether name is a safelisted response-header name per the Fetch standard.

Precondition: name is a valid and byte-lowercase header name.

func IsValid

func IsValid(name string) bool

IsValid reports whether name is a valid header name, per the Fetch standard.

Types

type SortedSet added in v0.1.3

type SortedSet struct {
	// contains filtered or unexported fields
}

A SortedSet represents a mathematical set of strings sorted in lexicographical order. Each element has a unique position ranging from 0 (inclusive) to the set's cardinality (exclusive). The zero value represents an empty set.

func NewSortedSet added in v0.1.3

func NewSortedSet(elems ...string) SortedSet

NewSortedSet returns a SortedSet that contains all of elems, but no other elements.

func (SortedSet) Size added in v0.1.3

func (set SortedSet) Size() int

Size returns the cardinality of set.

func (SortedSet) String added in v0.1.3

func (set SortedSet) String() string

String sorts joins the elements of set (in lexicographical order) with a comma and returns the resulting string.

func (SortedSet) Subsumes added in v0.1.3

func (set SortedSet) Subsumes(csv string) bool

Subsumes reports whether csv is a sequence of comma-separated names that are

  • all elements of set,
  • sorted in lexicographical order,
  • unique.

func (SortedSet) ToSortedSlice added in v0.2.0

func (set SortedSet) ToSortedSlice() []string

ToSortedSlice applies http.CanonicalHeaderKey to each element of s and returns a sorted slice containing the results.

Jump to

Keyboard shortcuts

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