blob: 897908ec30318a36a1015fb90f2d0703893f2572 [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 Hyltone6fdd042009-03-27 17:14:18 +0000316 self.headers = http.client.parse_headers(self.rfile)
Georg Brandl24420152008-05-26 16:32:26 +0000317
318 conntype = self.headers.get('Connection', "")
319 if conntype.lower() == 'close':
320 self.close_connection = 1
321 elif (conntype.lower() == 'keep-alive' and
322 self.protocol_version >= "HTTP/1.1"):
323 self.close_connection = 0
324 return True
325
326 def handle_one_request(self):
327 """Handle a single HTTP request.
328
329 You normally don't need to override this method; see the class
330 __doc__ string for information on how to handle specific HTTP
331 commands such as GET and POST.
332
333 """
334 self.raw_requestline = self.rfile.readline()
335 if not self.raw_requestline:
336 self.close_connection = 1
337 return
338 if not self.parse_request(): # An error code has been sent, just exit
339 return
340 mname = 'do_' + self.command
341 if not hasattr(self, mname):
342 self.send_error(501, "Unsupported method (%r)" % self.command)
343 return
344 method = getattr(self, mname)
345 method()
346
347 def handle(self):
348 """Handle multiple requests if necessary."""
349 self.close_connection = 1
350
351 self.handle_one_request()
352 while not self.close_connection:
353 self.handle_one_request()
354
355 def send_error(self, code, message=None):
356 """Send and log an error reply.
357
358 Arguments are the error code, and a detailed message.
359 The detailed message defaults to the short entry matching the
360 response code.
361
362 This sends an error response (so it must be called before any
363 output has been generated), logs the error, and finally sends
364 a piece of HTML explaining the error to the user.
365
366 """
367
368 try:
369 shortmsg, longmsg = self.responses[code]
370 except KeyError:
371 shortmsg, longmsg = '???', '???'
372 if message is None:
373 message = shortmsg
374 explain = longmsg
375 self.log_error("code %d, message %s", code, message)
376 # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
377 content = (self.error_message_format %
378 {'code': code, 'message': _quote_html(message), 'explain': explain})
379 self.send_response(code, message)
380 self.send_header("Content-Type", self.error_content_type)
381 self.send_header('Connection', 'close')
382 self.end_headers()
383 if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
384 self.wfile.write(content.encode('UTF-8', 'replace'))
385
386 def send_response(self, code, message=None):
387 """Send the response header and log the response code.
388
389 Also send two standard headers with the server software
390 version and the current date.
391
392 """
393 self.log_request(code)
394 if message is None:
395 if code in self.responses:
396 message = self.responses[code][0]
397 else:
398 message = ''
399 if self.request_version != 'HTTP/0.9':
400 self.wfile.write(("%s %d %s\r\n" %
401 (self.protocol_version, code, message)).encode('ASCII', 'strict'))
402 # print (self.protocol_version, code, message)
403 self.send_header('Server', self.version_string())
404 self.send_header('Date', self.date_time_string())
405
406 def send_header(self, keyword, value):
407 """Send a MIME header."""
408 if self.request_version != 'HTTP/0.9':
409 self.wfile.write(("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict'))
410
411 if keyword.lower() == 'connection':
412 if value.lower() == 'close':
413 self.close_connection = 1
414 elif value.lower() == 'keep-alive':
415 self.close_connection = 0
416
417 def end_headers(self):
418 """Send the blank line ending the MIME headers."""
419 if self.request_version != 'HTTP/0.9':
420 self.wfile.write(b"\r\n")
421
422 def log_request(self, code='-', size='-'):
423 """Log an accepted request.
424
425 This is called by send_response().
426
427 """
428
429 self.log_message('"%s" %s %s',
430 self.requestline, str(code), str(size))
431
432 def log_error(self, format, *args):
433 """Log an error.
434
435 This is called when a request cannot be fulfilled. By
436 default it passes the message on to log_message().
437
438 Arguments are the same as for log_message().
439
440 XXX This should go to the separate error log.
441
442 """
443
444 self.log_message(format, *args)
445
446 def log_message(self, format, *args):
447 """Log an arbitrary message.
448
449 This is used by all other logging functions. Override
450 it if you have specific logging wishes.
451
452 The first argument, FORMAT, is a format string for the
453 message to be logged. If the format string contains
454 any % escapes requiring parameters, they should be
455 specified as subsequent arguments (it's just like
456 printf!).
457
458 The client host and current date/time are prefixed to
459 every message.
460
461 """
462
463 sys.stderr.write("%s - - [%s] %s\n" %
464 (self.address_string(),
465 self.log_date_time_string(),
466 format%args))
467
468 def version_string(self):
469 """Return the server software version string."""
470 return self.server_version + ' ' + self.sys_version
471
472 def date_time_string(self, timestamp=None):
473 """Return the current date and time formatted for a message header."""
474 if timestamp is None:
475 timestamp = time.time()
476 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
477 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
478 self.weekdayname[wd],
479 day, self.monthname[month], year,
480 hh, mm, ss)
481 return s
482
483 def log_date_time_string(self):
484 """Return the current time formatted for logging."""
485 now = time.time()
486 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
487 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
488 day, self.monthname[month], year, hh, mm, ss)
489 return s
490
491 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
492
493 monthname = [None,
494 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
495 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
496
497 def address_string(self):
498 """Return the client address formatted for logging.
499
500 This version looks up the full hostname using gethostbyaddr(),
501 and tries to find a name that contains at least one dot.
502
503 """
504
505 host, port = self.client_address[:2]
506 return socket.getfqdn(host)
507
508 # Essentially static class variables
509
510 # The version of the HTTP protocol we support.
511 # Set this to HTTP/1.1 to enable automatic keepalive
512 protocol_version = "HTTP/1.0"
513
Barry Warsaw820c1202008-06-12 04:06:45 +0000514 # MessageClass used to parse headers
Barry Warsaw820c1202008-06-12 04:06:45 +0000515 MessageClass = http.client.HTTPMessage
Georg Brandl24420152008-05-26 16:32:26 +0000516
517 # Table mapping response codes to messages; entries have the
518 # form {code: (shortmessage, longmessage)}.
519 # See RFC 2616.
520 responses = {
521 100: ('Continue', 'Request received, please continue'),
522 101: ('Switching Protocols',
523 'Switching to new protocol; obey Upgrade header'),
524
525 200: ('OK', 'Request fulfilled, document follows'),
526 201: ('Created', 'Document created, URL follows'),
527 202: ('Accepted',
528 'Request accepted, processing continues off-line'),
529 203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
530 204: ('No Content', 'Request fulfilled, nothing follows'),
531 205: ('Reset Content', 'Clear input form for further input.'),
532 206: ('Partial Content', 'Partial content follows.'),
533
534 300: ('Multiple Choices',
535 'Object has several resources -- see URI list'),
536 301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
537 302: ('Found', 'Object moved temporarily -- see URI list'),
538 303: ('See Other', 'Object moved -- see Method and URL list'),
539 304: ('Not Modified',
540 'Document has not changed since given time'),
541 305: ('Use Proxy',
542 'You must use proxy specified in Location to access this '
543 'resource.'),
544 307: ('Temporary Redirect',
545 'Object moved temporarily -- see URI list'),
546
547 400: ('Bad Request',
548 'Bad request syntax or unsupported method'),
549 401: ('Unauthorized',
550 'No permission -- see authorization schemes'),
551 402: ('Payment Required',
552 'No payment -- see charging schemes'),
553 403: ('Forbidden',
554 'Request forbidden -- authorization will not help'),
555 404: ('Not Found', 'Nothing matches the given URI'),
556 405: ('Method Not Allowed',
557 'Specified method is invalid for this server.'),
558 406: ('Not Acceptable', 'URI not available in preferred format.'),
559 407: ('Proxy Authentication Required', 'You must authenticate with '
560 'this proxy before proceeding.'),
561 408: ('Request Timeout', 'Request timed out; try again later.'),
562 409: ('Conflict', 'Request conflict.'),
563 410: ('Gone',
564 'URI no longer exists and has been permanently removed.'),
565 411: ('Length Required', 'Client must specify Content-Length.'),
566 412: ('Precondition Failed', 'Precondition in headers is false.'),
567 413: ('Request Entity Too Large', 'Entity is too large.'),
568 414: ('Request-URI Too Long', 'URI is too long.'),
569 415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
570 416: ('Requested Range Not Satisfiable',
571 'Cannot satisfy request range.'),
572 417: ('Expectation Failed',
573 'Expect condition could not be satisfied.'),
574
575 500: ('Internal Server Error', 'Server got itself in trouble'),
576 501: ('Not Implemented',
577 'Server does not support this operation'),
578 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
579 503: ('Service Unavailable',
580 'The server cannot process the request due to a high load'),
581 504: ('Gateway Timeout',
582 'The gateway server did not receive a timely response'),
583 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
584 }
585
586
587class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
588
589 """Simple HTTP request handler with GET and HEAD commands.
590
591 This serves files from the current directory and any of its
592 subdirectories. The MIME type for files is determined by
593 calling the .guess_type() method.
594
595 The GET and HEAD requests are identical except that the HEAD
596 request omits the actual contents of the file.
597
598 """
599
600 server_version = "SimpleHTTP/" + __version__
601
602 def do_GET(self):
603 """Serve a GET request."""
604 f = self.send_head()
605 if f:
606 self.copyfile(f, self.wfile)
607 f.close()
608
609 def do_HEAD(self):
610 """Serve a HEAD request."""
611 f = self.send_head()
612 if f:
613 f.close()
614
615 def send_head(self):
616 """Common code for GET and HEAD commands.
617
618 This sends the response code and MIME headers.
619
620 Return value is either a file object (which has to be copied
621 to the outputfile by the caller unless the command was HEAD,
622 and must be closed by the caller under all circumstances), or
623 None, in which case the caller has nothing further to do.
624
625 """
626 path = self.translate_path(self.path)
627 f = None
628 if os.path.isdir(path):
629 if not self.path.endswith('/'):
630 # redirect browser - doing basically what apache does
631 self.send_response(301)
632 self.send_header("Location", self.path + "/")
633 self.end_headers()
634 return None
635 for index in "index.html", "index.htm":
636 index = os.path.join(path, index)
637 if os.path.exists(index):
638 path = index
639 break
640 else:
641 return self.list_directory(path)
642 ctype = self.guess_type(path)
643 try:
644 f = open(path, 'rb')
645 except IOError:
646 self.send_error(404, "File not found")
647 return None
648 self.send_response(200)
649 self.send_header("Content-type", ctype)
650 fs = os.fstat(f.fileno())
651 self.send_header("Content-Length", str(fs[6]))
652 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
653 self.end_headers()
654 return f
655
656 def list_directory(self, path):
657 """Helper to produce a directory listing (absent index.html).
658
659 Return value is either a file object, or None (indicating an
660 error). In either case, the headers are sent, making the
661 interface the same as for send_head().
662
663 """
664 try:
665 list = os.listdir(path)
666 except os.error:
667 self.send_error(404, "No permission to list directory")
668 return None
669 list.sort(key=lambda a: a.lower())
670 r = []
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000671 displaypath = cgi.escape(urllib.parse.unquote(self.path))
Georg Brandl24420152008-05-26 16:32:26 +0000672 r.append('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
673 r.append("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
674 r.append("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
675 r.append("<hr>\n<ul>\n")
676 for name in list:
677 fullname = os.path.join(path, name)
678 displayname = linkname = name
679 # Append / for directories or @ for symbolic links
680 if os.path.isdir(fullname):
681 displayname = name + "/"
682 linkname = name + "/"
683 if os.path.islink(fullname):
684 displayname = name + "@"
685 # Note: a link to a directory displays with @ and links with /
686 r.append('<li><a href="%s">%s</a>\n'
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000687 % (urllib.parse.quote(linkname), cgi.escape(displayname)))
Georg Brandl24420152008-05-26 16:32:26 +0000688 r.append("</ul>\n<hr>\n</body>\n</html>\n")
689 enc = sys.getfilesystemencoding()
690 encoded = ''.join(r).encode(enc)
691 f = io.BytesIO()
692 f.write(encoded)
693 f.seek(0)
694 self.send_response(200)
695 self.send_header("Content-type", "text/html; charset=%s" % enc)
696 self.send_header("Content-Length", str(len(encoded)))
697 self.end_headers()
698 return f
699
700 def translate_path(self, path):
701 """Translate a /-separated PATH to the local filename syntax.
702
703 Components that mean special things to the local file system
704 (e.g. drive or directory names) are ignored. (XXX They should
705 probably be diagnosed.)
706
707 """
708 # abandon query parameters
709 path = path.split('?',1)[0]
710 path = path.split('#',1)[0]
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000711 path = posixpath.normpath(urllib.parse.unquote(path))
Georg Brandl24420152008-05-26 16:32:26 +0000712 words = path.split('/')
713 words = filter(None, words)
714 path = os.getcwd()
715 for word in words:
716 drive, word = os.path.splitdrive(word)
717 head, word = os.path.split(word)
718 if word in (os.curdir, os.pardir): continue
719 path = os.path.join(path, word)
720 return path
721
722 def copyfile(self, source, outputfile):
723 """Copy all data between two file objects.
724
725 The SOURCE argument is a file object open for reading
726 (or anything with a read() method) and the DESTINATION
727 argument is a file object open for writing (or
728 anything with a write() method).
729
730 The only reason for overriding this would be to change
731 the block size or perhaps to replace newlines by CRLF
732 -- note however that this the default server uses this
733 to copy binary data as well.
734
735 """
736 shutil.copyfileobj(source, outputfile)
737
738 def guess_type(self, path):
739 """Guess the type of a file.
740
741 Argument is a PATH (a filename).
742
743 Return value is a string of the form type/subtype,
744 usable for a MIME Content-type header.
745
746 The default implementation looks the file's extension
747 up in the table self.extensions_map, using application/octet-stream
748 as a default; however it would be permissible (if
749 slow) to look inside the data to make a better guess.
750
751 """
752
753 base, ext = posixpath.splitext(path)
754 if ext in self.extensions_map:
755 return self.extensions_map[ext]
756 ext = ext.lower()
757 if ext in self.extensions_map:
758 return self.extensions_map[ext]
759 else:
760 return self.extensions_map['']
761
762 if not mimetypes.inited:
763 mimetypes.init() # try to read system mime.types
764 extensions_map = mimetypes.types_map.copy()
765 extensions_map.update({
766 '': 'application/octet-stream', # Default
767 '.py': 'text/plain',
768 '.c': 'text/plain',
769 '.h': 'text/plain',
770 })
771
772
773# Utilities for CGIHTTPRequestHandler
774
775nobody = None
776
777def nobody_uid():
778 """Internal routine to get nobody's uid"""
779 global nobody
780 if nobody:
781 return nobody
782 try:
783 import pwd
784 except ImportError:
785 return -1
786 try:
787 nobody = pwd.getpwnam('nobody')[2]
788 except KeyError:
789 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
790 return nobody
791
792
793def executable(path):
794 """Test for executable file."""
795 try:
796 st = os.stat(path)
797 except os.error:
798 return False
799 return st.st_mode & 0o111 != 0
800
801
802class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
803
804 """Complete HTTP server with GET, HEAD and POST commands.
805
806 GET and HEAD also support running CGI scripts.
807
808 The POST command is *only* implemented for CGI scripts.
809
810 """
811
812 # Determine platform specifics
813 have_fork = hasattr(os, 'fork')
Georg Brandl24420152008-05-26 16:32:26 +0000814
815 # Make rfile unbuffered -- we need to read one line and then pass
816 # the rest to a subprocess, so we can't use buffered input.
817 rbufsize = 0
818
819 def do_POST(self):
820 """Serve a POST request.
821
822 This is only implemented for CGI scripts.
823
824 """
825
826 if self.is_cgi():
827 self.run_cgi()
828 else:
829 self.send_error(501, "Can only POST to CGI scripts")
830
831 def send_head(self):
832 """Version of send_head that support CGI scripts"""
833 if self.is_cgi():
834 return self.run_cgi()
835 else:
836 return SimpleHTTPRequestHandler.send_head(self)
837
838 def is_cgi(self):
839 """Test whether self.path corresponds to a CGI script.
840
841 Return a tuple (dir, rest) if self.path requires running a
842 CGI script, None if not. Note that rest begins with a
843 slash if it is not empty.
844
845 The default implementation tests whether the path
846 begins with one of the strings in the list
847 self.cgi_directories (and the next character is a '/'
848 or the end of the string).
849
850 """
851
852 path = self.path
853
854 for x in self.cgi_directories:
855 i = len(x)
856 if path[:i] == x and (not path[i:] or path[i] == '/'):
857 self.cgi_info = path[:i], path[i+1:]
858 return True
859 return False
860
861 cgi_directories = ['/cgi-bin', '/htbin']
862
863 def is_executable(self, path):
864 """Test whether argument path is an executable file."""
865 return executable(path)
866
867 def is_python(self, path):
868 """Test whether argument path is a Python script."""
869 head, tail = os.path.splitext(path)
870 return tail.lower() in (".py", ".pyw")
871
872 def run_cgi(self):
873 """Execute a CGI script."""
874 path = self.path
875 dir, rest = self.cgi_info
876
877 i = path.find('/', len(dir) + 1)
878 while i >= 0:
879 nextdir = path[:i]
880 nextrest = path[i+1:]
881
882 scriptdir = self.translate_path(nextdir)
883 if os.path.isdir(scriptdir):
884 dir, rest = nextdir, nextrest
885 i = path.find('/', len(dir) + 1)
886 else:
887 break
888
889 # find an explicit query string, if present.
890 i = rest.rfind('?')
891 if i >= 0:
892 rest, query = rest[:i], rest[i+1:]
893 else:
894 query = ''
895
896 # dissect the part after the directory name into a script name &
897 # a possible additional path, to be stored in PATH_INFO.
898 i = rest.find('/')
899 if i >= 0:
900 script, rest = rest[:i], rest[i:]
901 else:
902 script, rest = rest, ''
903
904 scriptname = dir + '/' + script
905 scriptfile = self.translate_path(scriptname)
906 if not os.path.exists(scriptfile):
907 self.send_error(404, "No such CGI script (%r)" % scriptname)
908 return
909 if not os.path.isfile(scriptfile):
910 self.send_error(403, "CGI script is not a plain file (%r)" %
911 scriptname)
912 return
913 ispy = self.is_python(scriptname)
914 if not ispy:
Georg Brandl24420152008-05-26 16:32:26 +0000915 if not self.is_executable(scriptfile):
916 self.send_error(403, "CGI script is not executable (%r)" %
917 scriptname)
918 return
919
920 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
921 # XXX Much of the following could be prepared ahead of time!
922 env = {}
923 env['SERVER_SOFTWARE'] = self.version_string()
924 env['SERVER_NAME'] = self.server.server_name
925 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
926 env['SERVER_PROTOCOL'] = self.protocol_version
927 env['SERVER_PORT'] = str(self.server.server_port)
928 env['REQUEST_METHOD'] = self.command
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000929 uqrest = urllib.parse.unquote(rest)
Georg Brandl24420152008-05-26 16:32:26 +0000930 env['PATH_INFO'] = uqrest
931 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
932 env['SCRIPT_NAME'] = scriptname
933 if query:
934 env['QUERY_STRING'] = query
935 host = self.address_string()
936 if host != self.client_address[0]:
937 env['REMOTE_HOST'] = host
938 env['REMOTE_ADDR'] = self.client_address[0]
Barry Warsaw820c1202008-06-12 04:06:45 +0000939 authorization = self.headers.get("authorization")
Georg Brandl24420152008-05-26 16:32:26 +0000940 if authorization:
941 authorization = authorization.split()
942 if len(authorization) == 2:
943 import base64, binascii
944 env['AUTH_TYPE'] = authorization[0]
945 if authorization[0].lower() == "basic":
946 try:
947 authorization = authorization[1].encode('ascii')
948 authorization = base64.decodestring(authorization).\
949 decode('ascii')
950 except (binascii.Error, UnicodeError):
951 pass
952 else:
953 authorization = authorization.split(':')
954 if len(authorization) == 2:
955 env['REMOTE_USER'] = authorization[0]
956 # XXX REMOTE_IDENT
Barry Warsaw820c1202008-06-12 04:06:45 +0000957 if self.headers.get('content-type') is None:
958 env['CONTENT_TYPE'] = self.headers.get_content_type()
Georg Brandl24420152008-05-26 16:32:26 +0000959 else:
Barry Warsaw820c1202008-06-12 04:06:45 +0000960 env['CONTENT_TYPE'] = self.headers['content-type']
961 length = self.headers.get('content-length')
Georg Brandl24420152008-05-26 16:32:26 +0000962 if length:
963 env['CONTENT_LENGTH'] = length
Barry Warsaw820c1202008-06-12 04:06:45 +0000964 referer = self.headers.get('referer')
Georg Brandl24420152008-05-26 16:32:26 +0000965 if referer:
966 env['HTTP_REFERER'] = referer
967 accept = []
968 for line in self.headers.getallmatchingheaders('accept'):
969 if line[:1] in "\t\n\r ":
970 accept.append(line.strip())
971 else:
972 accept = accept + line[7:].split(',')
973 env['HTTP_ACCEPT'] = ','.join(accept)
Barry Warsaw820c1202008-06-12 04:06:45 +0000974 ua = self.headers.get('user-agent')
Georg Brandl24420152008-05-26 16:32:26 +0000975 if ua:
976 env['HTTP_USER_AGENT'] = ua
Barry Warsaw820c1202008-06-12 04:06:45 +0000977 co = filter(None, self.headers.get_all('cookie', []))
Georg Brandl24420152008-05-26 16:32:26 +0000978 if co:
979 env['HTTP_COOKIE'] = ', '.join(co)
980 # XXX Other HTTP_* headers
981 # Since we're setting the env in the parent, provide empty
982 # values to override previously set values
983 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
984 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
985 env.setdefault(k, "")
986 os.environ.update(env)
987
988 self.send_response(200, "Script output follows")
989
990 decoded_query = query.replace('+', ' ')
991
992 if self.have_fork:
993 # Unix -- fork as we should
994 args = [script]
995 if '=' not in decoded_query:
996 args.append(decoded_query)
997 nobody = nobody_uid()
998 self.wfile.flush() # Always flush before forking
999 pid = os.fork()
1000 if pid != 0:
1001 # Parent
1002 pid, sts = os.waitpid(pid, 0)
1003 # throw away additional data [see bug #427345]
1004 while select.select([self.rfile], [], [], 0)[0]:
1005 if not self.rfile.read(1):
1006 break
1007 if sts:
1008 self.log_error("CGI script exit status %#x", sts)
1009 return
1010 # Child
1011 try:
1012 try:
1013 os.setuid(nobody)
1014 except os.error:
1015 pass
1016 os.dup2(self.rfile.fileno(), 0)
1017 os.dup2(self.wfile.fileno(), 1)
1018 os.execve(scriptfile, args, os.environ)
1019 except:
1020 self.server.handle_error(self.request, self.client_address)
1021 os._exit(127)
1022
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001023 else:
1024 # Non-Unix -- use subprocess
1025 import subprocess
Georg Brandl24420152008-05-26 16:32:26 +00001026 cmdline = scriptfile
1027 if self.is_python(scriptfile):
1028 interp = sys.executable
1029 if interp.lower().endswith("w.exe"):
1030 # On Windows, use python.exe, not pythonw.exe
1031 interp = interp[:-5] + interp[-4:]
1032 cmdline = "%s -u %s" % (interp, cmdline)
1033 if '=' not in query and '"' not in query:
1034 cmdline = '%s "%s"' % (cmdline, query)
1035 self.log_message("command: %s", cmdline)
1036 try:
1037 nbytes = int(length)
1038 except (TypeError, ValueError):
1039 nbytes = 0
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001040 p = subprocess.Popen(cmdline,
1041 stdin=subprocess.PIPE,
1042 stdout=subprocess.PIPE,
1043 stderr=subprocess.PIPE,
1044 )
Georg Brandl24420152008-05-26 16:32:26 +00001045 if self.command.lower() == "post" and nbytes > 0:
1046 data = self.rfile.read(nbytes)
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001047 else:
1048 data = None
Georg Brandl24420152008-05-26 16:32:26 +00001049 # throw away additional data [see bug #427345]
1050 while select.select([self.rfile._sock], [], [], 0)[0]:
1051 if not self.rfile._sock.recv(1):
1052 break
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001053 stdout, stderr = p.communicate(data)
1054 self.wfile.write(stdout)
1055 if stderr:
1056 self.log_error('%s', stderr)
1057 status = p.returncode
1058 if status:
1059 self.log_error("CGI script exit status %#x", status)
Georg Brandl24420152008-05-26 16:32:26 +00001060 else:
1061 self.log_message("CGI script exited OK")
1062
1063
1064def test(HandlerClass = BaseHTTPRequestHandler,
1065 ServerClass = HTTPServer, protocol="HTTP/1.0"):
1066 """Test the HTTP request handler class.
1067
1068 This runs an HTTP server on port 8000 (or the first command line
1069 argument).
1070
1071 """
1072
1073 if sys.argv[1:]:
1074 port = int(sys.argv[1])
1075 else:
1076 port = 8000
1077 server_address = ('', port)
1078
1079 HandlerClass.protocol_version = protocol
1080 httpd = ServerClass(server_address, HandlerClass)
1081
1082 sa = httpd.socket.getsockname()
1083 print("Serving HTTP on", sa[0], "port", sa[1], "...")
1084 httpd.serve_forever()
1085
1086
1087if __name__ == '__main__':
1088 test(HandlerClass=BaseHTTPRequestHandler)
1089 test(HandlerClass=SimpleHTTPRequestHandler)
1090 test(HandlerClass=CGIHTTPRequestHandler)