duck.http.core.asgiΒΆ
This module provides the ASGI (Web Server Gateway Interface) for the Duck HTTP server.
ASGI is a specification that describes how a web server communicates with web applications. This module will define the ASGI callable that the server will use to serve requests.
Module ContentsΒΆ
ClassesΒΆ
Asynchronous Server Gateway Interface for the Duck HTTP server |
APIΒΆ
- class duck.http.core.asgi.ASGI(settings: Dict[str, Any])ΒΆ
Asynchronous Server Gateway Interface for the Duck HTTP server
Notes:
The ASGI callable is the entry point for the server to handle requests.
The ASGI callable will be called for each incoming request.
The ASGI callable will handle the request and send the response to the client.
The ASGI callable will be called with the following arguments: - application: The Duck application instance. - client_socket: The client xsocket object. - client_address: The client address tuple. - request_data: The raw request data from the client.
The ASGI is also responsible for sending request to remote servers like Django for processing.
Implement methods get_request, start_response and call to create your custom ASGI callable.
Initialization
- async __call__(application, client_socket: socket.socket, client_address: Tuple[str, int], request_data: duck.http.request_data.RequestData) Optional[duck.http.request.HttpRequest]ΒΆ
ASGI Application callable for handling requests
β¦ admonition:: Notes
This method is wrapped by a decorator (
if_error_log_then_raise), which handle any errors raised within this method.
- Parameters:
application β The Duck application instance.
client_socket β The client socket object.
client_address β The client address tuple.
request_data β The request data object
- Returns:
The handled request object.
- Return type:
HttpRequest
- async apply_middlewares_to_response(response, request)ΒΆ
Apply middlewares to the final response starting from the failed middleware or last middleware in list to the first middlewares. Its just like reversing middleware list and iterating through each and every one of them.
- async django_apply_middlewares_to_response(response: duck.http.core.proxyhandler.HttpProxyResponse, request)ΒΆ
Applies middlewares to the final http proxy response.
- async finalize_response(response, request: Optional[duck.http.request.HttpRequest] = None)ΒΆ
Finalizes response by adding final touches and sending response to client.
- async static get_request(client_socket: duck.utils.xsocket.xsocket, client_address: Tuple[str, int], request_data: duck.http.request_data.RequestData) duck.http.request.HttpRequestΒΆ
Construct a Request object from the data received from the client
- Parameters:
client_socket β The client xsocket object.
client_address β The client address tuple.
request_data β The request data object
- Returns:
The request object
- Return type:
HttpRequest
- async get_response(request: duck.http.request.HttpRequest) duck.http.response.HttpResponseΒΆ
Returns the full response for a request, with all middlewares and other configurations applied.
- Returns:
The corresponding HTTP response.
- Return type:
- Raises:
ExpectingNoResponse β Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
- async handle_django_connection_upgrade(upgrade_request: duck.http.request.HttpRequest, upgrade_response: duck.http.core.proxyhandler.HttpProxyResponse, protocol_receive_timeout: Union[int, float] = 10, protocol_receive_buffer: int = 4096)ΒΆ
This starts a loop for handling any external protocol like
WebSocketsfor Django.β¦ admonition:: Notes
This receives data to Django and sends the data back to client and it gets the data from client and send it to Django forever.
- async produce_final_response_failsafe(request: duck.http.request.HttpRequest, response_producer_callable: Callable, processor: Optional[AsyncRequestProcessor] = None) duck.http.response.HttpResponseΒΆ
Tries to produce a response a response using the given callable. It handles all errors and produces final response nomatter what.
This returns the full response for a request, with all middlewares and other configurations applied.
- Parameters:
request β Target HTTP request.
response_producer_callable β The callable for producing HTTP response.
processor β Asynchronous request processor for the request.
- Returns:
The corresponding HTTP response.
- Return type:
- Raises:
ExpectingNoResponse β Raised if we are never going to get a response e.g. when we reach a WebSocketView. This handles everything on its own and it will never return a response.
- async send_response(response, client_socket: duck.utils.xsocket.xsocket, request: Optional[duck.http.request.HttpRequest] = None, disable_logging: bool = False)ΒΆ
Asynchronously sends response to client.
- async start_response(request: duck.http.request.HttpRequest, response: Optional[duck.http.response.HttpResponse] = None)ΒΆ
Start the response to the client. This method should be called for handling and sending the response to client.
- Parameters:
request β The request object.
response β An optional response you want to send or the response will just be resolved by normal means.