header

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package header provides parsing rules for content negotiation & conditional requires headers according to RFC-7231 & RFC-7232.

For "Accept-Language", "Accept-Encoding" or "Accept-Charset" use the ParsePrecedenceValues function.

For "Accept" use the ParseMediaRanges function. This has more complex attributes and rules.

For "If-None-Match" use the ETagsOf function (also useful for "If-Match").

Accept

The Accept header is parsed using ParseMediaRanges(hdr), which returns the slice of media ranges, e.g.

// handle Accept-Language
mediaRanges := header.ParseMediaRanges("application/json;q=0.8, application/xml, application/*;q=0.1")

The resulting slice is ready-sorted according to precedence and quality rules, so in this example the order is {"application/xml", "application/json", "application/*"} because the middle item has an implied quality of 1, whereas the first item has a lower quality.

from https://tools.ietf.org/html/rfc7231#section-5.3.2:

The "Accept" header field can be used by user agents to specify response media types that are acceptable. Accept header fields can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.

A request without any Accept header field implies that the user agent will accept any media type in response.

If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response, or disregard the header field by treating the response as if it is not subject to content negotiation.

Example header

Accept: audio/*; q=0.2, audio/basic

Accept-Language

The other important content-negotiation headers, Accept-Language and Accept-Charset, are handled by the header.Parse method, e.g.

// handle Accept-Language
acceptLanguages := header.ParsePrecedenceValues("en-GB,fr;q=0.5,en;q=0.8")

This will contain {"en-GB", "en", "fr"} in a header.PrecedenceValues slice, sorted according to precedence rules with the most preferred first.

The acceptable.Parse function can be used for Accept-Encoding as well as Accept-Language and Accept-Charset. However, the Go standard library deals with Accept-Encoding, so you won't need to.

from https://tools.ietf.org/html/rfc7231#section-5.3.5:

The "Accept-Language" header field can be used by user agents to indicate the set of natural languages that are preferred in the response.

A request without any Accept-Language header field implies that the user agent will accept any language in response.

If the header field is present in a request and none of the available representations for the response have a matching language tag, the origin server can either disregard the header field by treating the response as if it is not subject to content negotiation or honor the header field by sending a 406 (Not Acceptable) response. However, the latter is not encouraged, as doing so can prevent users from accessing content that they might be able to use (with translation software, for example).

Example header

Accept-Language: da, en-gb;q=0.8, en;q=0.7

If-None-Match

This header is used for conditional requests where large responses can be avoided when they are already present in caches. Its use is closely related to that of If-Modified-Since, which uses a timestamp (in RFC1123 format), whilst If-None-Match uses entity tags.

from https://tools.ietf.org/html/rfc7232#section-3.2

The "If-None-Match" header field makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource, when the field-value is "*", or having a selected representation with an entity-tag that does not match any of those listed in the field-value.

Example header

If-None-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"

Index

Examples

Constants

View Source
const (
	// DefaultQuality is the default quality of a media range without explicit "q"
	// https://tools.ietf.org/html/rfc7231#section-5.3.1
	DefaultQuality float64 = 1.0 //e.g text/html;q=1

	// NotAcceptable is the value indicating that its item is not acceptable
	// https://tools.ietf.org/html/rfc7231#section-5.3.1
	NotAcceptable float64 = 0.0 //e.g text/foo;q=0
)
View Source
const (
	// XMLHttpRequest is the value used always with XRequestedWith for XHR.
	XMLHttpRequest = "xmlhttprequest"
)

Variables

View Source
var WildcardPrecedenceValue = []PrecedenceValue{{Value: "*", Quality: DefaultQuality}}

Functions

func ParseQuotedList added in v0.10.0

func ParseQuotedList(listHeader string) internal.Strings

ParseQuotedList extracts the comma-separated component parts from quoted headers such as If-None-Match. Surrounding spaces and quotes are removed.

Types

type ContentType

type ContentType struct {
	// Type and Subtype carry the media type, e.g. "text" and "html"
	Type, Subtype string
	// Params and Extensions hold optional parameter information
	Params []KV
}

ContentType is a media type as defined in RFC-2045, RFC-2046, RFC-2231 (https://tools.ietf.org/html/rfc2045, https://tools.ietf.org/html/rfc2046, https://tools.ietf.org/html/rfc2231) There may also be parameters (e.g. "charset=utf-8") and extension values.

func ParseContentType added in v0.31.0

func ParseContentType(ct string) ContentType

func (ContentType) AsMediaRange added in v0.21.0

func (ct ContentType) AsMediaRange(quality float64) MediaRange

AsMediaRange converts this ContentType to a MediaRange. The default quality should be 1.

func (ContentType) IsTextual added in v0.28.0

func (ct ContentType) IsTextual() bool

IsTextual returns true if the content represents a textual entity; false otherwise.

func (ContentType) String

func (ct ContentType) String() string

type ETag added in v0.11.0

type ETag struct {
	Hash string
	Weak bool
}

ETag is an entity tag used for content matching comparisons. See https://tools.ietf.org/html/rfc7232

func (ETag) String added in v0.11.0

func (etag ETag) String() string

type ETags added in v0.11.0

type ETags []ETag

ETag is a slice of ETag.

func ETagsOf added in v0.11.0

func ETagsOf(s string) ETags

ETagsOf splits an etag header string and parses each part.

func (ETags) String added in v0.13.0

func (etags ETags) String() string

func (ETags) StronglyMatches added in v0.11.0

func (es ETags) StronglyMatches(strongHash string) bool

StronglyMatches finds whether the tags match strongly. This ignores all weak etags in es.

func (ETags) WeaklyMatches added in v0.11.0

func (es ETags) WeaklyMatches(strongHash string) bool

WeaklyMatches finds whether the tags match weakly. See https://tools.ietf.org/html/rfc7232#section-2.3.2

type KV

type KV struct {
	Key, Value string
}

KV holds a parameter with a key and optional value.

type MediaRange

type MediaRange struct {
	ContentType
	Quality float64
}

MediaRange is a content type and associated quality between 0.0 and 1.0.

func (MediaRange) String

func (mr MediaRange) String() string

func (MediaRange) StrongerThan

func (mr MediaRange) StrongerThan(other MediaRange) bool

StrongerThan compares a media range with another value using the precedence rules.

func (MediaRange) Value

func (mr MediaRange) Value() string

Value gets the conjoined type and subtype string, plus any parameters. It does not include the quality value nor any of the extensions.

type MediaRanges

type MediaRanges []MediaRange

MediaRanges holds a slice of media ranges.

func ParseMediaRanges

func ParseMediaRanges(acceptHeader string) MediaRanges

ParseMediaRanges splits a prioritised "Accept" header value and sorts the parts based on quality values and precedence rules. These are returned in order with the most preferred first.

A request without any Accept header field implies that the user agent will accept any media type in response. If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response or disregard the header field by treating the response as if it is not subject to content negotiation.

Example
mrs := header.ParseMediaRanges("text/* ; q=0.3, TEXT/html ; Q=0.7, text/html;level=2; q=0.4, */*; q=0.5")

