2014-04-29 00:33:30 +03:00
|
|
|
from httpie.compat import urlsplit, str
|
2012-09-17 01:37:36 +03:00
|
|
|
|
2012-07-21 03:59:43 +03:00
|
|
|
|
|
|
|
class HTTPMessage(object):
|
2012-08-03 02:01:15 +03:00
|
|
|
"""Abstract class for HTTP messages."""
|
2012-07-21 03:59:43 +03:00
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
def __init__(self, orig):
|
|
|
|
self._orig = orig
|
|
|
|
|
2012-08-03 02:01:15 +03:00
|
|
|
def iter_body(self, chunk_size):
|
|
|
|
"""Return an iterator over the body."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def iter_lines(self, chunk_size):
|
|
|
|
"""Return an iterator over the body yielding (`line`, `line_feed`)."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def headers(self):
|
|
|
|
"""Return a `str` with the message's headers."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def encoding(self):
|
|
|
|
"""Return a `str` with the message's encoding, if known."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def body(self):
|
|
|
|
"""Return a `bytes` with the message's body."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
@property
|
|
|
|
def content_type(self):
|
2012-08-03 02:01:15 +03:00
|
|
|
"""Return the message content type."""
|
2014-04-26 18:16:11 +03:00
|
|
|
ct = self._orig.headers.get('Content-Type', '')
|
|
|
|
if not isinstance(ct, str):
|
|
|
|
ct = ct.decode('utf8')
|
|
|
|
return ct
|
2012-08-01 22:13:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPResponse(HTTPMessage):
|
2012-08-06 23:14:52 +03:00
|
|
|
"""A :class:`requests.models.Response` wrapper."""
|
2012-08-01 22:13:50 +03:00
|
|
|
|
2012-08-03 02:01:15 +03:00
|
|
|
def iter_body(self, chunk_size=1):
|
|
|
|
return self._orig.iter_content(chunk_size=chunk_size)
|
|
|
|
|
|
|
|
def iter_lines(self, chunk_size):
|
2012-08-10 02:07:01 +03:00
|
|
|
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))
|
2012-08-02 00:21:52 +03:00
|
|
|
|
2013-08-18 01:59:10 +03:00
|
|
|
#noinspection PyProtectedMember
|
2012-08-01 22:13:50 +03:00
|
|
|
@property
|
2012-08-03 02:01:15 +03:00
|
|
|
def headers(self):
|
2012-08-01 22:13:50 +03:00
|
|
|
original = self._orig.raw._original_response
|
2015-02-06 22:13:57 +02:00
|
|
|
version = {9: '0.9', 10: '1.0', 11: '1.1', 20: '2.0'}[original.version]
|
2012-08-03 02:01:15 +03:00
|
|
|
status_line = 'HTTP/{version} {status} {reason}'.format(
|
2014-04-28 01:08:20 +03:00
|
|
|
version=version,
|
2012-08-21 16:45:22 +03:00
|
|
|
status=original.status,
|
|
|
|
reason=original.reason
|
2012-08-10 02:07:01 +03:00
|
|
|
)
|
|
|
|
headers = [status_line]
|
|
|
|
try:
|
|
|
|
# `original.msg` is a `http.client.HTTPMessage` on Python 3
|
|
|
|
# `_headers` is a 2-tuple
|
|
|
|
headers.extend(
|
|
|
|
'%s: %s' % header for header in original.msg._headers)
|
|
|
|
except AttributeError:
|
|
|
|
# and a `httplib.HTTPMessage` on Python 2.x
|
|
|
|
# `headers` is a list of `name: val<CRLF>`.
|
|
|
|
headers.extend(h.strip() for h in original.msg.headers)
|
|
|
|
|
|
|
|
return '\r\n'.join(headers)
|
2012-07-28 06:45:44 +03:00
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
@property
|
|
|
|
def encoding(self):
|
|
|
|
return self._orig.encoding or 'utf8'
|
2012-07-21 03:59:43 +03:00
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
@property
|
|
|
|
def body(self):
|
|
|
|
# Only now the response body is fetched.
|
|
|
|
# Shouldn't be touched unless the body is actually needed.
|
|
|
|
return self._orig.content
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPRequest(HTTPMessage):
|
2012-08-06 23:14:52 +03:00
|
|
|
"""A :class:`requests.models.Request` wrapper."""
|
2012-08-01 22:13:50 +03:00
|
|
|
|
2012-08-03 02:01:15 +03:00
|
|
|
def iter_body(self, chunk_size):
|
2012-08-02 00:21:52 +03:00
|
|
|
yield self.body
|
|
|
|
|
2012-08-03 02:01:15 +03:00
|
|
|
def iter_lines(self, chunk_size):
|
|
|
|
yield self.body, b''
|
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
@property
|
2012-08-03 02:01:15 +03:00
|
|
|
def headers(self):
|
2013-01-03 16:12:27 +03:00
|
|
|
url = urlsplit(self._orig.url)
|
2012-07-21 03:59:43 +03:00
|
|
|
|
2012-08-03 02:01:15 +03:00
|
|
|
request_line = '{method} {path}{query} HTTP/1.1'.format(
|
2012-08-01 22:13:50 +03:00
|
|
|
method=self._orig.method,
|
2012-07-21 03:59:43 +03:00
|
|
|
path=url.path or '/',
|
2013-01-03 16:42:17 +03:00
|
|
|
query='?' + url.query if url.query else ''
|
2012-08-03 02:01:15 +03:00
|
|
|
)
|
2012-07-25 15:32:57 +03:00
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
headers = dict(self._orig.headers)
|
2014-06-28 14:24:14 +03:00
|
|
|
if 'Host' not in self._orig.headers:
|
2014-01-06 21:39:11 +03:00
|
|
|
headers['Host'] = url.netloc.split('@')[-1]
|
2012-08-01 22:13:50 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
headers = [
|
|
|
|
'%s: %s' % (
|
|
|
|
name,
|
|
|
|
value if isinstance(value, str) else value.decode('utf8')
|
|
|
|
)
|
|
|
|
for name, value in headers.items()
|
|
|
|
]
|
2012-08-03 02:01:15 +03:00
|
|
|
|
|
|
|
headers.insert(0, request_line)
|
2014-04-26 21:10:15 +03:00
|
|
|
headers = '\r\n'.join(headers).strip()
|
2012-08-03 02:01:15 +03:00
|
|
|
|
2014-04-26 21:10:15 +03:00
|
|
|
if isinstance(headers, bytes):
|
|
|
|
# Python < 3
|
|
|
|
headers = headers.decode('utf8')
|
|
|
|
return headers
|
2012-08-01 22:13:50 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def encoding(self):
|
|
|
|
return 'utf8'
|
2012-07-25 15:32:57 +03:00
|
|
|
|
2012-08-01 22:13:50 +03:00
|
|
|
@property
|
|
|
|
def body(self):
|
2013-01-03 16:54:34 +03:00
|
|
|
body = self._orig.body
|
|
|
|
if isinstance(body, str):
|
|
|
|
# Happens with JSON/form request data parsed from the command line.
|
|
|
|
body = body.encode('utf8')
|
|
|
|
return body or b''
|