2012-04-25 02:32:53 +03:00
|
|
|
from requests.compat import urlparse
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPMessage(object):
|
|
|
|
"""Model representing an HTTP message."""
|
|
|
|
|
|
|
|
def __init__(self, line, headers, body, content_type=None):
|
|
|
|
# {Request,Status}-Line
|
|
|
|
self.line = line
|
|
|
|
self.headers = headers
|
|
|
|
self.body = body
|
|
|
|
self.content_type = content_type
|
|
|
|
|
|
|
|
|
|
|
|
def from_request(request):
|
|
|
|
"""Make an `HTTPMessage` from `requests.models.Request`."""
|
|
|
|
url = urlparse(request.url)
|
|
|
|
request_headers = dict(request.headers)
|
|
|
|
if 'Host' not in request_headers:
|
|
|
|
request_headers['Host'] = url.netloc
|
2012-06-13 16:25:05 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
body = request.data
|
|
|
|
except AttributeError:
|
|
|
|
# requests < 0.12.1
|
|
|
|
body = request._enc_data
|
|
|
|
|
2012-04-25 02:32:53 +03:00
|
|
|
return HTTPMessage(
|
|
|
|
line='{method} {path} HTTP/1.1'.format(
|
|
|
|
method=request.method,
|
|
|
|
path=url.path or '/'),
|
|
|
|
headers='\n'.join(str('%s: %s') % (name, value)
|
|
|
|
for name, value
|
|
|
|
in request_headers.items()),
|
2012-06-13 16:25:05 +03:00
|
|
|
body=body,
|
2012-04-25 02:32:53 +03:00
|
|
|
content_type=request_headers.get('Content-Type')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def from_response(response):
|
|
|
|
"""Make an `HTTPMessage` from `requests.models.Response`."""
|
|
|
|
encoding = response.encoding or 'ISO-8859-1'
|
|
|
|
original = response.raw._original_response
|
|
|
|
response_headers = response.headers
|
|
|
|
return HTTPMessage(
|
|
|
|
line='HTTP/{version} {status} {reason}'.format(
|
|
|
|
version='.'.join(str(original.version)),
|
2012-04-26 15:48:38 +03:00
|
|
|
status=original.status, reason=original.reason),
|
2012-04-25 02:32:53 +03:00
|
|
|
headers=str(original.msg),
|
|
|
|
body=response.content.decode(encoding) if response.content else '',
|
|
|
|
content_type=response_headers.get('Content-Type'))
|
|
|
|
|
|
|
|
|
2012-04-28 15:13:40 +03:00
|
|
|
def format(message, prettifier=None,
|
2012-04-25 02:32:53 +03:00
|
|
|
with_headers=True, with_body=True):
|
|
|
|
"""Return a `unicode` representation of `message`. """
|
2012-04-28 15:13:40 +03:00
|
|
|
pretty = prettifier is not None
|
2012-04-25 02:32:53 +03:00
|
|
|
bits = []
|
2012-04-28 15:13:40 +03:00
|
|
|
|
2012-04-25 02:32:53 +03:00
|
|
|
if with_headers:
|
2012-04-26 15:48:38 +03:00
|
|
|
bits.append(message.line)
|
|
|
|
bits.append(message.headers)
|
2012-04-28 15:13:40 +03:00
|
|
|
if pretty:
|
|
|
|
bits = [prettifier.headers('\n'.join(bits))]
|
2012-04-25 02:32:53 +03:00
|
|
|
if with_body and message.body:
|
|
|
|
bits.append('\n')
|
2012-04-28 15:13:40 +03:00
|
|
|
|
2012-04-25 02:32:53 +03:00
|
|
|
if with_body and message.body:
|
2012-04-28 15:13:40 +03:00
|
|
|
if pretty and message.content_type:
|
|
|
|
bits.append(prettifier.body(message.body, message.content_type))
|
2012-04-25 02:32:53 +03:00
|
|
|
else:
|
|
|
|
bits.append(message.body)
|
2012-04-26 15:48:38 +03:00
|
|
|
|
2012-04-28 15:13:40 +03:00
|
|
|
return '\n'.join(bit.strip() for bit in bits)
|