Classes
BengalRequestHandler
Custom HTTP request handler with beautiful logging, custom 404 page, and live reload support.
This…
BengalRequestHandler
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
RequestLogger,LiveReloadMixin,http.server.SimpleHTTPRequestHandlerAttributes
| Name | Type | Description |
|---|---|---|
_html_cache |
dict[tuple[str, float], bytes] |
Methods 5
end_headers
end_headers
def end_headers(self) -> None
handle
Override handle to suppress BrokenPipeError tracebacks.
handle
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 …
translate_path
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:
-…
do_GET
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.
send_error
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…
__init__
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…
_might_be_html
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
True if request might return HTML, False if definitely not HTMLbool
—
_is_html_response
Check if response is HTML by inspecting headers and content.
_is_html_response
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
True if response is HTML, False otherwisebool
—