Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../http
File: server.py
"""HTTP server classes.
[0] Fix | Delete
[1] Fix | Delete
Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see
[2] Fix | Delete
SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST,
[3] Fix | Delete
and CGIHTTPRequestHandler for CGI scripts.
[4] Fix | Delete
[5] Fix | Delete
It does, however, optionally implement HTTP/1.1 persistent connections,
[6] Fix | Delete
as of version 0.3.
[7] Fix | Delete
[8] Fix | Delete
Notes on CGIHTTPRequestHandler
[9] Fix | Delete
------------------------------
[10] Fix | Delete
[11] Fix | Delete
This class implements GET and POST requests to cgi-bin scripts.
[12] Fix | Delete
[13] Fix | Delete
If the os.fork() function is not present (e.g. on Windows),
[14] Fix | Delete
subprocess.Popen() is used as a fallback, with slightly altered semantics.
[15] Fix | Delete
[16] Fix | Delete
In all cases, the implementation is intentionally naive -- all
[17] Fix | Delete
requests are executed synchronously.
[18] Fix | Delete
[19] Fix | Delete
SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
[20] Fix | Delete
-- it may execute arbitrary Python code or external programs.
[21] Fix | Delete
[22] Fix | Delete
Note that status code 200 is sent prior to execution of a CGI script, so
[23] Fix | Delete
scripts cannot send other status codes such as 302 (redirect).
[24] Fix | Delete
[25] Fix | Delete
XXX To do:
[26] Fix | Delete
[27] Fix | Delete
- log requests even later (to capture byte count)
[28] Fix | Delete
- log user-agent header and other interesting goodies
[29] Fix | Delete
- send error log to separate file
[30] Fix | Delete
"""
[31] Fix | Delete
[32] Fix | Delete
[33] Fix | Delete
# See also:
[34] Fix | Delete
#
[35] Fix | Delete
# HTTP Working Group T. Berners-Lee
[36] Fix | Delete
# INTERNET-DRAFT R. T. Fielding
[37] Fix | Delete
# <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
[38] Fix | Delete
# Expires September 8, 1995 March 8, 1995
[39] Fix | Delete
#
[40] Fix | Delete
# URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
[41] Fix | Delete
#
[42] Fix | Delete
# and
[43] Fix | Delete
#
[44] Fix | Delete
# Network Working Group R. Fielding
[45] Fix | Delete
# Request for Comments: 2616 et al
[46] Fix | Delete
# Obsoletes: 2068 June 1999
[47] Fix | Delete
# Category: Standards Track
[48] Fix | Delete
#
[49] Fix | Delete
# URL: http://www.faqs.org/rfcs/rfc2616.html
[50] Fix | Delete
[51] Fix | Delete
# Log files
[52] Fix | Delete
# ---------
[53] Fix | Delete
#
[54] Fix | Delete
# Here's a quote from the NCSA httpd docs about log file format.
[55] Fix | Delete
#
[56] Fix | Delete
# | The logfile format is as follows. Each line consists of:
[57] Fix | Delete
# |
[58] Fix | Delete
# | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
[59] Fix | Delete
# |
[60] Fix | Delete
# | host: Either the DNS name or the IP number of the remote client
[61] Fix | Delete
# | rfc931: Any information returned by identd for this person,
[62] Fix | Delete
# | - otherwise.
[63] Fix | Delete
# | authuser: If user sent a userid for authentication, the user name,
[64] Fix | Delete
# | - otherwise.
[65] Fix | Delete
# | DD: Day
[66] Fix | Delete
# | Mon: Month (calendar name)
[67] Fix | Delete
# | YYYY: Year
[68] Fix | Delete
# | hh: hour (24-hour format, the machine's timezone)
[69] Fix | Delete
# | mm: minutes
[70] Fix | Delete
# | ss: seconds
[71] Fix | Delete
# | request: The first line of the HTTP request as sent by the client.
[72] Fix | Delete
# | ddd: the status code returned by the server, - if not available.
[73] Fix | Delete
# | bbbb: the total number of bytes sent,
[74] Fix | Delete
# | *not including the HTTP/1.0 header*, - if not available
[75] Fix | Delete
# |
[76] Fix | Delete
# | You can determine the name of the file accessed through request.
[77] Fix | Delete
#
[78] Fix | Delete
# (Actually, the latter is only true if you know the server configuration
[79] Fix | Delete
# at the time the request was made!)
[80] Fix | Delete
[81] Fix | Delete
__version__ = "0.6"
[82] Fix | Delete
[83] Fix | Delete
__all__ = [
[84] Fix | Delete
"HTTPServer", "ThreadingHTTPServer", "BaseHTTPRequestHandler",
[85] Fix | Delete
"SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
[86] Fix | Delete
]
[87] Fix | Delete
[88] Fix | Delete
import copy
[89] Fix | Delete
import datetime
[90] Fix | Delete
import email.utils
[91] Fix | Delete
import html
[92] Fix | Delete
import http.client
[93] Fix | Delete
import io
[94] Fix | Delete
import itertools
[95] Fix | Delete
import mimetypes
[96] Fix | Delete
import os
[97] Fix | Delete
import posixpath
[98] Fix | Delete
import select
[99] Fix | Delete
import shutil
[100] Fix | Delete
import socket # For gethostbyaddr()
[101] Fix | Delete
import socketserver
[102] Fix | Delete
import sys
[103] Fix | Delete
import time
[104] Fix | Delete
import urllib.parse
[105] Fix | Delete
import contextlib
[106] Fix | Delete
from functools import partial
[107] Fix | Delete
[108] Fix | Delete
from http import HTTPStatus
[109] Fix | Delete
[110] Fix | Delete
[111] Fix | Delete
# Default error message template
[112] Fix | Delete
DEFAULT_ERROR_MESSAGE = """\
[113] Fix | Delete
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
[114] Fix | Delete
"http://www.w3.org/TR/html4/strict.dtd">
[115] Fix | Delete
<html>
[116] Fix | Delete
<head>
[117] Fix | Delete
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
[118] Fix | Delete
<title>Error response</title>
[119] Fix | Delete
</head>
[120] Fix | Delete
<body>
[121] Fix | Delete
<h1>Error response</h1>
[122] Fix | Delete
<p>Error code: %(code)d</p>
[123] Fix | Delete
<p>Message: %(message)s.</p>
[124] Fix | Delete
<p>Error code explanation: %(code)s - %(explain)s.</p>
[125] Fix | Delete
</body>
[126] Fix | Delete
</html>
[127] Fix | Delete
"""
[128] Fix | Delete
[129] Fix | Delete
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
[130] Fix | Delete
[131] Fix | Delete
class HTTPServer(socketserver.TCPServer):
[132] Fix | Delete
[133] Fix | Delete
allow_reuse_address = 1 # Seems to make sense in testing environment
[134] Fix | Delete
[135] Fix | Delete
def server_bind(self):
[136] Fix | Delete
"""Override server_bind to store the server name."""
[137] Fix | Delete
socketserver.TCPServer.server_bind(self)
[138] Fix | Delete
host, port = self.server_address[:2]
[139] Fix | Delete
self.server_name = socket.getfqdn(host)
[140] Fix | Delete
self.server_port = port
[141] Fix | Delete
[142] Fix | Delete
[143] Fix | Delete
class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
[144] Fix | Delete
daemon_threads = True
[145] Fix | Delete
[146] Fix | Delete
[147] Fix | Delete
class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
[148] Fix | Delete
[149] Fix | Delete
"""HTTP request handler base class.
[150] Fix | Delete
[151] Fix | Delete
The following explanation of HTTP serves to guide you through the
[152] Fix | Delete
code as well as to expose any misunderstandings I may have about
[153] Fix | Delete
HTTP (so you don't need to read the code to figure out I'm wrong
[154] Fix | Delete
:-).
[155] Fix | Delete
[156] Fix | Delete
HTTP (HyperText Transfer Protocol) is an extensible protocol on
[157] Fix | Delete
top of a reliable stream transport (e.g. TCP/IP). The protocol
[158] Fix | Delete
recognizes three parts to a request:
[159] Fix | Delete
[160] Fix | Delete
1. One line identifying the request type and path
[161] Fix | Delete
2. An optional set of RFC-822-style headers
[162] Fix | Delete
3. An optional data part
[163] Fix | Delete
[164] Fix | Delete
The headers and data are separated by a blank line.
[165] Fix | Delete
[166] Fix | Delete
The first line of the request has the form
[167] Fix | Delete
[168] Fix | Delete
<command> <path> <version>
[169] Fix | Delete
[170] Fix | Delete
where <command> is a (case-sensitive) keyword such as GET or POST,
[171] Fix | Delete
<path> is a string containing path information for the request,
[172] Fix | Delete
and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
[173] Fix | Delete
<path> is encoded using the URL encoding scheme (using %xx to signify
[174] Fix | Delete
the ASCII character with hex code xx).
[175] Fix | Delete
[176] Fix | Delete
The specification specifies that lines are separated by CRLF but
[177] Fix | Delete
for compatibility with the widest range of clients recommends
[178] Fix | Delete
servers also handle LF. Similarly, whitespace in the request line
[179] Fix | Delete
is treated sensibly (allowing multiple spaces between components
[180] Fix | Delete
and allowing trailing whitespace).
[181] Fix | Delete
[182] Fix | Delete
Similarly, for output, lines ought to be separated by CRLF pairs
[183] Fix | Delete
but most clients grok LF characters just fine.
[184] Fix | Delete
[185] Fix | Delete
If the first line of the request has the form
[186] Fix | Delete
[187] Fix | Delete
<command> <path>
[188] Fix | Delete
[189] Fix | Delete
(i.e. <version> is left out) then this is assumed to be an HTTP
[190] Fix | Delete
0.9 request; this form has no optional headers and data part and
[191] Fix | Delete
the reply consists of just the data.
[192] Fix | Delete
[193] Fix | Delete
The reply form of the HTTP 1.x protocol again has three parts:
[194] Fix | Delete
[195] Fix | Delete
1. One line giving the response code
[196] Fix | Delete
2. An optional set of RFC-822-style headers
[197] Fix | Delete
3. The data
[198] Fix | Delete
[199] Fix | Delete
Again, the headers and data are separated by a blank line.
[200] Fix | Delete
[201] Fix | Delete
The response code line has the form
[202] Fix | Delete
[203] Fix | Delete
<version> <responsecode> <responsestring>
[204] Fix | Delete
[205] Fix | Delete
where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
[206] Fix | Delete
<responsecode> is a 3-digit response code indicating success or
[207] Fix | Delete
failure of the request, and <responsestring> is an optional
[208] Fix | Delete
human-readable string explaining what the response code means.
[209] Fix | Delete
[210] Fix | Delete
This server parses the request and the headers, and then calls a
[211] Fix | Delete
function specific to the request type (<command>). Specifically,
[212] Fix | Delete
a request SPAM will be handled by a method do_SPAM(). If no
[213] Fix | Delete
such method exists the server sends an error response to the
[214] Fix | Delete
client. If it exists, it is called with no arguments:
[215] Fix | Delete
[216] Fix | Delete
do_SPAM()
[217] Fix | Delete
[218] Fix | Delete
Note that the request name is case sensitive (i.e. SPAM and spam
[219] Fix | Delete
are different requests).
[220] Fix | Delete
[221] Fix | Delete
The various request details are stored in instance variables:
[222] Fix | Delete
[223] Fix | Delete
- client_address is the client IP address in the form (host,
[224] Fix | Delete
port);
[225] Fix | Delete
[226] Fix | Delete
- command, path and version are the broken-down request line;
[227] Fix | Delete
[228] Fix | Delete
- headers is an instance of email.message.Message (or a derived
[229] Fix | Delete
class) containing the header information;
[230] Fix | Delete
[231] Fix | Delete
- rfile is a file object open for reading positioned at the
[232] Fix | Delete
start of the optional input data part;
[233] Fix | Delete
[234] Fix | Delete
- wfile is a file object open for writing.
[235] Fix | Delete
[236] Fix | Delete
IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
[237] Fix | Delete
[238] Fix | Delete
The first thing to be written must be the response line. Then
[239] Fix | Delete
follow 0 or more header lines, then a blank line, and then the
[240] Fix | Delete
actual data (if any). The meaning of the header lines depends on
[241] Fix | Delete
the command executed by the server; in most cases, when data is
[242] Fix | Delete
returned, there should be at least one header line of the form
[243] Fix | Delete
[244] Fix | Delete
Content-type: <type>/<subtype>
[245] Fix | Delete
[246] Fix | Delete
where <type> and <subtype> should be registered MIME types,
[247] Fix | Delete
e.g. "text/html" or "text/plain".
[248] Fix | Delete
[249] Fix | Delete
"""
[250] Fix | Delete
[251] Fix | Delete
# The Python system version, truncated to its first component.
[252] Fix | Delete
sys_version = "Python/" + sys.version.split()[0]
[253] Fix | Delete
[254] Fix | Delete
# The server software version. You may want to override this.
[255] Fix | Delete
# The format is multiple whitespace-separated strings,
[256] Fix | Delete
# where each string is of the form name[/version].
[257] Fix | Delete
server_version = "BaseHTTP/" + __version__
[258] Fix | Delete
[259] Fix | Delete
error_message_format = DEFAULT_ERROR_MESSAGE
[260] Fix | Delete
error_content_type = DEFAULT_ERROR_CONTENT_TYPE
[261] Fix | Delete
[262] Fix | Delete
# The default request version. This only affects responses up until
[263] Fix | Delete
# the point where the request line is parsed, so it mainly decides what
[264] Fix | Delete
# the client gets back when sending a malformed request line.
[265] Fix | Delete
# Most web servers default to HTTP 0.9, i.e. don't send a status line.
[266] Fix | Delete
default_request_version = "HTTP/0.9"
[267] Fix | Delete
[268] Fix | Delete
def parse_request(self):
[269] Fix | Delete
"""Parse a request (internal).
[270] Fix | Delete
[271] Fix | Delete
The request should be stored in self.raw_requestline; the results
[272] Fix | Delete
are in self.command, self.path, self.request_version and
[273] Fix | Delete
self.headers.
[274] Fix | Delete
[275] Fix | Delete
Return True for success, False for failure; on failure, any relevant
[276] Fix | Delete
error response has already been sent back.
[277] Fix | Delete
[278] Fix | Delete
"""
[279] Fix | Delete
self.command = None # set in case of error on the first line
[280] Fix | Delete
self.request_version = version = self.default_request_version
[281] Fix | Delete
self.close_connection = True
[282] Fix | Delete
requestline = str(self.raw_requestline, 'iso-8859-1')
[283] Fix | Delete
requestline = requestline.rstrip('\r\n')
[284] Fix | Delete
self.requestline = requestline
[285] Fix | Delete
words = requestline.split()
[286] Fix | Delete
if len(words) == 0:
[287] Fix | Delete
return False
[288] Fix | Delete
[289] Fix | Delete
if len(words) >= 3: # Enough to determine protocol version
[290] Fix | Delete
version = words[-1]
[291] Fix | Delete
try:
[292] Fix | Delete
if not version.startswith('HTTP/'):
[293] Fix | Delete
raise ValueError
[294] Fix | Delete
base_version_number = version.split('/', 1)[1]
[295] Fix | Delete
version_number = base_version_number.split(".")
[296] Fix | Delete
# RFC 2145 section 3.1 says there can be only one "." and
[297] Fix | Delete
# - major and minor numbers MUST be treated as
[298] Fix | Delete
# separate integers;
[299] Fix | Delete
# - HTTP/2.4 is a lower version than HTTP/2.13, which in
[300] Fix | Delete
# turn is lower than HTTP/12.3;
[301] Fix | Delete
# - Leading zeros MUST be ignored by recipients.
[302] Fix | Delete
if len(version_number) != 2:
[303] Fix | Delete
raise ValueError
[304] Fix | Delete
version_number = int(version_number[0]), int(version_number[1])
[305] Fix | Delete
except (ValueError, IndexError):
[306] Fix | Delete
self.send_error(
[307] Fix | Delete
HTTPStatus.BAD_REQUEST,
[308] Fix | Delete
"Bad request version (%r)" % version)
[309] Fix | Delete
return False
[310] Fix | Delete
if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
[311] Fix | Delete
self.close_connection = False
[312] Fix | Delete
if version_number >= (2, 0):
[313] Fix | Delete
self.send_error(
[314] Fix | Delete
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
[315] Fix | Delete
"Invalid HTTP version (%s)" % base_version_number)
[316] Fix | Delete
return False
[317] Fix | Delete
self.request_version = version
[318] Fix | Delete
[319] Fix | Delete
if not 2 <= len(words) <= 3:
[320] Fix | Delete
self.send_error(
[321] Fix | Delete
HTTPStatus.BAD_REQUEST,
[322] Fix | Delete
"Bad request syntax (%r)" % requestline)
[323] Fix | Delete
return False
[324] Fix | Delete
command, path = words[:2]
[325] Fix | Delete
if len(words) == 2:
[326] Fix | Delete
self.close_connection = True
[327] Fix | Delete
if command != 'GET':
[328] Fix | Delete
self.send_error(
[329] Fix | Delete
HTTPStatus.BAD_REQUEST,
[330] Fix | Delete
"Bad HTTP/0.9 request type (%r)" % command)
[331] Fix | Delete
return False
[332] Fix | Delete
self.command, self.path = command, path
[333] Fix | Delete
[334] Fix | Delete
# gh-87389: The purpose of replacing '//' with '/' is to protect
[335] Fix | Delete
# against open redirect attacks possibly triggered if the path starts
[336] Fix | Delete
# with '//' because http clients treat //path as an absolute URI
[337] Fix | Delete
# without scheme (similar to http://path) rather than a path.
[338] Fix | Delete
if self.path.startswith('//'):
[339] Fix | Delete
self.path = '/' + self.path.lstrip('/') # Reduce to a single /
[340] Fix | Delete
[341] Fix | Delete
# Examine the headers and look for a Connection directive.
[342] Fix | Delete
try:
[343] Fix | Delete
self.headers = http.client.parse_headers(self.rfile,
[344] Fix | Delete
_class=self.MessageClass)
[345] Fix | Delete
except http.client.LineTooLong as err:
[346] Fix | Delete
self.send_error(
[347] Fix | Delete
HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
[348] Fix | Delete
"Line too long",
[349] Fix | Delete
str(err))
[350] Fix | Delete
return False
[351] Fix | Delete
except http.client.HTTPException as err:
[352] Fix | Delete
self.send_error(
[353] Fix | Delete
HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
[354] Fix | Delete
"Too many headers",
[355] Fix | Delete
str(err)
[356] Fix | Delete
)
[357] Fix | Delete
return False
[358] Fix | Delete
[359] Fix | Delete
conntype = self.headers.get('Connection', "")
[360] Fix | Delete
if conntype.lower() == 'close':
[361] Fix | Delete
self.close_connection = True
[362] Fix | Delete
elif (conntype.lower() == 'keep-alive' and
[363] Fix | Delete
self.protocol_version >= "HTTP/1.1"):
[364] Fix | Delete
self.close_connection = False
[365] Fix | Delete
# Examine the headers and look for an Expect directive
[366] Fix | Delete
expect = self.headers.get('Expect', "")
[367] Fix | Delete
if (expect.lower() == "100-continue" and
[368] Fix | Delete
self.protocol_version >= "HTTP/1.1" and
[369] Fix | Delete
self.request_version >= "HTTP/1.1"):
[370] Fix | Delete
if not self.handle_expect_100():
[371] Fix | Delete
return False
[372] Fix | Delete
return True
[373] Fix | Delete
[374] Fix | Delete
def handle_expect_100(self):
[375] Fix | Delete
"""Decide what to do with an "Expect: 100-continue" header.
[376] Fix | Delete
[377] Fix | Delete
If the client is expecting a 100 Continue response, we must
[378] Fix | Delete
respond with either a 100 Continue or a final response before
[379] Fix | Delete
waiting for the request body. The default is to always respond
[380] Fix | Delete
with a 100 Continue. You can behave differently (for example,
[381] Fix | Delete
reject unauthorized requests) by overriding this method.
[382] Fix | Delete
[383] Fix | Delete
This method should either return True (possibly after sending
[384] Fix | Delete
a 100 Continue response) or send an error response and return
[385] Fix | Delete
False.
[386] Fix | Delete
[387] Fix | Delete
"""
[388] Fix | Delete
self.send_response_only(HTTPStatus.CONTINUE)
[389] Fix | Delete
self.end_headers()
[390] Fix | Delete
return True
[391] Fix | Delete
[392] Fix | Delete
def handle_one_request(self):
[393] Fix | Delete
"""Handle a single HTTP request.
[394] Fix | Delete
[395] Fix | Delete
You normally don't need to override this method; see the class
[396] Fix | Delete
__doc__ string for information on how to handle specific HTTP
[397] Fix | Delete
commands such as GET and POST.
[398] Fix | Delete
[399] Fix | Delete
"""
[400] Fix | Delete
try:
[401] Fix | Delete
self.raw_requestline = self.rfile.readline(65537)
[402] Fix | Delete
if len(self.raw_requestline) > 65536:
[403] Fix | Delete
self.requestline = ''
[404] Fix | Delete
self.request_version = ''
[405] Fix | Delete
self.command = ''
[406] Fix | Delete
self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
[407] Fix | Delete
return
[408] Fix | Delete
if not self.raw_requestline:
[409] Fix | Delete
self.close_connection = True
[410] Fix | Delete
return
[411] Fix | Delete
if not self.parse_request():
[412] Fix | Delete
# An error code has been sent, just exit
[413] Fix | Delete
return
[414] Fix | Delete
mname = 'do_' + self.command
[415] Fix | Delete
if not hasattr(self, mname):
[416] Fix | Delete
self.send_error(
[417] Fix | Delete
HTTPStatus.NOT_IMPLEMENTED,
[418] Fix | Delete
"Unsupported method (%r)" % self.command)
[419] Fix | Delete
return
[420] Fix | Delete
method = getattr(self, mname)
[421] Fix | Delete
method()
[422] Fix | Delete
self.wfile.flush() #actually send the response if not already done.
[423] Fix | Delete
except socket.timeout as e:
[424] Fix | Delete
#a read or a write timed out. Discard this connection
[425] Fix | Delete
self.log_error("Request timed out: %r", e)
[426] Fix | Delete
self.close_connection = True
[427] Fix | Delete
return
[428] Fix | Delete
[429] Fix | Delete
def handle(self):
[430] Fix | Delete
"""Handle multiple requests if necessary."""
[431] Fix | Delete
self.close_connection = True
[432] Fix | Delete
[433] Fix | Delete
self.handle_one_request()
[434] Fix | Delete
while not self.close_connection:
[435] Fix | Delete
self.handle_one_request()
[436] Fix | Delete
[437] Fix | Delete
def send_error(self, code, message=None, explain=None):
[438] Fix | Delete
"""Send and log an error reply.
[439] Fix | Delete
[440] Fix | Delete
Arguments are
[441] Fix | Delete
* code: an HTTP error code
[442] Fix | Delete
3 digits
[443] Fix | Delete
* message: a simple optional 1 line reason phrase.
[444] Fix | Delete
*( HTAB / SP / VCHAR / %x80-FF )
[445] Fix | Delete
defaults to short entry matching the response code
[446] Fix | Delete
* explain: a detailed message defaults to the long entry
[447] Fix | Delete
matching the response code.
[448] Fix | Delete
[449] Fix | Delete
This sends an error response (so it must be called before any
[450] Fix | Delete
output has been generated), logs the error, and finally sends
[451] Fix | Delete
a piece of HTML explaining the error to the user.
[452] Fix | Delete
[453] Fix | Delete
"""
[454] Fix | Delete
[455] Fix | Delete
try:
[456] Fix | Delete
shortmsg, longmsg = self.responses[code]
[457] Fix | Delete
except KeyError:
[458] Fix | Delete
shortmsg, longmsg = '???', '???'
[459] Fix | Delete
if message is None:
[460] Fix | Delete
message = shortmsg
[461] Fix | Delete
if explain is None:
[462] Fix | Delete
explain = longmsg
[463] Fix | Delete
self.log_error("code %d, message %s", code, message)
[464] Fix | Delete
self.send_response(code, message)
[465] Fix | Delete
self.send_header('Connection', 'close')
[466] Fix | Delete
[467] Fix | Delete
# Message body is omitted for cases described in:
[468] Fix | Delete
# - RFC7230: 3.3. 1xx, 204(No Content), 304(Not Modified)
[469] Fix | Delete
# - RFC7231: 6.3.6. 205(Reset Content)
[470] Fix | Delete
body = None
[471] Fix | Delete
if (code >= 200 and
[472] Fix | Delete
code not in (HTTPStatus.NO_CONTENT,
[473] Fix | Delete
HTTPStatus.RESET_CONTENT,
[474] Fix | Delete
HTTPStatus.NOT_MODIFIED)):
[475] Fix | Delete
# HTML encode to prevent Cross Site Scripting attacks
[476] Fix | Delete
# (see bug #1100201)
[477] Fix | Delete
content = (self.error_message_format % {
[478] Fix | Delete
'code': code,
[479] Fix | Delete
'message': html.escape(message, quote=False),
[480] Fix | Delete
'explain': html.escape(explain, quote=False)
[481] Fix | Delete
})
[482] Fix | Delete
body = content.encode('UTF-8', 'replace')
[483] Fix | Delete
self.send_header("Content-Type", self.error_content_type)
[484] Fix | Delete
self.send_header('Content-Length', str(len(body)))
[485] Fix | Delete
self.end_headers()
[486] Fix | Delete
[487] Fix | Delete
if self.command != 'HEAD' and body:
[488] Fix | Delete
self.wfile.write(body)
[489] Fix | Delete
[490] Fix | Delete
def send_response(self, code, message=None):
[491] Fix | Delete
"""Add the response header to the headers buffer and log the
[492] Fix | Delete
response code.
[493] Fix | Delete
[494] Fix | Delete
Also send two standard headers with the server software
[495] Fix | Delete
version and the current date.
[496] Fix | Delete
[497] Fix | Delete
"""
[498] Fix | Delete
self.log_request(code)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function