duck.http.core.proxyhandler¶
Module containing proxy handler class.
Module Contents¶
Classes¶
An asynchronous HttpProxyHandler class to handle forwarding TCP requests to target hosts. |
|
HttpProxyHandler class to handle forwarding TCP requests to target hosts. |
|
A synchronous subclass of StreamingHttpResponse that handles HTTP responses in a proxy scenario, including cases where the response content is incomplete or partially received. |
Data¶
API¶
- class duck.http.core.proxyhandler.AsyncHttpProxyHandler(target_host: str, target_port: int, uses_ipv6: bool = False, uses_ssl: bool = False)¶
Bases:
duck.http.core.proxyhandler.HttpProxyHandlerAn asynchronous HttpProxyHandler class to handle forwarding TCP requests to target hosts.
This class supports both IPv4 and IPv6 and allows modification of headers before forwarding the data to the client.
Initialization
Initializes the ProxyHandler.
- Parameters:
uses_ipv6 – Boolean indicating whether to use IPv6. Defaults to False.
- async connect_to_target() duck.utils.xsocket.xsocket¶
Asynchronously connects to the target server.
- Returns:
The xsocket object connected to the target server.
- Return type:
- async fetch_response_payload(target_socket: duck.utils.xsocket.xsocket) Tuple[duck.http.response_payload.SimpleHttpResponsePayload, bytes]¶
Returns received Response Payload and leftover data from target server.
- Returns:
This is SimpleHttpResponsePayload object and leftover_data (partial data sent from target server after headers).
- Return type:
SimpleHttpResponsePayload, bytes
- async forward_request_to_target(request: duck.http.request.HttpRequest, client_socket: duck.utils.xsocket.xsocket, target_socket: duck.utils.xsocket.xsocket)¶
Asynchronously forwards the client’s request to the target server.
- Parameters:
request – Client Http request object to forward to target server.
client_socket – The socket object connected to the client.
target_socket – The socket object connected to the target server.
- async get_response(request: duck.http.request.HttpRequest, client_socket: socket.socket) duck.http.core.proxyhandler.HttpProxyResponse¶
Asynchronously handles the client connection and forwards the request to the target server and returns partial response. To receive more response, use method HttpPartialResponse.recvmore, this method may raise
- Parameters:
request – Client Http request object.
client_socket – The socket object connected to the client.
- Returns:
The partial unfinished response ready for processing but lacking full content.
- Return type:
- exception duck.http.core.proxyhandler.BadGatewayError¶
Bases:
duck.http.core.proxyhandler.ReverseProxyErrorCustom exception for bad gateway errors (HTTP 502).
This exception is used to signal that the request could not be processed because of a server-side issue, often related to a gateway or proxy.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- duck.http.core.proxyhandler.CONNECT_TIMEOUT¶
None
- duck.http.core.proxyhandler.DJANGO_REQUEST_FIXER_MIDDLEWARE¶
‘x_import(…)’
- class duck.http.core.proxyhandler.HttpProxyHandler(target_host: str, target_port: int, uses_ipv6: bool = False, uses_ssl: bool = False)¶
HttpProxyHandler class to handle forwarding TCP requests to target hosts.
This class supports both IPv4 and IPv6 and allows modification of headers before forwarding the data to the client.
Initialization
Initializes the ProxyHandler.
- Parameters:
uses_ipv6 – Boolean indicating whether to use IPv6. Defaults to False.
- __slots__¶
(‘uses_ipv6’, ‘target_host’, ‘target_port’, ‘uses_ssl’)
- connect_to_target() duck.utils.xsocket.xsocket¶
Connects to the target server.
- Returns:
The xsocket object connected to the target server.
- Return type:
- fetch_response_payload(target_socket: duck.utils.xsocket.xsocket) Tuple[duck.http.response_payload.SimpleHttpResponsePayload, bytes]¶
Returns received Response Payload and leftover data from target server.
- Returns:
This is SimpleHttpResponsePayload object and leftover_data (partial data sent from target server after headers).
- Return type:
SimpleHttpResponsePayload, bytes
- forward_request_to_target(request: duck.http.request.HttpRequest, client_socket: duck.utils.xsocket.xsocket, target_socket: duck.utils.xsocket.xsocket)¶
Forwards the client’s request to the target server.
- Parameters:
request – Client Http request object to forward to target server.
client_socket – The socket object connected to the client.
target_socket – The socket object connected to the target server.
- get_response(request: duck.http.request.HttpRequest, client_socket: socket.socket) duck.http.core.proxyhandler.HttpProxyResponse¶
Handles the client connection and forwards the request to the target server and returns partial response. To receive more response, use method HttpPartialResponse.recvmore, this method may raise
- Parameters:
request – Client Http request object.
client_socket – The socket object connected to the client.
- Returns:
The partial unfinished response ready for processing but lacking full content.
- Return type:
- modify_client_request(request: duck.http.request.HttpRequest)¶
Modifies the Http request if needed.
- Parameters:
request – Client Http request object.
- class duck.http.core.proxyhandler.HttpProxyResponse(target_socket: duck.utils.xsocket.xsocket, payload_obj: duck.http.response_payload.BaseResponsePayload, content_obj: Optional[duck.http.content.Content] = None, chunk_size: int = STREAM_CHUNK_SIZE)¶
Bases:
duck.http.response.StreamingHttpResponseA synchronous subclass of StreamingHttpResponse that handles HTTP responses in a proxy scenario, including cases where the response content is incomplete or partially received.
This class is specifically designed to handle situations where:
Only the HTTP response headers are available, and content is streamed progressively.
The response content is incomplete, and the response is processed as it is received.
It is ideal for proxy servers or intermediary systems that need to pass along HTTP responses from a remote server to the client while processing and streaming the data in chunks, minimizing memory usage by not requiring the full response to be loaded at once.
Key Features:
Streaming: Supports efficient, memory-friendly streaming of content from the proxy server to the client.
Partial Responses: Handles scenarios where the response is incomplete or streaming, such as large files or slow connections.
Real-Time Processing: Allows the server to process parts of the response as they arrive without blocking or waiting for the full content.
This class can be extended for custom logic related to response manipulation, such as modifying headers, handling chunk sizes, or managing specific proxy behaviors.
Example Usage:
response = HttpProxyResponse( target_socket, payload_obj, content_obj, chunk_size, ) for content in response.iter_content(): # Content logic here pass- Variables:
target_socket – The socket already connected used for communication with the proxy server.
payload_obj – The response payload associated with the HTTP response.
content_obj – Content object with the initial or incomplete content.
chunk_size – The streaming chunk size.
Initialization
Initialize the HttpProxyResponse object.
- Parameters:
target_socket – The live xsocket connection used to receive content.
payload_obj – The response payload object containing headers and metadata.
content_obj – The initial partial content object, if available.
chunk_size – The size (in bytes) of each chunk to stream. Defaults to STREAM_CHUNK_SIZE.
- __repr__()¶
- async async_iter_content() AsyncGenerator[bytes, None]¶
An asynchronous generator to stream the current content followed by additional data as it is received.
- Yields:
- py:
class:
bytes– The next chunk of data to stream.
- async async_recv_more(buffer: int = 4096) bytes¶
Asynchronously receive additional data from the target socket.
- Parameters:
buffer – The buffer size for receiving data. Defaults to 4096 bytes.
- Returns:
The received data, or an empty byte string if the content is complete.
- Return type:
bytes
- iter_content() Generator[bytes, None, None]¶
A generator to stream the current content followed by additional data as it is received.
- Yields:
- py:
class:
bytes– The next chunk of data to stream.
- recv_more(buffer: int = 4096) bytes¶
Receive additional data from the target socket.
- Parameters:
buffer – The buffer size for receiving data. Defaults to 4096 bytes.
- Returns:
The received data, or an empty byte string if the content is complete.
- Return type:
bytes
- duck.http.core.proxyhandler.PAYLOAD_BUFFER_SIZE¶
None
- duck.http.core.proxyhandler.READ_TIMEOUT¶
None
- exception duck.http.core.proxyhandler.ReverseProxyError¶
Bases:
ExceptionRaised on exceptions encountered on reverse-proxy ops.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- duck.http.core.proxyhandler.STREAM_CHUNK_SIZE¶
None