blob: 5ebfd25ecece47003408c254282e041f1d42f44b [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 = """\
106<head>
107<title>Error response</title>
108</head>
109<body>
110<h1>Error response</h1>
111<p>Error code %(code)d.
112<p>Message: %(message)s.
113<p>Error code explanation: %(code)s = %(explain)s.
114</body>
115"""
116
117DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
118
119def _quote_html(html):
120 return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
121
122class HTTPServer(socketserver.TCPServer):
123
124 allow_reuse_address = 1 # Seems to make sense in testing environment
125
126 def server_bind(self):
127 """Override server_bind to store the server name."""
128 socketserver.TCPServer.server_bind(self)
129 host, port = self.socket.getsockname()[:2]
130 self.server_name = socket.getfqdn(host)
131 self.server_port = port
132
133
134class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
135
136 """HTTP request handler base class.
137
138 The following explanation of HTTP serves to guide you through the
139 code as well as to expose any misunderstandings I may have about
140 HTTP (so you don't need to read the code to figure out I'm wrong
141 :-).
142
143 HTTP (HyperText Transfer Protocol) is an extensible protocol on
144 top of a reliable stream transport (e.g. TCP/IP). The protocol
145 recognizes three parts to a request:
146
147 1. One line identifying the request type and path
148 2. An optional set of RFC-822-style headers
149 3. An optional data part
150
151 The headers and data are separated by a blank line.
152
153 The first line of the request has the form
154
155 <command> <path> <version>
156
157 where <command> is a (case-sensitive) keyword such as GET or POST,
158 <path> is a string containing path information for the request,
159 and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
160 <path> is encoded using the URL encoding scheme (using %xx to signify
161 the ASCII character with hex code xx).
162
163 The specification specifies that lines are separated by CRLF but
164 for compatibility with the widest range of clients recommends
165 servers also handle LF. Similarly, whitespace in the request line
166 is treated sensibly (allowing multiple spaces between components
167 and allowing trailing whitespace).
168
169 Similarly, for output, lines ought to be separated by CRLF pairs
170 but most clients grok LF characters just fine.
171
172 If the first line of the request has the form
173
174 <command> <path>
175
176 (i.e. <version> is left out) then this is assumed to be an HTTP
177 0.9 request; this form has no optional headers and data part and
178 the reply consists of just the data.
179
180 The reply form of the HTTP 1.x protocol again has three parts:
181
182 1. One line giving the response code
183 2. An optional set of RFC-822-style headers
184 3. The data
185
186 Again, the headers and data are separated by a blank line.
187
188 The response code line has the form
189
190 <version> <responsecode> <responsestring>
191
192 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
193 <responsecode> is a 3-digit response code indicating success or
194 failure of the request, and <responsestring> is an optional
195 human-readable string explaining what the response code means.
196
197 This server parses the request and the headers, and then calls a
198 function specific to the request type (<command>). Specifically,
199 a request SPAM will be handled by a method do_SPAM(). If no
200 such method exists the server sends an error response to the
201 client. If it exists, it is called with no arguments:
202
203 do_SPAM()
204
205 Note that the request name is case sensitive (i.e. SPAM and spam
206 are different requests).
207
208 The various request details are stored in instance variables:
209
210 - client_address is the client IP address in the form (host,
211 port);
212
213 - command, path and version are the broken-down request line;
214
Barry Warsaw820c1202008-06-12 04:06:45 +0000215 - headers is an instance of email.message.Message (or a derived
Georg Brandl24420152008-05-26 16:32:26 +0000216 class) containing the header information;
217
218 - rfile is a file object open for reading positioned at the
219 start of the optional input data part;
220
221 - wfile is a file object open for writing.
222
223 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
224
225 The first thing to be written must be the response line. Then
226 follow 0 or more header lines, then a blank line, and then the
227 actual data (if any). The meaning of the header lines depends on
228 the command executed by the server; in most cases, when data is
229 returned, there should be at least one header line of the form
230
231 Content-type: <type>/<subtype>
232
233 where <type> and <subtype> should be registered MIME types,
234 e.g. "text/html" or "text/plain".
235
236 """
237
238 # The Python system version, truncated to its first component.
239 sys_version = "Python/" + sys.version.split()[0]
240
241 # The server software version. You may want to override this.
242 # The format is multiple whitespace-separated strings,
243 # where each string is of the form name[/version].
244 server_version = "BaseHTTP/" + __version__
245
246 error_message_format = DEFAULT_ERROR_MESSAGE
247 error_content_type = DEFAULT_ERROR_CONTENT_TYPE
248
249 # The default request version. This only affects responses up until
250 # the point where the request line is parsed, so it mainly decides what
251 # the client gets back when sending a malformed request line.
252 # Most web servers default to HTTP 0.9, i.e. don't send a status line.
253 default_request_version = "HTTP/0.9"
254
255 def parse_request(self):
256 """Parse a request (internal).
257
258 The request should be stored in self.raw_requestline; the results
259 are in self.command, self.path, self.request_version and
260 self.headers.
261
262 Return True for success, False for failure; on failure, an
263 error is sent back.
264
265 """
266 self.command = None # set in case of error on the first line
267 self.request_version = version = self.default_request_version
268 self.close_connection = 1
269 requestline = str(self.raw_requestline, 'iso-8859-1')
270 if requestline[-2:] == '\r\n':
271 requestline = requestline[:-2]
272 elif requestline[-1:] == '\n':
273 requestline = requestline[:-1]
274 self.requestline = requestline
275 words = requestline.split()
276 if len(words) == 3:
277 [command, path, version] = words
278 if version[:5] != 'HTTP/':
279 self.send_error(400, "Bad request version (%r)" % version)
280 return False
281 try:
282 base_version_number = version.split('/', 1)[1]
283 version_number = base_version_number.split(".")
284 # RFC 2145 section 3.1 says there can be only one "." and
285 # - major and minor numbers MUST be treated as
286 # separate integers;
287 # - HTTP/2.4 is a lower version than HTTP/2.13, which in
288 # turn is lower than HTTP/12.3;
289 # - Leading zeros MUST be ignored by recipients.
290 if len(version_number) != 2:
291 raise ValueError
292 version_number = int(version_number[0]), int(version_number[1])
293 except (ValueError, IndexError):
294 self.send_error(400, "Bad request version (%r)" % version)
295 return False
296 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
297 self.close_connection = 0
298 if version_number >= (2, 0):
299 self.send_error(505,
300 "Invalid HTTP Version (%s)" % base_version_number)
301 return False
302 elif len(words) == 2:
303 [command, path] = words
304 self.close_connection = 1
305 if command != 'GET':
306 self.send_error(400,
307 "Bad HTTP/0.9 request type (%r)" % command)
308 return False
309 elif not words:
310 return False
311 else:
312 self.send_error(400, "Bad request syntax (%r)" % requestline)
313 return False
314 self.command, self.path, self.request_version = command, path, version
315
316 # Examine the headers and look for a Connection directive.
Jeremy Hylton98eb6c22009-03-27 18:31:36 +0000317 self.headers = http.client.parse_headers(self.rfile,
318 _class=self.MessageClass)
Georg Brandl24420152008-05-26 16:32:26 +0000319
320 conntype = self.headers.get('Connection', "")
321 if conntype.lower() == 'close':
322 self.close_connection = 1
323 elif (conntype.lower() == 'keep-alive' and
324 self.protocol_version >= "HTTP/1.1"):
325 self.close_connection = 0
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000326 # Examine the headers and look for an Expect directive
327 expect = self.headers.get('Expect', "")
328 if (expect.lower() == "100-continue" and
329 self.protocol_version >= "HTTP/1.1" and
330 self.request_version >= "HTTP/1.1"):
331 if not self.handle_expect_100():
332 return False
333 return True
334
335 def handle_expect_100(self):
336 """Decide what to do with an "Expect: 100-continue" header.
337
338 If the client is expecting a 100 Continue response, we must
339 respond with either a 100 Continue or a final response before
340 waiting for the request body. The default is to always respond
341 with a 100 Continue. You can behave differently (for example,
342 reject unauthorized requests) by overriding this method.
343
344 This method should either return True (possibly after sending
345 a 100 Continue response) or send an error response and return
346 False.
347
348 """
349 self.send_response_only(100)
Georg Brandl24420152008-05-26 16:32:26 +0000350 return True
351
352 def handle_one_request(self):
353 """Handle a single HTTP request.
354
355 You normally don't need to override this method; see the class
356 __doc__ string for information on how to handle specific HTTP
357 commands such as GET and POST.
358
359 """
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000360 try:
361 self.raw_requestline = self.rfile.readline()
362 if not self.raw_requestline:
363 self.close_connection = 1
364 return
365 if not self.parse_request():
366 # An error code has been sent, just exit
367 return
368 mname = 'do_' + self.command
369 if not hasattr(self, mname):
370 self.send_error(501, "Unsupported method (%r)" % self.command)
371 return
372 method = getattr(self, mname)
373 method()
374 self.wfile.flush() #actually send the response if not already done.
375 except socket.timeout as e:
376 #a read or a write timed out. Discard this connection
377 self.log_error("Request timed out: %r", e)
Georg Brandl24420152008-05-26 16:32:26 +0000378 self.close_connection = 1
379 return
Georg Brandl24420152008-05-26 16:32:26 +0000380
381 def handle(self):
382 """Handle multiple requests if necessary."""
383 self.close_connection = 1
384
385 self.handle_one_request()
386 while not self.close_connection:
387 self.handle_one_request()
388
389 def send_error(self, code, message=None):
390 """Send and log an error reply.
391
392 Arguments are the error code, and a detailed message.
393 The detailed message defaults to the short entry matching the
394 response code.
395
396 This sends an error response (so it must be called before any
397 output has been generated), logs the error, and finally sends
398 a piece of HTML explaining the error to the user.
399
400 """
401
402 try:
403 shortmsg, longmsg = self.responses[code]
404 except KeyError:
405 shortmsg, longmsg = '???', '???'
406 if message is None:
407 message = shortmsg
408 explain = longmsg
409 self.log_error("code %d, message %s", code, message)
410 # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
411 content = (self.error_message_format %
412 {'code': code, 'message': _quote_html(message), 'explain': explain})
413 self.send_response(code, message)
414 self.send_header("Content-Type", self.error_content_type)
415 self.send_header('Connection', 'close')
416 self.end_headers()
417 if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
418 self.wfile.write(content.encode('UTF-8', 'replace'))
419
420 def send_response(self, code, message=None):
421 """Send the response header and log the response code.
422
423 Also send two standard headers with the server software
424 version and the current date.
425
426 """
427 self.log_request(code)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000428 self.send_response_only(code, message)
429 self.send_header('Server', self.version_string())
430 self.send_header('Date', self.date_time_string())
431
432 def send_response_only(self, code, message=None):
433 """Send the response header only."""
Georg Brandl24420152008-05-26 16:32:26 +0000434 if message is None:
435 if code in self.responses:
436 message = self.responses[code][0]
437 else:
438 message = ''
439 if self.request_version != 'HTTP/0.9':
440 self.wfile.write(("%s %d %s\r\n" %
441 (self.protocol_version, code, message)).encode('ASCII', 'strict'))
Georg Brandl24420152008-05-26 16:32:26 +0000442
443 def send_header(self, keyword, value):
444 """Send a MIME header."""
445 if self.request_version != 'HTTP/0.9':
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000446 if not hasattr(self, '_headers_buffer'):
447 self._headers_buffer = []
448 self._headers_buffer.append(
449 ("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict'))
Georg Brandl24420152008-05-26 16:32:26 +0000450
451 if keyword.lower() == 'connection':
452 if value.lower() == 'close':
453 self.close_connection = 1
454 elif value.lower() == 'keep-alive':
455 self.close_connection = 0
456
457 def end_headers(self):
458 """Send the blank line ending the MIME headers."""
459 if self.request_version != 'HTTP/0.9':
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000460 self._headers_buffer.append(b"\r\n")
461 self.wfile.write(b"".join(self._headers_buffer))
462 self._headers_buffer = []
Georg Brandl24420152008-05-26 16:32:26 +0000463
464 def log_request(self, code='-', size='-'):
465 """Log an accepted request.
466
467 This is called by send_response().
468
469 """
470
471 self.log_message('"%s" %s %s',
472 self.requestline, str(code), str(size))
473
474 def log_error(self, format, *args):
475 """Log an error.
476
477 This is called when a request cannot be fulfilled. By
478 default it passes the message on to log_message().
479
480 Arguments are the same as for log_message().
481
482 XXX This should go to the separate error log.
483
484 """
485
486 self.log_message(format, *args)
487
488 def log_message(self, format, *args):
489 """Log an arbitrary message.
490
491 This is used by all other logging functions. Override
492 it if you have specific logging wishes.
493
494 The first argument, FORMAT, is a format string for the
495 message to be logged. If the format string contains
496 any % escapes requiring parameters, they should be
497 specified as subsequent arguments (it's just like
498 printf!).
499
500 The client host and current date/time are prefixed to
501 every message.
502
503 """
504
505 sys.stderr.write("%s - - [%s] %s\n" %
506 (self.address_string(),
507 self.log_date_time_string(),
508 format%args))
509
510 def version_string(self):
511 """Return the server software version string."""
512 return self.server_version + ' ' + self.sys_version
513
514 def date_time_string(self, timestamp=None):
515 """Return the current date and time formatted for a message header."""
516 if timestamp is None:
517 timestamp = time.time()
518 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
519 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
520 self.weekdayname[wd],
521 day, self.monthname[month], year,
522 hh, mm, ss)
523 return s
524
525 def log_date_time_string(self):
526 """Return the current time formatted for logging."""
527 now = time.time()
528 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
529 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
530 day, self.monthname[month], year, hh, mm, ss)
531 return s
532
533 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
534
535 monthname = [None,
536 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
537 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
538
539 def address_string(self):
540 """Return the client address formatted for logging.
541
542 This version looks up the full hostname using gethostbyaddr(),
543 and tries to find a name that contains at least one dot.
544
545 """
546
547 host, port = self.client_address[:2]
548 return socket.getfqdn(host)
549
550 # Essentially static class variables
551
552 # The version of the HTTP protocol we support.
553 # Set this to HTTP/1.1 to enable automatic keepalive
554 protocol_version = "HTTP/1.0"
555
Barry Warsaw820c1202008-06-12 04:06:45 +0000556 # MessageClass used to parse headers
Barry Warsaw820c1202008-06-12 04:06:45 +0000557 MessageClass = http.client.HTTPMessage
Georg Brandl24420152008-05-26 16:32:26 +0000558
559 # Table mapping response codes to messages; entries have the
560 # form {code: (shortmessage, longmessage)}.
561 # See RFC 2616.
562 responses = {
563 100: ('Continue', 'Request received, please continue'),
564 101: ('Switching Protocols',
565 'Switching to new protocol; obey Upgrade header'),
566
567 200: ('OK', 'Request fulfilled, document follows'),
568 201: ('Created', 'Document created, URL follows'),
569 202: ('Accepted',
570 'Request accepted, processing continues off-line'),
571 203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
572 204: ('No Content', 'Request fulfilled, nothing follows'),
573 205: ('Reset Content', 'Clear input form for further input.'),
574 206: ('Partial Content', 'Partial content follows.'),
575
576 300: ('Multiple Choices',
577 'Object has several resources -- see URI list'),
578 301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
579 302: ('Found', 'Object moved temporarily -- see URI list'),
580 303: ('See Other', 'Object moved -- see Method and URL list'),
581 304: ('Not Modified',
582 'Document has not changed since given time'),
583 305: ('Use Proxy',
584 'You must use proxy specified in Location to access this '
585 'resource.'),
586 307: ('Temporary Redirect',
587 'Object moved temporarily -- see URI list'),
588
589 400: ('Bad Request',
590 'Bad request syntax or unsupported method'),
591 401: ('Unauthorized',
592 'No permission -- see authorization schemes'),
593 402: ('Payment Required',
594 'No payment -- see charging schemes'),
595 403: ('Forbidden',
596 'Request forbidden -- authorization will not help'),
597 404: ('Not Found', 'Nothing matches the given URI'),
598 405: ('Method Not Allowed',
Senthil Kumaran7aa26212010-02-22 11:00:50 +0000599 'Specified method is invalid for this resource.'),
Georg Brandl24420152008-05-26 16:32:26 +0000600 406: ('Not Acceptable', 'URI not available in preferred format.'),
601 407: ('Proxy Authentication Required', 'You must authenticate with '
602 'this proxy before proceeding.'),
603 408: ('Request Timeout', 'Request timed out; try again later.'),
604 409: ('Conflict', 'Request conflict.'),
605 410: ('Gone',
606 'URI no longer exists and has been permanently removed.'),
607 411: ('Length Required', 'Client must specify Content-Length.'),
608 412: ('Precondition Failed', 'Precondition in headers is false.'),
609 413: ('Request Entity Too Large', 'Entity is too large.'),
610 414: ('Request-URI Too Long', 'URI is too long.'),
611 415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
612 416: ('Requested Range Not Satisfiable',
613 'Cannot satisfy request range.'),
614 417: ('Expectation Failed',
615 'Expect condition could not be satisfied.'),
616
617 500: ('Internal Server Error', 'Server got itself in trouble'),
618 501: ('Not Implemented',
619 'Server does not support this operation'),
620 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
621 503: ('Service Unavailable',
622 'The server cannot process the request due to a high load'),
623 504: ('Gateway Timeout',
624 'The gateway server did not receive a timely response'),
625 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
626 }
627
628
629class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
630
631 """Simple HTTP request handler with GET and HEAD commands.
632
633 This serves files from the current directory and any of its
634 subdirectories. The MIME type for files is determined by
635 calling the .guess_type() method.
636
637 The GET and HEAD requests are identical except that the HEAD
638 request omits the actual contents of the file.
639
640 """
641
642 server_version = "SimpleHTTP/" + __version__
643
644 def do_GET(self):
645 """Serve a GET request."""
646 f = self.send_head()
647 if f:
648 self.copyfile(f, self.wfile)
649 f.close()
650
651 def do_HEAD(self):
652 """Serve a HEAD request."""
653 f = self.send_head()
654 if f:
655 f.close()
656
657 def send_head(self):
658 """Common code for GET and HEAD commands.
659
660 This sends the response code and MIME headers.
661
662 Return value is either a file object (which has to be copied
663 to the outputfile by the caller unless the command was HEAD,
664 and must be closed by the caller under all circumstances), or
665 None, in which case the caller has nothing further to do.
666
667 """
668 path = self.translate_path(self.path)
669 f = None
670 if os.path.isdir(path):
671 if not self.path.endswith('/'):
672 # redirect browser - doing basically what apache does
673 self.send_response(301)
674 self.send_header("Location", self.path + "/")
675 self.end_headers()
676 return None
677 for index in "index.html", "index.htm":
678 index = os.path.join(path, index)
679 if os.path.exists(index):
680 path = index
681 break
682 else:
683 return self.list_directory(path)
684 ctype = self.guess_type(path)
685 try:
686 f = open(path, 'rb')
687 except IOError:
688 self.send_error(404, "File not found")
689 return None
690 self.send_response(200)
691 self.send_header("Content-type", ctype)
692 fs = os.fstat(f.fileno())
693 self.send_header("Content-Length", str(fs[6]))
694 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
695 self.end_headers()
696 return f
697
698 def list_directory(self, path):
699 """Helper to produce a directory listing (absent index.html).
700
701 Return value is either a file object, or None (indicating an
702 error). In either case, the headers are sent, making the
703 interface the same as for send_head().
704
705 """
706 try:
707 list = os.listdir(path)
708 except os.error:
709 self.send_error(404, "No permission to list directory")
710 return None
711 list.sort(key=lambda a: a.lower())
712 r = []
Georg Brandl1f7fffb2010-10-15 15:57:45 +0000713 displaypath = html.escape(urllib.parse.unquote(self.path))
Georg Brandl24420152008-05-26 16:32:26 +0000714 r.append('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
715 r.append("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
716 r.append("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
717 r.append("<hr>\n<ul>\n")
718 for name in list:
719 fullname = os.path.join(path, name)
720 displayname = linkname = name
721 # Append / for directories or @ for symbolic links
722 if os.path.isdir(fullname):
723 displayname = name + "/"
724 linkname = name + "/"
725 if os.path.islink(fullname):
726 displayname = name + "@"
727 # Note: a link to a directory displays with @ and links with /
728 r.append('<li><a href="%s">%s</a>\n'
Georg Brandl1f7fffb2010-10-15 15:57:45 +0000729 % (urllib.parse.quote(linkname), html.escape(displayname)))
Georg Brandl24420152008-05-26 16:32:26 +0000730 r.append("</ul>\n<hr>\n</body>\n</html>\n")
731 enc = sys.getfilesystemencoding()
732 encoded = ''.join(r).encode(enc)
733 f = io.BytesIO()
734 f.write(encoded)
735 f.seek(0)
736 self.send_response(200)
737 self.send_header("Content-type", "text/html; charset=%s" % enc)
738 self.send_header("Content-Length", str(len(encoded)))
739 self.end_headers()
740 return f
741
742 def translate_path(self, path):
743 """Translate a /-separated PATH to the local filename syntax.
744
745 Components that mean special things to the local file system
746 (e.g. drive or directory names) are ignored. (XXX They should
747 probably be diagnosed.)
748
749 """
750 # abandon query parameters
751 path = path.split('?',1)[0]
752 path = path.split('#',1)[0]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000753 path = posixpath.normpath(urllib.parse.unquote(path))
Georg Brandl24420152008-05-26 16:32:26 +0000754 words = path.split('/')
755 words = filter(None, words)
756 path = os.getcwd()
757 for word in words:
758 drive, word = os.path.splitdrive(word)
759 head, word = os.path.split(word)
760 if word in (os.curdir, os.pardir): continue
761 path = os.path.join(path, word)
762 return path
763
764 def copyfile(self, source, outputfile):
765 """Copy all data between two file objects.
766
767 The SOURCE argument is a file object open for reading
768 (or anything with a read() method) and the DESTINATION
769 argument is a file object open for writing (or
770 anything with a write() method).
771
772 The only reason for overriding this would be to change
773 the block size or perhaps to replace newlines by CRLF
774 -- note however that this the default server uses this
775 to copy binary data as well.
776
777 """
778 shutil.copyfileobj(source, outputfile)
779
780 def guess_type(self, path):
781 """Guess the type of a file.
782
783 Argument is a PATH (a filename).
784
785 Return value is a string of the form type/subtype,
786 usable for a MIME Content-type header.
787
788 The default implementation looks the file's extension
789 up in the table self.extensions_map, using application/octet-stream
790 as a default; however it would be permissible (if
791 slow) to look inside the data to make a better guess.
792
793 """
794
795 base, ext = posixpath.splitext(path)
796 if ext in self.extensions_map:
797 return self.extensions_map[ext]
798 ext = ext.lower()
799 if ext in self.extensions_map:
800 return self.extensions_map[ext]
801 else:
802 return self.extensions_map['']
803
804 if not mimetypes.inited:
805 mimetypes.init() # try to read system mime.types
806 extensions_map = mimetypes.types_map.copy()
807 extensions_map.update({
808 '': 'application/octet-stream', # Default
809 '.py': 'text/plain',
810 '.c': 'text/plain',
811 '.h': 'text/plain',
812 })
813
814
815# Utilities for CGIHTTPRequestHandler
816
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000817# TODO(gregory.p.smith): Move this into an appropriate library.
818def _url_collapse_path_split(path):
819 """
820 Given a URL path, remove extra '/'s and '.' path elements and collapse
821 any '..' references.
822
823 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
824
825 Returns: A tuple of (head, tail) where tail is everything after the final /
826 and head is everything before it. Head will always start with a '/' and,
827 if it contains anything else, never have a trailing '/'.
828
829 Raises: IndexError if too many '..' occur within the path.
830 """
831 # Similar to os.path.split(os.path.normpath(path)) but specific to URL
832 # path semantics rather than local operating system semantics.
833 path_parts = []
834 for part in path.split('/'):
835 if part == '.':
836 path_parts.append('')
837 else:
838 path_parts.append(part)
839 # Filter out blank non trailing parts before consuming the '..'.
840 path_parts = [part for part in path_parts[:-1] if part] + path_parts[-1:]
841 if path_parts:
842 tail_part = path_parts.pop()
843 else:
844 tail_part = ''
845 head_parts = []
846 for part in path_parts:
847 if part == '..':
848 head_parts.pop()
849 else:
850 head_parts.append(part)
851 if tail_part and tail_part == '..':
852 head_parts.pop()
853 tail_part = ''
854 return ('/' + '/'.join(head_parts), tail_part)
855
856
Georg Brandl24420152008-05-26 16:32:26 +0000857nobody = None
858
859def nobody_uid():
860 """Internal routine to get nobody's uid"""
861 global nobody
862 if nobody:
863 return nobody
864 try:
865 import pwd
866 except ImportError:
867 return -1
868 try:
869 nobody = pwd.getpwnam('nobody')[2]
870 except KeyError:
871 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
872 return nobody
873
874
875def executable(path):
876 """Test for executable file."""
877 try:
878 st = os.stat(path)
879 except os.error:
880 return False
881 return st.st_mode & 0o111 != 0
882
883
884class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
885
886 """Complete HTTP server with GET, HEAD and POST commands.
887
888 GET and HEAD also support running CGI scripts.
889
890 The POST command is *only* implemented for CGI scripts.
891
892 """
893
894 # Determine platform specifics
895 have_fork = hasattr(os, 'fork')
Georg Brandl24420152008-05-26 16:32:26 +0000896
897 # Make rfile unbuffered -- we need to read one line and then pass
898 # the rest to a subprocess, so we can't use buffered input.
899 rbufsize = 0
900
901 def do_POST(self):
902 """Serve a POST request.
903
904 This is only implemented for CGI scripts.
905
906 """
907
908 if self.is_cgi():
909 self.run_cgi()
910 else:
911 self.send_error(501, "Can only POST to CGI scripts")
912
913 def send_head(self):
914 """Version of send_head that support CGI scripts"""
915 if self.is_cgi():
916 return self.run_cgi()
917 else:
918 return SimpleHTTPRequestHandler.send_head(self)
919
920 def is_cgi(self):
921 """Test whether self.path corresponds to a CGI script.
922
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000923 Returns True and updates the cgi_info attribute to the tuple
924 (dir, rest) if self.path requires running a CGI script.
925 Returns False otherwise.
Georg Brandl24420152008-05-26 16:32:26 +0000926
Benjamin Petersona7deeee2009-05-08 20:54:42 +0000927 If any exception is raised, the caller should assume that
928 self.path was rejected as invalid and act accordingly.
929
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000930 The default implementation tests whether the normalized url
931 path begins with one of the strings in self.cgi_directories
932 (and the next character is a '/' or the end of the string).
Georg Brandl24420152008-05-26 16:32:26 +0000933
934 """
935
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000936 splitpath = _url_collapse_path_split(self.path)
937 if splitpath[0] in self.cgi_directories:
938 self.cgi_info = splitpath
939 return True
Georg Brandl24420152008-05-26 16:32:26 +0000940 return False
941
942 cgi_directories = ['/cgi-bin', '/htbin']
943
944 def is_executable(self, path):
945 """Test whether argument path is an executable file."""
946 return executable(path)
947
948 def is_python(self, path):
949 """Test whether argument path is a Python script."""
950 head, tail = os.path.splitext(path)
951 return tail.lower() in (".py", ".pyw")
952
953 def run_cgi(self):
954 """Execute a CGI script."""
955 path = self.path
956 dir, rest = self.cgi_info
957
958 i = path.find('/', len(dir) + 1)
959 while i >= 0:
960 nextdir = path[:i]
961 nextrest = path[i+1:]
962
963 scriptdir = self.translate_path(nextdir)
964 if os.path.isdir(scriptdir):
965 dir, rest = nextdir, nextrest
966 i = path.find('/', len(dir) + 1)
967 else:
968 break
969
970 # find an explicit query string, if present.
971 i = rest.rfind('?')
972 if i >= 0:
973 rest, query = rest[:i], rest[i+1:]
974 else:
975 query = ''
976
977 # dissect the part after the directory name into a script name &
978 # a possible additional path, to be stored in PATH_INFO.
979 i = rest.find('/')
980 if i >= 0:
981 script, rest = rest[:i], rest[i:]
982 else:
983 script, rest = rest, ''
984
985 scriptname = dir + '/' + script
986 scriptfile = self.translate_path(scriptname)
987 if not os.path.exists(scriptfile):
988 self.send_error(404, "No such CGI script (%r)" % scriptname)
989 return
990 if not os.path.isfile(scriptfile):
991 self.send_error(403, "CGI script is not a plain file (%r)" %
992 scriptname)
993 return
994 ispy = self.is_python(scriptname)
995 if not ispy:
Georg Brandl24420152008-05-26 16:32:26 +0000996 if not self.is_executable(scriptfile):
997 self.send_error(403, "CGI script is not executable (%r)" %
998 scriptname)
999 return
1000
1001 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
1002 # XXX Much of the following could be prepared ahead of time!
Senthil Kumaran42713722010-10-03 17:55:45 +00001003 env = copy.deepcopy(os.environ)
Georg Brandl24420152008-05-26 16:32:26 +00001004 env['SERVER_SOFTWARE'] = self.version_string()
1005 env['SERVER_NAME'] = self.server.server_name
1006 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
1007 env['SERVER_PROTOCOL'] = self.protocol_version
1008 env['SERVER_PORT'] = str(self.server.server_port)
1009 env['REQUEST_METHOD'] = self.command
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001010 uqrest = urllib.parse.unquote(rest)
Georg Brandl24420152008-05-26 16:32:26 +00001011 env['PATH_INFO'] = uqrest
1012 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
1013 env['SCRIPT_NAME'] = scriptname
1014 if query:
1015 env['QUERY_STRING'] = query
1016 host = self.address_string()
1017 if host != self.client_address[0]:
1018 env['REMOTE_HOST'] = host
1019 env['REMOTE_ADDR'] = self.client_address[0]
Barry Warsaw820c1202008-06-12 04:06:45 +00001020 authorization = self.headers.get("authorization")
Georg Brandl24420152008-05-26 16:32:26 +00001021 if authorization:
1022 authorization = authorization.split()
1023 if len(authorization) == 2:
1024 import base64, binascii
1025 env['AUTH_TYPE'] = authorization[0]
1026 if authorization[0].lower() == "basic":
1027 try:
1028 authorization = authorization[1].encode('ascii')
Georg Brandl706824f2009-06-04 09:42:55 +00001029 authorization = base64.decodebytes(authorization).\
Georg Brandl24420152008-05-26 16:32:26 +00001030 decode('ascii')
1031 except (binascii.Error, UnicodeError):
1032 pass
1033 else:
1034 authorization = authorization.split(':')
1035 if len(authorization) == 2:
1036 env['REMOTE_USER'] = authorization[0]
1037 # XXX REMOTE_IDENT
Barry Warsaw820c1202008-06-12 04:06:45 +00001038 if self.headers.get('content-type') is None:
1039 env['CONTENT_TYPE'] = self.headers.get_content_type()
Georg Brandl24420152008-05-26 16:32:26 +00001040 else:
Barry Warsaw820c1202008-06-12 04:06:45 +00001041 env['CONTENT_TYPE'] = self.headers['content-type']
1042 length = self.headers.get('content-length')
Georg Brandl24420152008-05-26 16:32:26 +00001043 if length:
1044 env['CONTENT_LENGTH'] = length
Barry Warsaw820c1202008-06-12 04:06:45 +00001045 referer = self.headers.get('referer')
Georg Brandl24420152008-05-26 16:32:26 +00001046 if referer:
1047 env['HTTP_REFERER'] = referer
1048 accept = []
1049 for line in self.headers.getallmatchingheaders('accept'):
1050 if line[:1] in "\t\n\r ":
1051 accept.append(line.strip())
1052 else:
1053 accept = accept + line[7:].split(',')
1054 env['HTTP_ACCEPT'] = ','.join(accept)
Barry Warsaw820c1202008-06-12 04:06:45 +00001055 ua = self.headers.get('user-agent')
Georg Brandl24420152008-05-26 16:32:26 +00001056 if ua:
1057 env['HTTP_USER_AGENT'] = ua
Barry Warsaw820c1202008-06-12 04:06:45 +00001058 co = filter(None, self.headers.get_all('cookie', []))
Georg Brandl62e2ca22010-07-31 21:54:24 +00001059 cookie_str = ', '.join(co)
1060 if cookie_str:
1061 env['HTTP_COOKIE'] = cookie_str
Georg Brandl24420152008-05-26 16:32:26 +00001062 # XXX Other HTTP_* headers
1063 # Since we're setting the env in the parent, provide empty
1064 # values to override previously set values
1065 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
1066 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
1067 env.setdefault(k, "")
Georg Brandl24420152008-05-26 16:32:26 +00001068
1069 self.send_response(200, "Script output follows")
1070
1071 decoded_query = query.replace('+', ' ')
1072
1073 if self.have_fork:
1074 # Unix -- fork as we should
1075 args = [script]
1076 if '=' not in decoded_query:
1077 args.append(decoded_query)
1078 nobody = nobody_uid()
1079 self.wfile.flush() # Always flush before forking
1080 pid = os.fork()
1081 if pid != 0:
1082 # Parent
1083 pid, sts = os.waitpid(pid, 0)
1084 # throw away additional data [see bug #427345]
1085 while select.select([self.rfile], [], [], 0)[0]:
1086 if not self.rfile.read(1):
1087 break
1088 if sts:
1089 self.log_error("CGI script exit status %#x", sts)
1090 return
1091 # Child
1092 try:
1093 try:
1094 os.setuid(nobody)
1095 except os.error:
1096 pass
1097 os.dup2(self.rfile.fileno(), 0)
1098 os.dup2(self.wfile.fileno(), 1)
Senthil Kumaran42713722010-10-03 17:55:45 +00001099 os.execve(scriptfile, args, env)
Georg Brandl24420152008-05-26 16:32:26 +00001100 except:
1101 self.server.handle_error(self.request, self.client_address)
1102 os._exit(127)
1103
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001104 else:
1105 # Non-Unix -- use subprocess
1106 import subprocess
Senthil Kumarane29cd162009-11-11 04:17:53 +00001107 cmdline = [scriptfile]
Georg Brandl24420152008-05-26 16:32:26 +00001108 if self.is_python(scriptfile):
1109 interp = sys.executable
1110 if interp.lower().endswith("w.exe"):
1111 # On Windows, use python.exe, not pythonw.exe
1112 interp = interp[:-5] + interp[-4:]
Senthil Kumarane29cd162009-11-11 04:17:53 +00001113 cmdline = [interp, '-u'] + cmdline
1114 if '=' not in query:
1115 cmdline.append(query)
1116 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
Georg Brandl24420152008-05-26 16:32:26 +00001117 try:
1118 nbytes = int(length)
1119 except (TypeError, ValueError):
1120 nbytes = 0
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001121 p = subprocess.Popen(cmdline,
1122 stdin=subprocess.PIPE,
1123 stdout=subprocess.PIPE,
Senthil Kumaran42713722010-10-03 17:55:45 +00001124 stderr=subprocess.PIPE,
1125 env = env
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001126 )
Georg Brandl24420152008-05-26 16:32:26 +00001127 if self.command.lower() == "post" and nbytes > 0:
1128 data = self.rfile.read(nbytes)
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001129 else:
1130 data = None
Georg Brandl24420152008-05-26 16:32:26 +00001131 # throw away additional data [see bug #427345]
1132 while select.select([self.rfile._sock], [], [], 0)[0]:
1133 if not self.rfile._sock.recv(1):
1134 break
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001135 stdout, stderr = p.communicate(data)
1136 self.wfile.write(stdout)
1137 if stderr:
1138 self.log_error('%s', stderr)
Brian Curtincbad4df2010-11-05 15:04:48 +00001139 p.stderr.close()
1140 p.stdout.close()
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001141 status = p.returncode
1142 if status:
1143 self.log_error("CGI script exit status %#x", status)
Georg Brandl24420152008-05-26 16:32:26 +00001144 else:
1145 self.log_message("CGI script exited OK")
1146
1147
1148def test(HandlerClass = BaseHTTPRequestHandler,
1149 ServerClass = HTTPServer, protocol="HTTP/1.0"):
1150 """Test the HTTP request handler class.
1151
1152 This runs an HTTP server on port 8000 (or the first command line
1153 argument).
1154
1155 """
1156
1157 if sys.argv[1:]:
1158 port = int(sys.argv[1])
1159 else:
1160 port = 8000
1161 server_address = ('', port)
1162
1163 HandlerClass.protocol_version = protocol
1164 httpd = ServerClass(server_address, HandlerClass)
1165
1166 sa = httpd.socket.getsockname()
1167 print("Serving HTTP on", sa[0], "port", sa[1], "...")
Alexandre Vassalottib5292a22009-04-03 07:16:55 +00001168 try:
1169 httpd.serve_forever()
1170 except KeyboardInterrupt:
1171 print("\nKeyboard interrupt received, exiting.")
1172 httpd.server_close()
1173 sys.exit(0)
Georg Brandl24420152008-05-26 16:32:26 +00001174
1175if __name__ == '__main__':
Georg Brandl24420152008-05-26 16:32:26 +00001176 test(HandlerClass=SimpleHTTPRequestHandler)