blob: 5b5ef0a48dcefb221117727fc91bfaa79c5f0c8e [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
776nobody = None
777
778def nobody_uid():
779 """Internal routine to get nobody's uid"""
780 global nobody
781 if nobody:
782 return nobody
783 try:
784 import pwd
785 except ImportError:
786 return -1
787 try:
788 nobody = pwd.getpwnam('nobody')[2]
789 except KeyError:
790 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
791 return nobody
792
793
794def executable(path):
795 """Test for executable file."""
796 try:
797 st = os.stat(path)
798 except os.error:
799 return False
800 return st.st_mode & 0o111 != 0
801
802
803class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
804
805 """Complete HTTP server with GET, HEAD and POST commands.
806
807 GET and HEAD also support running CGI scripts.
808
809 The POST command is *only* implemented for CGI scripts.
810
811 """
812
813 # Determine platform specifics
814 have_fork = hasattr(os, 'fork')
Georg Brandl24420152008-05-26 16:32:26 +0000815
816 # Make rfile unbuffered -- we need to read one line and then pass
817 # the rest to a subprocess, so we can't use buffered input.
818 rbufsize = 0
819
820 def do_POST(self):
821 """Serve a POST request.
822
823 This is only implemented for CGI scripts.
824
825 """
826
827 if self.is_cgi():
828 self.run_cgi()
829 else:
830 self.send_error(501, "Can only POST to CGI scripts")
831
832 def send_head(self):
833 """Version of send_head that support CGI scripts"""
834 if self.is_cgi():
835 return self.run_cgi()
836 else:
837 return SimpleHTTPRequestHandler.send_head(self)
838
839 def is_cgi(self):
840 """Test whether self.path corresponds to a CGI script.
841
842 Return a tuple (dir, rest) if self.path requires running a
843 CGI script, None if not. Note that rest begins with a
844 slash if it is not empty.
845
846 The default implementation tests whether the path
847 begins with one of the strings in the list
848 self.cgi_directories (and the next character is a '/'
849 or the end of the string).
850
851 """
852
853 path = self.path
854
855 for x in self.cgi_directories:
856 i = len(x)
857 if path[:i] == x and (not path[i:] or path[i] == '/'):
858 self.cgi_info = path[:i], path[i+1:]
859 return True
860 return False
861
862 cgi_directories = ['/cgi-bin', '/htbin']
863
864 def is_executable(self, path):
865 """Test whether argument path is an executable file."""
866 return executable(path)
867
868 def is_python(self, path):
869 """Test whether argument path is a Python script."""
870 head, tail = os.path.splitext(path)
871 return tail.lower() in (".py", ".pyw")
872
873 def run_cgi(self):
874 """Execute a CGI script."""
875 path = self.path
876 dir, rest = self.cgi_info
877
878 i = path.find('/', len(dir) + 1)
879 while i >= 0:
880 nextdir = path[:i]
881 nextrest = path[i+1:]
882
883 scriptdir = self.translate_path(nextdir)
884 if os.path.isdir(scriptdir):
885 dir, rest = nextdir, nextrest
886 i = path.find('/', len(dir) + 1)
887 else:
888 break
889
890 # find an explicit query string, if present.
891 i = rest.rfind('?')
892 if i >= 0:
893 rest, query = rest[:i], rest[i+1:]
894 else:
895 query = ''
896
897 # dissect the part after the directory name into a script name &
898 # a possible additional path, to be stored in PATH_INFO.
899 i = rest.find('/')
900 if i >= 0:
901 script, rest = rest[:i], rest[i:]
902 else:
903 script, rest = rest, ''
904
905 scriptname = dir + '/' + script
906 scriptfile = self.translate_path(scriptname)
907 if not os.path.exists(scriptfile):
908 self.send_error(404, "No such CGI script (%r)" % scriptname)
909 return
910 if not os.path.isfile(scriptfile):
911 self.send_error(403, "CGI script is not a plain file (%r)" %
912 scriptname)
913 return
914 ispy = self.is_python(scriptname)
915 if not ispy:
Georg Brandl24420152008-05-26 16:32:26 +0000916 if not self.is_executable(scriptfile):
917 self.send_error(403, "CGI script is not executable (%r)" %
918 scriptname)
919 return
920
921 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
922 # XXX Much of the following could be prepared ahead of time!
923 env = {}
924 env['SERVER_SOFTWARE'] = self.version_string()
925 env['SERVER_NAME'] = self.server.server_name
926 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
927 env['SERVER_PROTOCOL'] = self.protocol_version
928 env['SERVER_PORT'] = str(self.server.server_port)
929 env['REQUEST_METHOD'] = self.command
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000930 uqrest = urllib.parse.unquote(rest)
Georg Brandl24420152008-05-26 16:32:26 +0000931 env['PATH_INFO'] = uqrest
932 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
933 env['SCRIPT_NAME'] = scriptname
934 if query:
935 env['QUERY_STRING'] = query
936 host = self.address_string()
937 if host != self.client_address[0]:
938 env['REMOTE_HOST'] = host
939 env['REMOTE_ADDR'] = self.client_address[0]
Barry Warsaw820c1202008-06-12 04:06:45 +0000940 authorization = self.headers.get("authorization")
Georg Brandl24420152008-05-26 16:32:26 +0000941 if authorization:
942 authorization = authorization.split()
943 if len(authorization) == 2:
944 import base64, binascii
945 env['AUTH_TYPE'] = authorization[0]
946 if authorization[0].lower() == "basic":
947 try:
948 authorization = authorization[1].encode('ascii')
949 authorization = base64.decodestring(authorization).\
950 decode('ascii')
951 except (binascii.Error, UnicodeError):
952 pass
953 else:
954 authorization = authorization.split(':')
955 if len(authorization) == 2:
956 env['REMOTE_USER'] = authorization[0]
957 # XXX REMOTE_IDENT
Barry Warsaw820c1202008-06-12 04:06:45 +0000958 if self.headers.get('content-type') is None:
959 env['CONTENT_TYPE'] = self.headers.get_content_type()
Georg Brandl24420152008-05-26 16:32:26 +0000960 else:
Barry Warsaw820c1202008-06-12 04:06:45 +0000961 env['CONTENT_TYPE'] = self.headers['content-type']
962 length = self.headers.get('content-length')
Georg Brandl24420152008-05-26 16:32:26 +0000963 if length:
964 env['CONTENT_LENGTH'] = length
Barry Warsaw820c1202008-06-12 04:06:45 +0000965 referer = self.headers.get('referer')
Georg Brandl24420152008-05-26 16:32:26 +0000966 if referer:
967 env['HTTP_REFERER'] = referer
968 accept = []
969 for line in self.headers.getallmatchingheaders('accept'):
970 if line[:1] in "\t\n\r ":
971 accept.append(line.strip())
972 else:
973 accept = accept + line[7:].split(',')
974 env['HTTP_ACCEPT'] = ','.join(accept)
Barry Warsaw820c1202008-06-12 04:06:45 +0000975 ua = self.headers.get('user-agent')
Georg Brandl24420152008-05-26 16:32:26 +0000976 if ua:
977 env['HTTP_USER_AGENT'] = ua
Barry Warsaw820c1202008-06-12 04:06:45 +0000978 co = filter(None, self.headers.get_all('cookie', []))
Georg Brandl24420152008-05-26 16:32:26 +0000979 if co:
980 env['HTTP_COOKIE'] = ', '.join(co)
981 # XXX Other HTTP_* headers
982 # Since we're setting the env in the parent, provide empty
983 # values to override previously set values
984 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
985 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
986 env.setdefault(k, "")
987 os.environ.update(env)
988
989 self.send_response(200, "Script output follows")
990
991 decoded_query = query.replace('+', ' ')
992
993 if self.have_fork:
994 # Unix -- fork as we should
995 args = [script]
996 if '=' not in decoded_query:
997 args.append(decoded_query)
998 nobody = nobody_uid()
999 self.wfile.flush() # Always flush before forking
1000 pid = os.fork()
1001 if pid != 0:
1002 # Parent
1003 pid, sts = os.waitpid(pid, 0)
1004 # throw away additional data [see bug #427345]
1005 while select.select([self.rfile], [], [], 0)[0]:
1006 if not self.rfile.read(1):
1007 break
1008 if sts:
1009 self.log_error("CGI script exit status %#x", sts)
1010 return
1011 # Child
1012 try:
1013 try:
1014 os.setuid(nobody)
1015 except os.error:
1016 pass
1017 os.dup2(self.rfile.fileno(), 0)
1018 os.dup2(self.wfile.fileno(), 1)
1019 os.execve(scriptfile, args, os.environ)
1020 except:
1021 self.server.handle_error(self.request, self.client_address)
1022 os._exit(127)
1023
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001024 else:
1025 # Non-Unix -- use subprocess
1026 import subprocess
Georg Brandl24420152008-05-26 16:32:26 +00001027 cmdline = scriptfile
1028 if self.is_python(scriptfile):
1029 interp = sys.executable
1030 if interp.lower().endswith("w.exe"):
1031 # On Windows, use python.exe, not pythonw.exe
1032 interp = interp[:-5] + interp[-4:]
1033 cmdline = "%s -u %s" % (interp, cmdline)
1034 if '=' not in query and '"' not in query:
1035 cmdline = '%s "%s"' % (cmdline, query)
1036 self.log_message("command: %s", cmdline)
1037 try:
1038 nbytes = int(length)
1039 except (TypeError, ValueError):
1040 nbytes = 0
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001041 p = subprocess.Popen(cmdline,
1042 stdin=subprocess.PIPE,
1043 stdout=subprocess.PIPE,
1044 stderr=subprocess.PIPE,
1045 )
Georg Brandl24420152008-05-26 16:32:26 +00001046 if self.command.lower() == "post" and nbytes > 0:
1047 data = self.rfile.read(nbytes)
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001048 else:
1049 data = None
Georg Brandl24420152008-05-26 16:32:26 +00001050 # throw away additional data [see bug #427345]
1051 while select.select([self.rfile._sock], [], [], 0)[0]:
1052 if not self.rfile._sock.recv(1):
1053 break
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001054 stdout, stderr = p.communicate(data)
1055 self.wfile.write(stdout)
1056 if stderr:
1057 self.log_error('%s', stderr)
1058 status = p.returncode
1059 if status:
1060 self.log_error("CGI script exit status %#x", status)
Georg Brandl24420152008-05-26 16:32:26 +00001061 else:
1062 self.log_message("CGI script exited OK")
1063
1064
1065def test(HandlerClass = BaseHTTPRequestHandler,
1066 ServerClass = HTTPServer, protocol="HTTP/1.0"):
1067 """Test the HTTP request handler class.
1068
1069 This runs an HTTP server on port 8000 (or the first command line
1070 argument).
1071
1072 """
1073
1074 if sys.argv[1:]:
1075 port = int(sys.argv[1])
1076 else:
1077 port = 8000
1078 server_address = ('', port)
1079
1080 HandlerClass.protocol_version = protocol
1081 httpd = ServerClass(server_address, HandlerClass)
1082
1083 sa = httpd.socket.getsockname()
1084 print("Serving HTTP on", sa[0], "port", sa[1], "...")
Alexandre Vassalottib5292a22009-04-03 07:16:55 +00001085 try:
1086 httpd.serve_forever()
1087 except KeyboardInterrupt:
1088 print("\nKeyboard interrupt received, exiting.")
1089 httpd.server_close()
1090 sys.exit(0)
Georg Brandl24420152008-05-26 16:32:26 +00001091
1092if __name__ == '__main__':
Georg Brandl24420152008-05-26 16:32:26 +00001093 test(HandlerClass=SimpleHTTPRequestHandler)