[New-bugs-announce] [issue23255] SimpleHTTPRequestHandler refactor for more extensible usage.

Ent report at bugs.python.org
Sat Jan 17 14:51:58 CET 2015

New submission from Ent:

Use of http.server.BaseHTTPRequestHandler for exploratory or simple usage, SimpleHTTPRequestHandler provides a good platform to start on. However, the current code in SimpleHTTPRequestHandler's send_head is tightly coupled together as a single unit.

This patch aims to break send_head down into composable parts so that any developer can subclass SimpleHTTPRequestHandler to create one's own HTTPRequestHandler class with their own custom behaviour without having to rewrite a lot of repeated code.

For example consider a developer wanting to address two usecases
* Use SimpleHTTPRequestHandler, but use index.html from a different directory.
* For certain GET urls, call on a specific function to response.
* Use custom html page instead of index.html

class CustomHTTPRequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path =='/':
            f = self.home_head()
        elif self.path in self.custom_paths:
            f = self.special_head()
            f = self.send_head()
        # ...
        # Standard Code

As a result of the patch, in above scenario instead of copy-pasting logic for browser redirection, getting object for file or directory and, applying headers upon success; Developer can use methods redirect_browser, get_dir_or_html_dir_path and, apply_headers to reduce the code.

Basically, applying DRY principle.

Note: Since this is but refactoring of existing code without any new functionality being added, no test cases are provided.

components: Library (Lib)
files: simplehttp.patch
keywords: patch
messages: 234165
nosy: last-ent
priority: normal
severity: normal
status: open
title: SimpleHTTPRequestHandler refactor for more extensible usage.
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file37736/simplehttp.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list