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