headers

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Header and Headers implementation.

You all get used to a fact that headers in HTTP is a map. Actually, there is RFC2616 and its friends which tells that:

1. Header names are case-insensitive.

2. Header order does not matter.

This is of course not true, and this is a reason why API of httransform is so inconvenient at the first glance. The reason is that both points are invalid.

Many antibot systems treat header case differently. This is because browsers send them differently. Some of them prefer to send all headers lowercased. Some of them send headers in 'canonical' form. Some of them use mixed approach. This is specific to a browser. And if we want to give an ability to replicate that, we have to maintain a case of headers.

At the same time, header order really matters. Some headers can be merged as comma-delimited lists. Some of them are intentionally repeating on a wire like Set-Cookie or just Cookie headers.

A whole reason why httransform is using fasthttp is because this library gives an ability to work with headers with no additional processing. It does not turn them into map, it does not normalize them (it actually does, but there is an option to disable it).

Actually, when you work with headers in httransform, you work with instance of Headers struct which maintains a list of headers, not a map. But it also gives an ability to get values in case-sensitive fashion (and in general case-insensitive).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReleaseHeaderSet

func ReleaseHeaderSet(set *Headers)

ReleaseHeaderSet resets a Headers and returns it back to a pool. You should not use an instance of that header after this operation.

func Values

func Values(value string) []string

Values splits comma-delimited list of values and returns it as a list.

Some headers allow merged values. For example, 'Accept-Encoding: deflate, gzip, br' is actually has 3 values: deflate, gzip and br.

Types

type FastHTTPHeaderWrapper

type FastHTTPHeaderWrapper interface {
	// Read resets an underlying structure and populates it with raw
	// data which is coming from the given io.Reader instance.
	Read(io.Reader) error

	// DisableNormalizing explicitly prevents from normalizing of header
	// names and order.
	DisableNormalizing()

	// ResetConnectionClose drops a status of Connection header to
	// default state: absence.
	ResetConnectionClose()

	// Headers return a bytes containing raw headers (with a first line!).
	Headers() []byte
}

FastHTTPHeaderWrapper defines a wrapper for fasthttp Headers which has to be put into Headers on Init.

Unfortunately, fasthttp Headers are instances, not interfaces and some operations vary so a unified interface is required.

func NewRequestHeaderWrapper

func NewRequestHeaderWrapper(ref *fasthttp.RequestHeader) FastHTTPHeaderWrapper

NewRequestHeaderWrapper returns a new instance of requestHeaderWrapper for a given fasthttp.RequestHeader.

func NewResponseHeaderWrapper

func NewResponseHeaderWrapper(ref *fasthttp.ResponseHeader) FastHTTPHeaderWrapper

NewResponseHeaderWrapper returns a new instance of responseHeaderWrapper for a given fasthttp.ResponseHeader.

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

Header presents a single header instance with a name or value. Nothing is explicitly exposed because we do some additional actions to speedup some operations.

func NewHeader

func NewHeader(name, value string) Header

NewHeader return a Header instance based on given name and value.

func (*Header) CanonicalName

func (h *Header) CanonicalName() string

CanonicalName name returns a name in canonical form: dash-separated parts are titlecased. For example, a canonical form for 'aCCept-Encoding' is 'Accept-Encoding'.

func (*Header) ID

func (h *Header) ID() string

ID returns a unique identifier of the header.

func (*Header) Name

func (h *Header) Name() string

Name returns a header name. Case-sensitive.

func (*Header) String

func (h *Header) String() string

String complies with fmt.Stringer interface.

func (*Header) Value

func (h *Header) Value() string

Value returns a value of the header.

func (*Header) Values

func (h *Header) Values() []string

Values returns a list of header values. If name is a comma-delimited list, it will return a list of parts. For example, for header 'Connection: keep-alive, Upgrade', this one returns []string{"keep-alive", "Upgrade"}.

This is a shortcut to headers.Values(h.Value()).

func (*Header) WithName

func (h *Header) WithName(name string) Header

WithName returns a copy of header with modified name.

func (*Header) WithValue

func (h *Header) WithValue(value string) Header

WithValue returns a copy of header with modified value.

type Headers

type Headers struct {
	// A list of headers. Actually, you do not need to use all
	// operations from Headers struct to manage this list. These are
	// shortcuts. If you like to manipulate with array, nothing really
	// prevents you from going this way.
	Headers []Header
	// contains filtered or unexported fields
}

Headers present a list of Header with some convenient optimized shorthand operations. It also has a binding to corresponding fasthttp Header interface for synchronisation.

Once you instantiate a new Headers instance with either AcquireHeaderSet or simple headers.Headers{}, you need to fill it with values. Use Init function to do that. Each operation is not reflected to underlying fasthttp Header. To do that, you have to implicitly call Sync methods.

func AcquireHeaderSet

func AcquireHeaderSet() *Headers

AcquireHeaderSet takes a new Header instance from the pool. Please do not remember to return it back when you are done and do not use any references on that after.

func (*Headers) Append

func (h *Headers) Append(name, value string)

Append simply appends a header to the list of headers.

func (*Headers) GetAll

func (h *Headers) GetAll(key string) []*Header

GetAll returns a list of headers where name is given in a case-INSENSITIVE fashion.

func (*Headers) GetAllExact

func (h *Headers) GetAllExact(key string) []*Header

GetAllExact returns a list of headers where name is given in a case-SENSITIVE fashion.

func (*Headers) GetFirst

func (h *Headers) GetFirst(key string) *Header

GetLast returns a first header (or nil) where name is case-INSENSITIVE.

func (*Headers) GetFirstExact

func (h *Headers) GetFirstExact(key string) *Header

GetLast returns a first header (or nil) where name is case-SENSITIVE.

func (*Headers) GetLast

func (h *Headers) GetLast(key string) *Header

GetLast returns a last header (or nil) where name is case-INSENSITIVE.

func (*Headers) GetLastExact

func (h *Headers) GetLastExact(key string) *Header

GetLastExact returns a last header (or nil) where name is case-SENSITIVE.

func (*Headers) Pull added in v2.0.3

func (h *Headers) Pull() error

Pull reads underlying fasthttp.Header and fills a header list with its contents.

Ananlogy: git pull where origin is fasthttp.Header.

func (*Headers) Push added in v2.0.3

func (h *Headers) Push() error

Push resets underlying fasthttp Header structure and restores it based on a given header list.

Ananlogy: git push where origin is fasthttp.Header.

func (*Headers) Remove

func (h *Headers) Remove(key string)

Remove removes all headers from the list where name is case-INSENSITIVE. Order is kept.

func (*Headers) RemoveExact

func (h *Headers) RemoveExact(key string)

Remove removes all headers from the list where name is case-SENSITIVE. Order is kept.

func (*Headers) Reset

func (h *Headers) Reset(original FastHTTPHeaderWrapper)

Reset rebinds Headers to corresponding fasthttp data structure.

func (*Headers) Set

func (h *Headers) Set(name, value string, cleanupRest bool)

Set sets a value for the FIRST seen header where name is case-INSENSITIVE. cleanupRest parameter defines if the rest values should be wiped out.

func (*Headers) SetExact

func (h *Headers) SetExact(name, value string, cleanupRest bool)

SetExact sets a value for the FIRST seen header where name is case-SENSITIVE. cleanupRest parameter defines if the rest values should be wiped out.

func (*Headers) String

func (h *Headers) String() string

Strings here to comply with fmt.Stringer interface.

Jump to

Keyboard shortcuts

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