Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the

request handling into separate parse_request() and handle_request()
methods.
This commit is contained in:
Guido van Rossum 1999-10-26 13:01:36 +00:00
parent 32a15adeab
commit d65b53923e
1 changed files with 27 additions and 12 deletions

View File

@ -219,16 +219,17 @@ class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
# where each string is of the form name[/version].
server_version = "BaseHTTP/" + __version__
def handle(self):
"""Handle a single HTTP request.
def parse_request(self):
"""Parse a request (internal).
You normally don't need to override this method; see the class
__doc__ string for information on how to handle specific HTTP
commands such as GET and POST.
The request should be stored in self.raw_request; the results
are in self.command, self.path, self.request_version and
self.headers.
Return value is 1 for success, 0 for failure; on failure, an
error is sent back.
"""
self.raw_requestline = self.rfile.readline()
self.request_version = version = "HTTP/0.9" # Default
requestline = self.raw_requestline
if requestline[-2:] == '\r\n':
@ -241,21 +242,35 @@ class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
[command, path, version] = words
if version[:5] != 'HTTP/':
self.send_error(400, "Bad request version (%s)" % `version`)
return
return 0
elif len(words) == 2:
[command, path] = words
if command != 'GET':
self.send_error(400,
"Bad HTTP/0.9 request type (%s)" % `command`)
return
return 0
else:
self.send_error(400, "Bad request syntax (%s)" % `requestline`)
return
return 0
self.command, self.path, self.request_version = command, path, version
self.headers = self.MessageClass(self.rfile, 0)
mname = 'do_' + command
return 1
def handle(self):
"""Handle a single HTTP request.
You normally don't need to override this method; see the class
__doc__ string for information on how to handle specific HTTP
commands such as GET and POST.
"""
self.raw_requestline = self.rfile.readline()
if not self.parse_request(): # An error code has been sent, just exit
return
mname = 'do_' + self.command
if not hasattr(self, mname):
self.send_error(501, "Unsupported method (%s)" % `command`)
self.send_error(501, "Unsupported method (%s)" % `self.command`)
return
method = getattr(self, mname)
method()