duck.http.core.handlerΒΆ
Handles the response further after they have been processed. This includes sending response to client and logging the response.
Module ContentsΒΆ
ClassesΒΆ
Handles sending processed responses to clients via socket communication. This class contains methods for sending raw data and HTTP responses, along with optional error handling and logging. |
FunctionsΒΆ
Returns a log message formatted similarly to Django logs with color support for various HTTP statuses. |
|
This returns default duck formatted log with color support. |
|
Returns the respective color for the debug message of the status code. |
|
Returns a debug message corresponding to an HTTP status code. |
|
Logs a response to the console. |
DataΒΆ
APIΒΆ
- class duck.http.core.handler.ResponseHandlerΒΆ
Handles sending processed responses to clients via socket communication. This class contains methods for sending raw data and HTTP responses, along with optional error handling and logging.
- async _async_send_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], sock: duck.utils.xsocket.xsocket, suppress_errors: bool = False)ΒΆ
Asynchronously sends an HTTP response to the client socket.
Args: response (Union[BaseResponse, HttpResponse]): The HTTP response object containing the response data. sock (xsocket): The client socket object to which the response will be sent. suppress_errors (bool): If True, suppresses any errors that occur during the sending process. Defaults to False.
Raises: Exception: If there is an error during the data sending process (e.g., socket errors), unless suppressed.
- _send_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], sock: duck.utils.xsocket.xsocket, suppress_errors: bool = False)ΒΆ
Sends an HTTP response to the client socket.
- Parameters:
response β The HTTP response object containing the response data.
sock β The client socket object to which the response will be sent.
suppress_errors β If True, suppresses any errors that occur during the sending process. Defaults to False.
- Raises:
Exception β If there is an error during the data sending process (e.g., socket errors), unless suppressed.
- async classmethod async_close_streaming_response(response)ΒΆ
Asynchronously closes streaming response
streamusually after response sending.
- async async_send_http2_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], stream_id: int, sock: duck.utils.xsocket.xsocket, 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.
sock β The client socket object to which the response will be sent.
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
SocketIO.sendto 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.
- async async_send_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], sock: duck.utils.xsocket.xsocket, request: Optional[duck.http.request.HttpRequest] = None, disable_logging: bool = False, suppress_errors: bool = False, strictly_http1: bool = False) NoneΒΆ
Asynchronously sends an HTTP response to the client socket, optionally logging the response.
- Parameters:
response β The HTTP response object containing the response data.
sock β The client socket object to which the response will be sent.
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.
strictly_http1 β Strictly send response using
HTTP/2, even ifHTTP/2is enabled.
- Raises:
Exception β If there is an error during the data sending process (e.g., socket errors), unless suppressed.
This method calls
SocketIO.sendto 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.
- classmethod auto_log_response(response, request)ΒΆ
Logs a response to the console, considering middleware errors and any other issues. This automatically creates debug messages (if applicable).
Notes:
Nothing will be logged if response is
None.
- classmethod close_streaming_response(response)ΒΆ
Synchronously closes streaming response
streamusually after response sending.
- send_http2_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], stream_id: int, sock: duck.utils.xsocket.xsocket, 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:
AttributeError β If the xsocket doesnβt have the attribute
h2_protocol.ValueError β If request is not set to be be handled with HTTP/2.
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.
- send_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], sock: duck.utils.xsocket.xsocket, request: Optional[duck.http.request.HttpRequest] = None, disable_logging: bool = False, suppress_errors: bool = False, strictly_http1: bool = False) NoneΒΆ
Sends an HTTP response to the client socket, optionally logging the response.
- Parameters:
response β The HTTP response object containing the response data.
sock β The client socket object to which the response will be sent.
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.
strictly_http1 β Strictly send response using
HTTP/2, even ifHTTP/2is enabled.
- Raises:
Exception β If there is an error during the data sending process (e.g., socket errors), unless suppressed.
This method calls
SocketIO.sendto 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.
- duck.http.core.handler.get_django_formatted_log(response: duck.http.response.HttpResponse, request: Optional[duck.http.request.HttpRequest] = None, debug_message: Optional[Union[str, List[str]]] = None) strΒΆ
Returns a log message formatted similarly to Django logs with color support for various HTTP statuses.
- Parameters:
response β The HTTP response object.
request β The HTTP request object. Optional, used for adding more detailed log information.
debug_message β A custom debug message or list of messages to append to the log.
- Returns:
The formatted log message with color codes based on the HTTP status.
- Return type:
str
The log format includes:
HTTP status code, with color changes based on success, client errors, redirection, or server errors.
Optional request path for 500 errors.
Optional debug message if provided.
Support for redirections with status codes 301 and 307.
- duck.http.core.handler.get_duck_formatted_log(response: duck.http.response.HttpResponse, request: Optional[duck.http.request.HttpRequest] = None, debug_message: Optional[Union[str, List[str]]] = None) strΒΆ
This returns default duck formatted log with color support.
- Parameters:
response β The http response object.
request β The http request object.
debug_message β Custom debug message or list of messages to add to log.
- duck.http.core.handler.get_status_code_debug_color(status_code: int) strΒΆ
Returns the respective color for the debug message of the status code.
- duck.http.core.handler.get_status_debug_msg(response: duck.http.response.HttpResponse, request: duck.http.request.HttpRequest) Optional[str]ΒΆ
Returns a debug message corresponding to an HTTP status code.
This function is typically used when a request-response lacks an attached debug message, but the response carries special meaning that warrants additional debugging information based on the status code.
- Parameters:
response β The HTTP response for which the debug message is required.
request β The HTTP request associated with the status code.
- Returns:
A debug message that provides context or explanation for the given status code.
- Return type:
str
- duck.http.core.handler.log_response(response: Union[duck.http.response.BaseResponse, duck.http.response.HttpResponse], request: Optional[duck.http.request.HttpRequest] = None, debug_message: Optional[Union[str, List[str]]] = None) NoneΒΆ
Logs a response to the console.
- Parameters:
response β The http response object.
request β The http request object.
debug_message β Message or list of messages to display along the response, usually middleware error debug message.
- duck.http.core.handler.response_handlerΒΆ
βResponseHandler(β¦)β