blob: e571418cc002e4b157e065705bf0a3cd07dd3029 [file] [log] [blame]
Georg Brandl24420152008-05-26 16:32:26 +00001"""HTTP server classes.
2
3Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see
4SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST,
5and CGIHTTPRequestHandler for CGI scripts.
6
7It does, however, optionally implement HTTP/1.1 persistent connections,
8as of version 0.3.
9
10Notes on CGIHTTPRequestHandler
11------------------------------
12
13This class implements GET and POST requests to cgi-bin scripts.
14
15If the os.fork() function is not present (e.g. on Windows),
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +000016subprocess.Popen() is used as a fallback, with slightly altered semantics.
Georg Brandl24420152008-05-26 16:32:26 +000017
18In all cases, the implementation is intentionally naive -- all
19requests are executed synchronously.
20
21SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
22-- it may execute arbitrary Python code or external programs.
23
24Note that status code 200 is sent prior to execution of a CGI script, so
25scripts cannot send other status codes such as 302 (redirect).
26
27XXX To do:
28
29- log requests even later (to capture byte count)
30- log user-agent header and other interesting goodies
31- send error log to separate file
32"""
33
34
35# See also:
36#
37# HTTP Working Group T. Berners-Lee
38# INTERNET-DRAFT R. T. Fielding
39# <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
40# Expires September 8, 1995 March 8, 1995
41#
42# URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
43#
44# and
45#
46# Network Working Group R. Fielding
47# Request for Comments: 2616 et al
48# Obsoletes: 2068 June 1999
49# Category: Standards Track
50#
51# URL: http://www.faqs.org/rfcs/rfc2616.html
52
53# Log files
54# ---------
55#
56# Here's a quote from the NCSA httpd docs about log file format.
57#
58# | The logfile format is as follows. Each line consists of:
59# |
60# | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
61# |
62# | host: Either the DNS name or the IP number of the remote client
63# | rfc931: Any information returned by identd for this person,
64# | - otherwise.
65# | authuser: If user sent a userid for authentication, the user name,
66# | - otherwise.
67# | DD: Day
68# | Mon: Month (calendar name)
69# | YYYY: Year
70# | hh: hour (24-hour format, the machine's timezone)
71# | mm: minutes
72# | ss: seconds
73# | request: The first line of the HTTP request as sent by the client.
74# | ddd: the status code returned by the server, - if not available.
75# | bbbb: the total number of bytes sent,
76# | *not including the HTTP/1.0 header*, - if not available
77# |
78# | You can determine the name of the file accessed through request.
79#
80# (Actually, the latter is only true if you know the server configuration
81# at the time the request was made!)
82
83__version__ = "0.6"
84
85__all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
86
Georg Brandl1f7fffb2010-10-15 15:57:45 +000087import html
Barry Warsaw820c1202008-06-12 04:06:45 +000088import email.message
89import email.parser
Jeremy Hylton914ab452009-03-27 17:16:06 +000090import http.client
91import io
92import mimetypes
93import os
94import posixpath
95import select
96import shutil
97import socket # For gethostbyaddr()
98import socketserver
99import sys
100import time
101import urllib.parse
Senthil Kumaran42713722010-10-03 17:55:45 +0000102import copy
Georg Brandl24420152008-05-26 16:32:26 +0000103
104# Default error message template
105DEFAULT_ERROR_MESSAGE = """\
Senthil Kumaran1b407fe2011-03-20 10:44:30 +0800106<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
107 "http://www.w3.org/TR/html4/strict.dtd">
Senthil Kumaranb253c9f2011-03-17 16:43:22 +0800108 <head>
Senthil Kumaran1b407fe2011-03-20 10:44:30 +0800109 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
Senthil Kumaranb253c9f2011-03-17 16:43:22 +0800110 <title>Error response</title>
111 </head>
112 <body>
113 <h1>Error response</h1>
114 <p>Error code: %(code)d</p>
115 <p>Message: %(message)s.</p>
116 <p>Error code explanation: %(code)s - %(explain)s.</p>
117 </body>
118</html>
Georg Brandl24420152008-05-26 16:32:26 +0000119"""
120
121DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
122
123def _quote_html(html):
124 return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
125
126class HTTPServer(socketserver.TCPServer):
127
128 allow_reuse_address = 1 # Seems to make sense in testing environment
129
130 def server_bind(self):
131 """Override server_bind to store the server name."""
132 socketserver.TCPServer.server_bind(self)
133 host, port = self.socket.getsockname()[:2]
134 self.server_name = socket.getfqdn(host)
135 self.server_port = port
136
137
138class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
139
140 """HTTP request handler base class.
141
142 The following explanation of HTTP serves to guide you through the
143 code as well as to expose any misunderstandings I may have about
144 HTTP (so you don't need to read the code to figure out I'm wrong
145 :-).
146
147 HTTP (HyperText Transfer Protocol) is an extensible protocol on
148 top of a reliable stream transport (e.g. TCP/IP). The protocol
149 recognizes three parts to a request:
150
151 1. One line identifying the request type and path
152 2. An optional set of RFC-822-style headers
153 3. An optional data part
154
155 The headers and data are separated by a blank line.
156
157 The first line of the request has the form
158
159 <command> <path> <version>
160
161 where <command> is a (case-sensitive) keyword such as GET or POST,
162 <path> is a string containing path information for the request,
163 and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
164 <path> is encoded using the URL encoding scheme (using %xx to signify
165 the ASCII character with hex code xx).
166
167 The specification specifies that lines are separated by CRLF but
168 for compatibility with the widest range of clients recommends
169 servers also handle LF. Similarly, whitespace in the request line
170 is treated sensibly (allowing multiple spaces between components
171 and allowing trailing whitespace).
172
173 Similarly, for output, lines ought to be separated by CRLF pairs
174 but most clients grok LF characters just fine.
175
176 If the first line of the request has the form
177
178 <command> <path>
179
180 (i.e. <version> is left out) then this is assumed to be an HTTP
181 0.9 request; this form has no optional headers and data part and
182 the reply consists of just the data.
183
184 The reply form of the HTTP 1.x protocol again has three parts:
185
186 1. One line giving the response code
187 2. An optional set of RFC-822-style headers
188 3. The data
189
190 Again, the headers and data are separated by a blank line.
191
192 The response code line has the form
193
194 <version> <responsecode> <responsestring>
195
196 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
197 <responsecode> is a 3-digit response code indicating success or
198 failure of the request, and <responsestring> is an optional
199 human-readable string explaining what the response code means.
200
201 This server parses the request and the headers, and then calls a
202 function specific to the request type (<command>). Specifically,
203 a request SPAM will be handled by a method do_SPAM(). If no
204 such method exists the server sends an error response to the
205 client. If it exists, it is called with no arguments:
206
207 do_SPAM()
208
209 Note that the request name is case sensitive (i.e. SPAM and spam
210 are different requests).
211
212 The various request details are stored in instance variables:
213
214 - client_address is the client IP address in the form (host,
215 port);
216
217 - command, path and version are the broken-down request line;
218
Barry Warsaw820c1202008-06-12 04:06:45 +0000219 - headers is an instance of email.message.Message (or a derived
Georg Brandl24420152008-05-26 16:32:26 +0000220 class) containing the header information;
221
222 - rfile is a file object open for reading positioned at the
223 start of the optional input data part;
224
225 - wfile is a file object open for writing.
226
227 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
228
229 The first thing to be written must be the response line. Then
230 follow 0 or more header lines, then a blank line, and then the
231 actual data (if any). The meaning of the header lines depends on
232 the command executed by the server; in most cases, when data is
233 returned, there should be at least one header line of the form
234
235 Content-type: <type>/<subtype>
236
237 where <type> and <subtype> should be registered MIME types,
238 e.g. "text/html" or "text/plain".
239
240 """
241
242 # The Python system version, truncated to its first component.
243 sys_version = "Python/" + sys.version.split()[0]
244
245 # The server software version. You may want to override this.
246 # The format is multiple whitespace-separated strings,
247 # where each string is of the form name[/version].
248 server_version = "BaseHTTP/" + __version__
249
250 error_message_format = DEFAULT_ERROR_MESSAGE
251 error_content_type = DEFAULT_ERROR_CONTENT_TYPE
252
253 # The default request version. This only affects responses up until
254 # the point where the request line is parsed, so it mainly decides what
255 # the client gets back when sending a malformed request line.
256 # Most web servers default to HTTP 0.9, i.e. don't send a status line.
257 default_request_version = "HTTP/0.9"
258
259 def parse_request(self):
260 """Parse a request (internal).
261
262 The request should be stored in self.raw_requestline; the results
263 are in self.command, self.path, self.request_version and
264 self.headers.
265
266 Return True for success, False for failure; on failure, an
267 error is sent back.
268
269 """
270 self.command = None # set in case of error on the first line
271 self.request_version = version = self.default_request_version
272 self.close_connection = 1
273 requestline = str(self.raw_requestline, 'iso-8859-1')
274 if requestline[-2:] == '\r\n':
275 requestline = requestline[:-2]
276 elif requestline[-1:] == '\n':
277 requestline = requestline[:-1]
278 self.requestline = requestline
279 words = requestline.split()
280 if len(words) == 3:
281 [command, path, version] = words
282 if version[:5] != 'HTTP/':
283 self.send_error(400, "Bad request version (%r)" % version)
284 return False
285 try:
286 base_version_number = version.split('/', 1)[1]
287 version_number = base_version_number.split(".")
288 # RFC 2145 section 3.1 says there can be only one "." and
289 # - major and minor numbers MUST be treated as
290 # separate integers;
291 # - HTTP/2.4 is a lower version than HTTP/2.13, which in
292 # turn is lower than HTTP/12.3;
293 # - Leading zeros MUST be ignored by recipients.
294 if len(version_number) != 2:
295 raise ValueError
296 version_number = int(version_number[0]), int(version_number[1])
297 except (ValueError, IndexError):
298 self.send_error(400, "Bad request version (%r)" % version)
299 return False
300 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
301 self.close_connection = 0
302 if version_number >= (2, 0):
303 self.send_error(505,
304 "Invalid HTTP Version (%s)" % base_version_number)
305 return False
306 elif len(words) == 2:
307 [command, path] = words
308 self.close_connection = 1
309 if command != 'GET':
310 self.send_error(400,
311 "Bad HTTP/0.9 request type (%r)" % command)
312 return False
313 elif not words:
314 return False
315 else:
316 self.send_error(400, "Bad request syntax (%r)" % requestline)
317 return False
318 self.command, self.path, self.request_version = command, path, version
319
320 # Examine the headers and look for a Connection directive.
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000321 try:
322 self.headers = http.client.parse_headers(self.rfile,
323 _class=self.MessageClass)
324 except http.client.LineTooLong:
325 self.send_error(400, "Line too long")
326 return False
Georg Brandl24420152008-05-26 16:32:26 +0000327
328 conntype = self.headers.get('Connection', "")
329 if conntype.lower() == 'close':
330 self.close_connection = 1
331 elif (conntype.lower() == 'keep-alive' and
332 self.protocol_version >= "HTTP/1.1"):
333 self.close_connection = 0
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000334 # Examine the headers and look for an Expect directive
335 expect = self.headers.get('Expect', "")
336 if (expect.lower() == "100-continue" and
337 self.protocol_version >= "HTTP/1.1" and
338 self.request_version >= "HTTP/1.1"):
339 if not self.handle_expect_100():
340 return False
341 return True
342
343 def handle_expect_100(self):
344 """Decide what to do with an "Expect: 100-continue" header.
345
346 If the client is expecting a 100 Continue response, we must
347 respond with either a 100 Continue or a final response before
348 waiting for the request body. The default is to always respond
349 with a 100 Continue. You can behave differently (for example,
350 reject unauthorized requests) by overriding this method.
351
352 This method should either return True (possibly after sending
353 a 100 Continue response) or send an error response and return
354 False.
355
356 """
357 self.send_response_only(100)
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800358 self.flush_headers()
Georg Brandl24420152008-05-26 16:32:26 +0000359 return True
360
361 def handle_one_request(self):
362 """Handle a single HTTP request.
363
364 You normally don't need to override this method; see the class
365 __doc__ string for information on how to handle specific HTTP
366 commands such as GET and POST.
367
368 """
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000369 try:
Antoine Pitrouc4924372010-12-16 16:48:36 +0000370 self.raw_requestline = self.rfile.readline(65537)
371 if len(self.raw_requestline) > 65536:
372 self.requestline = ''
373 self.request_version = ''
374 self.command = ''
375 self.send_error(414)
376 return
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000377 if not self.raw_requestline:
378 self.close_connection = 1
379 return
380 if not self.parse_request():
381 # An error code has been sent, just exit
382 return
383 mname = 'do_' + self.command
384 if not hasattr(self, mname):
385 self.send_error(501, "Unsupported method (%r)" % self.command)
386 return
387 method = getattr(self, mname)
388 method()
389 self.wfile.flush() #actually send the response if not already done.
390 except socket.timeout as e:
391 #a read or a write timed out. Discard this connection
392 self.log_error("Request timed out: %r", e)
Georg Brandl24420152008-05-26 16:32:26 +0000393 self.close_connection = 1
394 return
Georg Brandl24420152008-05-26 16:32:26 +0000395
396 def handle(self):
397 """Handle multiple requests if necessary."""
398 self.close_connection = 1
399
400 self.handle_one_request()
401 while not self.close_connection:
402 self.handle_one_request()
403
404 def send_error(self, code, message=None):
405 """Send and log an error reply.
406
407 Arguments are the error code, and a detailed message.
408 The detailed message defaults to the short entry matching the
409 response code.
410
411 This sends an error response (so it must be called before any
412 output has been generated), logs the error, and finally sends
413 a piece of HTML explaining the error to the user.
414
415 """
416
417 try:
418 shortmsg, longmsg = self.responses[code]
419 except KeyError:
420 shortmsg, longmsg = '???', '???'
421 if message is None:
422 message = shortmsg
423 explain = longmsg
424 self.log_error("code %d, message %s", code, message)
425 # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
426 content = (self.error_message_format %
427 {'code': code, 'message': _quote_html(message), 'explain': explain})
428 self.send_response(code, message)
429 self.send_header("Content-Type", self.error_content_type)
430 self.send_header('Connection', 'close')
431 self.end_headers()
432 if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
433 self.wfile.write(content.encode('UTF-8', 'replace'))
434
435 def send_response(self, code, message=None):
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800436 """Add the response header to the headers buffer and log the
437 response code.
Georg Brandl24420152008-05-26 16:32:26 +0000438
439 Also send two standard headers with the server software
440 version and the current date.
441
442 """
443 self.log_request(code)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000444 self.send_response_only(code, message)
445 self.send_header('Server', self.version_string())
446 self.send_header('Date', self.date_time_string())
447
448 def send_response_only(self, code, message=None):
449 """Send the response header only."""
Georg Brandl24420152008-05-26 16:32:26 +0000450 if message is None:
451 if code in self.responses:
452 message = self.responses[code][0]
453 else:
454 message = ''
455 if self.request_version != 'HTTP/0.9':
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800456 if not hasattr(self, '_headers_buffer'):
457 self._headers_buffer = []
458 self._headers_buffer.append(("%s %d %s\r\n" %
459 (self.protocol_version, code, message)).encode(
460 'latin-1', 'strict'))
Georg Brandl24420152008-05-26 16:32:26 +0000461
462 def send_header(self, keyword, value):
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800463 """Send a MIME header to the headers buffer."""
Georg Brandl24420152008-05-26 16:32:26 +0000464 if self.request_version != 'HTTP/0.9':
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000465 if not hasattr(self, '_headers_buffer'):
466 self._headers_buffer = []
467 self._headers_buffer.append(
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000468 ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict'))
Georg Brandl24420152008-05-26 16:32:26 +0000469
470 if keyword.lower() == 'connection':
471 if value.lower() == 'close':
472 self.close_connection = 1
473 elif value.lower() == 'keep-alive':
474 self.close_connection = 0
475
476 def end_headers(self):
477 """Send the blank line ending the MIME headers."""
478 if self.request_version != 'HTTP/0.9':
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000479 self._headers_buffer.append(b"\r\n")
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800480 self.flush_headers()
481
482 def flush_headers(self):
483 if hasattr(self, '_headers_buffer'):
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000484 self.wfile.write(b"".join(self._headers_buffer))
485 self._headers_buffer = []
Georg Brandl24420152008-05-26 16:32:26 +0000486
487 def log_request(self, code='-', size='-'):
488 """Log an accepted request.
489
490 This is called by send_response().
491
492 """
493
494 self.log_message('"%s" %s %s',
495 self.requestline, str(code), str(size))
496
497 def log_error(self, format, *args):
498 """Log an error.
499
500 This is called when a request cannot be fulfilled. By
501 default it passes the message on to log_message().
502
503 Arguments are the same as for log_message().
504
505 XXX This should go to the separate error log.
506
507 """
508
509 self.log_message(format, *args)
510
511 def log_message(self, format, *args):
512 """Log an arbitrary message.
513
514 This is used by all other logging functions. Override
515 it if you have specific logging wishes.
516
517 The first argument, FORMAT, is a format string for the
518 message to be logged. If the format string contains
519 any % escapes requiring parameters, they should be
520 specified as subsequent arguments (it's just like
521 printf!).
522
523 The client host and current date/time are prefixed to
524 every message.
525
526 """
527
528 sys.stderr.write("%s - - [%s] %s\n" %
529 (self.address_string(),
530 self.log_date_time_string(),
531 format%args))
532
533 def version_string(self):
534 """Return the server software version string."""
535 return self.server_version + ' ' + self.sys_version
536
537 def date_time_string(self, timestamp=None):
538 """Return the current date and time formatted for a message header."""
539 if timestamp is None:
540 timestamp = time.time()
541 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
542 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
543 self.weekdayname[wd],
544 day, self.monthname[month], year,
545 hh, mm, ss)
546 return s
547
548 def log_date_time_string(self):
549 """Return the current time formatted for logging."""
550 now = time.time()
551 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
552 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
553 day, self.monthname[month], year, hh, mm, ss)
554 return s
555
556 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
557
558 monthname = [None,
559 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
560 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
561
562 def address_string(self):
563 """Return the client address formatted for logging.
564
565 This version looks up the full hostname using gethostbyaddr(),
566 and tries to find a name that contains at least one dot.
567
568 """
569
570 host, port = self.client_address[:2]
571 return socket.getfqdn(host)
572
573 # Essentially static class variables
574
575 # The version of the HTTP protocol we support.
576 # Set this to HTTP/1.1 to enable automatic keepalive
577 protocol_version = "HTTP/1.0"
578
Barry Warsaw820c1202008-06-12 04:06:45 +0000579 # MessageClass used to parse headers
Barry Warsaw820c1202008-06-12 04:06:45 +0000580 MessageClass = http.client.HTTPMessage
Georg Brandl24420152008-05-26 16:32:26 +0000581
582 # Table mapping response codes to messages; entries have the
583 # form {code: (shortmessage, longmessage)}.
584 # See RFC 2616.
585 responses = {
586 100: ('Continue', 'Request received, please continue'),
587 101: ('Switching Protocols',
588 'Switching to new protocol; obey Upgrade header'),
589
590 200: ('OK', 'Request fulfilled, document follows'),
591 201: ('Created', 'Document created, URL follows'),
592 202: ('Accepted',
593 'Request accepted, processing continues off-line'),
594 203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
595 204: ('No Content', 'Request fulfilled, nothing follows'),
596 205: ('Reset Content', 'Clear input form for further input.'),
597 206: ('Partial Content', 'Partial content follows.'),
598
599 300: ('Multiple Choices',
600 'Object has several resources -- see URI list'),
601 301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
602 302: ('Found', 'Object moved temporarily -- see URI list'),
603 303: ('See Other', 'Object moved -- see Method and URL list'),
604 304: ('Not Modified',
605 'Document has not changed since given time'),
606 305: ('Use Proxy',
607 'You must use proxy specified in Location to access this '
608 'resource.'),
609 307: ('Temporary Redirect',
610 'Object moved temporarily -- see URI list'),
611
612 400: ('Bad Request',
613 'Bad request syntax or unsupported method'),
614 401: ('Unauthorized',
615 'No permission -- see authorization schemes'),
616 402: ('Payment Required',
617 'No payment -- see charging schemes'),
618 403: ('Forbidden',
619 'Request forbidden -- authorization will not help'),
620 404: ('Not Found', 'Nothing matches the given URI'),
621 405: ('Method Not Allowed',
Senthil Kumaran7aa26212010-02-22 11:00:50 +0000622 'Specified method is invalid for this resource.'),
Georg Brandl24420152008-05-26 16:32:26 +0000623 406: ('Not Acceptable', 'URI not available in preferred format.'),
624 407: ('Proxy Authentication Required', 'You must authenticate with '
625 'this proxy before proceeding.'),
626 408: ('Request Timeout', 'Request timed out; try again later.'),
627 409: ('Conflict', 'Request conflict.'),
628 410: ('Gone',
629 'URI no longer exists and has been permanently removed.'),
630 411: ('Length Required', 'Client must specify Content-Length.'),
631 412: ('Precondition Failed', 'Precondition in headers is false.'),
632 413: ('Request Entity Too Large', 'Entity is too large.'),
633 414: ('Request-URI Too Long', 'URI is too long.'),
634 415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
635 416: ('Requested Range Not Satisfiable',
636 'Cannot satisfy request range.'),
637 417: ('Expectation Failed',
638 'Expect condition could not be satisfied.'),
639
640 500: ('Internal Server Error', 'Server got itself in trouble'),
641 501: ('Not Implemented',
642 'Server does not support this operation'),
643 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
644 503: ('Service Unavailable',
645 'The server cannot process the request due to a high load'),
646 504: ('Gateway Timeout',
647 'The gateway server did not receive a timely response'),
648 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
649 }
650
651
652class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
653
654 """Simple HTTP request handler with GET and HEAD commands.
655
656 This serves files from the current directory and any of its
657 subdirectories. The MIME type for files is determined by
658 calling the .guess_type() method.
659
660 The GET and HEAD requests are identical except that the HEAD
661 request omits the actual contents of the file.
662
663 """
664
665 server_version = "SimpleHTTP/" + __version__
666
667 def do_GET(self):
668 """Serve a GET request."""
669 f = self.send_head()
670 if f:
671 self.copyfile(f, self.wfile)
672 f.close()
673
674 def do_HEAD(self):
675 """Serve a HEAD request."""
676 f = self.send_head()
677 if f:
678 f.close()
679
680 def send_head(self):
681 """Common code for GET and HEAD commands.
682
683 This sends the response code and MIME headers.
684
685 Return value is either a file object (which has to be copied
686 to the outputfile by the caller unless the command was HEAD,
687 and must be closed by the caller under all circumstances), or
688 None, in which case the caller has nothing further to do.
689
690 """
691 path = self.translate_path(self.path)
692 f = None
693 if os.path.isdir(path):
694 if not self.path.endswith('/'):
695 # redirect browser - doing basically what apache does
696 self.send_response(301)
697 self.send_header("Location", self.path + "/")
698 self.end_headers()
699 return None
700 for index in "index.html", "index.htm":
701 index = os.path.join(path, index)
702 if os.path.exists(index):
703 path = index
704 break
705 else:
706 return self.list_directory(path)
707 ctype = self.guess_type(path)
708 try:
709 f = open(path, 'rb')
710 except IOError:
711 self.send_error(404, "File not found")
712 return None
713 self.send_response(200)
714 self.send_header("Content-type", ctype)
715 fs = os.fstat(f.fileno())
716 self.send_header("Content-Length", str(fs[6]))
717 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
718 self.end_headers()
719 return f
720
721 def list_directory(self, path):
722 """Helper to produce a directory listing (absent index.html).
723
724 Return value is either a file object, or None (indicating an
725 error). In either case, the headers are sent, making the
726 interface the same as for send_head().
727
728 """
729 try:
730 list = os.listdir(path)
731 except os.error:
732 self.send_error(404, "No permission to list directory")
733 return None
734 list.sort(key=lambda a: a.lower())
735 r = []
Georg Brandl1f7fffb2010-10-15 15:57:45 +0000736 displaypath = html.escape(urllib.parse.unquote(self.path))
Georg Brandl24420152008-05-26 16:32:26 +0000737 r.append('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
738 r.append("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
739 r.append("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
740 r.append("<hr>\n<ul>\n")
741 for name in list:
742 fullname = os.path.join(path, name)
743 displayname = linkname = name
744 # Append / for directories or @ for symbolic links
745 if os.path.isdir(fullname):
746 displayname = name + "/"
747 linkname = name + "/"
748 if os.path.islink(fullname):
749 displayname = name + "@"
750 # Note: a link to a directory displays with @ and links with /
751 r.append('<li><a href="%s">%s</a>\n'
Georg Brandl1f7fffb2010-10-15 15:57:45 +0000752 % (urllib.parse.quote(linkname), html.escape(displayname)))
Georg Brandl24420152008-05-26 16:32:26 +0000753 r.append("</ul>\n<hr>\n</body>\n</html>\n")
754 enc = sys.getfilesystemencoding()
755 encoded = ''.join(r).encode(enc)
756 f = io.BytesIO()
757 f.write(encoded)
758 f.seek(0)
759 self.send_response(200)
760 self.send_header("Content-type", "text/html; charset=%s" % enc)
761 self.send_header("Content-Length", str(len(encoded)))
762 self.end_headers()
763 return f
764
765 def translate_path(self, path):
766 """Translate a /-separated PATH to the local filename syntax.
767
768 Components that mean special things to the local file system
769 (e.g. drive or directory names) are ignored. (XXX They should
770 probably be diagnosed.)
771
772 """
773 # abandon query parameters
774 path = path.split('?',1)[0]
775 path = path.split('#',1)[0]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000776 path = posixpath.normpath(urllib.parse.unquote(path))
Georg Brandl24420152008-05-26 16:32:26 +0000777 words = path.split('/')
778 words = filter(None, words)
779 path = os.getcwd()
780 for word in words:
781 drive, word = os.path.splitdrive(word)
782 head, word = os.path.split(word)
783 if word in (os.curdir, os.pardir): continue
784 path = os.path.join(path, word)
785 return path
786
787 def copyfile(self, source, outputfile):
788 """Copy all data between two file objects.
789
790 The SOURCE argument is a file object open for reading
791 (or anything with a read() method) and the DESTINATION
792 argument is a file object open for writing (or
793 anything with a write() method).
794
795 The only reason for overriding this would be to change
796 the block size or perhaps to replace newlines by CRLF
797 -- note however that this the default server uses this
798 to copy binary data as well.
799
800 """
801 shutil.copyfileobj(source, outputfile)
802
803 def guess_type(self, path):
804 """Guess the type of a file.
805
806 Argument is a PATH (a filename).
807
808 Return value is a string of the form type/subtype,
809 usable for a MIME Content-type header.
810
811 The default implementation looks the file's extension
812 up in the table self.extensions_map, using application/octet-stream
813 as a default; however it would be permissible (if
814 slow) to look inside the data to make a better guess.
815
816 """
817
818 base, ext = posixpath.splitext(path)
819 if ext in self.extensions_map:
820 return self.extensions_map[ext]
821 ext = ext.lower()
822 if ext in self.extensions_map:
823 return self.extensions_map[ext]
824 else:
825 return self.extensions_map['']
826
827 if not mimetypes.inited:
828 mimetypes.init() # try to read system mime.types
829 extensions_map = mimetypes.types_map.copy()
830 extensions_map.update({
831 '': 'application/octet-stream', # Default
832 '.py': 'text/plain',
833 '.c': 'text/plain',
834 '.h': 'text/plain',
835 })
836
837
838# Utilities for CGIHTTPRequestHandler
839
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000840# TODO(gregory.p.smith): Move this into an appropriate library.
841def _url_collapse_path_split(path):
842 """
843 Given a URL path, remove extra '/'s and '.' path elements and collapse
844 any '..' references.
845
846 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
847
848 Returns: A tuple of (head, tail) where tail is everything after the final /
849 and head is everything before it. Head will always start with a '/' and,
850 if it contains anything else, never have a trailing '/'.
851
852 Raises: IndexError if too many '..' occur within the path.
853 """
854 # Similar to os.path.split(os.path.normpath(path)) but specific to URL
855 # path semantics rather than local operating system semantics.
856 path_parts = []
857 for part in path.split('/'):
858 if part == '.':
859 path_parts.append('')
860 else:
861 path_parts.append(part)
862 # Filter out blank non trailing parts before consuming the '..'.
863 path_parts = [part for part in path_parts[:-1] if part] + path_parts[-1:]
864 if path_parts:
865 tail_part = path_parts.pop()
866 else:
867 tail_part = ''
868 head_parts = []
869 for part in path_parts:
870 if part == '..':
871 head_parts.pop()
872 else:
873 head_parts.append(part)
874 if tail_part and tail_part == '..':
875 head_parts.pop()
876 tail_part = ''
877 return ('/' + '/'.join(head_parts), tail_part)
878
879
Georg Brandl24420152008-05-26 16:32:26 +0000880nobody = None
881
882def nobody_uid():
883 """Internal routine to get nobody's uid"""
884 global nobody
885 if nobody:
886 return nobody
887 try:
888 import pwd
889 except ImportError:
890 return -1
891 try:
892 nobody = pwd.getpwnam('nobody')[2]
893 except KeyError:
Georg Brandlcbd2ab12010-12-04 10:39:14 +0000894 nobody = 1 + max(x[2] for x in pwd.getpwall())
Georg Brandl24420152008-05-26 16:32:26 +0000895 return nobody
896
897
898def executable(path):
899 """Test for executable file."""
Victor Stinnerfb25ba92011-06-20 17:45:54 +0200900 return os.access(path, os.X_OK)
Georg Brandl24420152008-05-26 16:32:26 +0000901
902
903class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
904
905 """Complete HTTP server with GET, HEAD and POST commands.
906
907 GET and HEAD also support running CGI scripts.
908
909 The POST command is *only* implemented for CGI scripts.
910
911 """
912
913 # Determine platform specifics
914 have_fork = hasattr(os, 'fork')
Georg Brandl24420152008-05-26 16:32:26 +0000915
916 # Make rfile unbuffered -- we need to read one line and then pass
917 # the rest to a subprocess, so we can't use buffered input.
918 rbufsize = 0
919
920 def do_POST(self):
921 """Serve a POST request.
922
923 This is only implemented for CGI scripts.
924
925 """
926
927 if self.is_cgi():
928 self.run_cgi()
929 else:
930 self.send_error(501, "Can only POST to CGI scripts")
931
932 def send_head(self):
933 """Version of send_head that support CGI scripts"""
934 if self.is_cgi():
935 return self.run_cgi()
936 else:
937 return SimpleHTTPRequestHandler.send_head(self)
938
939 def is_cgi(self):
940 """Test whether self.path corresponds to a CGI script.
941
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000942 Returns True and updates the cgi_info attribute to the tuple
943 (dir, rest) if self.path requires running a CGI script.
944 Returns False otherwise.
Georg Brandl24420152008-05-26 16:32:26 +0000945
Benjamin Petersona7deeee2009-05-08 20:54:42 +0000946 If any exception is raised, the caller should assume that
947 self.path was rejected as invalid and act accordingly.
948
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000949 The default implementation tests whether the normalized url
950 path begins with one of the strings in self.cgi_directories
951 (and the next character is a '/' or the end of the string).
Georg Brandl24420152008-05-26 16:32:26 +0000952
953 """
954
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000955 splitpath = _url_collapse_path_split(self.path)
956 if splitpath[0] in self.cgi_directories:
957 self.cgi_info = splitpath
958 return True
Georg Brandl24420152008-05-26 16:32:26 +0000959 return False
960
961 cgi_directories = ['/cgi-bin', '/htbin']
962
963 def is_executable(self, path):
964 """Test whether argument path is an executable file."""
965 return executable(path)
966
967 def is_python(self, path):
968 """Test whether argument path is a Python script."""
969 head, tail = os.path.splitext(path)
970 return tail.lower() in (".py", ".pyw")
971
972 def run_cgi(self):
973 """Execute a CGI script."""
974 path = self.path
975 dir, rest = self.cgi_info
976
977 i = path.find('/', len(dir) + 1)
978 while i >= 0:
979 nextdir = path[:i]
980 nextrest = path[i+1:]
981
982 scriptdir = self.translate_path(nextdir)
983 if os.path.isdir(scriptdir):
984 dir, rest = nextdir, nextrest
985 i = path.find('/', len(dir) + 1)
986 else:
987 break
988
989 # find an explicit query string, if present.
990 i = rest.rfind('?')
991 if i >= 0:
992 rest, query = rest[:i], rest[i+1:]
993 else:
994 query = ''
995
996 # dissect the part after the directory name into a script name &
997 # a possible additional path, to be stored in PATH_INFO.
998 i = rest.find('/')
999 if i >= 0:
1000 script, rest = rest[:i], rest[i:]
1001 else:
1002 script, rest = rest, ''
1003
1004 scriptname = dir + '/' + script
1005 scriptfile = self.translate_path(scriptname)
1006 if not os.path.exists(scriptfile):
1007 self.send_error(404, "No such CGI script (%r)" % scriptname)
1008 return
1009 if not os.path.isfile(scriptfile):
1010 self.send_error(403, "CGI script is not a plain file (%r)" %
1011 scriptname)
1012 return
1013 ispy = self.is_python(scriptname)
Victor Stinnerfb25ba92011-06-20 17:45:54 +02001014 if self.have_fork or not ispy:
Georg Brandl24420152008-05-26 16:32:26 +00001015 if not self.is_executable(scriptfile):
1016 self.send_error(403, "CGI script is not executable (%r)" %
1017 scriptname)
1018 return
1019
1020 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
1021 # XXX Much of the following could be prepared ahead of time!
Senthil Kumaran42713722010-10-03 17:55:45 +00001022 env = copy.deepcopy(os.environ)
Georg Brandl24420152008-05-26 16:32:26 +00001023 env['SERVER_SOFTWARE'] = self.version_string()
1024 env['SERVER_NAME'] = self.server.server_name
1025 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
1026 env['SERVER_PROTOCOL'] = self.protocol_version
1027 env['SERVER_PORT'] = str(self.server.server_port)
1028 env['REQUEST_METHOD'] = self.command
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001029 uqrest = urllib.parse.unquote(rest)
Georg Brandl24420152008-05-26 16:32:26 +00001030 env['PATH_INFO'] = uqrest
1031 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
1032 env['SCRIPT_NAME'] = scriptname
1033 if query:
1034 env['QUERY_STRING'] = query
1035 host = self.address_string()
1036 if host != self.client_address[0]:
1037 env['REMOTE_HOST'] = host
1038 env['REMOTE_ADDR'] = self.client_address[0]
Barry Warsaw820c1202008-06-12 04:06:45 +00001039 authorization = self.headers.get("authorization")
Georg Brandl24420152008-05-26 16:32:26 +00001040 if authorization:
1041 authorization = authorization.split()
1042 if len(authorization) == 2:
1043 import base64, binascii
1044 env['AUTH_TYPE'] = authorization[0]
1045 if authorization[0].lower() == "basic":
1046 try:
1047 authorization = authorization[1].encode('ascii')
Georg Brandl706824f2009-06-04 09:42:55 +00001048 authorization = base64.decodebytes(authorization).\
Georg Brandl24420152008-05-26 16:32:26 +00001049 decode('ascii')
1050 except (binascii.Error, UnicodeError):
1051 pass
1052 else:
1053 authorization = authorization.split(':')
1054 if len(authorization) == 2:
1055 env['REMOTE_USER'] = authorization[0]
1056 # XXX REMOTE_IDENT
Barry Warsaw820c1202008-06-12 04:06:45 +00001057 if self.headers.get('content-type') is None:
1058 env['CONTENT_TYPE'] = self.headers.get_content_type()
Georg Brandl24420152008-05-26 16:32:26 +00001059 else:
Barry Warsaw820c1202008-06-12 04:06:45 +00001060 env['CONTENT_TYPE'] = self.headers['content-type']
1061 length = self.headers.get('content-length')
Georg Brandl24420152008-05-26 16:32:26 +00001062 if length:
1063 env['CONTENT_LENGTH'] = length
Barry Warsaw820c1202008-06-12 04:06:45 +00001064 referer = self.headers.get('referer')
Georg Brandl24420152008-05-26 16:32:26 +00001065 if referer:
1066 env['HTTP_REFERER'] = referer
1067 accept = []
1068 for line in self.headers.getallmatchingheaders('accept'):
1069 if line[:1] in "\t\n\r ":
1070 accept.append(line.strip())
1071 else:
1072 accept = accept + line[7:].split(',')
1073 env['HTTP_ACCEPT'] = ','.join(accept)
Barry Warsaw820c1202008-06-12 04:06:45 +00001074 ua = self.headers.get('user-agent')
Georg Brandl24420152008-05-26 16:32:26 +00001075 if ua:
1076 env['HTTP_USER_AGENT'] = ua
Barry Warsaw820c1202008-06-12 04:06:45 +00001077 co = filter(None, self.headers.get_all('cookie', []))
Georg Brandl62e2ca22010-07-31 21:54:24 +00001078 cookie_str = ', '.join(co)
1079 if cookie_str:
1080 env['HTTP_COOKIE'] = cookie_str
Georg Brandl24420152008-05-26 16:32:26 +00001081 # XXX Other HTTP_* headers
1082 # Since we're setting the env in the parent, provide empty
1083 # values to override previously set values
1084 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
1085 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
1086 env.setdefault(k, "")
Georg Brandl24420152008-05-26 16:32:26 +00001087
1088 self.send_response(200, "Script output follows")
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +08001089 self.flush_headers()
Georg Brandl24420152008-05-26 16:32:26 +00001090
1091 decoded_query = query.replace('+', ' ')
1092
1093 if self.have_fork:
1094 # Unix -- fork as we should
1095 args = [script]
1096 if '=' not in decoded_query:
1097 args.append(decoded_query)
1098 nobody = nobody_uid()
1099 self.wfile.flush() # Always flush before forking
1100 pid = os.fork()
1101 if pid != 0:
1102 # Parent
1103 pid, sts = os.waitpid(pid, 0)
1104 # throw away additional data [see bug #427345]
1105 while select.select([self.rfile], [], [], 0)[0]:
1106 if not self.rfile.read(1):
1107 break
1108 if sts:
1109 self.log_error("CGI script exit status %#x", sts)
1110 return
1111 # Child
1112 try:
1113 try:
1114 os.setuid(nobody)
1115 except os.error:
1116 pass
1117 os.dup2(self.rfile.fileno(), 0)
1118 os.dup2(self.wfile.fileno(), 1)
Senthil Kumaran42713722010-10-03 17:55:45 +00001119 os.execve(scriptfile, args, env)
Georg Brandl24420152008-05-26 16:32:26 +00001120 except:
1121 self.server.handle_error(self.request, self.client_address)
1122 os._exit(127)
1123
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001124 else:
1125 # Non-Unix -- use subprocess
1126 import subprocess
Senthil Kumarane29cd162009-11-11 04:17:53 +00001127 cmdline = [scriptfile]
Georg Brandl24420152008-05-26 16:32:26 +00001128 if self.is_python(scriptfile):
1129 interp = sys.executable
1130 if interp.lower().endswith("w.exe"):
1131 # On Windows, use python.exe, not pythonw.exe
1132 interp = interp[:-5] + interp[-4:]
Senthil Kumarane29cd162009-11-11 04:17:53 +00001133 cmdline = [interp, '-u'] + cmdline
1134 if '=' not in query:
1135 cmdline.append(query)
1136 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
Georg Brandl24420152008-05-26 16:32:26 +00001137 try:
1138 nbytes = int(length)
1139 except (TypeError, ValueError):
1140 nbytes = 0
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001141 p = subprocess.Popen(cmdline,
1142 stdin=subprocess.PIPE,
1143 stdout=subprocess.PIPE,
Senthil Kumaran42713722010-10-03 17:55:45 +00001144 stderr=subprocess.PIPE,
1145 env = env
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001146 )
Georg Brandl24420152008-05-26 16:32:26 +00001147 if self.command.lower() == "post" and nbytes > 0:
1148 data = self.rfile.read(nbytes)
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001149 else:
1150 data = None
Georg Brandl24420152008-05-26 16:32:26 +00001151 # throw away additional data [see bug #427345]
1152 while select.select([self.rfile._sock], [], [], 0)[0]:
1153 if not self.rfile._sock.recv(1):
1154 break
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001155 stdout, stderr = p.communicate(data)
1156 self.wfile.write(stdout)
1157 if stderr:
1158 self.log_error('%s', stderr)
Brian Curtincbad4df2010-11-05 15:04:48 +00001159 p.stderr.close()
1160 p.stdout.close()
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001161 status = p.returncode
1162 if status:
1163 self.log_error("CGI script exit status %#x", status)
Georg Brandl24420152008-05-26 16:32:26 +00001164 else:
1165 self.log_message("CGI script exited OK")
1166
1167
1168def test(HandlerClass = BaseHTTPRequestHandler,
1169 ServerClass = HTTPServer, protocol="HTTP/1.0"):
1170 """Test the HTTP request handler class.
1171
1172 This runs an HTTP server on port 8000 (or the first command line
1173 argument).
1174
1175 """
1176
1177 if sys.argv[1:]:
1178 port = int(sys.argv[1])
1179 else:
1180 port = 8000
1181 server_address = ('', port)
1182
1183 HandlerClass.protocol_version = protocol
1184 httpd = ServerClass(server_address, HandlerClass)
1185
1186 sa = httpd.socket.getsockname()
1187 print("Serving HTTP on", sa[0], "port", sa[1], "...")
Alexandre Vassalottib5292a22009-04-03 07:16:55 +00001188 try:
1189 httpd.serve_forever()
1190 except KeyboardInterrupt:
1191 print("\nKeyboard interrupt received, exiting.")
1192 httpd.server_close()
1193 sys.exit(0)
Georg Brandl24420152008-05-26 16:32:26 +00001194
1195if __name__ == '__main__':
Georg Brandl24420152008-05-26 16:32:26 +00001196 test(HandlerClass=SimpleHTTPRequestHandler)