2012-07-21 02:59:43 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2013-01-03 14:12:27 +01:00
|
|
|
from .compat import urlsplit, is_windows, bytes, str
|
2012-07-21 02:59:43 +02:00
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
from .config import DEFAULT_CONFIG_DIR, Config
|
|
|
|
|
2012-07-21 02:59:43 +02:00
|
|
|
|
|
|
|
class Environment(object):
|
2012-07-26 06:37:03 +02:00
|
|
|
"""Holds information about the execution context.
|
|
|
|
|
|
|
|
Groups various aspects of the environment in a changeable object
|
|
|
|
and allows for mocking.
|
|
|
|
|
|
|
|
"""
|
2012-07-30 10:58:16 +02:00
|
|
|
|
|
|
|
is_windows = is_windows
|
|
|
|
|
2012-07-27 18:08:33 +02:00
|
|
|
progname = os.path.basename(sys.argv[0])
|
|
|
|
if progname not in ['http', 'https']:
|
|
|
|
progname = 'http'
|
|
|
|
|
2012-07-21 02:59:43 +02:00
|
|
|
stdin_isatty = sys.stdin.isatty()
|
|
|
|
stdin = sys.stdin
|
|
|
|
stdout_isatty = sys.stdout.isatty()
|
2012-08-17 06:35:18 +02:00
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
config_dir = DEFAULT_CONFIG_DIR
|
|
|
|
|
2012-08-17 06:35:18 +02:00
|
|
|
if stdout_isatty and is_windows:
|
|
|
|
from colorama.initialise import wrap_stream
|
|
|
|
stdout = wrap_stream(sys.stdout, convert=None,
|
|
|
|
strip=None, autoreset=True, wrap=True)
|
|
|
|
else:
|
|
|
|
stdout = sys.stdout
|
2012-07-24 01:09:14 +02:00
|
|
|
stderr = sys.stderr
|
|
|
|
|
2012-07-21 02:59:43 +02:00
|
|
|
# Can be set to 0 to disable colors completely.
|
|
|
|
colors = 256 if '256color' in os.environ.get('TERM', '') else 88
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2012-08-06 22:14:52 +02:00
|
|
|
assert all(hasattr(type(self), attr)
|
|
|
|
for attr in kwargs.keys())
|
2012-07-21 02:59:43 +02:00
|
|
|
self.__dict__.update(**kwargs)
|
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
if not hasattr(self, '_config'):
|
2012-09-17 02:15:00 +02:00
|
|
|
self._config = Config(directory=self.config_dir)
|
2012-09-17 00:37:36 +02:00
|
|
|
if self._config.is_new:
|
|
|
|
self._config.save()
|
|
|
|
else:
|
|
|
|
self._config.load()
|
|
|
|
return self._config
|
|
|
|
|
2012-07-21 02:59:43 +02:00
|
|
|
|
|
|
|
class HTTPMessage(object):
|
2012-08-03 01:01:15 +02:00
|
|
|
"""Abstract class for HTTP messages."""
|
2012-07-21 02:59:43 +02:00
|
|
|
|
2012-08-01 21:13:50 +02:00
|
|
|
def __init__(self, orig):
|
|
|
|
self._orig = orig
|
|
|
|
|
2012-08-03 01:01:15 +02: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 21:13:50 +02:00
|
|
|
@property
|
|
|
|
def content_type(self):
|
2012-08-03 01:01:15 +02:00
|
|
|
"""Return the message content type."""
|
|
|
|
ct = self._orig.headers.get('Content-Type', '')
|
|
|
|
if isinstance(ct, bytes):
|
|
|
|
ct = ct.decode()
|
|
|
|
return ct
|
2012-08-01 21:13:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPResponse(HTTPMessage):
|
2012-08-06 22:14:52 +02:00
|
|
|
"""A :class:`requests.models.Response` wrapper."""
|
2012-08-01 21:13:50 +02:00
|
|
|
|
2012-08-03 01:01:15 +02: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 01:07:01 +02:00
|
|
|
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))
|
2012-08-01 23:21:52 +02:00
|
|
|
|
2012-08-01 21:13:50 +02:00
|
|
|
@property
|
2012-08-03 01:01:15 +02:00
|
|
|
def headers(self):
|
2012-08-01 21:13:50 +02:00
|
|
|
original = self._orig.raw._original_response
|
2012-08-03 01:01:15 +02:00
|
|
|
status_line = 'HTTP/{version} {status} {reason}'.format(
|
2012-08-21 15:45:22 +02:00
|
|
|
version='.'.join(str(original.version)),
|
|
|
|
status=original.status,
|
|
|
|
reason=original.reason
|
2012-08-10 01:07:01 +02: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 05:45:44 +02:00
|
|
|
|
2012-08-01 21:13:50 +02:00
|
|
|
@property
|
|
|
|
def encoding(self):
|
|
|
|
return self._orig.encoding or 'utf8'
|
2012-07-21 02:59:43 +02:00
|
|
|
|
2012-08-01 21:13:50 +02: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 22:14:52 +02:00
|
|
|
"""A :class:`requests.models.Request` wrapper."""
|
2012-08-01 21:13:50 +02:00
|
|
|
|
2012-08-03 01:01:15 +02:00
|
|
|
def iter_body(self, chunk_size):
|
2012-08-01 23:21:52 +02:00
|
|
|
yield self.body
|
|
|
|
|
2012-08-03 01:01:15 +02:00
|
|
|
def iter_lines(self, chunk_size):
|
|
|
|
yield self.body, b''
|
|
|
|
|
2012-08-01 21:13:50 +02:00
|
|
|
@property
|
2012-08-03 01:01:15 +02:00
|
|
|
def headers(self):
|
2013-01-03 14:12:27 +01:00
|
|
|
url = urlsplit(self._orig.url)
|
2012-07-21 02:59:43 +02:00
|
|
|
|
2012-07-25 14:32:57 +02:00
|
|
|
# Querystring
|
|
|
|
qs = ''
|
2012-08-01 21:13:50 +02:00
|
|
|
if url.query or self._orig.params:
|
2012-07-25 14:32:57 +02:00
|
|
|
qs = '?'
|
|
|
|
if url.query:
|
|
|
|
qs += url.query
|
|
|
|
# Requests doesn't make params part of ``request.url``.
|
2012-08-01 21:13:50 +02:00
|
|
|
if self._orig.params:
|
2012-07-25 14:32:57 +02:00
|
|
|
if url.query:
|
|
|
|
qs += '&'
|
2012-08-01 21:13:50 +02:00
|
|
|
qs += type(self._orig)._encode_params(self._orig.params)
|
2012-07-25 14:32:57 +02:00
|
|
|
|
|
|
|
# Request-Line
|
2012-08-03 01:01:15 +02:00
|
|
|
request_line = '{method} {path}{query} HTTP/1.1'.format(
|
2012-08-01 21:13:50 +02:00
|
|
|
method=self._orig.method,
|
2012-07-21 02:59:43 +02:00
|
|
|
path=url.path or '/',
|
2012-07-25 14:32:57 +02:00
|
|
|
query=qs
|
2012-08-03 01:01:15 +02:00
|
|
|
)
|
2012-07-25 14:32:57 +02:00
|
|
|
|
2012-08-01 21:13:50 +02:00
|
|
|
headers = dict(self._orig.headers)
|
2012-07-28 05:45:44 +02:00
|
|
|
|
2012-07-25 14:32:57 +02:00
|
|
|
if 'Host' not in headers:
|
2013-01-03 14:12:27 +01:00
|
|
|
headers['Host'] = urlsplit(self._orig.url).netloc
|
2012-08-01 21:13:50 +02:00
|
|
|
|
2012-08-03 01:01:15 +02:00
|
|
|
headers = ['%s: %s' % (name, value)
|
|
|
|
for name, value in headers.items()]
|
|
|
|
|
|
|
|
headers.insert(0, request_line)
|
|
|
|
|
2012-08-10 01:07:01 +02:00
|
|
|
return '\r\n'.join(headers).strip()
|
2012-08-01 21:13:50 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def encoding(self):
|
|
|
|
return 'utf8'
|
2012-07-25 14:32:57 +02:00
|
|
|
|
2012-08-01 21:13:50 +02:00
|
|
|
@property
|
|
|
|
def body(self):
|
2012-12-19 11:34:30 +01:00
|
|
|
return self._orig.body or b''
|