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