Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30f8674

Browse files
committed
Do not close socket when a Content-Length is 0. This make the
interface consistent: The client is responsible for closing the socket, regardless of the amount of data received. Restore suport for set_debuglevel call.
1 parent 69cc715 commit 30f8674

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

Lib/httplib.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@
8787

8888

8989
class HTTPResponse:
90-
def __init__(self, sock):
90+
def __init__(self, sock, debuglevel=0):
9191
self.fp = sock.makefile('rb', 0)
92+
self.debuglevel = debuglevel
9293

9394
self.msg = None
9495

@@ -108,6 +109,8 @@ def begin(self):
108109
return
109110

110111
line = self.fp.readline()
112+
if self.debuglevel > 0:
113+
print "reply:", repr(line)
111114
try:
112115
[version, status, reason] = string.split(line, None, 2)
113116
except ValueError:
@@ -132,6 +135,9 @@ def begin(self):
132135
raise UnknownProtocol(version)
133136

134137
self.msg = mimetools.Message(self.fp, 0)
138+
if self.debuglevel > 0:
139+
for hdr in self.msg.headers:
140+
print "header:", hdr,
135141

136142
# don't let the msg keep an fp
137143
self.msg.fp = None
@@ -187,11 +193,6 @@ def begin(self):
187193
self.length is None:
188194
self.will_close = 1
189195

190-
# if there is no body, then close NOW. read() may never be called, thus
191-
# we will never mark self as closed.
192-
if self.length == 0:
193-
self.close()
194-
195196
def close(self):
196197
if self.fp:
197198
self.fp.close()
@@ -273,12 +274,6 @@ def read(self, amt=None):
273274
# (for example, reading in 1k chunks)
274275
s = self.fp.read(amt)
275276

276-
# close our "file" if we know we should
277-
### I'm not sure about the len(s) < amt part; we should be safe because
278-
### we shouldn't be using non-blocking sockets
279-
if self.length == 0 or len(s) < amt:
280-
self.close()
281-
282277
return s
283278

284279
def _safe_read(self, amt):
@@ -318,6 +313,7 @@ class HTTPConnection:
318313
response_class = HTTPResponse
319314
default_port = HTTP_PORT
320315
auto_open = 1
316+
debuglevel = 0
321317

322318
def __init__(self, host, port=None):
323319
self.sock = None
@@ -337,9 +333,14 @@ def _set_hostport(self, host, port):
337333
self.host = host
338334
self.port = port
339335

336+
def set_debuglevel(self, level):
337+
self.debuglevel = level
338+
340339
def connect(self):
341340
"""Connect to the host and port specified in __init__."""
342341
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
342+
if self.debuglevel > 0:
343+
print "connect: (%s, %s)" % (self.host, self.port)
343344
self.sock.connect((self.host, self.port))
344345

345346
def close(self):
@@ -365,6 +366,8 @@ def send(self, str):
365366
#
366367
# NOTE: we DO propagate the error, though, because we cannot simply
367368
# ignore the error... the caller will know if they can retry.
369+
if self.debuglevel > 0:
370+
print "send:", repr(str)
368371
try:
369372
self.sock.send(str)
370373
except socket.error, v:
@@ -524,7 +527,10 @@ def getresponse(self):
524527
if self.__state != _CS_REQ_SENT or self.__response:
525528
raise ResponseNotReady()
526529

527-
response = self.response_class(self.sock)
530+
if self.debuglevel > 0:
531+
response = self.response_class(self.sock, self.debuglevel)
532+
else:
533+
response = self.response_class(self.sock)
528534

529535
response.begin()
530536
self.__state = _CS_IDLE
@@ -647,8 +653,7 @@ def connect(self, host=None, port=None):
647653
self._conn.connect()
648654

649655
def set_debuglevel(self, debuglevel):
650-
"The class no longer supports the debuglevel."
651-
pass
656+
self._conn.set_debuglevel(debuglevel)
652657

653658
def getfile(self):
654659
"Provide a getfile, since the superclass' does not use this concept."

0 commit comments

Comments
 (0)