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