[Python-Dev] BaseHTTPServer parsing

Guido van Rossum guido@python.org
Mon, 02 Jun 2003 09:23:42 -0400


>    I would like to use the code in BaseHTTPServer to parse the incoming
> message for Twisted.  One way is to write an adapter which looks like
> 
> class _ParseHTTPRequest(BaseHTTPServer.BaseHTTPRequestHandler):
>    def __init__(self):
>      # Don't worry about doing the real constructor; I just want access 
> to
>      # the 'parse_request' function.
>      # I could also say "parse_request = 
> BaseHTTPServer.BaseHTTPRequestHandler.parse_request
>      pass
> 
>    def parse(self, raw_requestline, infile):
>      self.raw_requestline = raw_requestline
>      self.rfile = infile
>      self.error_info = False
>      self.parse_request()
> 
>    def send_error(self, code, message=None):
>      self.error_info = (code, message)
> 
> def parse(raw_requestline, infile):
>    p = _ParseHTTPRequest()
>    p.parse(raw_requestline, infile)
>    return p
> 
> # Then in the Twisted code
> 
>    def lineReceived(self, line):
>      if self.__first_line:
>        self.raw_requestline = line
>        self.__first_line = 0
>        self.rfile = StringIO()
>      else:
>        self.rfile.write(line + "\n")
>        if line == "": # end of headers
>          p = _ParseHTTPRequest()
>          if p.error_info is not None:
>             ... handle errors
>          else:
>             work with p.headers, p.comand, etc.
> 
> 
> That should work, but I note that the 'parse_request' function is
> listed as "internal", which I take to mean that people like me
> shouldn't make any assumptions that it even exists, much less that I
> can make use of it for my own nefarious reasons.
> 
> Can I get a better guarantee than that?  I ask because I would
> rather that Twisted and Python work the same way (esp. since the
> Python code is generally more robust).

Can't you just copy the code?  I'm not sure I understand the desire to
share code here.  Given that it's labeled internal, there is the real
possibility (even though there are no plans ATM) that
BaseHTTPRequestHandler will be refactored so that your adapter stops
working.

--Guido van Rossum (home page: http://www.python.org/~guido/)