class documentation

class Headers: (source)

Constructor: Headers(rawHeaders)

View In Hierarchy

Stores HTTP headers in a key and multiple value format.

When passed str, header names (e.g. 'Content-Type') are encoded using ISO-8859-1 and header values (e.g. 'text/html;charset=utf-8') are encoded using UTF-8. Some methods that return values will return them in the same type as the name given.

If the header keys or values cannot be encoded or decoded using the rules above, using just bytes arguments to the methods of this class will ensure no decoding or encoding is done, and Headers will treat the keys and values as opaque byte strings.

Method __cmp__ Define Headers instances as being equal to each other if they have the same raw headers.
Method __init__ Undocumented
Method __repr__ Return a string fully describing the headers set on this object.
Method addRawHeader Add a new raw value for the given header.
Method copy Return a copy of itself with the same headers set.
Method getAllRawHeaders Return an iterator of key, value pairs of all headers contained in this object, as bytes. The keys are capitalized in canonical capitalization.
Method getRawHeaders Returns a sequence of headers matching the given name as the raw string given.
Method hasHeader Check for the existence of a given header.
Method removeHeader Remove the named header from this header object.
Method setRawHeaders Sets the raw representation of the given header.
Method _canonicalNameCaps Return the canonical name for the given header.
Method _encodeName Encode the name of a header (eg 'Content-Type') to an ISO-8859-1 encoded bytestring if required.
Class Variable _caseMappings A dict that maps lowercase header names to their canonicalized representation.
Instance Variable _rawHeaders A dict mapping header names as bytes to lists of header values as bytes.
def __cmp__(self, other): (source)

Define Headers instances as being equal to each other if they have the same raw headers.

def __init__(self, rawHeaders: Optional[Mapping[AnyStr, Sequence[AnyStr]]] = None): (source)

Undocumented

def __repr__(self) -> str: (source)

Return a string fully describing the headers set on this object.

def addRawHeader(self, name: Union[str, bytes], value: Union[str, bytes]): (source)

Add a new raw value for the given header.

Parameters
name:Union[str, bytes]The name of the header for which to set the value.
value:Union[str, bytes]The value to set for the named header.
def copy(self): (source)

Return a copy of itself with the same headers set.

Returns
A new Headers
def getAllRawHeaders(self) -> Iterator[Tuple[bytes, Sequence[bytes]]]: (source)

Return an iterator of key, value pairs of all headers contained in this object, as bytes. The keys are capitalized in canonical capitalization.

@overload
def getRawHeaders(self, name: AnyStr) -> Optional[Sequence[AnyStr]]:
@overload
def getRawHeaders(self, name: AnyStr, default: _T) -> Union[Sequence[AnyStr], _T]:
(source)

Returns a sequence of headers matching the given name as the raw string given.

Parameters
name:AnyStrThe name of the HTTP header to get the values of.
default:Optional[_T]The value to return if no header with the given name exists.
Returns
Union[Sequence[AnyStr], Optional[_T]]If the named header is present, a sequence of its values. Otherwise, default.
def hasHeader(self, name: AnyStr) -> bool: (source)

Check for the existence of a given header.

Parameters
name:AnyStrThe name of the HTTP header to check for.
Returns
boolTrue if the header exists, otherwise False.
def removeHeader(self, name: AnyStr): (source)

Remove the named header from this header object.

Parameters
name:AnyStrThe name of the HTTP header to remove.
Returns
None
@overload
def setRawHeaders(self, name: Union[str, bytes], values: Sequence[bytes]):
@overload
def setRawHeaders(self, name: Union[str, bytes], values: Sequence[str]):
@overload
def setRawHeaders(self, name: Union[str, bytes], values: Sequence[Union[str, bytes]]):
(source)

Sets the raw representation of the given header.

Parameters
name:Union[str, bytes]The name of the HTTP header to set the values for.
values:objectA list of strings each one being a header value of the given name.
Returns
None
Raises
TypeErrorRaised if values is not a sequence of bytes or str, or if name is not bytes or str.
def _canonicalNameCaps(self, name: bytes) -> bytes: (source)

Return the canonical name for the given header.

Parameters
name:bytesThe all-lowercase header name to capitalize in its canonical form.
Returns
bytesThe canonical name of the header.
def _encodeName(self, name: Union[str, bytes]) -> bytes: (source)

Encode the name of a header (eg 'Content-Type') to an ISO-8859-1 encoded bytestring if required.

Parameters
name:Union[str, bytes]A HTTP header name
Returns
bytesname, encoded if required, lowercased
_caseMappings: dict[bytes, bytes] = (source)

A dict that maps lowercase header names to their canonicalized representation.

A dict mapping header names as bytes to lists of header values as bytes.