mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Fix several more paths from the SSL code.
In particular, watch out for comparing b"" to "". They're not equal, but you can start at the code asking whether buf == "" for a long time before realizing that it will never be True.
This commit is contained in:
parent
6252083f5f
commit
0ee5eeb8ff
1 changed files with 14 additions and 15 deletions
|
|
@ -435,14 +435,13 @@ class HTTPResponse:
|
||||||
|
|
||||||
# do we have a Content-Length?
|
# do we have a Content-Length?
|
||||||
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
|
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
|
||||||
|
self.length = None
|
||||||
length = self.msg.getheader("content-length")
|
length = self.msg.getheader("content-length")
|
||||||
if length and not self.chunked:
|
if length and not self.chunked:
|
||||||
try:
|
try:
|
||||||
self.length = int(length)
|
self.length = int(length)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.length = None
|
pass
|
||||||
else:
|
|
||||||
self.length = None
|
|
||||||
|
|
||||||
# does the body have a fixed length? (of zero)
|
# does the body have a fixed length? (of zero)
|
||||||
if (status == NO_CONTENT or status == NOT_MODIFIED or
|
if (status == NO_CONTENT or status == NOT_MODIFIED or
|
||||||
|
|
@ -453,9 +452,9 @@ class HTTPResponse:
|
||||||
# if the connection remains open, and we aren't using chunked, and
|
# if the connection remains open, and we aren't using chunked, and
|
||||||
# a content-length was not provided, then assume that the connection
|
# a content-length was not provided, then assume that the connection
|
||||||
# WILL close.
|
# WILL close.
|
||||||
if not self.will_close and \
|
if (not self.will_close and
|
||||||
not self.chunked and \
|
not self.chunked and
|
||||||
self.length is None:
|
self.length is None):
|
||||||
self.will_close = 1
|
self.will_close = 1
|
||||||
|
|
||||||
def _check_close(self):
|
def _check_close(self):
|
||||||
|
|
@ -998,11 +997,11 @@ class SSLFile(SharedSocketClient):
|
||||||
def __init__(self, sock, ssl, bufsize=None):
|
def __init__(self, sock, ssl, bufsize=None):
|
||||||
SharedSocketClient.__init__(self, sock)
|
SharedSocketClient.__init__(self, sock)
|
||||||
self._ssl = ssl
|
self._ssl = ssl
|
||||||
self._buf = ''
|
self._buf = b""
|
||||||
self._bufsize = bufsize or self.__class__.BUFSIZE
|
self._bufsize = bufsize or self.__class__.BUFSIZE
|
||||||
|
|
||||||
def _read(self):
|
def _read(self):
|
||||||
buf = ''
|
buf = b""
|
||||||
# put in a loop so that we retry on transient errors
|
# put in a loop so that we retry on transient errors
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|
@ -1033,13 +1032,13 @@ class SSLFile(SharedSocketClient):
|
||||||
avail = len(self._buf)
|
avail = len(self._buf)
|
||||||
while size is None or avail < size:
|
while size is None or avail < size:
|
||||||
s = self._read()
|
s = self._read()
|
||||||
if s == "":
|
if s == b"":
|
||||||
break
|
break
|
||||||
L.append(s)
|
L.append(s)
|
||||||
avail += len(s)
|
avail += len(s)
|
||||||
all = "".join(L)
|
all = b"".join(L)
|
||||||
if size is None:
|
if size is None:
|
||||||
self._buf = ""
|
self._buf = b""
|
||||||
return all
|
return all
|
||||||
else:
|
else:
|
||||||
self._buf = all[size:]
|
self._buf = all[size:]
|
||||||
|
|
@ -1047,20 +1046,20 @@ class SSLFile(SharedSocketClient):
|
||||||
|
|
||||||
def readline(self):
|
def readline(self):
|
||||||
L = [self._buf]
|
L = [self._buf]
|
||||||
self._buf = ''
|
self._buf = b""
|
||||||
while 1:
|
while 1:
|
||||||
i = L[-1].find("\n")
|
i = L[-1].find("\n")
|
||||||
if i >= 0:
|
if i >= 0:
|
||||||
break
|
break
|
||||||
s = self._read()
|
s = self._read()
|
||||||
if s == '':
|
if s == b"":
|
||||||
break
|
break
|
||||||
L.append(s)
|
L.append(s)
|
||||||
if i == -1:
|
if i == -1:
|
||||||
# loop exited because there is no more data
|
# loop exited because there is no more data
|
||||||
return "".join(L)
|
return b"".join(L)
|
||||||
else:
|
else:
|
||||||
all = "".join(L)
|
all = b"".join(L)
|
||||||
# XXX could do enough bookkeeping not to do a 2nd search
|
# XXX could do enough bookkeeping not to do a 2nd search
|
||||||
i = all.find("\n") + 1
|
i = all.find("\n") + 1
|
||||||
line = all[:i]
|
line = all[:i]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue