annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/http/server.py @ 68:5028fdace37b

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