blob: fbee6a932de108a310a8bb7bd689cf9838a1eaa0 [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
Berker Peksag366c5702015-02-13 20:48:15 +020085__all__ = [
86 "HTTPServer", "BaseHTTPRequestHandler",
87 "SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
88]
Georg Brandl24420152008-05-26 16:32:26 +000089
Berker Peksag04bc5b92016-03-14 06:06:03 +020090import email.utils
Georg Brandl1f7fffb2010-10-15 15:57:45 +000091import html
Jeremy Hylton914ab452009-03-27 17:16:06 +000092import http.client
93import io
94import mimetypes
95import os
96import posixpath
97import select
98import shutil
99import socket # For gethostbyaddr()
100import socketserver
101import sys
102import time
103import urllib.parse
Senthil Kumaran42713722010-10-03 17:55:45 +0000104import copy
Senthil Kumaran1251faf2012-06-03 16:15:54 +0800105import argparse
106
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200107from http import HTTPStatus
108
Georg Brandl24420152008-05-26 16:32:26 +0000109
110# Default error message template
111DEFAULT_ERROR_MESSAGE = """\
Senthil Kumaran1b407fe2011-03-20 10:44:30 +0800112<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
113 "http://www.w3.org/TR/html4/strict.dtd">
Ezio Melottica897e92011-11-02 19:33:29 +0200114<html>
Senthil Kumaranb253c9f2011-03-17 16:43:22 +0800115 <head>
Senthil Kumaran1b407fe2011-03-20 10:44:30 +0800116 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
Senthil Kumaranb253c9f2011-03-17 16:43:22 +0800117 <title>Error response</title>
118 </head>
119 <body>
120 <h1>Error response</h1>
121 <p>Error code: %(code)d</p>
122 <p>Message: %(message)s.</p>
123 <p>Error code explanation: %(code)s - %(explain)s.</p>
124 </body>
125</html>
Georg Brandl24420152008-05-26 16:32:26 +0000126"""
127
128DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
129
Georg Brandl24420152008-05-26 16:32:26 +0000130class HTTPServer(socketserver.TCPServer):
131
132 allow_reuse_address = 1 # Seems to make sense in testing environment
133
134 def server_bind(self):
135 """Override server_bind to store the server name."""
136 socketserver.TCPServer.server_bind(self)
Martin Panter50badad2016-04-03 01:28:53 +0000137 host, port = self.server_address[:2]
Georg Brandl24420152008-05-26 16:32:26 +0000138 self.server_name = socket.getfqdn(host)
139 self.server_port = port
140
141
142class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
143
144 """HTTP request handler base class.
145
146 The following explanation of HTTP serves to guide you through the
147 code as well as to expose any misunderstandings I may have about
148 HTTP (so you don't need to read the code to figure out I'm wrong
149 :-).
150
151 HTTP (HyperText Transfer Protocol) is an extensible protocol on
152 top of a reliable stream transport (e.g. TCP/IP). The protocol
153 recognizes three parts to a request:
154
155 1. One line identifying the request type and path
156 2. An optional set of RFC-822-style headers
157 3. An optional data part
158
159 The headers and data are separated by a blank line.
160
161 The first line of the request has the form
162
163 <command> <path> <version>
164
165 where <command> is a (case-sensitive) keyword such as GET or POST,
166 <path> is a string containing path information for the request,
167 and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
168 <path> is encoded using the URL encoding scheme (using %xx to signify
169 the ASCII character with hex code xx).
170
171 The specification specifies that lines are separated by CRLF but
172 for compatibility with the widest range of clients recommends
173 servers also handle LF. Similarly, whitespace in the request line
174 is treated sensibly (allowing multiple spaces between components
175 and allowing trailing whitespace).
176
177 Similarly, for output, lines ought to be separated by CRLF pairs
178 but most clients grok LF characters just fine.
179
180 If the first line of the request has the form
181
182 <command> <path>
183
184 (i.e. <version> is left out) then this is assumed to be an HTTP
185 0.9 request; this form has no optional headers and data part and
186 the reply consists of just the data.
187
188 The reply form of the HTTP 1.x protocol again has three parts:
189
190 1. One line giving the response code
191 2. An optional set of RFC-822-style headers
192 3. The data
193
194 Again, the headers and data are separated by a blank line.
195
196 The response code line has the form
197
198 <version> <responsecode> <responsestring>
199
200 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
201 <responsecode> is a 3-digit response code indicating success or
202 failure of the request, and <responsestring> is an optional
203 human-readable string explaining what the response code means.
204
205 This server parses the request and the headers, and then calls a
206 function specific to the request type (<command>). Specifically,
207 a request SPAM will be handled by a method do_SPAM(). If no
208 such method exists the server sends an error response to the
209 client. If it exists, it is called with no arguments:
210
211 do_SPAM()
212
213 Note that the request name is case sensitive (i.e. SPAM and spam
214 are different requests).
215
216 The various request details are stored in instance variables:
217
218 - client_address is the client IP address in the form (host,
219 port);
220
221 - command, path and version are the broken-down request line;
222
Barry Warsaw820c1202008-06-12 04:06:45 +0000223 - headers is an instance of email.message.Message (or a derived
Georg Brandl24420152008-05-26 16:32:26 +0000224 class) containing the header information;
225
226 - rfile is a file object open for reading positioned at the
227 start of the optional input data part;
228
229 - wfile is a file object open for writing.
230
231 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
232
233 The first thing to be written must be the response line. Then
234 follow 0 or more header lines, then a blank line, and then the
235 actual data (if any). The meaning of the header lines depends on
236 the command executed by the server; in most cases, when data is
237 returned, there should be at least one header line of the form
238
239 Content-type: <type>/<subtype>
240
241 where <type> and <subtype> should be registered MIME types,
242 e.g. "text/html" or "text/plain".
243
244 """
245
246 # The Python system version, truncated to its first component.
247 sys_version = "Python/" + sys.version.split()[0]
248
249 # The server software version. You may want to override this.
250 # The format is multiple whitespace-separated strings,
251 # where each string is of the form name[/version].
252 server_version = "BaseHTTP/" + __version__
253
254 error_message_format = DEFAULT_ERROR_MESSAGE
255 error_content_type = DEFAULT_ERROR_CONTENT_TYPE
256
257 # The default request version. This only affects responses up until
258 # the point where the request line is parsed, so it mainly decides what
259 # the client gets back when sending a malformed request line.
260 # Most web servers default to HTTP 0.9, i.e. don't send a status line.
261 default_request_version = "HTTP/0.9"
262
263 def parse_request(self):
264 """Parse a request (internal).
265
266 The request should be stored in self.raw_requestline; the results
267 are in self.command, self.path, self.request_version and
268 self.headers.
269
270 Return True for success, False for failure; on failure, an
271 error is sent back.
272
273 """
274 self.command = None # set in case of error on the first line
275 self.request_version = version = self.default_request_version
Benjamin Peterson70e28472015-02-17 21:11:10 -0500276 self.close_connection = True
Georg Brandl24420152008-05-26 16:32:26 +0000277 requestline = str(self.raw_requestline, 'iso-8859-1')
Senthil Kumaran30755492011-12-23 17:03:41 +0800278 requestline = requestline.rstrip('\r\n')
Georg Brandl24420152008-05-26 16:32:26 +0000279 self.requestline = requestline
280 words = requestline.split()
281 if len(words) == 3:
Senthil Kumaran30755492011-12-23 17:03:41 +0800282 command, path, version = words
Georg Brandl24420152008-05-26 16:32:26 +0000283 try:
Martin Panter50badad2016-04-03 01:28:53 +0000284 if version[:5] != 'HTTP/':
285 raise ValueError
Georg Brandl24420152008-05-26 16:32:26 +0000286 base_version_number = version.split('/', 1)[1]
287 version_number = base_version_number.split(".")
288 # RFC 2145 section 3.1 says there can be only one "." and
289 # - major and minor numbers MUST be treated as
290 # separate integers;
291 # - HTTP/2.4 is a lower version than HTTP/2.13, which in
292 # turn is lower than HTTP/12.3;
293 # - Leading zeros MUST be ignored by recipients.
294 if len(version_number) != 2:
295 raise ValueError
296 version_number = int(version_number[0]), int(version_number[1])
297 except (ValueError, IndexError):
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200298 self.send_error(
299 HTTPStatus.BAD_REQUEST,
300 "Bad request version (%r)" % version)
Georg Brandl24420152008-05-26 16:32:26 +0000301 return False
302 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
Benjamin Peterson70e28472015-02-17 21:11:10 -0500303 self.close_connection = False
Georg Brandl24420152008-05-26 16:32:26 +0000304 if version_number >= (2, 0):
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200305 self.send_error(
306 HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
Martin Panter50badad2016-04-03 01:28:53 +0000307 "Invalid HTTP version (%s)" % base_version_number)
Georg Brandl24420152008-05-26 16:32:26 +0000308 return False
309 elif len(words) == 2:
Senthil Kumaran30755492011-12-23 17:03:41 +0800310 command, path = words
Benjamin Peterson70e28472015-02-17 21:11:10 -0500311 self.close_connection = True
Georg Brandl24420152008-05-26 16:32:26 +0000312 if command != 'GET':
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200313 self.send_error(
314 HTTPStatus.BAD_REQUEST,
315 "Bad HTTP/0.9 request type (%r)" % command)
Georg Brandl24420152008-05-26 16:32:26 +0000316 return False
317 elif not words:
318 return False
319 else:
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200320 self.send_error(
321 HTTPStatus.BAD_REQUEST,
322 "Bad request syntax (%r)" % requestline)
Georg Brandl24420152008-05-26 16:32:26 +0000323 return False
324 self.command, self.path, self.request_version = command, path, version
325
326 # Examine the headers and look for a Connection directive.
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000327 try:
328 self.headers = http.client.parse_headers(self.rfile,
329 _class=self.MessageClass)
Martin Panter50badad2016-04-03 01:28:53 +0000330 except http.client.LineTooLong as err:
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200331 self.send_error(
Martin Panter50badad2016-04-03 01:28:53 +0000332 HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
333 "Line too long",
334 str(err))
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000335 return False
Martin Panteracc03192016-04-03 00:45:46 +0000336 except http.client.HTTPException as err:
337 self.send_error(
338 HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
339 "Too many headers",
340 str(err)
341 )
342 return False
Georg Brandl24420152008-05-26 16:32:26 +0000343
344 conntype = self.headers.get('Connection', "")
345 if conntype.lower() == 'close':
Benjamin Peterson70e28472015-02-17 21:11:10 -0500346 self.close_connection = True
Georg Brandl24420152008-05-26 16:32:26 +0000347 elif (conntype.lower() == 'keep-alive' and
348 self.protocol_version >= "HTTP/1.1"):
Benjamin Peterson70e28472015-02-17 21:11:10 -0500349 self.close_connection = False
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000350 # Examine the headers and look for an Expect directive
351 expect = self.headers.get('Expect', "")
352 if (expect.lower() == "100-continue" and
353 self.protocol_version >= "HTTP/1.1" and
354 self.request_version >= "HTTP/1.1"):
355 if not self.handle_expect_100():
356 return False
357 return True
358
359 def handle_expect_100(self):
360 """Decide what to do with an "Expect: 100-continue" header.
361
362 If the client is expecting a 100 Continue response, we must
363 respond with either a 100 Continue or a final response before
364 waiting for the request body. The default is to always respond
365 with a 100 Continue. You can behave differently (for example,
366 reject unauthorized requests) by overriding this method.
367
368 This method should either return True (possibly after sending
369 a 100 Continue response) or send an error response and return
370 False.
371
372 """
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200373 self.send_response_only(HTTPStatus.CONTINUE)
Benjamin Peterson04424232014-01-18 21:50:18 -0500374 self.end_headers()
Georg Brandl24420152008-05-26 16:32:26 +0000375 return True
376
377 def handle_one_request(self):
378 """Handle a single HTTP request.
379
380 You normally don't need to override this method; see the class
381 __doc__ string for information on how to handle specific HTTP
382 commands such as GET and POST.
383
384 """
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000385 try:
Antoine Pitrouc4924372010-12-16 16:48:36 +0000386 self.raw_requestline = self.rfile.readline(65537)
387 if len(self.raw_requestline) > 65536:
388 self.requestline = ''
389 self.request_version = ''
390 self.command = ''
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200391 self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
Antoine Pitrouc4924372010-12-16 16:48:36 +0000392 return
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000393 if not self.raw_requestline:
Benjamin Peterson70e28472015-02-17 21:11:10 -0500394 self.close_connection = True
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000395 return
396 if not self.parse_request():
397 # An error code has been sent, just exit
398 return
399 mname = 'do_' + self.command
400 if not hasattr(self, mname):
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200401 self.send_error(
402 HTTPStatus.NOT_IMPLEMENTED,
403 "Unsupported method (%r)" % self.command)
Kristján Valur Jónsson985fc6a2009-07-01 10:01:31 +0000404 return
405 method = getattr(self, mname)
406 method()
407 self.wfile.flush() #actually send the response if not already done.
408 except socket.timeout as e:
409 #a read or a write timed out. Discard this connection
410 self.log_error("Request timed out: %r", e)
Benjamin Peterson70e28472015-02-17 21:11:10 -0500411 self.close_connection = True
Georg Brandl24420152008-05-26 16:32:26 +0000412 return
Georg Brandl24420152008-05-26 16:32:26 +0000413
414 def handle(self):
415 """Handle multiple requests if necessary."""
Benjamin Peterson70e28472015-02-17 21:11:10 -0500416 self.close_connection = True
Georg Brandl24420152008-05-26 16:32:26 +0000417
418 self.handle_one_request()
419 while not self.close_connection:
420 self.handle_one_request()
421
Senthil Kumaran26886442013-03-15 07:53:21 -0700422 def send_error(self, code, message=None, explain=None):
Georg Brandl24420152008-05-26 16:32:26 +0000423 """Send and log an error reply.
424
Senthil Kumaran26886442013-03-15 07:53:21 -0700425 Arguments are
426 * code: an HTTP error code
427 3 digits
428 * message: a simple optional 1 line reason phrase.
429 *( HTAB / SP / VCHAR / %x80-FF )
430 defaults to short entry matching the response code
431 * explain: a detailed message defaults to the long entry
432 matching the response code.
Georg Brandl24420152008-05-26 16:32:26 +0000433
434 This sends an error response (so it must be called before any
435 output has been generated), logs the error, and finally sends
436 a piece of HTML explaining the error to the user.
437
438 """
439
440 try:
441 shortmsg, longmsg = self.responses[code]
442 except KeyError:
443 shortmsg, longmsg = '???', '???'
444 if message is None:
445 message = shortmsg
Senthil Kumaran26886442013-03-15 07:53:21 -0700446 if explain is None:
447 explain = longmsg
Georg Brandl24420152008-05-26 16:32:26 +0000448 self.log_error("code %d, message %s", code, message)
Martin Panterda3bb382016-04-11 00:40:08 +0000449 # HTML encode to prevent Cross Site Scripting attacks (see bug #1100201)
450 content = (self.error_message_format % {
451 'code': code,
452 'message': html.escape(message, quote=False),
453 'explain': html.escape(explain, quote=False)
454 })
Senthil Kumaran52d27202012-10-10 23:16:21 -0700455 body = content.encode('UTF-8', 'replace')
Senthil Kumaran1e7551d2013-03-05 02:25:58 -0800456 self.send_response(code, message)
Georg Brandl24420152008-05-26 16:32:26 +0000457 self.send_header("Content-Type", self.error_content_type)
458 self.send_header('Connection', 'close')
Senthil Kumaran52d27202012-10-10 23:16:21 -0700459 self.send_header('Content-Length', int(len(body)))
Georg Brandl24420152008-05-26 16:32:26 +0000460 self.end_headers()
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200461
462 if (self.command != 'HEAD' and
463 code >= 200 and
464 code not in (
465 HTTPStatus.NO_CONTENT, HTTPStatus.NOT_MODIFIED)):
Senthil Kumaran52d27202012-10-10 23:16:21 -0700466 self.wfile.write(body)
Georg Brandl24420152008-05-26 16:32:26 +0000467
468 def send_response(self, code, message=None):
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800469 """Add the response header to the headers buffer and log the
470 response code.
Georg Brandl24420152008-05-26 16:32:26 +0000471
472 Also send two standard headers with the server software
473 version and the current date.
474
475 """
476 self.log_request(code)
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000477 self.send_response_only(code, message)
478 self.send_header('Server', self.version_string())
479 self.send_header('Date', self.date_time_string())
480
481 def send_response_only(self, code, message=None):
482 """Send the response header only."""
Georg Brandl24420152008-05-26 16:32:26 +0000483 if self.request_version != 'HTTP/0.9':
Martin Panter50badad2016-04-03 01:28:53 +0000484 if message is None:
485 if code in self.responses:
486 message = self.responses[code][0]
487 else:
488 message = ''
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800489 if not hasattr(self, '_headers_buffer'):
490 self._headers_buffer = []
491 self._headers_buffer.append(("%s %d %s\r\n" %
492 (self.protocol_version, code, message)).encode(
493 'latin-1', 'strict'))
Georg Brandl24420152008-05-26 16:32:26 +0000494
495 def send_header(self, keyword, value):
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800496 """Send a MIME header to the headers buffer."""
Georg Brandl24420152008-05-26 16:32:26 +0000497 if self.request_version != 'HTTP/0.9':
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000498 if not hasattr(self, '_headers_buffer'):
499 self._headers_buffer = []
500 self._headers_buffer.append(
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000501 ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict'))
Georg Brandl24420152008-05-26 16:32:26 +0000502
503 if keyword.lower() == 'connection':
504 if value.lower() == 'close':
Benjamin Peterson70e28472015-02-17 21:11:10 -0500505 self.close_connection = True
Georg Brandl24420152008-05-26 16:32:26 +0000506 elif value.lower() == 'keep-alive':
Benjamin Peterson70e28472015-02-17 21:11:10 -0500507 self.close_connection = False
Georg Brandl24420152008-05-26 16:32:26 +0000508
509 def end_headers(self):
510 """Send the blank line ending the MIME headers."""
511 if self.request_version != 'HTTP/0.9':
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000512 self._headers_buffer.append(b"\r\n")
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800513 self.flush_headers()
514
515 def flush_headers(self):
516 if hasattr(self, '_headers_buffer'):
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000517 self.wfile.write(b"".join(self._headers_buffer))
518 self._headers_buffer = []
Georg Brandl24420152008-05-26 16:32:26 +0000519
520 def log_request(self, code='-', size='-'):
521 """Log an accepted request.
522
523 This is called by send_response().
524
525 """
Serhiy Storchakac0a23e62015-03-07 11:51:37 +0200526 if isinstance(code, HTTPStatus):
527 code = code.value
Georg Brandl24420152008-05-26 16:32:26 +0000528 self.log_message('"%s" %s %s',
529 self.requestline, str(code), str(size))
530
531 def log_error(self, format, *args):
532 """Log an error.
533
534 This is called when a request cannot be fulfilled. By
535 default it passes the message on to log_message().
536
537 Arguments are the same as for log_message().
538
539 XXX This should go to the separate error log.
540
541 """
542
543 self.log_message(format, *args)
544
545 def log_message(self, format, *args):
546 """Log an arbitrary message.
547
548 This is used by all other logging functions. Override
549 it if you have specific logging wishes.
550
551 The first argument, FORMAT, is a format string for the
552 message to be logged. If the format string contains
553 any % escapes requiring parameters, they should be
554 specified as subsequent arguments (it's just like
555 printf!).
556
Senthil Kumarandb727b42012-04-29 13:41:03 +0800557 The client ip and current date/time are prefixed to
Georg Brandl24420152008-05-26 16:32:26 +0000558 every message.
559
560 """
561
562 sys.stderr.write("%s - - [%s] %s\n" %
563 (self.address_string(),
564 self.log_date_time_string(),
565 format%args))
566
567 def version_string(self):
568 """Return the server software version string."""
569 return self.server_version + ' ' + self.sys_version
570
571 def date_time_string(self, timestamp=None):
572 """Return the current date and time formatted for a message header."""
573 if timestamp is None:
574 timestamp = time.time()
Berker Peksag04bc5b92016-03-14 06:06:03 +0200575 return email.utils.formatdate(timestamp, usegmt=True)
Georg Brandl24420152008-05-26 16:32:26 +0000576
577 def log_date_time_string(self):
578 """Return the current time formatted for logging."""
579 now = time.time()
580 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
581 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
582 day, self.monthname[month], year, hh, mm, ss)
583 return s
584
585 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
586
587 monthname = [None,
588 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
589 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
590
591 def address_string(self):
Senthil Kumaran1aacba42012-04-29 12:51:54 +0800592 """Return the client address."""
Georg Brandl24420152008-05-26 16:32:26 +0000593
Senthil Kumaran1aacba42012-04-29 12:51:54 +0800594 return self.client_address[0]
Georg Brandl24420152008-05-26 16:32:26 +0000595
596 # Essentially static class variables
597
598 # The version of the HTTP protocol we support.
599 # Set this to HTTP/1.1 to enable automatic keepalive
600 protocol_version = "HTTP/1.0"
601
Barry Warsaw820c1202008-06-12 04:06:45 +0000602 # MessageClass used to parse headers
Barry Warsaw820c1202008-06-12 04:06:45 +0000603 MessageClass = http.client.HTTPMessage
Georg Brandl24420152008-05-26 16:32:26 +0000604
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200605 # hack to maintain backwards compatibility
Georg Brandl24420152008-05-26 16:32:26 +0000606 responses = {
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200607 v: (v.phrase, v.description)
608 for v in HTTPStatus.__members__.values()
609 }
Georg Brandl24420152008-05-26 16:32:26 +0000610
611
612class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
613
614 """Simple HTTP request handler with GET and HEAD commands.
615
616 This serves files from the current directory and any of its
617 subdirectories. The MIME type for files is determined by
618 calling the .guess_type() method.
619
620 The GET and HEAD requests are identical except that the HEAD
621 request omits the actual contents of the file.
622
623 """
624
625 server_version = "SimpleHTTP/" + __version__
626
627 def do_GET(self):
628 """Serve a GET request."""
629 f = self.send_head()
630 if f:
Serhiy Storchaka91b0bc22014-01-25 19:43:02 +0200631 try:
632 self.copyfile(f, self.wfile)
633 finally:
634 f.close()
Georg Brandl24420152008-05-26 16:32:26 +0000635
636 def do_HEAD(self):
637 """Serve a HEAD request."""
638 f = self.send_head()
639 if f:
640 f.close()
641
642 def send_head(self):
643 """Common code for GET and HEAD commands.
644
645 This sends the response code and MIME headers.
646
647 Return value is either a file object (which has to be copied
648 to the outputfile by the caller unless the command was HEAD,
649 and must be closed by the caller under all circumstances), or
650 None, in which case the caller has nothing further to do.
651
652 """
653 path = self.translate_path(self.path)
654 f = None
655 if os.path.isdir(path):
Benjamin Peterson94cb7a22014-12-26 10:53:43 -0600656 parts = urllib.parse.urlsplit(self.path)
657 if not parts.path.endswith('/'):
Georg Brandl24420152008-05-26 16:32:26 +0000658 # redirect browser - doing basically what apache does
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200659 self.send_response(HTTPStatus.MOVED_PERMANENTLY)
Benjamin Peterson94cb7a22014-12-26 10:53:43 -0600660 new_parts = (parts[0], parts[1], parts[2] + '/',
661 parts[3], parts[4])
662 new_url = urllib.parse.urlunsplit(new_parts)
663 self.send_header("Location", new_url)
Georg Brandl24420152008-05-26 16:32:26 +0000664 self.end_headers()
665 return None
666 for index in "index.html", "index.htm":
667 index = os.path.join(path, index)
668 if os.path.exists(index):
669 path = index
670 break
671 else:
672 return self.list_directory(path)
673 ctype = self.guess_type(path)
674 try:
675 f = open(path, 'rb')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200676 except OSError:
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200677 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
Georg Brandl24420152008-05-26 16:32:26 +0000678 return None
Serhiy Storchaka91b0bc22014-01-25 19:43:02 +0200679 try:
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200680 self.send_response(HTTPStatus.OK)
Serhiy Storchaka91b0bc22014-01-25 19:43:02 +0200681 self.send_header("Content-type", ctype)
682 fs = os.fstat(f.fileno())
683 self.send_header("Content-Length", str(fs[6]))
684 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
685 self.end_headers()
686 return f
687 except:
688 f.close()
689 raise
Georg Brandl24420152008-05-26 16:32:26 +0000690
691 def list_directory(self, path):
692 """Helper to produce a directory listing (absent index.html).
693
694 Return value is either a file object, or None (indicating an
695 error). In either case, the headers are sent, making the
696 interface the same as for send_head().
697
698 """
699 try:
700 list = os.listdir(path)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200701 except OSError:
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200702 self.send_error(
703 HTTPStatus.NOT_FOUND,
704 "No permission to list directory")
Georg Brandl24420152008-05-26 16:32:26 +0000705 return None
706 list.sort(key=lambda a: a.lower())
707 r = []
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300708 try:
709 displaypath = urllib.parse.unquote(self.path,
710 errors='surrogatepass')
711 except UnicodeDecodeError:
712 displaypath = urllib.parse.unquote(path)
Martin Panterda3bb382016-04-11 00:40:08 +0000713 displaypath = html.escape(displaypath, quote=False)
Ezio Melottica897e92011-11-02 19:33:29 +0200714 enc = sys.getfilesystemencoding()
715 title = 'Directory listing for %s' % displaypath
716 r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
717 '"http://www.w3.org/TR/html4/strict.dtd">')
718 r.append('<html>\n<head>')
719 r.append('<meta http-equiv="Content-Type" '
720 'content="text/html; charset=%s">' % enc)
721 r.append('<title>%s</title>\n</head>' % title)
722 r.append('<body>\n<h1>%s</h1>' % title)
723 r.append('<hr>\n<ul>')
Georg Brandl24420152008-05-26 16:32:26 +0000724 for name in list:
725 fullname = os.path.join(path, name)
726 displayname = linkname = name
727 # Append / for directories or @ for symbolic links
728 if os.path.isdir(fullname):
729 displayname = name + "/"
730 linkname = name + "/"
731 if os.path.islink(fullname):
732 displayname = name + "@"
733 # Note: a link to a directory displays with @ and links with /
Ezio Melottica897e92011-11-02 19:33:29 +0200734 r.append('<li><a href="%s">%s</a></li>'
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300735 % (urllib.parse.quote(linkname,
736 errors='surrogatepass'),
Martin Panterda3bb382016-04-11 00:40:08 +0000737 html.escape(displayname, quote=False)))
Ezio Melottica897e92011-11-02 19:33:29 +0200738 r.append('</ul>\n<hr>\n</body>\n</html>\n')
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300739 encoded = '\n'.join(r).encode(enc, 'surrogateescape')
Georg Brandl24420152008-05-26 16:32:26 +0000740 f = io.BytesIO()
741 f.write(encoded)
742 f.seek(0)
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200743 self.send_response(HTTPStatus.OK)
Georg Brandl24420152008-05-26 16:32:26 +0000744 self.send_header("Content-type", "text/html; charset=%s" % enc)
745 self.send_header("Content-Length", str(len(encoded)))
746 self.end_headers()
747 return f
748
749 def translate_path(self, path):
750 """Translate a /-separated PATH to the local filename syntax.
751
752 Components that mean special things to the local file system
753 (e.g. drive or directory names) are ignored. (XXX They should
754 probably be diagnosed.)
755
756 """
757 # abandon query parameters
758 path = path.split('?',1)[0]
759 path = path.split('#',1)[0]
Senthil Kumaran72c238e2013-09-13 00:21:18 -0700760 # Don't forget explicit trailing slash when normalizing. Issue17324
Senthil Kumaran600b7352013-09-29 18:59:04 -0700761 trailing_slash = path.rstrip().endswith('/')
Serhiy Storchakacb5bc402014-08-17 08:22:11 +0300762 try:
763 path = urllib.parse.unquote(path, errors='surrogatepass')
764 except UnicodeDecodeError:
765 path = urllib.parse.unquote(path)
766 path = posixpath.normpath(path)
Georg Brandl24420152008-05-26 16:32:26 +0000767 words = path.split('/')
768 words = filter(None, words)
769 path = os.getcwd()
770 for word in words:
771 drive, word = os.path.splitdrive(word)
772 head, word = os.path.split(word)
773 if word in (os.curdir, os.pardir): continue
774 path = os.path.join(path, word)
Senthil Kumaran72c238e2013-09-13 00:21:18 -0700775 if trailing_slash:
776 path += '/'
Georg Brandl24420152008-05-26 16:32:26 +0000777 return path
778
779 def copyfile(self, source, outputfile):
780 """Copy all data between two file objects.
781
782 The SOURCE argument is a file object open for reading
783 (or anything with a read() method) and the DESTINATION
784 argument is a file object open for writing (or
785 anything with a write() method).
786
787 The only reason for overriding this would be to change
788 the block size or perhaps to replace newlines by CRLF
789 -- note however that this the default server uses this
790 to copy binary data as well.
791
792 """
793 shutil.copyfileobj(source, outputfile)
794
795 def guess_type(self, path):
796 """Guess the type of a file.
797
798 Argument is a PATH (a filename).
799
800 Return value is a string of the form type/subtype,
801 usable for a MIME Content-type header.
802
803 The default implementation looks the file's extension
804 up in the table self.extensions_map, using application/octet-stream
805 as a default; however it would be permissible (if
806 slow) to look inside the data to make a better guess.
807
808 """
809
810 base, ext = posixpath.splitext(path)
811 if ext in self.extensions_map:
812 return self.extensions_map[ext]
813 ext = ext.lower()
814 if ext in self.extensions_map:
815 return self.extensions_map[ext]
816 else:
817 return self.extensions_map['']
818
819 if not mimetypes.inited:
820 mimetypes.init() # try to read system mime.types
821 extensions_map = mimetypes.types_map.copy()
822 extensions_map.update({
823 '': 'application/octet-stream', # Default
824 '.py': 'text/plain',
825 '.c': 'text/plain',
826 '.h': 'text/plain',
827 })
828
829
830# Utilities for CGIHTTPRequestHandler
831
Senthil Kumarand70846b2012-04-12 02:34:32 +0800832def _url_collapse_path(path):
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000833 """
834 Given a URL path, remove extra '/'s and '.' path elements and collapse
Martin Panter9955a372015-10-07 10:26:23 +0000835 any '..' references and returns a collapsed path.
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000836
837 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
Senthil Kumarand70846b2012-04-12 02:34:32 +0800838 The utility of this function is limited to is_cgi method and helps
839 preventing some security attacks.
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000840
Martin Pantercb29e8c2015-10-03 05:55:46 +0000841 Returns: The reconstituted URL, which will always start with a '/'.
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000842
843 Raises: IndexError if too many '..' occur within the path.
Senthil Kumarand70846b2012-04-12 02:34:32 +0800844
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000845 """
Martin Pantercb29e8c2015-10-03 05:55:46 +0000846 # Query component should not be involved.
847 path, _, query = path.partition('?')
848 path = urllib.parse.unquote(path)
849
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000850 # Similar to os.path.split(os.path.normpath(path)) but specific to URL
851 # path semantics rather than local operating system semantics.
Senthil Kumarand70846b2012-04-12 02:34:32 +0800852 path_parts = path.split('/')
853 head_parts = []
854 for part in path_parts[:-1]:
855 if part == '..':
856 head_parts.pop() # IndexError if more '..' than prior parts
857 elif part and part != '.':
858 head_parts.append( part )
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000859 if path_parts:
Senthil Kumarandbb369d2012-04-11 03:15:28 +0800860 tail_part = path_parts.pop()
Senthil Kumarand70846b2012-04-12 02:34:32 +0800861 if tail_part:
862 if tail_part == '..':
863 head_parts.pop()
864 tail_part = ''
865 elif tail_part == '.':
866 tail_part = ''
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000867 else:
868 tail_part = ''
Senthil Kumarand70846b2012-04-12 02:34:32 +0800869
Martin Pantercb29e8c2015-10-03 05:55:46 +0000870 if query:
871 tail_part = '?'.join((tail_part, query))
872
Senthil Kumarand70846b2012-04-12 02:34:32 +0800873 splitpath = ('/' + '/'.join(head_parts), tail_part)
874 collapsed_path = "/".join(splitpath)
875
876 return collapsed_path
877
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000878
879
Georg Brandl24420152008-05-26 16:32:26 +0000880nobody = None
881
882def nobody_uid():
883 """Internal routine to get nobody's uid"""
884 global nobody
885 if nobody:
886 return nobody
887 try:
888 import pwd
Brett Cannoncd171c82013-07-04 17:43:24 -0400889 except ImportError:
Georg Brandl24420152008-05-26 16:32:26 +0000890 return -1
891 try:
892 nobody = pwd.getpwnam('nobody')[2]
893 except KeyError:
Georg Brandlcbd2ab12010-12-04 10:39:14 +0000894 nobody = 1 + max(x[2] for x in pwd.getpwall())
Georg Brandl24420152008-05-26 16:32:26 +0000895 return nobody
896
897
898def executable(path):
899 """Test for executable file."""
Victor Stinnerfb25ba92011-06-20 17:45:54 +0200900 return os.access(path, os.X_OK)
Georg Brandl24420152008-05-26 16:32:26 +0000901
902
903class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
904
905 """Complete HTTP server with GET, HEAD and POST commands.
906
907 GET and HEAD also support running CGI scripts.
908
909 The POST command is *only* implemented for CGI scripts.
910
911 """
912
913 # Determine platform specifics
914 have_fork = hasattr(os, 'fork')
Georg Brandl24420152008-05-26 16:32:26 +0000915
916 # Make rfile unbuffered -- we need to read one line and then pass
917 # the rest to a subprocess, so we can't use buffered input.
918 rbufsize = 0
919
920 def do_POST(self):
921 """Serve a POST request.
922
923 This is only implemented for CGI scripts.
924
925 """
926
927 if self.is_cgi():
928 self.run_cgi()
929 else:
Serhiy Storchakae4db7692014-12-23 16:28:28 +0200930 self.send_error(
931 HTTPStatus.NOT_IMPLEMENTED,
932 "Can only POST to CGI scripts")
Georg Brandl24420152008-05-26 16:32:26 +0000933
934 def send_head(self):
935 """Version of send_head that support CGI scripts"""
936 if self.is_cgi():
937 return self.run_cgi()
938 else:
939 return SimpleHTTPRequestHandler.send_head(self)
940
941 def is_cgi(self):
942 """Test whether self.path corresponds to a CGI script.
943
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000944 Returns True and updates the cgi_info attribute to the tuple
945 (dir, rest) if self.path requires running a CGI script.
946 Returns False otherwise.
Georg Brandl24420152008-05-26 16:32:26 +0000947
Benjamin Petersona7deeee2009-05-08 20:54:42 +0000948 If any exception is raised, the caller should assume that
949 self.path was rejected as invalid and act accordingly.
950
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000951 The default implementation tests whether the normalized url
952 path begins with one of the strings in self.cgi_directories
953 (and the next character is a '/' or the end of the string).
Georg Brandl24420152008-05-26 16:32:26 +0000954
955 """
Martin Pantercb29e8c2015-10-03 05:55:46 +0000956 collapsed_path = _url_collapse_path(self.path)
Senthil Kumarand70846b2012-04-12 02:34:32 +0800957 dir_sep = collapsed_path.find('/', 1)
958 head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
Senthil Kumarandbb369d2012-04-11 03:15:28 +0800959 if head in self.cgi_directories:
960 self.cgi_info = head, tail
Benjamin Petersonad71f0f2009-04-11 20:12:10 +0000961 return True
Georg Brandl24420152008-05-26 16:32:26 +0000962 return False
963
Senthil Kumarand70846b2012-04-12 02:34:32 +0800964
Georg Brandl24420152008-05-26 16:32:26 +0000965 cgi_directories = ['/cgi-bin', '/htbin']
966
967 def is_executable(self, path):
968 """Test whether argument path is an executable file."""
969 return executable(path)
970
971 def is_python(self, path):
972 """Test whether argument path is a Python script."""
973 head, tail = os.path.splitext(path)
974 return tail.lower() in (".py", ".pyw")
975
976 def run_cgi(self):
977 """Execute a CGI script."""
Georg Brandl24420152008-05-26 16:32:26 +0000978 dir, rest = self.cgi_info
Ned Deily915a30f2014-07-12 22:06:26 -0700979 path = dir + '/' + rest
980 i = path.find('/', len(dir)+1)
Georg Brandl24420152008-05-26 16:32:26 +0000981 while i >= 0:
Ned Deily915a30f2014-07-12 22:06:26 -0700982 nextdir = path[:i]
983 nextrest = path[i+1:]
Georg Brandl24420152008-05-26 16:32:26 +0000984
985 scriptdir = self.translate_path(nextdir)
986 if os.path.isdir(scriptdir):
987 dir, rest = nextdir, nextrest
Ned Deily915a30f2014-07-12 22:06:26 -0700988 i = path.find('/', len(dir)+1)
Georg Brandl24420152008-05-26 16:32:26 +0000989 else:
990 break
991
992 # find an explicit query string, if present.
Martin Pantera02e18a2015-10-03 05:38:07 +0000993 rest, _, query = rest.partition('?')
Georg Brandl24420152008-05-26 16:32:26 +0000994
995 # dissect the part after the directory name into a script name &
996 # a possible additional path, to be stored in PATH_INFO.
997 i = rest.find('/')
998 if i >= 0:
999 script, rest = rest[:i], rest[i:]
1000 else:
1001 script, rest = rest, ''
1002
1003 scriptname = dir + '/' + script
1004 scriptfile = self.translate_path(scriptname)
1005 if not os.path.exists(scriptfile):
Serhiy Storchakae4db7692014-12-23 16:28:28 +02001006 self.send_error(
1007 HTTPStatus.NOT_FOUND,
1008 "No such CGI script (%r)" % scriptname)
Georg Brandl24420152008-05-26 16:32:26 +00001009 return
1010 if not os.path.isfile(scriptfile):
Serhiy Storchakae4db7692014-12-23 16:28:28 +02001011 self.send_error(
1012 HTTPStatus.FORBIDDEN,
1013 "CGI script is not a plain file (%r)" % scriptname)
Georg Brandl24420152008-05-26 16:32:26 +00001014 return
1015 ispy = self.is_python(scriptname)
Victor Stinnerfb25ba92011-06-20 17:45:54 +02001016 if self.have_fork or not ispy:
Georg Brandl24420152008-05-26 16:32:26 +00001017 if not self.is_executable(scriptfile):
Serhiy Storchakae4db7692014-12-23 16:28:28 +02001018 self.send_error(
1019 HTTPStatus.FORBIDDEN,
1020 "CGI script is not executable (%r)" % scriptname)
Georg Brandl24420152008-05-26 16:32:26 +00001021 return
1022
1023 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
1024 # XXX Much of the following could be prepared ahead of time!
Senthil Kumaran42713722010-10-03 17:55:45 +00001025 env = copy.deepcopy(os.environ)
Georg Brandl24420152008-05-26 16:32:26 +00001026 env['SERVER_SOFTWARE'] = self.version_string()
1027 env['SERVER_NAME'] = self.server.server_name
1028 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
1029 env['SERVER_PROTOCOL'] = self.protocol_version
1030 env['SERVER_PORT'] = str(self.server.server_port)
1031 env['REQUEST_METHOD'] = self.command
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001032 uqrest = urllib.parse.unquote(rest)
Georg Brandl24420152008-05-26 16:32:26 +00001033 env['PATH_INFO'] = uqrest
1034 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
1035 env['SCRIPT_NAME'] = scriptname
1036 if query:
1037 env['QUERY_STRING'] = query
Georg Brandl24420152008-05-26 16:32:26 +00001038 env['REMOTE_ADDR'] = self.client_address[0]
Barry Warsaw820c1202008-06-12 04:06:45 +00001039 authorization = self.headers.get("authorization")
Georg Brandl24420152008-05-26 16:32:26 +00001040 if authorization:
1041 authorization = authorization.split()
1042 if len(authorization) == 2:
1043 import base64, binascii
1044 env['AUTH_TYPE'] = authorization[0]
1045 if authorization[0].lower() == "basic":
1046 try:
1047 authorization = authorization[1].encode('ascii')
Georg Brandl706824f2009-06-04 09:42:55 +00001048 authorization = base64.decodebytes(authorization).\
Georg Brandl24420152008-05-26 16:32:26 +00001049 decode('ascii')
1050 except (binascii.Error, UnicodeError):
1051 pass
1052 else:
1053 authorization = authorization.split(':')
1054 if len(authorization) == 2:
1055 env['REMOTE_USER'] = authorization[0]
1056 # XXX REMOTE_IDENT
Barry Warsaw820c1202008-06-12 04:06:45 +00001057 if self.headers.get('content-type') is None:
1058 env['CONTENT_TYPE'] = self.headers.get_content_type()
Georg Brandl24420152008-05-26 16:32:26 +00001059 else:
Barry Warsaw820c1202008-06-12 04:06:45 +00001060 env['CONTENT_TYPE'] = self.headers['content-type']
1061 length = self.headers.get('content-length')
Georg Brandl24420152008-05-26 16:32:26 +00001062 if length:
1063 env['CONTENT_LENGTH'] = length
Barry Warsaw820c1202008-06-12 04:06:45 +00001064 referer = self.headers.get('referer')
Georg Brandl24420152008-05-26 16:32:26 +00001065 if referer:
1066 env['HTTP_REFERER'] = referer
1067 accept = []
1068 for line in self.headers.getallmatchingheaders('accept'):
1069 if line[:1] in "\t\n\r ":
1070 accept.append(line.strip())
1071 else:
1072 accept = accept + line[7:].split(',')
1073 env['HTTP_ACCEPT'] = ','.join(accept)
Barry Warsaw820c1202008-06-12 04:06:45 +00001074 ua = self.headers.get('user-agent')
Georg Brandl24420152008-05-26 16:32:26 +00001075 if ua:
1076 env['HTTP_USER_AGENT'] = ua
Barry Warsaw820c1202008-06-12 04:06:45 +00001077 co = filter(None, self.headers.get_all('cookie', []))
Georg Brandl62e2ca22010-07-31 21:54:24 +00001078 cookie_str = ', '.join(co)
1079 if cookie_str:
1080 env['HTTP_COOKIE'] = cookie_str
Georg Brandl24420152008-05-26 16:32:26 +00001081 # XXX Other HTTP_* headers
1082 # Since we're setting the env in the parent, provide empty
1083 # values to override previously set values
1084 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
1085 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
1086 env.setdefault(k, "")
Georg Brandl24420152008-05-26 16:32:26 +00001087
Serhiy Storchakae4db7692014-12-23 16:28:28 +02001088 self.send_response(HTTPStatus.OK, "Script output follows")
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +08001089 self.flush_headers()
Georg Brandl24420152008-05-26 16:32:26 +00001090
1091 decoded_query = query.replace('+', ' ')
1092
1093 if self.have_fork:
1094 # Unix -- fork as we should
1095 args = [script]
1096 if '=' not in decoded_query:
1097 args.append(decoded_query)
1098 nobody = nobody_uid()
1099 self.wfile.flush() # Always flush before forking
1100 pid = os.fork()
1101 if pid != 0:
1102 # Parent
1103 pid, sts = os.waitpid(pid, 0)
1104 # throw away additional data [see bug #427345]
1105 while select.select([self.rfile], [], [], 0)[0]:
1106 if not self.rfile.read(1):
1107 break
1108 if sts:
1109 self.log_error("CGI script exit status %#x", sts)
1110 return
1111 # Child
1112 try:
1113 try:
1114 os.setuid(nobody)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +02001115 except OSError:
Georg Brandl24420152008-05-26 16:32:26 +00001116 pass
1117 os.dup2(self.rfile.fileno(), 0)
1118 os.dup2(self.wfile.fileno(), 1)
Senthil Kumaran42713722010-10-03 17:55:45 +00001119 os.execve(scriptfile, args, env)
Georg Brandl24420152008-05-26 16:32:26 +00001120 except:
1121 self.server.handle_error(self.request, self.client_address)
1122 os._exit(127)
1123
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001124 else:
1125 # Non-Unix -- use subprocess
1126 import subprocess
Senthil Kumarane29cd162009-11-11 04:17:53 +00001127 cmdline = [scriptfile]
Georg Brandl24420152008-05-26 16:32:26 +00001128 if self.is_python(scriptfile):
1129 interp = sys.executable
1130 if interp.lower().endswith("w.exe"):
1131 # On Windows, use python.exe, not pythonw.exe
1132 interp = interp[:-5] + interp[-4:]
Senthil Kumarane29cd162009-11-11 04:17:53 +00001133 cmdline = [interp, '-u'] + cmdline
1134 if '=' not in query:
1135 cmdline.append(query)
1136 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
Georg Brandl24420152008-05-26 16:32:26 +00001137 try:
1138 nbytes = int(length)
1139 except (TypeError, ValueError):
1140 nbytes = 0
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001141 p = subprocess.Popen(cmdline,
1142 stdin=subprocess.PIPE,
1143 stdout=subprocess.PIPE,
Senthil Kumaran42713722010-10-03 17:55:45 +00001144 stderr=subprocess.PIPE,
1145 env = env
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001146 )
Georg Brandl24420152008-05-26 16:32:26 +00001147 if self.command.lower() == "post" and nbytes > 0:
1148 data = self.rfile.read(nbytes)
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001149 else:
1150 data = None
Georg Brandl24420152008-05-26 16:32:26 +00001151 # throw away additional data [see bug #427345]
1152 while select.select([self.rfile._sock], [], [], 0)[0]:
1153 if not self.rfile._sock.recv(1):
1154 break
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001155 stdout, stderr = p.communicate(data)
1156 self.wfile.write(stdout)
1157 if stderr:
1158 self.log_error('%s', stderr)
Brian Curtincbad4df2010-11-05 15:04:48 +00001159 p.stderr.close()
1160 p.stdout.close()
Amaury Forgeot d'Arccb0d2d72008-06-18 22:19:22 +00001161 status = p.returncode
1162 if status:
1163 self.log_error("CGI script exit status %#x", status)
Georg Brandl24420152008-05-26 16:32:26 +00001164 else:
1165 self.log_message("CGI script exited OK")
1166
1167
Senthil Kumarandefe7f42013-09-15 09:37:27 -07001168def test(HandlerClass=BaseHTTPRequestHandler,
1169 ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):
Georg Brandl24420152008-05-26 16:32:26 +00001170 """Test the HTTP request handler class.
1171
Robert Collins9644f242015-08-17 12:18:35 +12001172 This runs an HTTP server on port 8000 (or the port argument).
Georg Brandl24420152008-05-26 16:32:26 +00001173
1174 """
Senthil Kumarandefe7f42013-09-15 09:37:27 -07001175 server_address = (bind, port)
Georg Brandl24420152008-05-26 16:32:26 +00001176
1177 HandlerClass.protocol_version = protocol
1178 httpd = ServerClass(server_address, HandlerClass)
1179
1180 sa = httpd.socket.getsockname()
1181 print("Serving HTTP on", sa[0], "port", sa[1], "...")
Alexandre Vassalottib5292a22009-04-03 07:16:55 +00001182 try:
1183 httpd.serve_forever()
1184 except KeyboardInterrupt:
1185 print("\nKeyboard interrupt received, exiting.")
1186 httpd.server_close()
1187 sys.exit(0)
Georg Brandl24420152008-05-26 16:32:26 +00001188
1189if __name__ == '__main__':
Senthil Kumaran1251faf2012-06-03 16:15:54 +08001190 parser = argparse.ArgumentParser()
1191 parser.add_argument('--cgi', action='store_true',
1192 help='Run as CGI Server')
Senthil Kumarandefe7f42013-09-15 09:37:27 -07001193 parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
1194 help='Specify alternate bind address '
1195 '[default: all interfaces]')
Senthil Kumaran1251faf2012-06-03 16:15:54 +08001196 parser.add_argument('port', action='store',
1197 default=8000, type=int,
1198 nargs='?',
1199 help='Specify alternate port [default: 8000]')
1200 args = parser.parse_args()
1201 if args.cgi:
Senthil Kumarandefe7f42013-09-15 09:37:27 -07001202 handler_class = CGIHTTPRequestHandler
Senthil Kumaran1251faf2012-06-03 16:15:54 +08001203 else:
Senthil Kumarandefe7f42013-09-15 09:37:27 -07001204 handler_class = SimpleHTTPRequestHandler
1205 test(HandlerClass=handler_class, port=args.port, bind=args.bind)