Debian Codegraph @

Lib/http/server.py

raw
1 """HTTP server classes.
2
3 Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see
4 SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST,
5 and (deprecated) CGIHTTPRequestHandler for CGI scripts.
6
7 It does, however, optionally implement HTTP/1.1 persistent connections.
8
9 Notes on CGIHTTPRequestHandler
10 ------------------------------
11
12 This class is deprecated. It implements GET and POST requests to cgi-bin scripts.
13
14 If the os.fork() function is not present (Windows), subprocess.Popen() is used,
15 with slightly altered but never documented semantics. Use from a threaded
16 process is likely to trigger a warning at os.fork() time.
17
18 In all cases, the implementation is intentionally naive -- all
19 requests are executed synchronously.
20
21 SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
22 -- it may execute arbitrary Python code or external programs.
23
24 Note that status code 200 is sent prior to execution of a CGI script, so
25 scripts cannot send other status codes such as 302 (redirect).
26
27 XXX 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__ = [
86 "HTTPServer", "ThreadingHTTPServer",
87 "HTTPSServer", "ThreadingHTTPSServer",
88 "BaseHTTPRequestHandler", "SimpleHTTPRequestHandler",
89 "CGIHTTPRequestHandler",
90 ]
91
92 import copy
93 import datetime
94 import email.utils
95 import html
96 import http.client
97 import io
98 import itertools
99 import mimetypes
100 import os
101 import posixpath
102 import select
103 import shutil
104 import socket
105 import socketserver
106 import sys
107 import time
108 import urllib.parse
109
110 from http import HTTPStatus
111
112
113 # Default error message template
114 DEFAULT_ERROR_MESSAGE = """\
115 <!DOCTYPE HTML>
116 <html lang="en">
117 <head>
118 <meta charset="utf-8">
119 <style type="text/css">
120 :root {
121 color-scheme: light dark;
122 }
123 </style>
124 <title>Error response</title>
125 </head>
126 <body>
127 <h1>Error response</h1>
128 <p>Error code: %(code)d</p>
129 <p>Message: %(message)s.</p>
130 <p>Error code explanation: %(code)s - %(explain)s.</p>
131 </body>
132 </html>
133 """
134
135 DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
136
137 # Data larger than this will be read in chunks, to prevent extreme
138 # overallocation.
139 _MIN_READ_BUF_SIZE = 1 << 20
140
141 class HTTPServer(socketserver.TCPServer):
142
143 allow_reuse_address = True # Seems to make sense in testing environment
144 allow_reuse_port = False
145
146 def server_bind(self):
147 """Override server_bind to store the server name."""
148 socketserver.TCPServer.server_bind(self)
149 host, port = self.server_address[:2]
150 self.server_name = socket.getfqdn(host)
151 self.server_port = port
152
153
154 class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
155 daemon_threads = True
156
157
158 class HTTPSServer(HTTPServer):
159 def __init__(self, server_address, RequestHandlerClass,
160 bind_and_activate=True, *, certfile, keyfile=None,
161 password=None, alpn_protocols=None):
162 try:
163 import ssl
164 except ImportError:
165 raise RuntimeError("SSL module is missing; "
166 "HTTPS support is unavailable")
167
168 self.ssl = ssl
169 self.certfile = certfile
170 self.keyfile = keyfile
171 self.password = password
172 # Support by default HTTP/1.1
173 self.alpn_protocols = (
174 ["http/1.1"] if alpn_protocols is None else alpn_protocols
175 )
176
177 super().__init__(server_address,
178 RequestHandlerClass,
179 bind_and_activate)
180
181 def server_activate(self):
182 """Wrap the socket in SSLSocket."""
183 super().server_activate()
184 context = self._create_context()
185 self.socket = context.wrap_socket(self.socket, server_side=True)
186
187 def _create_context(self):
188 """Create a secure SSL context."""
189 context = self.ssl.create_default_context(self.ssl.Purpose.CLIENT_AUTH)
190 context.load_cert_chain(self.certfile, self.keyfile, self.password)
191 context.set_alpn_protocols(self.alpn_protocols)
192 return context
193
194
195 class ThreadingHTTPSServer(socketserver.ThreadingMixIn, HTTPSServer):
196 daemon_threads = True
197
198
199 class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
200
201 """HTTP request handler base class.
202
203 The following explanation of HTTP serves to guide you through the
204 code as well as to expose any misunderstandings I may have about
205 HTTP (so you don't need to read the code to figure out I'm wrong
206 :-).
207
208 HTTP (HyperText Transfer Protocol) is an extensible protocol on
209 top of a reliable stream transport (e.g. TCP/IP). The protocol
210 recognizes three parts to a request:
211
212 1. One line identifying the request type and path
213 2. An optional set of RFC-822-style headers
214 3. An optional data part
215
216 The headers and data are separated by a blank line.
217
218 The first line of the request has the form
219
220 <command> <path> <version>
221
222 where <command> is a (case-sensitive) keyword such as GET or POST,
223 <path> is a string containing path information for the request,
224 and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
225 <path> is encoded using the URL encoding scheme (using %xx to signify
226 the ASCII character with hex code xx).
227
228 The specification specifies that lines are separated by CRLF but
229 for compatibility with the widest range of clients recommends
230 servers also handle LF. Similarly, whitespace in the request line
231 is treated sensibly (allowing multiple spaces between components
232 and allowing trailing whitespace).
233
234 Similarly, for output, lines ought to be separated by CRLF pairs
235 but most clients grok LF characters just fine.
236
237 If the first line of the request has the form
238
239 <command> <path>
240
241 (i.e. <version> is left out) then this is assumed to be an HTTP
242 0.9 request; this form has no optional headers and data part and
243 the reply consists of just the data.
244
245 The reply form of the HTTP 1.x protocol again has three parts:
246
247 1. One line giving the response code
248 2. An optional set of RFC-822-style headers
249 3. The data
250
251 Again, the headers and data are separated by a blank line.
252
253 The response code line has the form
254
255 <version> <responsecode> <responsestring>
256
257 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
258 <responsecode> is a 3-digit response code indicating success or
259 failure of the request, and <responsestring> is an optional
260 human-readable string explaining what the response code means.
261
262 This server parses the request and the headers, and then calls a
263 function specific to the request type (<command>). Specifically,
264 a request SPAM will be handled by a method do_SPAM(). If no
265 such method exists the server sends an error response to the
266 client. If it exists, it is called with no arguments:
267
268 do_SPAM()
269
270 Note that the request name is case sensitive (i.e. SPAM and spam
271 are different requests).
272
273 The various request details are stored in instance variables:
274
275 - client_address is the client IP address in the form (host,
276 port);
277
278 - command, path and version are the broken-down request line;
279
280 - headers is an instance of email.message.Message (or a derived
281 class) containing the header information;
282
283 - rfile is a file object open for reading positioned at the
284 start of the optional input data part;
285
286 - wfile is a file object open for writing.
287
288 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
289
290 The first thing to be written must be the response line. Then
291 follow 0 or more header lines, then a blank line, and then the
292 actual data (if any). The meaning of the header lines depends on
293 the command executed by the server; in most cases, when data is
294 returned, there should be at least one header line of the form
295
296 Content-type: <type>/<subtype>
297
298 where <type> and <subtype> should be registered MIME types,
299 e.g. "text/html" or "text/plain".
300
301 """
302
303 # The Python system version, truncated to its first component.
304 sys_version = "Python/" + sys.version.split()[0]
305
306 # The server software version. You may want to override this.
307 # The format is multiple whitespace-separated strings,
308 # where each string is of the form name[/version].
309 server_version = "BaseHTTP/" + __version__
310
311 error_message_format = DEFAULT_ERROR_MESSAGE
312 error_content_type = DEFAULT_ERROR_CONTENT_TYPE
313
314 # The default request version. This only affects responses up until
315 # the point where the request line is parsed, so it mainly decides what
316 # the client gets back when sending a malformed request line.
317 # Most web servers default to HTTP 0.9, i.e. don't send a status line.
318 default_request_version = "HTTP/0.9"
319
320 def parse_request(self):
321 """Parse a request (internal).
322
323 The request should be stored in self.raw_requestline; the results
324 are in self.command, self.path, self.request_version and
325 self.headers.
326
327 Return True for success, False for failure; on failure, any relevant
328 error response has already been sent back.
329
330 """
331 is_http_0_9 = False
332 self.command = None # set in case of error on the first line
333 self.request_version = version = self.default_request_version
334 self.close_connection = True
335 requestline = str(self.raw_requestline, 'iso-8859-1')
336 requestline = requestline.rstrip('\r\n')
337 self.requestline = requestline
338 words = requestline.split()
339 if len(words) == 0:
340 return False
341
342 if len(words) >= 3: # Enough to determine protocol version
343 version = words[-1]
344 try:
345 if not version.startswith('HTTP/'):
346 raise ValueError
347 base_version_number = version.split('/', 1)[1]
348 version_number = base_version_number.split(".")
349 # RFC 2145 section 3.1 says there can be only one "." and
350 # - major and minor numbers MUST be treated as
351 # separate integers;
352 # - HTTP/2.4 is a lower version than HTTP/2.13, which in
353 # turn is lower than HTTP/12.3;
354 # - Leading zeros MUST be ignored by recipients.
355 if len(version_number) != 2:
356 raise ValueError
357 if any(not component.isdigit() for component in version_number):
358 raise ValueError("non digit in http version")
359 if any(len(component) > 10 for component in version_number):
360 raise ValueError("unreasonable length http version")
361 version_number = int(version_number[0]), int(version_number[1])
362 except (ValueError, IndexError):
363 # Send the error response with a status line and headers.
364 self.request_version = ''
365 self.send_error(
366 HTTPStatus.BAD_REQUEST,
367 "Bad request version (%r)" % version)
368 return False
369 self.request_version = version
370 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
371 self.close_connection = False
372 if version_number >= (2, 0):
373 self.send_error(
374 HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
375 "Invalid HTTP version (%s)" % base_version_number)
376 return False
377
378 if not 2 <= len(words) <= 3:
379 self.request_version = ''
380 self.send_error(
381 HTTPStatus.BAD_REQUEST,
382 "Bad request syntax (%r)" % requestline)
383 return False
384 command, path = words[:2]
385 if len(words) == 2:
386 self.close_connection = True
387 if command != 'GET':
388 self.request_version = ''
389 self.send_error(
390 HTTPStatus.BAD_REQUEST,
391 "Bad HTTP/0.9 request type (%r)" % command)
392 return False
393 is_http_0_9 = True
394 self.command, self.path = command, path
395
396 # gh-87389: The purpose of replacing '//' with '/' is to protect
397 # against open redirect attacks possibly triggered if the path starts
398 # with '//' because http clients treat //path as an absolute URI
399 # without scheme (similar to http://path) rather than a path.
400 if self.path.startswith('//'):
401 self.path = '/' + self.path.lstrip('/') # Reduce to a single /
402
403 # For HTTP/0.9, headers are not expected at all.
404 if is_http_0_9:
405 self.headers = {}
406 return True
407
408 # Examine the headers and look for a Connection directive.
409 try:
410 self.headers = http.client.parse_headers(self.rfile,
411 _class=self.MessageClass)
412 except http.client.LineTooLong as err:
413 self.send_error(
414 HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
415 "Line too long",
416 str(err))
417 return False
418 except http.client.HTTPException as err:
419 self.send_error(
420 HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
421 "Too many headers",
422 str(err)
423 )
424 return False
425
426 conntype = self.headers.get('Connection', "")
427 if conntype.lower() == 'close':
428 self.close_connection = True
429 elif (conntype.lower() == 'keep-alive' and
430 self.protocol_version >= "HTTP/1.1"):
431 self.close_connection = False
432 # Examine the headers and look for an Expect directive
433 expect = self.headers.get('Expect', "")
434 if (expect.lower() == "100-continue" and
435 self.protocol_version >= "HTTP/1.1" and
436 self.request_version >= "HTTP/1.1"):
437 if not self.handle_expect_100():
438 return False
439 return True
440
441 def handle_expect_100(self):
442 """Decide what to do with an "Expect: 100-continue" header.
443
444 If the client is expecting a 100 Continue response, we must
445 respond with either a 100 Continue or a final response before
446 waiting for the request body. The default is to always respond
447 with a 100 Continue. You can behave differently (for example,
448 reject unauthorized requests) by overriding this method.
449
450 This method should either return True (possibly after sending
451 a 100 Continue response) or send an error response and return
452 False.
453
454 """
455 self.send_response_only(HTTPStatus.CONTINUE)
456 self.end_headers()
457 return True
458
459 def handle_one_request(self):
460 """Handle a single HTTP request.
461
462 You normally don't need to override this method; see the class
463 __doc__ string for information on how to handle specific HTTP
464 commands such as GET and POST.
465
466 """
467 try:
468 self.raw_requestline = self.rfile.readline(65537)
469 if len(self.raw_requestline) > 65536:
470 self.requestline = ''
471 self.request_version = ''
472 self.command = ''
473 self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
474 return
475 if not self.raw_requestline:
476 self.close_connection = True
477 return
478 if not self.parse_request():
479 # An error code has been sent, just exit
480 return
481 mname = 'do_' + self.command
482 if not hasattr(self, mname):
483 self.send_error(
484 HTTPStatus.NOT_IMPLEMENTED,
485 "Unsupported method (%r)" % self.command)
486 return
487 method = getattr(self, mname)
488 method()
489 self.wfile.flush() #actually send the response if not already done.
490 except TimeoutError as e:
491 #a read or a write timed out. Discard this connection
492 self.log_error("Request timed out: %r", e)
493 self.close_connection = True
494 return
495
496 def handle(self):
497 """Handle multiple requests if necessary."""
498 self.close_connection = True
499
500 self.handle_one_request()
501 while not self.close_connection:
502 self.handle_one_request()
503
504 def send_error(self, code, message=None, explain=None):
505 """Send and log an error reply.
506
507 Arguments are
508 * code: an HTTP error code
509 3 digits
510 * message: a simple optional 1 line reason phrase.
511 *( HTAB / SP / VCHAR / %x80-FF )
512 defaults to short entry matching the response code
513 * explain: a detailed message defaults to the long entry
514 matching the response code.
515
516 This sends an error response (so it must be called before any
517 output has been generated), logs the error, and finally sends
518 a piece of HTML explaining the error to the user.
519
520 """
521
522 try:
523 shortmsg, longmsg = self.responses[code]
524 except KeyError:
525 shortmsg, longmsg = '???', '???'
526 if message is None:
527 message = shortmsg
528 if explain is None:
529 explain = longmsg
530 self.log_error("code %d, message %s", code, message)
531 self.send_response(code, message)
532 self.send_header('Connection', 'close')
533
534 # Message body is omitted for cases described in:
535 # - RFC7230: 3.3. 1xx, 204(No Content), 304(Not Modified)
536 # - RFC7231: 6.3.6. 205(Reset Content)
537 body = None
538 if (code >= 200 and
539 code not in (HTTPStatus.NO_CONTENT,
540 HTTPStatus.RESET_CONTENT,
541 HTTPStatus.NOT_MODIFIED)):
542 # HTML encode to prevent Cross Site Scripting attacks
543 # (see bug #1100201)
544 content = (self.error_message_format % {
545 'code': code,
546 'message': html.escape(message, quote=False),
547 'explain': html.escape(explain, quote=False)
548 })
549 body = content.encode('UTF-8', 'replace')
550 self.send_header("Content-Type", self.error_content_type)
551 self.send_header('Content-Length', str(len(body)))
552 self.end_headers()
553
554 if self.command != 'HEAD' and body:
555 self.wfile.write(body)
556
557 def send_response(self, code, message=None):
558 """Add the response header to the headers buffer and log the
559 response code.
560
561 Also send two standard headers with the server software
562 version and the current date.
563
564 """
565 self.log_request(code)
566 self.send_response_only(code, message)
567 self.send_header('Server', self.version_string())
568 self.send_header('Date', self.date_time_string())
569
570 def send_response_only(self, code, message=None):
571 """Send the response header only."""
572 if self.request_version != 'HTTP/0.9':
573 if message is None:
574 if code in self.responses:
575 message = self.responses[code][0]
576 else:
577 message = ''
578 if not hasattr(self, '_headers_buffer'):
579 self._headers_buffer = []
580 self._headers_buffer.append(("%s %d %s\r\n" %
581 (self.protocol_version, code, message)).encode(
582 'latin-1', 'strict'))
583
584 def send_header(self, keyword, value):
585 """Send a MIME header to the headers buffer."""
586 if self.request_version != 'HTTP/0.9':
587 if not hasattr(self, '_headers_buffer'):
588 self._headers_buffer = []
589 self._headers_buffer.append(
590 ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict'))
591
592 if keyword.lower() == 'connection':
593 if value.lower() == 'close':
594 self.close_connection = True
595 elif value.lower() == 'keep-alive':
596 self.close_connection = False
597
598 def end_headers(self):
599 """Send the blank line ending the MIME headers."""
600 if self.request_version != 'HTTP/0.9':
601 self._headers_buffer.append(b"\r\n")
602 self.flush_headers()
603
604 def flush_headers(self):
605 if hasattr(self, '_headers_buffer'):
606 self.wfile.write(b"".join(self._headers_buffer))
607 self._headers_buffer = []
608
609 def log_request(self, code='-', size='-'):
610 """Log an accepted request.
611
612 This is called by send_response().
613
614 """
615 if isinstance(code, HTTPStatus):
616 code = code.value
617 self.log_message('"%s" %s %s',
618 self.requestline, str(code), str(size))
619
620 def log_error(self, format, *args):
621 """Log an error.
622
623 This is called when a request cannot be fulfilled. By
624 default it passes the message on to log_message().
625
626 Arguments are the same as for log_message().
627
628 XXX This should go to the separate error log.
629
630 """
631
632 self.log_message(format, *args)
633
634 # https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes
635 _control_char_table = str.maketrans(
636 {c: fr'\x{c:02x}' for c in itertools.chain(range(0x20), range(0x7f,0xa0))})
637 _control_char_table[ord('\\')] = r'\\'
638
639 def log_message(self, format, *args):
640 """Log an arbitrary message.
641
642 This is used by all other logging functions. Override
643 it if you have specific logging wishes.
644
645 The first argument, FORMAT, is a format string for the
646 message to be logged. If the format string contains
647 any % escapes requiring parameters, they should be
648 specified as subsequent arguments (it's just like
649 printf!).
650
651 The client ip and current date/time are prefixed to
652 every message.
653
654 Unicode control characters are replaced with escaped hex
655 before writing the output to stderr.
656
657 """
658
659 message = format % args
660 sys.stderr.write("%s - - [%s] %s\n" %
661 (self.address_string(),
662 self.log_date_time_string(),
663 message.translate(self._control_char_table)))
664
665 def version_string(self):
666 """Return the server software version string."""
667 return self.server_version + ' ' + self.sys_version
668
669 def date_time_string(self, timestamp=None):
670 """Return the current date and time formatted for a message header."""
671 if timestamp is None:
672 timestamp = time.time()
673 return email.utils.formatdate(timestamp, usegmt=True)
674
675 def log_date_time_string(self):
676 """Return the current time formatted for logging."""
677 now = time.time()
678 year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
679 s = "%02d/%3s/%04d %02d:%02d:%02d" % (
680 day, self.monthname[month], year, hh, mm, ss)
681 return s
682
683 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
684
685 monthname = [None,
686 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
687 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
688
689 def address_string(self):
690 """Return the client address."""
691
692 return self.client_address[0]
693
694 # Essentially static class variables
695
696 # The version of the HTTP protocol we support.
697 # Set this to HTTP/1.1 to enable automatic keepalive
698 protocol_version = "HTTP/1.0"
699
700 # MessageClass used to parse headers
701 MessageClass = http.client.HTTPMessage
702
703 # hack to maintain backwards compatibility
704 responses = {
705 v: (v.phrase, v.description)
706 for v in HTTPStatus.__members__.values()
707 }
708
709
710 class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
711
712 """Simple HTTP request handler with GET and HEAD commands.
713
714 This serves files from the current directory and any of its
715 subdirectories. The MIME type for files is determined by
716 calling the .guess_type() method.
717
718 The GET and HEAD requests are identical except that the HEAD
719 request omits the actual contents of the file.
720
721 """
722
723 server_version = "SimpleHTTP/" + __version__
724 index_pages = ("index.html", "index.htm")
725 extensions_map = _encodings_map_default = {
726 '.gz': 'application/gzip',
727 '.Z': 'application/octet-stream',
728 '.bz2': 'application/x-bzip2',
729 '.xz': 'application/x-xz',
730 }
731
732 def __init__(self, *args, directory=None, **kwargs):
733 if directory is None:
734 directory = os.getcwd()
735 self.directory = os.fspath(directory)
736 super().__init__(*args, **kwargs)
737
738 def do_GET(self):
739 """Serve a GET request."""
740 f = self.send_head()
741 if f:
742 try:
743 self.copyfile(f, self.wfile)
744 finally:
745 f.close()
746
747 def do_HEAD(self):
748 """Serve a HEAD request."""
749 f = self.send_head()
750 if f:
751 f.close()
752
753 def send_head(self):
754 """Common code for GET and HEAD commands.
755
756 This sends the response code and MIME headers.
757
758 Return value is either a file object (which has to be copied
759 to the outputfile by the caller unless the command was HEAD,
760 and must be closed by the caller under all circumstances), or
761 None, in which case the caller has nothing further to do.
762
763 """
764 path = self.translate_path(self.path)
765 f = None
766 if os.path.isdir(path):
767 parts = urllib.parse.urlsplit(self.path)
768 if not parts.path.endswith(('/', '%2f', '%2F')):
769 # redirect browser - doing basically what apache does
770 self.send_response(HTTPStatus.MOVED_PERMANENTLY)
771 new_parts = (parts[0], parts[1], parts[2] + '/',
772 parts[3], parts[4])
773 new_url = urllib.parse.urlunsplit(new_parts)
774 self.send_header("Location", new_url)
775 self.send_header("Content-Length", "0")
776 self.end_headers()
777 return None
778 for index in self.index_pages:
779 index = os.path.join(path, index)
780 if os.path.isfile(index):
781 path = index
782 break
783 else:
784 return self.list_directory(path)
785 ctype = self.guess_type(path)
786 # check for trailing "/" which should return 404. See Issue17324
787 # The test for this was added in test_httpserver.py
788 # However, some OS platforms accept a trailingSlash as a filename
789 # See discussion on python-dev and Issue34711 regarding
790 # parsing and rejection of filenames with a trailing slash
791 if path.endswith("/"):
792 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
793 return None
794 try:
795 f = open(path, 'rb')
796 except OSError:
797 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
798 return None
799
800 try:
801 fs = os.fstat(f.fileno())
802 # Use browser cache if possible
803 if ("If-Modified-Since" in self.headers
804 and "If-None-Match" not in self.headers):
805 # compare If-Modified-Since and time of last file modification
806 try:
807 ims = email.utils.parsedate_to_datetime(
808 self.headers["If-Modified-Since"])
809 except (TypeError, IndexError, OverflowError, ValueError):
810 # ignore ill-formed values
811 pass
812 else:
813 if ims.tzinfo is None:
814 # obsolete format with no timezone, cf.
815 # https://tools.ietf.org/html/rfc7231#section-7.1.1.1
816 ims = ims.replace(tzinfo=datetime.timezone.utc)
817 if ims.tzinfo is datetime.timezone.utc:
818 # compare to UTC datetime of last modification
819 last_modif = datetime.datetime.fromtimestamp(
820 fs.st_mtime, datetime.timezone.utc)
821 # remove microseconds, like in If-Modified-Since
822 last_modif = last_modif.replace(microsecond=0)
823
824 if last_modif <= ims:
825 self.send_response(HTTPStatus.NOT_MODIFIED)
826 self.end_headers()
827 f.close()
828 return None
829
830 self.send_response(HTTPStatus.OK)
831 self.send_header("Content-type", ctype)
832 self.send_header("Content-Length", str(fs[6]))
833 self.send_header("Last-Modified",
834 self.date_time_string(fs.st_mtime))
835 self.end_headers()
836 return f
837 except:
838 f.close()
839 raise
840
841 def list_directory(self, path):
842 """Helper to produce a directory listing (absent index.html).
843
844 Return value is either a file object, or None (indicating an
845 error). In either case, the headers are sent, making the
846 interface the same as for send_head().
847
848 """
849 try:
850 list = os.listdir(path)
851 except OSError:
852 self.send_error(
853 HTTPStatus.NOT_FOUND,
854 "No permission to list directory")
855 return None
856 list.sort(key=lambda a: a.lower())
857 r = []
858 displaypath = self.path
859 displaypath = displaypath.split('#', 1)[0]
860 displaypath = displaypath.split('?', 1)[0]
861 try:
862 displaypath = urllib.parse.unquote(displaypath,
863 errors='surrogatepass')
864 except UnicodeDecodeError:
865 displaypath = urllib.parse.unquote(displaypath)
866 displaypath = html.escape(displaypath, quote=False)
867 enc = sys.getfilesystemencoding()
868 title = f'Directory listing for {displaypath}'
869 r.append('<!DOCTYPE HTML>')
870 r.append('<html lang="en">')
871 r.append('<head>')
872 r.append(f'<meta charset="{enc}">')
873 r.append('<style type="text/css">\n:root {\ncolor-scheme: light dark;\n}\n</style>')
874 r.append(f'<title>{title}</title>\n</head>')
875 r.append(f'<body>\n<h1>{title}</h1>')
876 r.append('<hr>\n<ul>')
877 for name in list:
878 fullname = os.path.join(path, name)
879 displayname = linkname = name
880 # Append / for directories or @ for symbolic links
881 if os.path.isdir(fullname):
882 displayname = name + "/"
883 linkname = name + "/"
884 if os.path.islink(fullname):
885 displayname = name + "@"
886 # Note: a link to a directory displays with @ and links with /
887 r.append('<li><a href="%s">%s</a></li>'
888 % (urllib.parse.quote(linkname,
889 errors='surrogatepass'),
890 html.escape(displayname, quote=False)))
891 r.append('</ul>\n<hr>\n</body>\n</html>\n')
892 encoded = '\n'.join(r).encode(enc, 'surrogateescape')
893 f = io.BytesIO()
894 f.write(encoded)
895 f.seek(0)
896 self.send_response(HTTPStatus.OK)
897 self.send_header("Content-type", "text/html; charset=%s" % enc)
898 self.send_header("Content-Length", str(len(encoded)))
899 self.end_headers()
900 return f
901
902 def translate_path(self, path):
903 """Translate a /-separated PATH to the local filename syntax.
904
905 Components that mean special things to the local file system
906 (e.g. drive or directory names) are ignored. (XXX They should
907 probably be diagnosed.)
908
909 """
910 # abandon query parameters
911 path = path.split('#', 1)[0]
912 path = path.split('?', 1)[0]
913 # Don't forget explicit trailing slash when normalizing. Issue17324
914 try:
915 path = urllib.parse.unquote(path, errors='surrogatepass')
916 except UnicodeDecodeError:
917 path = urllib.parse.unquote(path)
918 trailing_slash = path.endswith('/')
919 path = posixpath.normpath(path)
920 words = path.split('/')
921 words = filter(None, words)
922 path = self.directory
923 for word in words:
924 if os.path.dirname(word) or word in (os.curdir, os.pardir):
925 # Ignore components that are not a simple file/directory name
926 continue
927 path = os.path.join(path, word)
928 if trailing_slash:
929 path += '/'
930 return path
931
932 def copyfile(self, source, outputfile):
933 """Copy all data between two file objects.
934
935 The SOURCE argument is a file object open for reading
936 (or anything with a read() method) and the DESTINATION
937 argument is a file object open for writing (or
938 anything with a write() method).
939
940 The only reason for overriding this would be to change
941 the block size or perhaps to replace newlines by CRLF
942 -- note however that this the default server uses this
943 to copy binary data as well.
944
945 """
946 shutil.copyfileobj(source, outputfile)
947
948 def guess_type(self, path):
949 """Guess the type of a file.
950
951 Argument is a PATH (a filename).
952
953 Return value is a string of the form type/subtype,
954 usable for a MIME Content-type header.
955
956 The default implementation looks the file's extension
957 up in the table self.extensions_map, using application/octet-stream
958 as a default; however it would be permissible (if
959 slow) to look inside the data to make a better guess.
960
961 """
962 base, ext = posixpath.splitext(path)
963 if ext in self.extensions_map:
964 return self.extensions_map[ext]
965 ext = ext.lower()
966 if ext in self.extensions_map:
967 return self.extensions_map[ext]
968 guess, _ = mimetypes.guess_file_type(path)
969 if guess:
970 return guess
971 return 'application/octet-stream'
972
973
974 # Utilities for CGIHTTPRequestHandler
975
976 def _url_collapse_path(path):
977 """
978 Given a URL path, remove extra '/'s and '.' path elements and collapse
979 any '..' references and returns a collapsed path.
980
981 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
982 The utility of this function is limited to is_cgi method and helps
983 preventing some security attacks.
984
985 Returns: The reconstituted URL, which will always start with a '/'.
986
987 Raises: IndexError if too many '..' occur within the path.
988
989 """
990 # Query component should not be involved.
991 path, _, query = path.partition('?')
992 path = urllib.parse.unquote(path)
993
994 # Similar to os.path.split(os.path.normpath(path)) but specific to URL
995 # path semantics rather than local operating system semantics.
996 path_parts = path.split('/')
997 head_parts = []
998 for part in path_parts[:-1]:
999 if part == '..':
1000 head_parts.pop() # IndexError if more '..' than prior parts
1001 elif part and part != '.':
1002 head_parts.append( part )
1003 if path_parts:
1004 tail_part = path_parts.pop()
1005 if tail_part:
1006 if tail_part == '..':
1007 head_parts.pop()
1008 tail_part = ''
1009 elif tail_part == '.':
1010 tail_part = ''
1011 else:
1012 tail_part = ''
1013
1014 if query:
1015 tail_part = '?'.join((tail_part, query))
1016
1017 splitpath = ('/' + '/'.join(head_parts), tail_part)
1018 collapsed_path = "/".join(splitpath)
1019
1020 return collapsed_path
1021
1022
1023
1024 nobody = None
1025
1026 def nobody_uid():
1027 """Internal routine to get nobody's uid"""
1028 global nobody
1029 if nobody:
1030 return nobody
1031 try:
1032 import pwd
1033 except ImportError:
1034 return -1
1035 try:
1036 nobody = pwd.getpwnam('nobody')[2]
1037 except KeyError:
1038 nobody = 1 + max(x[2] for x in pwd.getpwall())
1039 return nobody
1040
1041
1042 def executable(path):
1043 """Test for executable file."""
1044 return os.access(path, os.X_OK)
1045
1046
1047 class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
1048
1049 """Complete HTTP server with GET, HEAD and POST commands.
1050
1051 GET and HEAD also support running CGI scripts.
1052
1053 The POST command is *only* implemented for CGI scripts.
1054
1055 """
1056
1057 def __init__(self, *args, **kwargs):
1058 import warnings
1059 warnings._deprecated("http.server.CGIHTTPRequestHandler",
1060 remove=(3, 15))
1061 super().__init__(*args, **kwargs)
1062
1063 # Determine platform specifics
1064 have_fork = hasattr(os, 'fork')
1065
1066 # Make rfile unbuffered -- we need to read one line and then pass
1067 # the rest to a subprocess, so we can't use buffered input.
1068 rbufsize = 0
1069
1070 def do_POST(self):
1071 """Serve a POST request.
1072
1073 This is only implemented for CGI scripts.
1074
1075 """
1076
1077 if self.is_cgi():
1078 self.run_cgi()
1079 else:
1080 self.send_error(
1081 HTTPStatus.NOT_IMPLEMENTED,
1082 "Can only POST to CGI scripts")
1083
1084 def send_head(self):
1085 """Version of send_head that support CGI scripts"""
1086 if self.is_cgi():
1087 return self.run_cgi()
1088 else:
1089 return SimpleHTTPRequestHandler.send_head(self)
1090
1091 def is_cgi(self):
1092 """Test whether self.path corresponds to a CGI script.
1093
1094 Returns True and updates the cgi_info attribute to the tuple
1095 (dir, rest) if self.path requires running a CGI script.
1096 Returns False otherwise.
1097
1098 If any exception is raised, the caller should assume that
1099 self.path was rejected as invalid and act accordingly.
1100
1101 The default implementation tests whether the normalized url
1102 path begins with one of the strings in self.cgi_directories
1103 (and the next character is a '/' or the end of the string).
1104
1105 """
1106 collapsed_path = _url_collapse_path(self.path)
1107 dir_sep = collapsed_path.find('/', 1)
1108 while dir_sep > 0 and not collapsed_path[:dir_sep] in self.cgi_directories:
1109 dir_sep = collapsed_path.find('/', dir_sep+1)
1110 if dir_sep > 0:
1111 head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
1112 self.cgi_info = head, tail
1113 return True
1114 return False
1115
1116
1117 cgi_directories = ['/cgi-bin', '/htbin']
1118
1119 def is_executable(self, path):
1120 """Test whether argument path is an executable file."""
1121 return executable(path)
1122
1123 def is_python(self, path):
1124 """Test whether argument path is a Python script."""
1125 head, tail = os.path.splitext(path)
1126 return tail.lower() in (".py", ".pyw")
1127
1128 def run_cgi(self):
1129 """Execute a CGI script."""
1130 dir, rest = self.cgi_info
1131 path = dir + '/' + rest
1132 i = path.find('/', len(dir)+1)
1133 while i >= 0:
1134 nextdir = path[:i]
1135 nextrest = path[i+1:]
1136
1137 scriptdir = self.translate_path(nextdir)
1138 if os.path.isdir(scriptdir):
1139 dir, rest = nextdir, nextrest
1140 i = path.find('/', len(dir)+1)
1141 else:
1142 break
1143
1144 # find an explicit query string, if present.
1145 rest, _, query = rest.partition('?')
1146
1147 # dissect the part after the directory name into a script name &
1148 # a possible additional path, to be stored in PATH_INFO.
1149 i = rest.find('/')
1150 if i >= 0:
1151 script, rest = rest[:i], rest[i:]
1152 else:
1153 script, rest = rest, ''
1154
1155 scriptname = dir + '/' + script
1156 scriptfile = self.translate_path(scriptname)
1157 if not os.path.exists(scriptfile):
1158 self.send_error(
1159 HTTPStatus.NOT_FOUND,
1160 "No such CGI script (%r)" % scriptname)
1161 return
1162 if not os.path.isfile(scriptfile):
1163 self.send_error(
1164 HTTPStatus.FORBIDDEN,
1165 "CGI script is not a plain file (%r)" % scriptname)
1166 return
1167 ispy = self.is_python(scriptname)
1168 if self.have_fork or not ispy:
1169 if not self.is_executable(scriptfile):
1170 self.send_error(
1171 HTTPStatus.FORBIDDEN,
1172 "CGI script is not executable (%r)" % scriptname)
1173 return
1174
1175 # Reference: https://www6.uniovi.es/~antonio/ncsa_httpd/cgi/env.html
1176 # XXX Much of the following could be prepared ahead of time!
1177 env = copy.deepcopy(os.environ)
1178 env['SERVER_SOFTWARE'] = self.version_string()
1179 env['SERVER_NAME'] = self.server.server_name
1180 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
1181 env['SERVER_PROTOCOL'] = self.protocol_version
1182 env['SERVER_PORT'] = str(self.server.server_port)
1183 env['REQUEST_METHOD'] = self.command
1184 uqrest = urllib.parse.unquote(rest)
1185 env['PATH_INFO'] = uqrest
1186 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
1187 env['SCRIPT_NAME'] = scriptname
1188 env['QUERY_STRING'] = query
1189 env['REMOTE_ADDR'] = self.client_address[0]
1190 authorization = self.headers.get("authorization")
1191 if authorization:
1192 authorization = authorization.split()
1193 if len(authorization) == 2:
1194 import base64, binascii
1195 env['AUTH_TYPE'] = authorization[0]
1196 if authorization[0].lower() == "basic":
1197 try:
1198 authorization = authorization[1].encode('ascii')
1199 authorization = base64.decodebytes(authorization).\
1200 decode('ascii')
1201 except (binascii.Error, UnicodeError):
1202 pass
1203 else:
1204 authorization = authorization.split(':')
1205 if len(authorization) == 2:
1206 env['REMOTE_USER'] = authorization[0]
1207 # XXX REMOTE_IDENT
1208 if self.headers.get('content-type') is None:
1209 env['CONTENT_TYPE'] = self.headers.get_content_type()
1210 else:
1211 env['CONTENT_TYPE'] = self.headers['content-type']
1212 length = self.headers.get('content-length')
1213 if length:
1214 env['CONTENT_LENGTH'] = length
1215 referer = self.headers.get('referer')
1216 if referer:
1217 env['HTTP_REFERER'] = referer
1218 accept = self.headers.get_all('accept', ())
1219 env['HTTP_ACCEPT'] = ','.join(accept)
1220 ua = self.headers.get('user-agent')
1221 if ua:
1222 env['HTTP_USER_AGENT'] = ua
1223 co = filter(None, self.headers.get_all('cookie', []))
1224 cookie_str = ', '.join(co)
1225 if cookie_str:
1226 env['HTTP_COOKIE'] = cookie_str
1227 # XXX Other HTTP_* headers
1228 # Since we're setting the env in the parent, provide empty
1229 # values to override previously set values
1230 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
1231 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
1232 env.setdefault(k, "")
1233
1234 self.send_response(HTTPStatus.OK, "Script output follows")
1235 self.flush_headers()
1236
1237 decoded_query = query.replace('+', ' ')
1238
1239 if self.have_fork:
1240 # Unix -- fork as we should
1241 args = [script]
1242 if '=' not in decoded_query:
1243 args.append(decoded_query)
1244 nobody = nobody_uid()
1245 self.wfile.flush() # Always flush before forking
1246 pid = os.fork()
1247 if pid != 0:
1248 # Parent
1249 pid, sts = os.waitpid(pid, 0)
1250 # throw away additional data [see bug #427345]
1251 while select.select([self.rfile], [], [], 0)[0]:
1252 if not self.rfile.read(1):
1253 break
1254 exitcode = os.waitstatus_to_exitcode(sts)
1255 if exitcode:
1256 self.log_error(f"CGI script exit code {exitcode}")
1257 return
1258 # Child
1259 try:
1260 try:
1261 os.setuid(nobody)
1262 except OSError:
1263 pass
1264 os.dup2(self.rfile.fileno(), 0)
1265 os.dup2(self.wfile.fileno(), 1)
1266 os.execve(scriptfile, args, env)
1267 except:
1268 self.server.handle_error(self.request, self.client_address)
1269 os._exit(127)
1270
1271 else:
1272 # Non-Unix -- use subprocess
1273 import subprocess
1274 cmdline = [scriptfile]
1275 if self.is_python(scriptfile):
1276 interp = sys.executable
1277 if interp.lower().endswith("w.exe"):
1278 # On Windows, use python.exe, not pythonw.exe
1279 interp = interp[:-5] + interp[-4:]
1280 cmdline = [interp, '-u'] + cmdline
1281 if '=' not in query:
1282 cmdline.append(query)
1283 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
1284 try:
1285 nbytes = int(length)
1286 except (TypeError, ValueError):
1287 nbytes = 0
1288 p = subprocess.Popen(cmdline,
1289 stdin=subprocess.PIPE,
1290 stdout=subprocess.PIPE,
1291 stderr=subprocess.PIPE,
1292 env = env
1293 )
1294 if self.command.lower() == "post" and nbytes > 0:
1295 cursize = 0
1296 data = self.rfile.read(min(nbytes, _MIN_READ_BUF_SIZE))
1297 while len(data) < nbytes and len(data) != cursize:
1298 cursize = len(data)
1299 # This is a geometric increase in read size (never more
1300 # than doubling out the current length of data per loop
1301 # iteration).
1302 delta = min(cursize, nbytes - cursize)
1303 try:
1304 data += self.rfile.read(delta)
1305 except TimeoutError:
1306 break
1307 else:
1308 data = None
1309 # throw away additional data [see bug #427345]
1310 while select.select([self.rfile._sock], [], [], 0)[0]:
1311 if not self.rfile._sock.recv(1):
1312 break
1313 stdout, stderr = p.communicate(data)
1314 self.wfile.write(stdout)
1315 if stderr:
1316 self.log_error('%s', stderr)
1317 p.stderr.close()
1318 p.stdout.close()
1319 status = p.returncode
1320 if status:
1321 self.log_error("CGI script exit status %#x", status)
1322 else:
1323 self.log_message("CGI script exited OK")
1324
1325
1326 def _get_best_family(*address):
1327 infos = socket.getaddrinfo(
1328 *address,
1329 type=socket.SOCK_STREAM,
1330 flags=socket.AI_PASSIVE,
1331 )
1332 family, type, proto, canonname, sockaddr = next(iter(infos))
1333 return family, sockaddr
1334
1335
1336 def test(HandlerClass=BaseHTTPRequestHandler,
1337 ServerClass=ThreadingHTTPServer,
1338 protocol="HTTP/1.0", port=8000, bind=None,
1339 tls_cert=None, tls_key=None, tls_password=None):
1340 """Test the HTTP request handler class.
1341
1342 This runs an HTTP server on port 8000 (or the port argument).
1343
1344 """
1345 ServerClass.address_family, addr = _get_best_family(bind, port)
1346 HandlerClass.protocol_version = protocol
1347
1348 if tls_cert:
1349 server = ServerClass(addr, HandlerClass, certfile=tls_cert,
1350 keyfile=tls_key, password=tls_password)
1351 else:
1352 server = ServerClass(addr, HandlerClass)
1353
1354 with server as httpd:
1355 host, port = httpd.socket.getsockname()[:2]
1356 url_host = f'[{host}]' if ':' in host else host
1357 protocol = 'HTTPS' if tls_cert else 'HTTP'
1358 print(
1359 f"Serving {protocol} on {host} port {port} "
1360 f"({protocol.lower()}://{url_host}:{port}/) ..."
1361 )
1362 try:
1363 httpd.serve_forever()
1364 except KeyboardInterrupt:
1365 print("\nKeyboard interrupt received, exiting.")
1366 sys.exit(0)
1367
1368 if __name__ == '__main__':
1369 import argparse
1370 import contextlib
1371
1372 parser = argparse.ArgumentParser(color=True)
1373 parser.add_argument('--cgi', action='store_true',
1374 help='run as CGI server')
1375 parser.add_argument('-b', '--bind', metavar='ADDRESS',
1376 help='bind to this address '
1377 '(default: all interfaces)')
1378 parser.add_argument('-d', '--directory', default=os.getcwd(),
1379 help='serve this directory '
1380 '(default: current directory)')
1381 parser.add_argument('-p', '--protocol', metavar='VERSION',
1382 default='HTTP/1.0',
1383 help='conform to this HTTP version '
1384 '(default: %(default)s)')
1385 parser.add_argument('--tls-cert', metavar='PATH',
1386 help='path to the TLS certificate chain file')
1387 parser.add_argument('--tls-key', metavar='PATH',
1388 help='path to the TLS key file')
1389 parser.add_argument('--tls-password-file', metavar='PATH',
1390 help='path to the password file for the TLS key')
1391 parser.add_argument('port', default=8000, type=int, nargs='?',
1392 help='bind to this port '
1393 '(default: %(default)s)')
1394 args = parser.parse_args()
1395
1396 if not args.tls_cert and args.tls_key:
1397 parser.error("--tls-key requires --tls-cert to be set")
1398
1399 tls_key_password = None
1400 if args.tls_password_file:
1401 if not args.tls_cert:
1402 parser.error("--tls-password-file requires --tls-cert to be set")
1403
1404 try:
1405 with open(args.tls_password_file, "r", encoding="utf-8") as f:
1406 tls_key_password = f.read().strip()
1407 except OSError as e:
1408 parser.error(f"Failed to read TLS password file: {e}")
1409
1410 if args.cgi:
1411 handler_class = CGIHTTPRequestHandler
1412 else:
1413 handler_class = SimpleHTTPRequestHandler
1414
1415 # ensure dual-stack is not disabled; ref #38907
1416 class DualStackServerMixin:
1417
1418 def server_bind(self):
1419 # suppress exception when protocol is IPv4
1420 with contextlib.suppress(Exception):
1421 self.socket.setsockopt(
1422 socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
1423 return super().server_bind()
1424
1425 def finish_request(self, request, client_address):
1426 self.RequestHandlerClass(request, client_address, self,
1427 directory=args.directory)
1428
1429 class HTTPDualStackServer(DualStackServerMixin, ThreadingHTTPServer):
1430 pass
1431 class HTTPSDualStackServer(DualStackServerMixin, ThreadingHTTPSServer):
1432 pass
1433
1434 ServerClass = HTTPSDualStackServer if args.tls_cert else HTTPDualStackServer
1435
1436 test(
1437 HandlerClass=handler_class,
1438 ServerClass=ServerClass,
1439 port=args.port,
1440 bind=args.bind,
1441 protocol=args.protocol,
1442 tls_cert=args.tls_cert,
1443 tls_key=args.tls_key,
1444 tls_password=tls_key_password,
1445 )