[Python-Dev] BaseHTTPServer parsing
Andrew Dalke
dalke@dalkescientific.com
Mon, 2 Jun 2003 01:43:14 -0600
Hi,
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).
Andrew
dalke@dalkescientific.com