header

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2021 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

package header provides parsing rules for content negotiation headers according to RFC-7231.

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

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

Index

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
)

Variables

This section is empty.

Functions

This section is empty.

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
	Extensions []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 ContentTypeOf

func ContentTypeOf(typ, subtype string, paramKV ...string) ContentType

ContentTypeOf builds a content type value with optional parameters. The parameters are passed in as literal strings, e.g. "charset=utf-8".

func (ContentType) String

func (ct ContentType) String() string

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.

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 Parse

func Parse(acceptXyzHeader string) PrecedenceValues

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

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