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ΒΆ

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.

FunctionsΒΆ

get_django_formatted_log

Returns a log message formatted similarly to Django logs with color support for various HTTP statuses.

get_duck_formatted_log

This returns default duck formatted log with color support.

get_status_code_debug_color

Returns the respective color for the debug message of the status code.

get_status_debug_msg

Returns a debug message corresponding to an HTTP status code.

log_response

Logs a response to the console.

DataΒΆ

response_handler

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 stream usually 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.send to transmit the raw response data to the client and performs logging if disable_logging is 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 if HTTP/2 is enabled.

Raises:

Exception – If there is an error during the data sending process (e.g., socket errors), unless suppressed.

This method calls SocketIO.send to transmit the raw response data to the client and performs logging if disable_logging is 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 stream usually 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_response is that it submits the coroutine for sending response to AsyncioLoopManager and returns a SyncFuture you 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 if HTTP/2 is enabled.

Raises:

Exception – If there is an error during the data sending process (e.g., socket errors), unless suppressed.

This method calls SocketIO.send to transmit the raw response data to the client and performs logging if disable_logging is 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(…)’