ServerHandler
This class extends the standard library's server handler to provide specialized request and response management for development environments. It utilizes a limited input stream to ensure unread request data is properly handled and implements specific HTTP/1.1 header logic for persistent connections and HEAD requests. The handler also ensures connection state is correctly synchronized with the underlying request handler based on content length and server capabilities.
Attributes
| Attribute | Type | Description |
|---|---|---|
| http_version | string = 1.1 | The HTTP protocol version used for the server response, set to "1.1" to support persistent connections and RFC-compliant header handling. |
Constructor
Signature
def ServerHandler(
stdin: file-like object,
stdout: file-like object,
stderr: file-like object,
environ: dict,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| stdin | file-like object | The standard input stream for the request. |
| stdout | file-like object | The standard output stream for the response. |
| stderr | file-like object | The standard error stream. |
| environ | dict | A dictionary containing WSGI environment variables. |
| **kwargs | dict | Additional keyword arguments passed to the parent class constructor. |
Signature
def ServerHandler(
stdin: file-like object,
stdout: file-like object,
stderr: file-like object,
environ: dict,
kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| stdin | file-like object | The input stream containing the raw request body data |
| stdout | file-like object | The output stream where the response data is written |
| stderr | file-like object | The error stream for logging server-side issues |
| environ | dict | The WSGI environment dictionary containing request metadata |
| kwargs | dict | Additional keyword arguments passed to the base ServerHandler |
Methods
cleanup_headers()
@classmethod
def cleanup_headers() - > null
Adjusts HTTP headers to ensure compliance with HTTP/1.1 persistent connection rules and RFC 9110 HEAD request specifications. It forces a 'close' connection header if content length is unknown or if the server does not support threading.
Returns
| Type | Description |
|---|---|
null | None |
close()
@classmethod
def close() - > null
Closes the handler after consuming any remaining unread data from the input stream. This ensures the request is fully processed before the connection is terminated.
Returns
| Type | Description |
|---|---|
null | None |
finish_response()
@classmethod
def finish_response() - > null
Completes the HTTP response by either consuming the result iterator for HEAD requests without sending a body, or delegating to the standard response finalization for other methods.
Returns
| Type | Description |
|---|---|
null | None |