headers#

class starlite.datastructures.headers.Headers#

Bases: CIMultiDictProxy[str], MultiMixin[str]

An immutable, case-insensitive for HTTP headers.

__init__(headers: Optional[Union[Mapping[str, str], RawHeaders, MultiMapping]] = None) None#

Initialize Headers.

Parameters:

headers – Initial value.

classmethod from_scope(scope: HeaderScope) Headers#

Create headers from a send-message.

Parameters:

scope – The ASGI connection scope.

Returns:

Headers

Raises:

ValueError – If the message does not have a headers key

to_header_list() RawHeadersList#

Raw header value.

Returns:

A list of tuples contain the header and header-value as bytes

class starlite.datastructures.headers.MutableScopeHeaders#

Bases: MutableMapping

A case-insensitive, multidict-like structure that can be used to mutate headers within a Scope

__init__(scope: Optional[HeaderScope] = None) None#

Initialize MutableScopeHeaders from a HeaderScope.

Parameters:

scope – The ASGI connection scope.

classmethod from_message(message: Message) MutableScopeHeaders#

Construct a header from a message object.

Parameters:

messageMessage.

Returns:

MutableScopeHeaders.

Raises:

ValueError – If the message does not have a headers key.

add(key: str, value: str) None#

Add a header to the scope.

Notes

  • This method keeps duplicates.

Parameters:
  • key – Header key.

  • value – Header value.

Returns:

None.

getall(key: str, default: Optional[List[str]] = None) List[str]#

Get all values of a header.

Parameters:
  • key – Header key.

  • default – Default value to return if name is not found.

Returns:

A list of strings.

Raises:

KeyError – if no header for name was found and default is not given.

extend_header_value(key: str, value: str) None#

Extend a multivalued header.

Notes

  • A multivalues header is a header that can take a comma separated list.

  • If the header previously did not exist, it will be added.

Parameters:
  • key – Header key.

  • value – Header value to add,

Returns:

None

__getitem__(key: str) str#

Get the first header matching name

__setitem__(key: str, value: str) None#

Set a header in the scope, overwriting duplicates.

__delitem__(key: str) None#

Delete all headers matching name

__len__() int#

Return the length of the internally stored headers, including duplicates.

__iter__() Iterator[str]#

Create an iterator of header names including duplicates.

class starlite.datastructures.headers.Header#

Bases: BaseModel, ABC

An abstract type for HTTP headers.

documentation_only: bool#

Defines the header instance as for OpenAPI documentation purpose only.

abstract classmethod from_header(header_value: str) Header#

Construct a header from its string representation.

to_header(include_header_name: bool = False) str#

Get the header as string.

Parameters:

include_header_name – should include the header name in the return value. If set to false the return value will only include the header value. if set to true the return value will be: <header name>: <header value>. Defaults to false.

class starlite.datastructures.headers.CacheControlHeader#

Bases: Header

A cache-control header.

max_age: Optional[int]#

Accessor for the max-age directive.

s_maxage: Optional[int]#

Accessor for the s-maxage directive.

no_cache: Optional[bool]#

Accessor for the no-cache directive.

no_store: Optional[bool]#

Accessor for the no-store directive.

private: Optional[bool]#

Accessor for the private directive.

public: Optional[bool]#

Accessor for the public directive.

no_transform: Optional[bool]#

Accessor for the no-transform directive.

must_revalidate: Optional[bool]#

Accessor for the must-revalidate directive.

proxy_revalidate: Optional[bool]#

Accessor for the proxy-revalidate directive.

must_understand: Optional[bool]#

Accessor for the must-understand directive.

immutable: Optional[bool]#

Accessor for the immutable directive.

stale_while_revalidate: Optional[int]#

Accessor for the stale-while-revalidate directive.

classmethod from_header(header_value: str) CacheControlHeader#

Create a CacheControlHeader instance from the header value.

Parameters:

header_value – the header value as string

Returns:

An instance of CacheControlHeader

classmethod prevent_storing() CacheControlHeader#

Create a cache-control header with the no-store directive which indicates that any caches of any kind (private or shared) should not store this response.

class starlite.datastructures.headers.ETag#

Bases: Header

An etag header.

classmethod from_header(header_value: str) ETag#

Construct an etag header from its string representation.

Note that this will unquote etag-values

classmethod validate_value(value: Any, values: Dict[str, Any]) Any#

Ensure that either value is set or the instance is for documentation_only.