mirror of
https://github.com/python/cpython.git
synced 2025-10-13 10:23:28 +00:00
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:
parent
32a15adeab
commit
d65b53923e
1 changed files with 27 additions and 12 deletions
|
@ -219,16 +219,17 @@ class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
|
||||||
# where each string is of the form name[/version].
|
# where each string is of the form name[/version].
|
||||||
server_version = "BaseHTTP/" + __version__
|
server_version = "BaseHTTP/" + __version__
|
||||||
|
|
||||||
def handle(self):
|
def parse_request(self):
|
||||||
"""Handle a single HTTP request.
|
"""Parse a request (internal).
|
||||||
|
|
||||||
You normally don't need to override this method; see the class
|
The request should be stored in self.raw_request; the results
|
||||||
__doc__ string for information on how to handle specific HTTP
|
are in self.command, self.path, self.request_version and
|
||||||
commands such as GET and POST.
|
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
|
self.request_version = version = "HTTP/0.9" # Default
|
||||||
requestline = self.raw_requestline
|
requestline = self.raw_requestline
|
||||||
if requestline[-2:] == '\r\n':
|
if requestline[-2:] == '\r\n':
|
||||||
|
@ -241,21 +242,35 @@ class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
|
||||||
[command, path, version] = words
|
[command, path, version] = words
|
||||||
if version[:5] != 'HTTP/':
|
if version[:5] != 'HTTP/':
|
||||||
self.send_error(400, "Bad request version (%s)" % `version`)
|
self.send_error(400, "Bad request version (%s)" % `version`)
|
||||||
return
|
return 0
|
||||||
elif len(words) == 2:
|
elif len(words) == 2:
|
||||||
[command, path] = words
|
[command, path] = words
|
||||||
if command != 'GET':
|
if command != 'GET':
|
||||||
self.send_error(400,
|
self.send_error(400,
|
||||||
"Bad HTTP/0.9 request type (%s)" % `command`)
|
"Bad HTTP/0.9 request type (%s)" % `command`)
|
||||||
return
|
return 0
|
||||||
else:
|
else:
|
||||||
self.send_error(400, "Bad request syntax (%s)" % `requestline`)
|
self.send_error(400, "Bad request syntax (%s)" % `requestline`)
|
||||||
return
|
return 0
|
||||||
self.command, self.path, self.request_version = command, path, version
|
self.command, self.path, self.request_version = command, path, version
|
||||||
self.headers = self.MessageClass(self.rfile, 0)
|
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):
|
if not hasattr(self, mname):
|
||||||
self.send_error(501, "Unsupported method (%s)" % `command`)
|
self.send_error(501, "Unsupported method (%s)" % `self.command`)
|
||||||
return
|
return
|
||||||
method = getattr(self, mname)
|
method = getattr(self, mname)
|
||||||
method()
|
method()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue