summaryrefslogtreecommitdiffstats
path: root/Lib/BaseHTTPServer.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-10-26 13:01:36 (GMT)
committerGuido van Rossum <guido@python.org>1999-10-26 13:01:36 (GMT)
commitd65b53923ecefc96f24d9cbc4e3e83dd2194d6b4 (patch)
tree6181bfd5ca1bb24690c24de9d78f914f6e36ff8f /Lib/BaseHTTPServer.py
parent32a15adeab771ab8867b11dd2027be2b7e5d3f75 (diff)
downloadcpython-d65b53923ecefc96f24d9cbc4e3e83dd2194d6b4.zip
cpython-d65b53923ecefc96f24d9cbc4e3e83dd2194d6b4.tar.gz
cpython-d65b53923ecefc96f24d9cbc4e3e83dd2194d6b4.tar.bz2
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
request handling into separate parse_request() and handle_request() methods.
Diffstat (limited to 'Lib/BaseHTTPServer.py')
-rw-r--r--Lib/BaseHTTPServer.py39
1 files changed, 27 insertions, 12 deletions
diff --git a/Lib/BaseHTTPServer.py b/Lib/BaseHTTPServer.py
index 6707df2..10a706e 100644
--- a/Lib/BaseHTTPServer.py
+++ b/Lib/BaseHTTPServer.py
@@ -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()