Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3..../http
File: client.py
r"""HTTP/1.1 client library
[0] Fix | Delete
[1] Fix | Delete
<intro stuff goes here>
[2] Fix | Delete
<other stuff, too>
[3] Fix | Delete
[4] Fix | Delete
HTTPConnection goes through a number of "states", which define when a client
[5] Fix | Delete
may legally make another request or fetch the response for a particular
[6] Fix | Delete
request. This diagram details these state transitions:
[7] Fix | Delete
[8] Fix | Delete
(null)
[9] Fix | Delete
|
[10] Fix | Delete
| HTTPConnection()
[11] Fix | Delete
v
[12] Fix | Delete
Idle
[13] Fix | Delete
|
[14] Fix | Delete
| putrequest()
[15] Fix | Delete
v
[16] Fix | Delete
Request-started
[17] Fix | Delete
|
[18] Fix | Delete
| ( putheader() )* endheaders()
[19] Fix | Delete
v
[20] Fix | Delete
Request-sent
[21] Fix | Delete
|\_____________________________
[22] Fix | Delete
| | getresponse() raises
[23] Fix | Delete
| response = getresponse() | ConnectionError
[24] Fix | Delete
v v
[25] Fix | Delete
Unread-response Idle
[26] Fix | Delete
[Response-headers-read]
[27] Fix | Delete
|\____________________
[28] Fix | Delete
| |
[29] Fix | Delete
| response.read() | putrequest()
[30] Fix | Delete
v v
[31] Fix | Delete
Idle Req-started-unread-response
[32] Fix | Delete
______/|
[33] Fix | Delete
/ |
[34] Fix | Delete
response.read() | | ( putheader() )* endheaders()
[35] Fix | Delete
v v
[36] Fix | Delete
Request-started Req-sent-unread-response
[37] Fix | Delete
|
[38] Fix | Delete
| response.read()
[39] Fix | Delete
v
[40] Fix | Delete
Request-sent
[41] Fix | Delete
[42] Fix | Delete
This diagram presents the following rules:
[43] Fix | Delete
-- a second request may not be started until {response-headers-read}
[44] Fix | Delete
-- a response [object] cannot be retrieved until {request-sent}
[45] Fix | Delete
-- there is no differentiation between an unread response body and a
[46] Fix | Delete
partially read response body
[47] Fix | Delete
[48] Fix | Delete
Note: this enforcement is applied by the HTTPConnection class. The
[49] Fix | Delete
HTTPResponse class does not enforce this state machine, which
[50] Fix | Delete
implies sophisticated clients may accelerate the request/response
[51] Fix | Delete
pipeline. Caution should be taken, though: accelerating the states
[52] Fix | Delete
beyond the above pattern may imply knowledge of the server's
[53] Fix | Delete
connection-close behavior for certain requests. For example, it
[54] Fix | Delete
is impossible to tell whether the server will close the connection
[55] Fix | Delete
UNTIL the response headers have been read; this means that further
[56] Fix | Delete
requests cannot be placed into the pipeline until it is known that
[57] Fix | Delete
the server will NOT be closing the connection.
[58] Fix | Delete
[59] Fix | Delete
Logical State __state __response
[60] Fix | Delete
------------- ------- ----------
[61] Fix | Delete
Idle _CS_IDLE None
[62] Fix | Delete
Request-started _CS_REQ_STARTED None
[63] Fix | Delete
Request-sent _CS_REQ_SENT None
[64] Fix | Delete
Unread-response _CS_IDLE <response_class>
[65] Fix | Delete
Req-started-unread-response _CS_REQ_STARTED <response_class>
[66] Fix | Delete
Req-sent-unread-response _CS_REQ_SENT <response_class>
[67] Fix | Delete
"""
[68] Fix | Delete
[69] Fix | Delete
import email.parser
[70] Fix | Delete
import email.message
[71] Fix | Delete
import http
[72] Fix | Delete
import io
[73] Fix | Delete
import os
[74] Fix | Delete
import re
[75] Fix | Delete
import socket
[76] Fix | Delete
import collections
[77] Fix | Delete
from urllib.parse import urlsplit
[78] Fix | Delete
[79] Fix | Delete
# HTTPMessage, parse_headers(), and the HTTP status code constants are
[80] Fix | Delete
# intentionally omitted for simplicity
[81] Fix | Delete
__all__ = ["HTTPResponse", "HTTPConnection",
[82] Fix | Delete
"HTTPException", "NotConnected", "UnknownProtocol",
[83] Fix | Delete
"UnknownTransferEncoding", "UnimplementedFileMode",
[84] Fix | Delete
"IncompleteRead", "InvalidURL", "ImproperConnectionState",
[85] Fix | Delete
"CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
[86] Fix | Delete
"BadStatusLine", "LineTooLong", "RemoteDisconnected", "error",
[87] Fix | Delete
"responses"]
[88] Fix | Delete
[89] Fix | Delete
HTTP_PORT = 80
[90] Fix | Delete
HTTPS_PORT = 443
[91] Fix | Delete
[92] Fix | Delete
_UNKNOWN = 'UNKNOWN'
[93] Fix | Delete
[94] Fix | Delete
# connection states
[95] Fix | Delete
_CS_IDLE = 'Idle'
[96] Fix | Delete
_CS_REQ_STARTED = 'Request-started'
[97] Fix | Delete
_CS_REQ_SENT = 'Request-sent'
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
# hack to maintain backwards compatibility
[101] Fix | Delete
globals().update(http.HTTPStatus.__members__)
[102] Fix | Delete
[103] Fix | Delete
# another hack to maintain backwards compatibility
[104] Fix | Delete
# Mapping status codes to official W3C names
[105] Fix | Delete
responses = {v: v.phrase for v in http.HTTPStatus.__members__.values()}
[106] Fix | Delete
[107] Fix | Delete
# maximal amount of data to read at one time in _safe_read
[108] Fix | Delete
MAXAMOUNT = 1048576
[109] Fix | Delete
[110] Fix | Delete
# maximal line length when calling readline().
[111] Fix | Delete
_MAXLINE = 65536
[112] Fix | Delete
_MAXHEADERS = 100
[113] Fix | Delete
[114] Fix | Delete
# Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
[115] Fix | Delete
#
[116] Fix | Delete
# VCHAR = %x21-7E
[117] Fix | Delete
# obs-text = %x80-FF
[118] Fix | Delete
# header-field = field-name ":" OWS field-value OWS
[119] Fix | Delete
# field-name = token
[120] Fix | Delete
# field-value = *( field-content / obs-fold )
[121] Fix | Delete
# field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
[122] Fix | Delete
# field-vchar = VCHAR / obs-text
[123] Fix | Delete
#
[124] Fix | Delete
# obs-fold = CRLF 1*( SP / HTAB )
[125] Fix | Delete
# ; obsolete line folding
[126] Fix | Delete
# ; see Section 3.2.4
[127] Fix | Delete
[128] Fix | Delete
# token = 1*tchar
[129] Fix | Delete
#
[130] Fix | Delete
# tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
[131] Fix | Delete
# / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
[132] Fix | Delete
# / DIGIT / ALPHA
[133] Fix | Delete
# ; any VCHAR, except delimiters
[134] Fix | Delete
#
[135] Fix | Delete
# VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
[136] Fix | Delete
[137] Fix | Delete
# the patterns for both name and value are more lenient than RFC
[138] Fix | Delete
# definitions to allow for backwards compatibility
[139] Fix | Delete
_is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
[140] Fix | Delete
_is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
[141] Fix | Delete
[142] Fix | Delete
# These characters are not allowed within HTTP URL paths.
[143] Fix | Delete
# See https://tools.ietf.org/html/rfc3986#section-3.3 and the
[144] Fix | Delete
# https://tools.ietf.org/html/rfc3986#appendix-A pchar definition.
[145] Fix | Delete
# Prevents CVE-2019-9740. Includes control characters such as \r\n.
[146] Fix | Delete
# We don't restrict chars above \x7f as putrequest() limits us to ASCII.
[147] Fix | Delete
_contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')
[148] Fix | Delete
# Arguably only these _should_ allowed:
[149] Fix | Delete
# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
[150] Fix | Delete
# We are more lenient for assumed real world compatibility purposes.
[151] Fix | Delete
[152] Fix | Delete
# These characters are not allowed within HTTP method names
[153] Fix | Delete
# to prevent http header injection.
[154] Fix | Delete
_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
[155] Fix | Delete
[156] Fix | Delete
# We always set the Content-Length header for these methods because some
[157] Fix | Delete
# servers will otherwise respond with a 411
[158] Fix | Delete
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
[159] Fix | Delete
[160] Fix | Delete
[161] Fix | Delete
def _encode(data, name='data'):
[162] Fix | Delete
"""Call data.encode("latin-1") but show a better error message."""
[163] Fix | Delete
try:
[164] Fix | Delete
return data.encode("latin-1")
[165] Fix | Delete
except UnicodeEncodeError as err:
[166] Fix | Delete
raise UnicodeEncodeError(
[167] Fix | Delete
err.encoding,
[168] Fix | Delete
err.object,
[169] Fix | Delete
err.start,
[170] Fix | Delete
err.end,
[171] Fix | Delete
"%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') "
[172] Fix | Delete
"if you want to send it encoded in UTF-8." %
[173] Fix | Delete
(name.title(), data[err.start:err.end], name)) from None
[174] Fix | Delete
[175] Fix | Delete
[176] Fix | Delete
class HTTPMessage(email.message.Message):
[177] Fix | Delete
# XXX The only usage of this method is in
[178] Fix | Delete
# http.server.CGIHTTPRequestHandler. Maybe move the code there so
[179] Fix | Delete
# that it doesn't need to be part of the public API. The API has
[180] Fix | Delete
# never been defined so this could cause backwards compatibility
[181] Fix | Delete
# issues.
[182] Fix | Delete
[183] Fix | Delete
def getallmatchingheaders(self, name):
[184] Fix | Delete
"""Find all header lines matching a given header name.
[185] Fix | Delete
[186] Fix | Delete
Look through the list of headers and find all lines matching a given
[187] Fix | Delete
header name (and their continuation lines). A list of the lines is
[188] Fix | Delete
returned, without interpretation. If the header does not occur, an
[189] Fix | Delete
empty list is returned. If the header occurs multiple times, all
[190] Fix | Delete
occurrences are returned. Case is not important in the header name.
[191] Fix | Delete
[192] Fix | Delete
"""
[193] Fix | Delete
name = name.lower() + ':'
[194] Fix | Delete
n = len(name)
[195] Fix | Delete
lst = []
[196] Fix | Delete
hit = 0
[197] Fix | Delete
for line in self.keys():
[198] Fix | Delete
if line[:n].lower() == name:
[199] Fix | Delete
hit = 1
[200] Fix | Delete
elif not line[:1].isspace():
[201] Fix | Delete
hit = 0
[202] Fix | Delete
if hit:
[203] Fix | Delete
lst.append(line)
[204] Fix | Delete
return lst
[205] Fix | Delete
[206] Fix | Delete
def _read_headers(fp):
[207] Fix | Delete
"""Reads potential header lines into a list from a file pointer.
[208] Fix | Delete
[209] Fix | Delete
Length of line is limited by _MAXLINE, and number of
[210] Fix | Delete
headers is limited by _MAXHEADERS.
[211] Fix | Delete
"""
[212] Fix | Delete
headers = []
[213] Fix | Delete
while True:
[214] Fix | Delete
line = fp.readline(_MAXLINE + 1)
[215] Fix | Delete
if len(line) > _MAXLINE:
[216] Fix | Delete
raise LineTooLong("header line")
[217] Fix | Delete
headers.append(line)
[218] Fix | Delete
if len(headers) > _MAXHEADERS:
[219] Fix | Delete
raise HTTPException("got more than %d headers" % _MAXHEADERS)
[220] Fix | Delete
if line in (b'\r\n', b'\n', b''):
[221] Fix | Delete
break
[222] Fix | Delete
return headers
[223] Fix | Delete
[224] Fix | Delete
def parse_headers(fp, _class=HTTPMessage):
[225] Fix | Delete
"""Parses only RFC2822 headers from a file pointer.
[226] Fix | Delete
[227] Fix | Delete
email Parser wants to see strings rather than bytes.
[228] Fix | Delete
But a TextIOWrapper around self.rfile would buffer too many bytes
[229] Fix | Delete
from the stream, bytes which we later need to read as bytes.
[230] Fix | Delete
So we read the correct bytes here, as bytes, for email Parser
[231] Fix | Delete
to parse.
[232] Fix | Delete
[233] Fix | Delete
"""
[234] Fix | Delete
headers = _read_headers(fp)
[235] Fix | Delete
hstring = b''.join(headers).decode('iso-8859-1')
[236] Fix | Delete
return email.parser.Parser(_class=_class).parsestr(hstring)
[237] Fix | Delete
[238] Fix | Delete
[239] Fix | Delete
class HTTPResponse(io.BufferedIOBase):
[240] Fix | Delete
[241] Fix | Delete
# See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
[242] Fix | Delete
[243] Fix | Delete
# The bytes from the socket object are iso-8859-1 strings.
[244] Fix | Delete
# See RFC 2616 sec 2.2 which notes an exception for MIME-encoded
[245] Fix | Delete
# text following RFC 2047. The basic status line parsing only
[246] Fix | Delete
# accepts iso-8859-1.
[247] Fix | Delete
[248] Fix | Delete
def __init__(self, sock, debuglevel=0, method=None, url=None):
[249] Fix | Delete
# If the response includes a content-length header, we need to
[250] Fix | Delete
# make sure that the client doesn't read more than the
[251] Fix | Delete
# specified number of bytes. If it does, it will block until
[252] Fix | Delete
# the server times out and closes the connection. This will
[253] Fix | Delete
# happen if a self.fp.read() is done (without a size) whether
[254] Fix | Delete
# self.fp is buffered or not. So, no self.fp.read() by
[255] Fix | Delete
# clients unless they know what they are doing.
[256] Fix | Delete
self.fp = sock.makefile("rb")
[257] Fix | Delete
self.debuglevel = debuglevel
[258] Fix | Delete
self._method = method
[259] Fix | Delete
[260] Fix | Delete
# The HTTPResponse object is returned via urllib. The clients
[261] Fix | Delete
# of http and urllib expect different attributes for the
[262] Fix | Delete
# headers. headers is used here and supports urllib. msg is
[263] Fix | Delete
# provided as a backwards compatibility layer for http
[264] Fix | Delete
# clients.
[265] Fix | Delete
[266] Fix | Delete
self.headers = self.msg = None
[267] Fix | Delete
[268] Fix | Delete
# from the Status-Line of the response
[269] Fix | Delete
self.version = _UNKNOWN # HTTP-Version
[270] Fix | Delete
self.status = _UNKNOWN # Status-Code
[271] Fix | Delete
self.reason = _UNKNOWN # Reason-Phrase
[272] Fix | Delete
[273] Fix | Delete
self.chunked = _UNKNOWN # is "chunked" being used?
[274] Fix | Delete
self.chunk_left = _UNKNOWN # bytes left to read in current chunk
[275] Fix | Delete
self.length = _UNKNOWN # number of bytes left in response
[276] Fix | Delete
self.will_close = _UNKNOWN # conn will close at end of response
[277] Fix | Delete
[278] Fix | Delete
def _read_status(self):
[279] Fix | Delete
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
[280] Fix | Delete
if len(line) > _MAXLINE:
[281] Fix | Delete
raise LineTooLong("status line")
[282] Fix | Delete
if self.debuglevel > 0:
[283] Fix | Delete
print("reply:", repr(line))
[284] Fix | Delete
if not line:
[285] Fix | Delete
# Presumably, the server closed the connection before
[286] Fix | Delete
# sending a valid response.
[287] Fix | Delete
raise RemoteDisconnected("Remote end closed connection without"
[288] Fix | Delete
" response")
[289] Fix | Delete
try:
[290] Fix | Delete
version, status, reason = line.split(None, 2)
[291] Fix | Delete
except ValueError:
[292] Fix | Delete
try:
[293] Fix | Delete
version, status = line.split(None, 1)
[294] Fix | Delete
reason = ""
[295] Fix | Delete
except ValueError:
[296] Fix | Delete
# empty version will cause next test to fail.
[297] Fix | Delete
version = ""
[298] Fix | Delete
if not version.startswith("HTTP/"):
[299] Fix | Delete
self._close_conn()
[300] Fix | Delete
raise BadStatusLine(line)
[301] Fix | Delete
[302] Fix | Delete
# The status code is a three-digit number
[303] Fix | Delete
try:
[304] Fix | Delete
status = int(status)
[305] Fix | Delete
if status < 100 or status > 999:
[306] Fix | Delete
raise BadStatusLine(line)
[307] Fix | Delete
except ValueError:
[308] Fix | Delete
raise BadStatusLine(line)
[309] Fix | Delete
return version, status, reason
[310] Fix | Delete
[311] Fix | Delete
def begin(self):
[312] Fix | Delete
if self.headers is not None:
[313] Fix | Delete
# we've already started reading the response
[314] Fix | Delete
return
[315] Fix | Delete
[316] Fix | Delete
# read until we get a non-100 response
[317] Fix | Delete
while True:
[318] Fix | Delete
version, status, reason = self._read_status()
[319] Fix | Delete
if status != CONTINUE:
[320] Fix | Delete
break
[321] Fix | Delete
# skip the header from the 100 response
[322] Fix | Delete
skipped_headers = _read_headers(self.fp)
[323] Fix | Delete
if self.debuglevel > 0:
[324] Fix | Delete
print("headers:", skipped_headers)
[325] Fix | Delete
del skipped_headers
[326] Fix | Delete
[327] Fix | Delete
self.code = self.status = status
[328] Fix | Delete
self.reason = reason.strip()
[329] Fix | Delete
if version in ("HTTP/1.0", "HTTP/0.9"):
[330] Fix | Delete
# Some servers might still return "0.9", treat it as 1.0 anyway
[331] Fix | Delete
self.version = 10
[332] Fix | Delete
elif version.startswith("HTTP/1."):
[333] Fix | Delete
self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
[334] Fix | Delete
else:
[335] Fix | Delete
raise UnknownProtocol(version)
[336] Fix | Delete
[337] Fix | Delete
self.headers = self.msg = parse_headers(self.fp)
[338] Fix | Delete
[339] Fix | Delete
if self.debuglevel > 0:
[340] Fix | Delete
for hdr in self.headers:
[341] Fix | Delete
print("header:", hdr + ":", self.headers.get(hdr))
[342] Fix | Delete
[343] Fix | Delete
# are we using the chunked-style of transfer encoding?
[344] Fix | Delete
tr_enc = self.headers.get("transfer-encoding")
[345] Fix | Delete
if tr_enc and tr_enc.lower() == "chunked":
[346] Fix | Delete
self.chunked = True
[347] Fix | Delete
self.chunk_left = None
[348] Fix | Delete
else:
[349] Fix | Delete
self.chunked = False
[350] Fix | Delete
[351] Fix | Delete
# will the connection close at the end of the response?
[352] Fix | Delete
self.will_close = self._check_close()
[353] Fix | Delete
[354] Fix | Delete
# do we have a Content-Length?
[355] Fix | Delete
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
[356] Fix | Delete
self.length = None
[357] Fix | Delete
length = self.headers.get("content-length")
[358] Fix | Delete
[359] Fix | Delete
# are we using the chunked-style of transfer encoding?
[360] Fix | Delete
tr_enc = self.headers.get("transfer-encoding")
[361] Fix | Delete
if length and not self.chunked:
[362] Fix | Delete
try:
[363] Fix | Delete
self.length = int(length)
[364] Fix | Delete
except ValueError:
[365] Fix | Delete
self.length = None
[366] Fix | Delete
else:
[367] Fix | Delete
if self.length < 0: # ignore nonsensical negative lengths
[368] Fix | Delete
self.length = None
[369] Fix | Delete
else:
[370] Fix | Delete
self.length = None
[371] Fix | Delete
[372] Fix | Delete
# does the body have a fixed length? (of zero)
[373] Fix | Delete
if (status == NO_CONTENT or status == NOT_MODIFIED or
[374] Fix | Delete
100 <= status < 200 or # 1xx codes
[375] Fix | Delete
self._method == "HEAD"):
[376] Fix | Delete
self.length = 0
[377] Fix | Delete
[378] Fix | Delete
# if the connection remains open, and we aren't using chunked, and
[379] Fix | Delete
# a content-length was not provided, then assume that the connection
[380] Fix | Delete
# WILL close.
[381] Fix | Delete
if (not self.will_close and
[382] Fix | Delete
not self.chunked and
[383] Fix | Delete
self.length is None):
[384] Fix | Delete
self.will_close = True
[385] Fix | Delete
[386] Fix | Delete
def _check_close(self):
[387] Fix | Delete
conn = self.headers.get("connection")
[388] Fix | Delete
if self.version == 11:
[389] Fix | Delete
# An HTTP/1.1 proxy is assumed to stay open unless
[390] Fix | Delete
# explicitly closed.
[391] Fix | Delete
conn = self.headers.get("connection")
[392] Fix | Delete
if conn and "close" in conn.lower():
[393] Fix | Delete
return True
[394] Fix | Delete
return False
[395] Fix | Delete
[396] Fix | Delete
# Some HTTP/1.0 implementations have support for persistent
[397] Fix | Delete
# connections, using rules different than HTTP/1.1.
[398] Fix | Delete
[399] Fix | Delete
# For older HTTP, Keep-Alive indicates persistent connection.
[400] Fix | Delete
if self.headers.get("keep-alive"):
[401] Fix | Delete
return False
[402] Fix | Delete
[403] Fix | Delete
# At least Akamai returns a "Connection: Keep-Alive" header,
[404] Fix | Delete
# which was supposed to be sent by the client.
[405] Fix | Delete
if conn and "keep-alive" in conn.lower():
[406] Fix | Delete
return False
[407] Fix | Delete
[408] Fix | Delete
# Proxy-Connection is a netscape hack.
[409] Fix | Delete
pconn = self.headers.get("proxy-connection")
[410] Fix | Delete
if pconn and "keep-alive" in pconn.lower():
[411] Fix | Delete
return False
[412] Fix | Delete
[413] Fix | Delete
# otherwise, assume it will close
[414] Fix | Delete
return True
[415] Fix | Delete
[416] Fix | Delete
def _close_conn(self):
[417] Fix | Delete
fp = self.fp
[418] Fix | Delete
self.fp = None
[419] Fix | Delete
fp.close()
[420] Fix | Delete
[421] Fix | Delete
def close(self):
[422] Fix | Delete
try:
[423] Fix | Delete
super().close() # set "closed" flag
[424] Fix | Delete
finally:
[425] Fix | Delete
if self.fp:
[426] Fix | Delete
self._close_conn()
[427] Fix | Delete
[428] Fix | Delete
# These implementations are for the benefit of io.BufferedReader.
[429] Fix | Delete
[430] Fix | Delete
# XXX This class should probably be revised to act more like
[431] Fix | Delete
# the "raw stream" that BufferedReader expects.
[432] Fix | Delete
[433] Fix | Delete
def flush(self):
[434] Fix | Delete
super().flush()
[435] Fix | Delete
if self.fp:
[436] Fix | Delete
self.fp.flush()
[437] Fix | Delete
[438] Fix | Delete
def readable(self):
[439] Fix | Delete
"""Always returns True"""
[440] Fix | Delete
return True
[441] Fix | Delete
[442] Fix | Delete
# End of "raw stream" methods
[443] Fix | Delete
[444] Fix | Delete
def isclosed(self):
[445] Fix | Delete
"""True if the connection is closed."""
[446] Fix | Delete
# NOTE: it is possible that we will not ever call self.close(). This
[447] Fix | Delete
# case occurs when will_close is TRUE, length is None, and we
[448] Fix | Delete
# read up to the last byte, but NOT past it.
[449] Fix | Delete
#
[450] Fix | Delete
# IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
[451] Fix | Delete
# called, meaning self.isclosed() is meaningful.
[452] Fix | Delete
return self.fp is None
[453] Fix | Delete
[454] Fix | Delete
def read(self, amt=None):
[455] Fix | Delete
if self.fp is None:
[456] Fix | Delete
return b""
[457] Fix | Delete
[458] Fix | Delete
if self._method == "HEAD":
[459] Fix | Delete
self._close_conn()
[460] Fix | Delete
return b""
[461] Fix | Delete
[462] Fix | Delete
if amt is not None:
[463] Fix | Delete
# Amount is given, implement using readinto
[464] Fix | Delete
b = bytearray(amt)
[465] Fix | Delete
n = self.readinto(b)
[466] Fix | Delete
return memoryview(b)[:n].tobytes()
[467] Fix | Delete
else:
[468] Fix | Delete
# Amount is not given (unbounded read) so we must check self.length
[469] Fix | Delete
# and self.chunked
[470] Fix | Delete
[471] Fix | Delete
if self.chunked:
[472] Fix | Delete
return self._readall_chunked()
[473] Fix | Delete
[474] Fix | Delete
if self.length is None:
[475] Fix | Delete
s = self.fp.read()
[476] Fix | Delete
else:
[477] Fix | Delete
try:
[478] Fix | Delete
s = self._safe_read(self.length)
[479] Fix | Delete
except IncompleteRead:
[480] Fix | Delete
self._close_conn()
[481] Fix | Delete
raise
[482] Fix | Delete
self.length = 0
[483] Fix | Delete
self._close_conn() # we read everything
[484] Fix | Delete
return s
[485] Fix | Delete
[486] Fix | Delete
def readinto(self, b):
[487] Fix | Delete
"""Read up to len(b) bytes into bytearray b and return the number
[488] Fix | Delete
of bytes read.
[489] Fix | Delete
"""
[490] Fix | Delete
[491] Fix | Delete
if self.fp is None:
[492] Fix | Delete
return 0
[493] Fix | Delete
[494] Fix | Delete
if self._method == "HEAD":
[495] Fix | Delete
self._close_conn()
[496] Fix | Delete
return 0
[497] Fix | Delete
[498] Fix | Delete
if self.chunked:
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function