blob: 6707df21e33312627a85838331370afdfab20ff2 [file] [log] [blame]
Guido van Rossume7e578f1995-08-04 04:00:20 +00001"""HTTP server base class.
2
3Note: the class in this module doesn't implement any HTTP request; see
4SimpleHTTPServer for simple implementations of GET, HEAD and POST
5(including CGI scripts).
6
7Contents:
8
9- BaseHTTPRequestHandler: HTTP request handler base class
10- test: test function
11
12XXX To do:
13
14- send server version
15- log requests even later (to capture byte count)
16- log user-agent header and other interesting goodies
17- send error log to separate file
18- are request names really case sensitive?
19
20"""
21
22
23# See also:
24#
25# HTTP Working Group T. Berners-Lee
26# INTERNET-DRAFT R. T. Fielding
27# <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
28# Expires September 8, 1995 March 8, 1995
29#
30# URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
31
32
33# Log files
34# ---------
35#
36# Here's a quote from the NCSA httpd docs about log file format.
37#
38# | The logfile format is as follows. Each line consists of:
39# |
40# | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
41# |
42# | host: Either the DNS name or the IP number of the remote client
43# | rfc931: Any information returned by identd for this person,
44# | - otherwise.
45# | authuser: If user sent a userid for authentication, the user name,
46# | - otherwise.
47# | DD: Day
48# | Mon: Month (calendar name)
49# | YYYY: Year
50# | hh: hour (24-hour format, the machine's timezone)
51# | mm: minutes
52# | ss: seconds
53# | request: The first line of the HTTP request as sent by the client.
54# | ddd: the status code returned by the server, - if not available.
55# | bbbb: the total number of bytes sent,
56# | *not including the HTTP/1.0 header*, - if not available
57# |
58# | You can determine the name of the file accessed through request.
59#
60# (Actually, the latter is only true if you know the server configuration
61# at the time the request was made!)
62
63
64__version__ = "0.2"
65
66
67import sys
68import time
69import socket # For gethostbyaddr()
70import string
Guido van Rossume7e578f1995-08-04 04:00:20 +000071import mimetools
72import SocketServer
73
74# Default error message
75DEFAULT_ERROR_MESSAGE = """\
76<head>
77<title>Error response</title>
78</head>
79<body>
80<h1>Error response</h1>
81<p>Error code %(code)d.
82<p>Message: %(message)s.
83<p>Error code explanation: %(code)s = %(explain)s.
84</body>
85"""
86
87
88class HTTPServer(SocketServer.TCPServer):
89
90 def server_bind(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000091 """Override server_bind to store the server name."""
92 SocketServer.TCPServer.server_bind(self)
93 host, port = self.socket.getsockname()
94 if not host or host == '0.0.0.0':
95 host = socket.gethostname()
Guido van Rossum145a5f71999-06-09 15:05:47 +000096 try:
97 hostname, hostnames, hostaddrs = socket.gethostbyaddr(host)
98 except socket.error:
99 hostname = host
100 else:
101 if '.' not in hostname:
102 for host in hostnames:
103 if '.' in host:
104 hostname = host
105 break
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000106 self.server_name = hostname
107 self.server_port = port
Guido van Rossume7e578f1995-08-04 04:00:20 +0000108
109
110class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
111
112 """HTTP request handler base class.
113
114 The following explanation of HTTP serves to guide you through the
115 code as well as to expose any misunderstandings I may have about
116 HTTP (so you don't need to read the code to figure out I'm wrong
117 :-).
118
119 HTTP (HyperText Transfer Protocol) is an extensible protocol on
120 top of a reliable stream transport (e.g. TCP/IP). The protocol
121 recognizes three parts to a request:
122
123 1. One line identifying the request type and path
124 2. An optional set of RFC-822-style headers
125 3. An optional data part
126
127 The headers and data are separated by a blank line.
128
129 The first line of the request has the form
130
131 <command> <path> <version>
132
133 where <command> is a (case-sensitive) keyword such as GET or POST,
134 <path> is a string containing path information for the request,
135 and <version> should be the string "HTTP/1.0". <path> is encoded
136 using the URL encoding scheme (using %xx to signify the ASCII
137 character with hex code xx).
138
139 The protocol is vague about whether lines are separated by LF
140 characters or by CRLF pairs -- for compatibility with the widest
141 range of clients, both should be accepted. Similarly, whitespace
142 in the request line should be treated sensibly (allowing multiple
143 spaces between components and allowing trailing whitespace).
144
145 Similarly, for output, lines ought to be separated by CRLF pairs
146 but most clients grok LF characters just fine.
147
148 If the first line of the request has the form
149
150 <command> <path>
151
152 (i.e. <version> is left out) then this is assumed to be an HTTP
153 0.9 request; this form has no optional headers and data part and
154 the reply consists of just the data.
155
156 The reply form of the HTTP 1.0 protocol again has three parts:
157
158 1. One line giving the response code
159 2. An optional set of RFC-822-style headers
160 3. The data
161
162 Again, the headers and data are separated by a blank line.
163
164 The response code line has the form
165
166 <version> <responsecode> <responsestring>
167
168 where <version> is the protocol version (always "HTTP/1.0"),
169 <responsecode> is a 3-digit response code indicating success or
170 failure of the request, and <responsestring> is an optional
171 human-readable string explaining what the response code means.
172
173 This server parses the request and the headers, and then calls a
174 function specific to the request type (<command>). Specifically,
Guido van Rossumba895d81999-09-15 15:28:25 +0000175 a request SPAM will be handled by a method do_SPAM(). If no
Guido van Rossume7e578f1995-08-04 04:00:20 +0000176 such method exists the server sends an error response to the
177 client. If it exists, it is called with no arguments:
178
179 do_SPAM()
180
181 Note that the request name is case sensitive (i.e. SPAM and spam
182 are different requests).
183
184 The various request details are stored in instance variables:
185
186 - client_address is the client IP address in the form (host,
187 port);
188
189 - command, path and version are the broken-down request line;
190
191 - headers is an instance of mimetools.Message (or a derived
192 class) containing the header information;
193
194 - rfile is a file object open for reading positioned at the
195 start of the optional input data part;
196
197 - wfile is a file object open for writing.
198
199 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
200
201 The first thing to be written must be the response line. Then
202 follow 0 or more header lines, then a blank line, and then the
203 actual data (if any). The meaning of the header lines depends on
204 the command executed by the server; in most cases, when data is
205 returned, there should be at least one header line of the form
206
207 Content-type: <type>/<subtype>
208
209 where <type> and <subtype> should be registered MIME types,
210 e.g. "text/html" or "text/plain".
211
212 """
213
214 # The Python system version, truncated to its first component.
215 sys_version = "Python/" + string.split(sys.version)[0]
216
217 # The server software version. You may want to override this.
218 # The format is multiple whitespace-separated strings,
219 # where each string is of the form name[/version].
220 server_version = "BaseHTTP/" + __version__
221
222 def handle(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000223 """Handle a single HTTP request.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000224
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000225 You normally don't need to override this method; see the class
226 __doc__ string for information on how to handle specific HTTP
227 commands such as GET and POST.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000228
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000229 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000230
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000231 self.raw_requestline = self.rfile.readline()
232 self.request_version = version = "HTTP/0.9" # Default
233 requestline = self.raw_requestline
234 if requestline[-2:] == '\r\n':
235 requestline = requestline[:-2]
236 elif requestline[-1:] == '\n':
237 requestline = requestline[:-1]
238 self.requestline = requestline
239 words = string.split(requestline)
240 if len(words) == 3:
241 [command, path, version] = words
242 if version[:5] != 'HTTP/':
243 self.send_error(400, "Bad request version (%s)" % `version`)
244 return
245 elif len(words) == 2:
246 [command, path] = words
247 if command != 'GET':
248 self.send_error(400,
249 "Bad HTTP/0.9 request type (%s)" % `command`)
250 return
251 else:
252 self.send_error(400, "Bad request syntax (%s)" % `requestline`)
253 return
254 self.command, self.path, self.request_version = command, path, version
255 self.headers = self.MessageClass(self.rfile, 0)
256 mname = 'do_' + command
257 if not hasattr(self, mname):
Guido van Rossum60e73301999-03-30 20:17:31 +0000258 self.send_error(501, "Unsupported method (%s)" % `command`)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000259 return
260 method = getattr(self, mname)
261 method()
Guido van Rossume7e578f1995-08-04 04:00:20 +0000262
263 def send_error(self, code, message=None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000264 """Send and log an error reply.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000265
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000266 Arguments are the error code, and a detailed message.
267 The detailed message defaults to the short entry matching the
268 response code.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000269
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000270 This sends an error response (so it must be called before any
271 output has been generated), logs the error, and finally sends
272 a piece of HTML explaining the error to the user.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000273
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000274 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000275
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000276 try:
277 short, long = self.responses[code]
278 except KeyError:
279 short, long = '???', '???'
280 if not message:
281 message = short
282 explain = long
283 self.log_error("code %d, message %s", code, message)
284 self.send_response(code, message)
285 self.end_headers()
286 self.wfile.write(self.error_message_format %
287 {'code': code,
288 'message': message,
289 'explain': explain})
Guido van Rossume7e578f1995-08-04 04:00:20 +0000290
291 error_message_format = DEFAULT_ERROR_MESSAGE
292
293 def send_response(self, code, message=None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000294 """Send the response header and log the response code.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000295
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000296 Also send two standard headers with the server software
297 version and the current date.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000298
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000299 """
300 self.log_request(code)
301 if message is None:
302 if self.responses.has_key(code):
303 message = self.responses[code][0]
304 else:
305 message = ''
306 if self.request_version != 'HTTP/0.9':
307 self.wfile.write("%s %s %s\r\n" %
308 (self.protocol_version, str(code), message))
309 self.send_header('Server', self.version_string())
310 self.send_header('Date', self.date_time_string())
Guido van Rossume7e578f1995-08-04 04:00:20 +0000311
312 def send_header(self, keyword, value):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000313 """Send a MIME header."""
314 if self.request_version != 'HTTP/0.9':
315 self.wfile.write("%s: %s\r\n" % (keyword, value))
Guido van Rossume7e578f1995-08-04 04:00:20 +0000316
317 def end_headers(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000318 """Send the blank line ending the MIME headers."""
319 if self.request_version != 'HTTP/0.9':
320 self.wfile.write("\r\n")
Guido van Rossume7e578f1995-08-04 04:00:20 +0000321
322 def log_request(self, code='-', size='-'):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000323 """Log an accepted request.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000324
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000325 This is called by send_reponse().
Guido van Rossume7e578f1995-08-04 04:00:20 +0000326
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000327 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000328
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000329 self.log_message('"%s" %s %s',
330 self.requestline, str(code), str(size))
Guido van Rossume7e578f1995-08-04 04:00:20 +0000331
332 def log_error(self, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000333 """Log an error.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000334
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000335 This is called when a request cannot be fulfilled. By
336 default it passes the message on to log_message().
Guido van Rossume7e578f1995-08-04 04:00:20 +0000337
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000338 Arguments are the same as for log_message().
Guido van Rossume7e578f1995-08-04 04:00:20 +0000339
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000340 XXX This should go to the separate error log.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000341
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000342 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000343
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000344 apply(self.log_message, args)
Guido van Rossume7e578f1995-08-04 04:00:20 +0000345
346 def log_message(self, format, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000347 """Log an arbitrary message.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000348
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000349 This is used by all other logging functions. Override
350 it if you have specific logging wishes.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000351
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000352 The first argument, FORMAT, is a format string for the
353 message to be logged. If the format string contains
354 any % escapes requiring parameters, they should be
355 specified as subsequent arguments (it's just like
356 printf!).
Guido van Rossume7e578f1995-08-04 04:00:20 +0000357
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000358 The client host and current date/time are prefixed to
359 every message.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000360
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000361 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000362
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000363 sys.stderr.write("%s - - [%s] %s\n" %
364 (self.address_string(),
365 self.log_date_time_string(),
366 format%args))
Guido van Rossume7e578f1995-08-04 04:00:20 +0000367
368 def version_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000369 """Return the server software version string."""
370 return self.server_version + ' ' + self.sys_version
Guido van Rossume7e578f1995-08-04 04:00:20 +0000371
372 def date_time_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000373 """Return the current date and time formatted for a message header."""
374 now = time.time()
375 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
376 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
377 self.weekdayname[wd],
378 day, self.monthname[month], year,
379 hh, mm, ss)
380 return s
Guido van Rossume7e578f1995-08-04 04:00:20 +0000381
382 def log_date_time_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000383 """Return the current time formatted for logging."""
384 now = time.time()
385 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
386 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
387 day, self.monthname[month], year, hh, mm, ss)
388 return s
Guido van Rossume7e578f1995-08-04 04:00:20 +0000389
390 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
391
392 monthname = [None,
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000393 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
394 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Guido van Rossume7e578f1995-08-04 04:00:20 +0000395
396 def address_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000397 """Return the client address formatted for logging.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000398
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000399 This version looks up the full hostname using gethostbyaddr(),
400 and tries to find a name that contains at least one dot.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000401
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000402 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000403
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000404 (host, port) = self.client_address
405 try:
406 name, names, addresses = socket.gethostbyaddr(host)
407 except socket.error, msg:
408 return host
409 names.insert(0, name)
410 for name in names:
411 if '.' in name: return name
412 return names[0]
Guido van Rossume7e578f1995-08-04 04:00:20 +0000413
414
415 # Essentially static class variables
416
417 # The version of the HTTP protocol we support.
418 # Don't override unless you know what you're doing (hint: incoming
419 # requests are required to have exactly this version string).
420 protocol_version = "HTTP/1.0"
421
422 # The Message-like class used to parse headers
423 MessageClass = mimetools.Message
424
425 # Table mapping response codes to messages; entries have the
426 # form {code: (shortmessage, longmessage)}.
427 # See http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
428 responses = {
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000429 200: ('OK', 'Request fulfilled, document follows'),
430 201: ('Created', 'Document created, URL follows'),
431 202: ('Accepted',
432 'Request accepted, processing continues off-line'),
433 203: ('Partial information', 'Request fulfilled from cache'),
434 204: ('No response', 'Request fulfilled, nothing follows'),
435
436 301: ('Moved', 'Object moved permanently -- see URI list'),
437 302: ('Found', 'Object moved temporarily -- see URI list'),
438 303: ('Method', 'Object moved -- see Method and URL list'),
439 304: ('Not modified',
440 'Document has not changed singe given time'),
441
442 400: ('Bad request',
443 'Bad request syntax or unsupported method'),
444 401: ('Unauthorized',
445 'No permission -- see authorization schemes'),
446 402: ('Payment required',
447 'No payment -- see charging schemes'),
448 403: ('Forbidden',
449 'Request forbidden -- authorization will not help'),
450 404: ('Not found', 'Nothing matches the given URI'),
451
452 500: ('Internal error', 'Server got itself in trouble'),
453 501: ('Not implemented',
454 'Server does not support this operation'),
455 502: ('Service temporarily overloaded',
456 'The server cannot process the request due to a high load'),
457 503: ('Gateway timeout',
458 'The gateway server did not receive a timely response'),
459
460 }
Guido van Rossume7e578f1995-08-04 04:00:20 +0000461
462
463def test(HandlerClass = BaseHTTPRequestHandler,
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000464 ServerClass = HTTPServer):
Guido van Rossume7e578f1995-08-04 04:00:20 +0000465 """Test the HTTP request handler class.
466
467 This runs an HTTP server on port 8000 (or the first command line
468 argument).
469
470 """
471
472 if sys.argv[1:]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000473 port = string.atoi(sys.argv[1])
Guido van Rossume7e578f1995-08-04 04:00:20 +0000474 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000475 port = 8000
Guido van Rossume7e578f1995-08-04 04:00:20 +0000476 server_address = ('', port)
477
478 httpd = ServerClass(server_address, HandlerClass)
479
480 print "Serving HTTP on port", port, "..."
481 httpd.serve_forever()
482
483
484if __name__ == '__main__':
485 test()