Module

server.request_handler

Custom HTTP request handler for the dev server.

Provides beautiful logging, custom 404 pages, and live reload support.

Classes

BengalRequestHandler
Custom HTTP request handler with beautiful logging, custom 404 page, and live reload support. This…
8

Custom HTTP request handler with beautiful logging, custom 404 page, and live reload support.

This handler combines:

  • RequestLogger: Beautiful, minimal HTTP request logging
  • LiveReloadMixin: Server-Sent Events for hot reload
  • SimpleHTTPRequestHandler: Standard HTTP file serving
Inherits from RequestLogger,LiveReloadMixin,http.server.SimpleHTTPRequestHandler

Attributes

Name Type Description
_html_cache dict[tuple[str, float], bytes]

Methods 5

end_headers
0 None
def end_headers(self) -> None
handle
Override handle to suppress BrokenPipeError tracebacks.
0 None
def handle(self) -> None

Override handle to suppress BrokenPipeError tracebacks.

translate_path
Override translate_path to ensure it never returns None. In Python 3.14, when …
1 str
def translate_path(self, path: str) -> str

Override translate_path to ensure it never returns None.

In Python 3.14, when self.directory is None and os.getcwd() fails, translate_path returns None. This causes crashes in the parent class's do_GET() -> send_head() -> os.path.isdir(path).

We guard against this by falling back to the configured directory.

Parameters 1
path str
Returns

str

do_GET
Override GET to support SSE and safe HTML injection via mixin. Request flow: -…
0 None
def do_GET(self) -> None

Override GET to support SSE and safe HTML injection via mixin.

Request flow:

  • Serve SSE endpoint at /bengal_reload (long-lived connection)
  • Try to serve HTML with injected live-reload script
  • Fallback to default file serving for non-HTML
send_error
Override send_error to serve custom 404 page.
3 None
def send_error(self, code: int, message: str | None = None, explain: str | None = None) -> None

Override send_error to serve custom 404 page.

Parameters 3
code int

HTTP error code

message str | None

Error message

explain str | None

Detailed explanation

Internal Methods 3
__init__
Initialize the request handler. Pre-initializes headers and request_version to…
0 None
def __init__(self, *args: Any, **kwargs: Any) -> None

Initialize the request handler.

Pre-initializes headers and request_version to avoid AttributeError when tests bypass normal request parsing flow. The parent class will properly set these during normal HTTP request handling.

_might_be_html
Quick check if request might return HTML. This is a fast pre-filter to avoid b…
1 bool
def _might_be_html(self, path: str) -> bool

Quick check if request might return HTML.

This is a fast pre-filter to avoid buffering responses that are definitely not HTML (like CSS, JS, images).

Parameters 1
path str

Request path

Returns

bool

True if request might return HTML, False if definitely not HTML

_is_html_response
Check if response is HTML by inspecting headers and content.
1 bool
def _is_html_response(self, response_data: bytes) -> bool

Check if response is HTML by inspecting headers and content.

Parameters 1
response_data bytes

Complete HTTP response (headers + body)

Returns

bool

True if response is HTML, False otherwise