From: Guido van Rossum Date: Tue, 26 Oct 1999 13:01:36 +0000 (+0000) Subject: Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the X-Git-Tag: v1.6a1~797 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d65b53923ecefc96f24d9cbc4e3e83dd2194d6b4;p=python Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the request handling into separate parse_request() and handle_request() methods. --- diff --git a/Lib/BaseHTTPServer.py b/Lib/BaseHTTPServer.py index 6707df21e3..10a706e1f8 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()