sanic.http.constants.HTTP#

Enum for representing HTTP versions

Inherits from: IntEnum, int, ReprEnum, Enum

class HTTP(value, names = None, module = None, qualname = None, type = None, start = 1, boundary = None)

sanic.http.constants.Stage#

Enum for representing the stage of the request/response cycle

Inherits from: Enum

class Stage(value, names = None, module = None, qualname = None, type = None, start = 1, boundary = None)

| IDLE Waiting for request | REQUEST Request headers being received | HANDLER Headers done, handler running | RESPONSE Response headers sent, body in progress | FAILED Unrecoverable state (error while sending response) |

sanic.http.http1.Http#

"Internal helper for managing the HTTP/1.1 request/response cycle.

Inherits from: Stream

class Http(protocol)

Raises
BadRequest

If the request body is malformed.

Exception

If the request is malformed.

ExpectationFailed

If the request is malformed.

PayloadTooLarge

If the request body exceeds the size limit.

RuntimeError

If the response status is invalid.

ServerError

If the handler does not produce a response.

ServerError

If the response is bigger than the content-length.

create_empty_request#

Create an empty request object for error handling use.

def create_empty_request(self): -> None

Current error handling code needs a request object that won't exist if an error occurred during before a request was received. Create a bogus response for error handling use.

error_response#

Handle response when exception encountered

async def error_response(self, exception: Exception): -> None

head_response_ignored#

HEAD response: body data silently ignored.

def head_response_ignored(self, data: bytes, end_stream: bool): -> None

http1#

HTTP 1.1 connection handler

async def http1(self)

http1_request_header#

Receive and parse request header into self.request.

async def http1_request_header(self)

http1_response_chunked#

Format a part of response body in chunked encoding.

async def http1_response_chunked(self, data: bytes, end_stream: bool): -> None

http1_response_header#

Format response header and send it.

async def http1_response_header(self, data: <class 'bytes'>, end_stream: <class 'bool'>): -> None

http1_response_normal#

Format / keep track of non-chunked response.

async def http1_response_normal(self, data: bytes, end_stream: bool): -> None

init_for_request#

Init/reset all per-request variables.

def init_for_request(self)

log_response#

Helper method provided to enable the logging of responses in case if the HttpProtocol.access_log is enabled.

def log_response(self): -> None

read#

Read some bytes of request body.

async def read(self): -> typing.Optional[bytes]

respond#

Initiate new streaming response.

def respond(self, response: BaseHTTPResponse): -> BaseHTTPResponse

Nothing is sent until the first send() call on the returned object, and calling this function multiple times will just alter the response to be given.

send#

@property
def send(self)

set_header_max_size#

@classmethod
def set_header_max_size(sizes: int)

sanic.http.http3.HTTP3Transport#

HTTP/3 transport implementation.

Inherits from: TransportProtocol, BaseTransport

class HTTP3Transport(protocol: Http3Protocol)

close#

Close the transport.

def close(self)

Buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol's connection_lost() method will (eventually) be called with None as its argument.

get_extra_info#

Get optional transport information.

def get_extra_info(self, info: str, default: Any = None): -> Any

get_protocol#

Return the current protocol.

def get_protocol(self): -> Http3Protocol

is_closing#

Return True if the transport is closing or closed.

def is_closing(self)

set_protocol#

Set a new protocol.

def set_protocol(self, protocol)

sanic.http.http3.HTTPReceiver#

HTTP/3 receiver implementation.

Inherits from: Receiver, ABC, Stream

class HTTPReceiver(args, kwargs): -> None

error_response#

Handle response when exception encountered

async def error_response(self, exception: Exception): -> None

receive_body#

Receive request body from client

def receive_body(self, data: bytes): -> None

respond#

Prepare response to client

def respond(self, response: BaseHTTPResponse): -> BaseHTTPResponse

run#

Handle the request and response cycle.

async def run(self, exception: Optional[Exception] = None)

send#

Send data to client

async def send(self, data: bytes, end_stream: bool): -> None

send_headers#

Send response headers to client

def send_headers(self): -> None

sanic.http.http3.Http3#

Internal helper for managing the HTTP/3 request/response cycle

class Http3(protocol: Http3Protocol, transmit: Callable[[], None]): -> None

get_or_make_receiver#