for i, mr := range mrs {
	fmt.Printf("mr%d = %s\n", i, mr)
}
Output:

mr0 = text/html;q=0.7
mr1 = */*;q=0.5
mr2 = text/html;level=2;q=0.4
mr3 = text/*;q=0.3

func (MediaRanges) String

func (mrs MediaRanges) String() string

func (MediaRanges) WithDefault

func (mrs MediaRanges) WithDefault() MediaRanges

WithDefault returns a list of media ranges that is always non-empty. If the input list is empty, the result holds a wildcard entry ("*/*").

type PrecedenceValue

type PrecedenceValue struct {
	Value   string
	Quality float64
}

PrecedenceValue is a value and associate quality between 0.0 and 1.0

func (PrecedenceValue) String

func (pv PrecedenceValue) String() string

type PrecedenceValues

type PrecedenceValues []PrecedenceValue

PrecedenceValues holds a slice of precedence values.

func ParsePrecedenceValues added in v0.10.0

func ParsePrecedenceValues(acceptXyzHeader string) PrecedenceValues

ParsePrecedenceValues splits a prioritised "Accept-Language", "Accept-Encoding" or "Accept-Charset" header value and sorts the parts. These are returned in order with the most preferred first.

Example
pvs := ParsePrecedenceValues("da, en-gb;q=0.8, en;q=0.7")

for i, pv := range pvs {
	fmt.Printf("pv%d = %s\n", i, pv)
}
Output:

pv0 = da
pv1 = en-gb;q=0.8
pv2 = en;q=0.7

func (PrecedenceValues) Contains added in v0.6.0

func (pvs PrecedenceValues) Contains(value string) bool

func (PrecedenceValues) String

func (pvs PrecedenceValues) String() string

func (PrecedenceValues) WithDefault

func (pvs PrecedenceValues) WithDefault() PrecedenceValues

Jump to

Keyboard shortcuts

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