duck.http.core.httpd.http2.protocol¶
H2 Protocol responsible for handling H2 Connections.
Module Contents¶
Classes¶
Asynchronous H2 Protocol class. |
API¶
- class duck.http.core.httpd.http2.protocol.H2Protocol(sock: duck.utils.xsocket.xsocket, addr: Tuple[str, int], conn: h2.connection.H2Connection, event_handler: duck.http.core.httpd.http2.event_handler.EventHandler, event_loop: asyncio.BaseEventLoop = None, sync_queue: Optional[queue.Queue] = None)¶
Asynchronous H2 Protocol class.
Initialization
H2Protocol initialization.
- Parameters:
sock – Client socket object.
addr – Client address.
conn – H2 Connection object.
event_handler – Asynchrous
H2event handler class.event_loop – The target event loop, only used in
WSGImode…sync_queue – A queue for adding tasks that needs to be executed outside async context, useful when threads are used and
ASYNC_HANDLING=False
- __slots__¶
(‘sock’, ‘addr’, ‘conn’, ‘server’, ‘event_handler’, ‘event_loop’, ‘sync_queue’, ‘__closing’)
- async async_flush_response_data(data: bytes, stream_id: int, end_stream: bool = False)¶
Asynchronously sends response data directly to client socket.
Notes:
This first response data to queue by sending it to
H2Connectionthen fetch the response data fromH2Connection.data_to_send()then sends the data.
- async async_send_data(data: bytes, stream_id: int, end_stream: bool = False, on_data_sent: Callable = None, flow_control_timeout: float = 30.0)¶
Asynchronously send data for a given HTTP/2 stream, respecting flow control.
This method automatically chunks data according to the current flow control window and the maximum outbound frame size. It also ensures that sending pauses until the remote peer grants enough window updates.
- Parameters:
data – The payload to send.
stream_id – The ID of the target HTTP/2 stream.
end_stream – Whether to close the stream after sending all data. Defaults to False.
on_data_sent – Optional callback or coroutine to execute once the data has been fully sent. The callback receives
original_dataas its only argument.flow_control_timeout – Max time (in seconds) to wait for flow control window updates before giving up. Defaults to 30.0s.
- Raises:
asyncio.CancelledError – If task is cancelled during sending.
- async async_send_goaway(error_code, debug_message: bytes = None)¶
Sends a
GOAWAYframe with the given error code and debug_message.
- async async_send_pending_data()¶
Asynchronously sends any pending data from
H2Connection.data_to_send()asynchronously.
- async async_send_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], stream_id: int, request: Optional[duck.http.request.HttpRequest] = None, disable_logging: bool = False, suppress_errors: bool = False) None¶
Asynchronously sends an HTTP/2 response to the H2Connection.
- Parameters:
response – The HTTP response object containing the response data.
stream_id – The target H2 stream ID.
request – The request object associated with the response. Used for logging and debugging purposes.
disable_logging – If True, disables logging of the response. Defaults to False.
suppress_errors – If True, suppresses any errors that occur during the sending process (only sending data). Defaults to False.
- Raises:
Exception – If there is an error during the data sending process (e.g., socket errors), unless suppressed.
This method calls
async_send_datato transmit the raw response data to the client and performs logging ifdisable_loggingis False. If the request object contains debug information or failed middleware details, they are included in the logs.
- close_connection(error_code: int = 0, debug_message: bytes = None)¶
Closes the socket connection.
- property closing: bool¶
Returns a boolean on whether if protocol is in closing state.
- connection_lost(*_)¶
Called on socket connection lost.
- async run_forever()¶
Runs the loop for handling further client requests.
- send_pending_data()¶
Synchronously sends any pending data from
H2Connection.data_to_send().
- send_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], stream_id: int, request: Optional[duck.http.request.HttpRequest] = None, disable_logging: bool = False, suppress_errors: bool = False, return_future: bool = False) Optional[duck.utils.asyncio.eventloop.SyncFuture]¶
This sends an HTTP/2 response to the H2Connection.
Notes:
This sends data asynchronously but the difference between this method and
async_send_responseis that it submits the coroutine for sending response toAsyncioLoopManagerand returns aSyncFutureyou can wait for.
- Parameters:
response – The HTTP response object containing the response data.
stream_id – The target H2 stream ID.
request – The request object associated with the response. Used for logging and debugging purposes.
disable_logging – If True, disables logging of the response. Defaults to False.
suppress_errors – If True, suppresses any errors that occur during the sending process (only sending data). Defaults to False.
return_future – Whether to return sync future.
- Returns:
A synchronous future you can wait for upon response send completion.
- Return type:
Optional[SyncFuture]
- Raises:
DisallowedAction – If ASYNC_HANDLING is True in settings.
Exception – If there is an error during the data sending process (e.g., socket errors), unless suppressed.