classification
Title: Make WSGIRequestHandler easier to be customized by the user
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Manjusaka
Priority: normal Keywords: patch

Created on 2020-05-20 15:54 by Manjusaka, last changed 2020-05-28 15:09 by Manjusaka.

Pull Requests
URL Status Linked Edit
PR 20262 open Manjusaka, 2020-05-20 16:12
Messages (2)
msg369465 - (view) Author: Manjusaka (Manjusaka) * Date: 2020-05-20 15:54
Hello everyone

I think we can make WSGIRequestHandler in wsgiref easier to be customized by the user

Here's the detail

the WSGIRequestHandler in wsgiref.simple_server has some code like this

class WSGIRequestHandler(BaseHTTPRequestHandler):
    def handle(self):
        """Handle a single HTTP request"""

        self.raw_requestline = self.rfile.readline(65537)
        if len(self.raw_requestline) > 65536:
            self.requestline = ''
            self.request_version = ''
            self.command = ''
            self.send_error(414)
            return

        if not self.parse_request(): # An error code has been sent, just exit
            return

        handler = ServerHandler(
            self.rfile, self.wfile, self.get_stderr(), self.get_environ(),
            multithread=False,
        )
        handler.request_handler = self      # backpointer for logging
        handler.run(self.server.get_app())

If people want just to replace the ServerHandler, they need to override all the handle method, I don't think this is a good way to use.

I prefer do something like this

class WSGIRequestHandler(BaseHTTPRequestHandler):

    server_version = "WSGIServer/" + __version__
    server_handler = ServerHandler

    def handle(self):
        """Handle a single HTTP request"""

        self.raw_requestline = self.rfile.readline(65537)
        if len(self.raw_requestline) > 65536:
            self.requestline = ''
            self.request_version = ''
            self.command = ''
            self.send_error(414)
            return

        if not self.parse_request(): # An error code has been sent, just exit
            return

        handler = self.server_handler(
            self.rfile, self.wfile, self.get_stderr(), self.get_environ(),
            multithread=False,
        )
        handler.request_handler = self      # backpointer for logging
        handler.run(self.server.get_app())

Now if people just need simple code to replace the ServerHandler, like this

class CustomWSGIRequestHandler(WSGIRequestHandler):
    server_handler = CustomeServerHandler

what do you think, I'm glad to make a PR for this
msg370224 - (view) Author: Manjusaka (Manjusaka) * Date: 2020-05-28 15:09
ping~
History
Date User Action Args
2020-05-28 15:09:56Manjusakasetmessages: + msg370224
2020-05-20 16:12:50Manjusakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request19548
2020-05-20 15:54:01Manjusakacreate