Commit 30f86742 authored by Jeremy Hylton's avatar Jeremy Hylton

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.
parent 69cc7153
...@@ -87,8 +87,9 @@ _CS_REQ_SENT = 'Request-sent' ...@@ -87,8 +87,9 @@ _CS_REQ_SENT = 'Request-sent'
class HTTPResponse: class HTTPResponse:
def __init__(self, sock): def __init__(self, sock, debuglevel=0):
self.fp = sock.makefile('rb', 0) self.fp = sock.makefile('rb', 0)
self.debuglevel = debuglevel
self.msg = None self.msg = None
...@@ -108,6 +109,8 @@ class HTTPResponse: ...@@ -108,6 +109,8 @@ class HTTPResponse:
return return
line = self.fp.readline() line = self.fp.readline()
if self.debuglevel > 0:
print "reply:", repr(line)
try: try:
[version, status, reason] = string.split(line, None, 2) [version, status, reason] = string.split(line, None, 2)
except ValueError: except ValueError:
...@@ -132,6 +135,9 @@ class HTTPResponse: ...@@ -132,6 +135,9 @@ class HTTPResponse:
raise UnknownProtocol(version) raise UnknownProtocol(version)
self.msg = mimetools.Message(self.fp, 0) self.msg = mimetools.Message(self.fp, 0)
if self.debuglevel > 0:
for hdr in self.msg.headers:
print "header:", hdr,
# don't let the msg keep an fp # don't let the msg keep an fp
self.msg.fp = None self.msg.fp = None
...@@ -187,11 +193,6 @@ class HTTPResponse: ...@@ -187,11 +193,6 @@ class HTTPResponse:
self.length is None: self.length is None:
self.will_close = 1 self.will_close = 1
# if there is no body, then close NOW. read() may never be called, thus
# we will never mark self as closed.
if self.length == 0:
self.close()
def close(self): def close(self):
if self.fp: if self.fp:
self.fp.close() self.fp.close()
...@@ -273,12 +274,6 @@ class HTTPResponse: ...@@ -273,12 +274,6 @@ class HTTPResponse:
# (for example, reading in 1k chunks) # (for example, reading in 1k chunks)
s = self.fp.read(amt) s = self.fp.read(amt)
# close our "file" if we know we should
### I'm not sure about the len(s) < amt part; we should be safe because
### we shouldn't be using non-blocking sockets
if self.length == 0 or len(s) < amt:
self.close()
return s return s
def _safe_read(self, amt): def _safe_read(self, amt):
...@@ -318,6 +313,7 @@ class HTTPConnection: ...@@ -318,6 +313,7 @@ class HTTPConnection:
response_class = HTTPResponse response_class = HTTPResponse
default_port = HTTP_PORT default_port = HTTP_PORT
auto_open = 1 auto_open = 1
debuglevel = 0
def __init__(self, host, port=None): def __init__(self, host, port=None):
self.sock = None self.sock = None
...@@ -337,9 +333,14 @@ class HTTPConnection: ...@@ -337,9 +333,14 @@ class HTTPConnection:
self.host = host self.host = host
self.port = port self.port = port
def set_debuglevel(self, level):
self.debuglevel = level
def connect(self): def connect(self):
"""Connect to the host and port specified in __init__.""" """Connect to the host and port specified in __init__."""
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.debuglevel > 0:
print "connect: (%s, %s)" % (self.host, self.port)
self.sock.connect((self.host, self.port)) self.sock.connect((self.host, self.port))
def close(self): def close(self):
...@@ -365,6 +366,8 @@ class HTTPConnection: ...@@ -365,6 +366,8 @@ class HTTPConnection:
# #
# NOTE: we DO propagate the error, though, because we cannot simply # NOTE: we DO propagate the error, though, because we cannot simply
# ignore the error... the caller will know if they can retry. # ignore the error... the caller will know if they can retry.
if self.debuglevel > 0:
print "send:", repr(str)
try: try:
self.sock.send(str) self.sock.send(str)
except socket.error, v: except socket.error, v:
...@@ -524,7 +527,10 @@ class HTTPConnection: ...@@ -524,7 +527,10 @@ class HTTPConnection:
if self.__state != _CS_REQ_SENT or self.__response: if self.__state != _CS_REQ_SENT or self.__response:
raise ResponseNotReady() raise ResponseNotReady()
response = self.response_class(self.sock) if self.debuglevel > 0:
response = self.response_class(self.sock, self.debuglevel)
else:
response = self.response_class(self.sock)
response.begin() response.begin()
self.__state = _CS_IDLE self.__state = _CS_IDLE
...@@ -647,8 +653,7 @@ class HTTP: ...@@ -647,8 +653,7 @@ class HTTP:
self._conn.connect() self._conn.connect()
def set_debuglevel(self, debuglevel): def set_debuglevel(self, debuglevel):
"The class no longer supports the debuglevel." self._conn.set_debuglevel(debuglevel)
pass
def getfile(self): def getfile(self):
"Provide a getfile, since the superclass' does not use this concept." "Provide a getfile, since the superclass' does not use this concept."
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment