Skip to main content

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

AttributeTypeDescription
http_versionstring = 1.1The 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

NameTypeDescription
stdinfile-like objectThe standard input stream for the request.
stdoutfile-like objectThe standard output stream for the response.
stderrfile-like objectThe standard error stream.
environdictA dictionary containing WSGI environment variables.
**kwargsdictAdditional 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

NameTypeDescription
stdinfile-like objectThe input stream containing the raw request body data
stdoutfile-like objectThe output stream where the response data is written
stderrfile-like objectThe error stream for logging server-side issues
environdictThe WSGI environment dictionary containing request metadata
kwargsdictAdditional 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

TypeDescription
nullNone

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

TypeDescription
nullNone

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

TypeDescription
nullNone