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 ¶
- func ReleaseHeaderSet(set *Headers)
- func Values(value string) []string
- type FastHTTPHeaderWrapper
- type Header
- type Headers
- func (h *Headers) Append(name, value string)
- func (h *Headers) GetAll(key string) []*Header
- func (h *Headers) GetAllExact(key string) []*Header
- func (h *Headers) GetFirst(key string) *Header
- func (h *Headers) GetFirstExact(key string) *Header
- func (h *Headers) GetLast(key string) *Header
- func (h *Headers) GetLastExact(key string) *Header
- func (h *Headers) Pull() error
- func (h *Headers) Push() error
- func (h *Headers) Remove(key string)
- func (h *Headers) RemoveExact(key string)
- func (h *Headers) Reset(original FastHTTPHeaderWrapper)
- func (h *Headers) Set(name, value string, cleanupRest bool)
- func (h *Headers) SetExact(name, value string, cleanupRest bool)
- func (h *Headers) String() string
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.
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 ¶
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 (*Header) CanonicalName ¶
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) Values ¶
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()).
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) GetAll ¶
GetAll returns a list of headers where name is given in a case-INSENSITIVE fashion.
func (*Headers) GetAllExact ¶
GetAllExact returns a list of headers where name is given in a case-SENSITIVE fashion.
func (*Headers) GetFirstExact ¶
GetLast returns a first header (or nil) where name is case-SENSITIVE.
func (*Headers) GetLastExact ¶
GetLastExact returns a last header (or nil) where name is case-SENSITIVE.
func (*Headers) Pull ¶ added in v2.0.3
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
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 ¶
Remove removes all headers from the list where name is case-INSENSITIVE. Order is kept.
func (*Headers) RemoveExact ¶
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 ¶
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.