Skip to content

Commit

Permalink
Changed it so one can access the StringIO buffer
Browse files Browse the repository at this point in the history
This allows for people to use the file-like object
that already exists without having to convert the body
back to the StringIO
  • Loading branch information
mikelewis authored and Ben Darnell committed May 31, 2010
1 parent 6f6c950 commit d5a3d8a
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions tornado/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ def fetch(self, request, **kwargs):
if code < 200 or code >= 300:
raise HTTPError(code)
effective_url = self._curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
return HTTPResponse(
request=request, code=code, headers=headers,
body=buffer.getvalue(), effective_url=effective_url)
buffer=buffer, effective_url=effective_url)
except pycurl.error, e:
raise CurlError(*e)
finally:
buffer.close()
raise CurlError(*e)


class AsyncHTTPClient(object):
Expand Down Expand Up @@ -273,20 +273,22 @@ def _finish(self, curl, curl_error=None, curl_message=None):
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
body = None
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
body = info["buffer"].getvalue()
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
info["buffer"].close()
buffer.seek(0)
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
body=body, effective_url=effective_url, error=error,
buffer=buffer, effective_url=effective_url, error=error,
request_time=time.time() - info["start_time"]))


Expand Down Expand Up @@ -323,12 +325,13 @@ def __init__(self, url, method="GET", headers={}, body=None,


class HTTPResponse(object):
def __init__(self, request, code, headers={}, body="", effective_url=None,
def __init__(self, request, code, headers={}, buffer=None, effective_url=None,
error=None, request_time=None):
self.request = request
self.code = code
self.headers = headers
self.body = body
self.buffer = buffer
self._body = None
if effective_url is None:
self.effective_url = request.url
else:
Expand All @@ -342,6 +345,16 @@ def __init__(self, request, code, headers={}, body="", effective_url=None,
self.error = error
self.request_time = request_time

def _get_body(self):
if self.buffer is None:
return None
elif self._body is None:
self._body = self.buffer.getvalue()

return self._body

body = property(_get_body)

def rethrow(self):
if self.error:
raise self.error
Expand All @@ -350,6 +363,10 @@ def __repr__(self):
args = ",".join("%s=%r" % i for i in self.__dict__.iteritems())
return "%s(%s)" % (self.__class__.__name__, args)

def __del__(self):
if self.buffer is not None:
self.buffer.close()


class HTTPError(Exception):
def __init__(self, code, message=None):
Expand Down

0 comments on commit d5a3d8a

Please sign in to comment.