def get_or_make_receiver(self, event: H3Event): -> Tuple[Receiver, bool]

get_receiver_by_stream_id#

def get_receiver_by_stream_id(self, stream_id: int): -> Receiver

http_event_received#

def http_event_received(self, event: H3Event): -> None

sanic.http.http3.Receiver#

HTTP/3 receiver base class.

Inherits from: ABC

class Receiver(transmit, protocol, request: Request): -> None

run#

async def run(self)

sanic.http.http3.SessionTicketStore#

Simple in-memory store for session tickets.

class SessionTicketStore(): -> None

add#

def add(self, ticket: SessionTicket): -> None

pop#

def pop(self, label: bytes): -> Optional[SessionTicket]

sanic.http.http3.WebTransportReceiver#

WebTransport receiver implementation.

Inherits from: Receiver, ABC

class WebTransportReceiver(transmit, protocol, request: Request): -> None

run#

async def run(self)

sanic.http.http3.WebsocketReceiver#

Websocket receiver implementation.

Inherits from: Receiver, ABC

class WebsocketReceiver(transmit, protocol, request: Request): -> None

run#

async def run(self)

sanic.http.http3.get_config#

def get_config(app: Sanic, ssl: Union[SanicSSLContext, CertSelector, SSLContext])

sanic.http.stream.Stream#

class Stream()

respond#

def respond(self, response: BaseHTTPResponse): -> BaseHTTPResponse

sanic.http.tls.context.CertSelector#

Automatically select SSL certificate based on the hostname that the

Inherits from: SSLContext, _SSLContext

class CertSelector(ctxs)

client is trying to access, via SSL SNI. Paths to certificate folders with privkey.pem and fullchain.pem in them should be provided, and will be matched in the order given whenever there is a new connection.

hostname_checks_common_name#

@property
def hostname_checks_common_name(self)

load_default_certs#

def load_default_certs(self, purpose = Purpose.SERVER_AUTH)

maximum_version#

@property
def maximum_version(self)

minimum_version#

@property
def minimum_version(self)

options#

@property
def options(self)

protocol#

@property
def protocol(self)

set_alpn_protocols#

def set_alpn_protocols(self, alpn_protocols)

set_npn_protocols#

def set_npn_protocols(self, npn_protocols)

set_servername_callback#

def set_servername_callback(self, server_name_callback)

verify_flags#

@property
def verify_flags(self)

verify_mode#

@property
def verify_mode(self)

wrap_bio#

def wrap_bio(self, incoming, outgoing, server_side = False, server_hostname = None, session = None)

wrap_socket#

def wrap_socket(self, sock, server_side = False, do_handshake_on_connect = True, suppress_ragged_eofs = True, server_hostname = None, session = None)

sanic.http.tls.context.CertSimple#

A wrapper for creating SSLContext with a sanic attribute.

Inherits from: SanicSSLContext, SSLContext, _SSLContext

class CertSimple(cert, key, kw)

create_from_ssl_context#

def create_from_ssl_context(context: ssl.SSLContext)

hostname_checks_common_name#

@property
def hostname_checks_common_name(self)

load_default_certs#

def load_default_certs(self, purpose = Purpose.SERVER_AUTH)

maximum_version#

@property
def maximum_version(self)

minimum_version#

@property
def minimum_version(self)

options#

@property
def options(self)

protocol#

@property
def protocol(self)

set_alpn_protocols#

def set_alpn_protocols(self, alpn_protocols)

set_npn_protocols#

def set_npn_protocols(self, npn_protocols)

set_servername_callback#

def set_servername_callback(self, server_name_callback)

verify_flags#

@property
def verify_flags(self)

verify_mode#

@property
def verify_mode(self)

wrap_bio#

def wrap_bio(self, incoming, outgoing, server_side = False, server_hostname = None, session = None)

wrap_socket#

def wrap_socket(self, sock, server_side = False, do_handshake_on_connect = True, suppress_ragged_eofs = True, server_hostname = None, session = None)

sanic.http.tls.context.SanicSSLContext#

An SSLContext holds various SSL-related configuration options and

Inherits from: SSLContext, _SSLContext

class SanicSSLContext(protocol = None, args, kwargs)

data, such as certificates and possibly a private key.

create_from_ssl_context#

@classmethod
def create_from_ssl_context(context: ssl.SSLContext)

hostname_checks_common_name#

@property
def hostname_checks_common_name(self)

load_default_certs#

def load_default_certs(self, purpose = Purpose.SERVER_AUTH)

maximum_version#

@property
def maximum_version(self)

minimum_version#

@property
def minimum_version(self)

options#

@property
def options(self)

protocol#

@property
def protocol(self)

set_alpn_protocols#

def set_alpn_protocols(self, alpn_protocols)

set_npn_protocols#

def set_npn_protocols(self, npn_protocols)

set_servername_callback#

def set_servername_callback(self, server_name_callback)

verify_flags#

@property
def verify_flags(self)

verify_mode#

@property
def verify_mode(self)

wrap_bio#

def wrap_bio(self, incoming, outgoing, server_side = False, server_hostname = None, session = None)

wrap_socket#

def wrap_socket(self, sock, server_side = False, do_handshake_on_connect = True, suppress_ragged_eofs = True, server_hostname = None, session = None)

sanic.http.tls.context.create_context#

Create a context with secure crypto and HTTP/1.1 in protocols.

def create_context(certfile: Optional[str] = None, keyfile: Optional[str] = None, password: Optional[str] = None, purpose: ssl.Purpose = Purpose.CLIENT_AUTH): -> ssl.SSLContext

sanic.http.tls.context.find_cert#

Find the first certificate that matches the given SNI.

def find_cert(self: CertSelector, server_name: str)

Raises
ssl.CertificateError

No matching certificate found.

sanic.http.tls.context.load_cert_dir#

def load_cert_dir(p: str): -> ssl.SSLContext

sanic.http.tls.context.match_hostname#

Match names from CertSelector against a received hostname.

def match_hostname(ctx: Union[ssl.SSLContext, CertSelector], hostname: str): -> bool

sanic.http.tls.context.process_to_context#

Process app.run ssl argument from easy formats to full SSLContext.

def process_to_context(ssldef: Union[None, ssl.SSLContext, dict, str, list, tuple]): -> Optional[ssl.SSLContext]

sanic.http.tls.context.selector_sni_callback#

Select a certificate matching the SNI.

def selector_sni_callback(sslobj: ssl.SSLObject, server_name: str, ctx: CertSelector): -> Optional[int]

sanic.http.tls.context.server_name_callback#

Store the received SNI as sslobj.sanic_server_name.

def server_name_callback(sslobj: ssl.SSLObject, server_name: str, ctx: ssl.SSLContext): -> None

sanic.http.tls.context.shorthand_to_ctx#

Convert an ssl argument shorthand to an SSLContext object.

def shorthand_to_ctx(ctxdef: Union[None, ssl.SSLContext, dict, str]): -> Optional[ssl.SSLContext]

sanic.http.tls.creators.CertCreator#

Helper class that provides a standard way to create an ABC using

Inherits from: ABC

class CertCreator(app, key, cert): -> None

inheritance.

check_supported#

def check_supported(self): -> None

generate_cert#

def generate_cert(self, localhost: str): -> ssl.SSLContext

select#

@classmethod
def select(app: Sanic, cert_creator: LocalCertCreator, local_tls_key, local_tls_cert): -> CertCreator

sanic.http.tls.creators.MkcertCreator#

Helper class that provides a standard way to create an ABC using

Inherits from: CertCreator, ABC

class MkcertCreator(app, key, cert): -> None

inheritance.

check_supported#

def check_supported(self): -> None

generate_cert#

def generate_cert(self, localhost: str): -> ssl.SSLContext

select#

def select(app: Sanic, cert_creator: LocalCertCreator, local_tls_key, local_tls_cert): -> CertCreator

sanic.http.tls.creators.TrustmeCreator#

Helper class that provides a standard way to create an ABC using

Inherits from: CertCreator, ABC

class TrustmeCreator(app, key, cert): -> None

inheritance.

check_supported#

def check_supported(self): -> None

generate_cert#

def generate_cert(self, localhost: str): -> ssl.SSLContext

select#

def select(app: Sanic, cert_creator: LocalCertCreator, local_tls_key, local_tls_cert): -> CertCreator

sanic.http.tls.creators.get_ssl_context#

def get_ssl_context(app: Sanic, ssl: Optional[ssl.SSLContext]): -> ssl.SSLContext