2019-08-31 17:52:56 +02:00
|
|
|
import argparse
|
|
|
|
import http.client
|
2012-08-18 00:23:02 +03:00
|
|
|
import json
|
|
|
|
import sys
|
2019-08-31 17:52:56 +02:00
|
|
|
import zlib
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from pathlib import Path
|
2012-08-18 00:23:02 +03:00
|
|
|
|
|
|
|
import requests
|
2016-03-02 06:12:05 +02:00
|
|
|
from requests.adapters import HTTPAdapter
|
2012-08-18 00:23:02 +03:00
|
|
|
|
2019-08-31 17:52:56 +02:00
|
|
|
from httpie import __version__, sessions
|
2019-08-31 15:17:10 +02:00
|
|
|
from httpie.cli.constants import SSL_VERSION_ARG_MAPPING
|
2019-08-31 17:52:56 +02:00
|
|
|
from httpie.cli.dicts import RequestHeadersDict
|
2014-04-29 00:33:30 +03:00
|
|
|
from httpie.plugins import plugin_manager
|
2019-08-31 18:00:03 +02:00
|
|
|
from httpie.utils import repr_dict
|
2012-08-18 00:23:02 +03:00
|
|
|
|
2019-08-29 10:44:59 +02:00
|
|
|
|
2015-12-02 19:50:48 +02:00
|
|
|
try:
|
2017-12-28 19:32:12 +02:00
|
|
|
# noinspection PyPackageRequirements
|
|
|
|
import urllib3
|
2019-09-01 11:38:14 +02:00
|
|
|
# <https://urllib3.readthedocs.io/en/latest/security.html>
|
2015-12-02 19:50:48 +02:00
|
|
|
urllib3.disable_warnings()
|
2017-12-28 19:32:12 +02:00
|
|
|
except (ImportError, AttributeError):
|
2015-12-02 19:50:48 +02:00
|
|
|
pass
|
2015-02-16 20:36:02 +02:00
|
|
|
|
2016-07-02 14:18:36 +02:00
|
|
|
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8'
|
|
|
|
JSON_CONTENT_TYPE = 'application/json'
|
2019-08-31 15:17:10 +02:00
|
|
|
JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*'
|
2019-08-31 17:52:56 +02:00
|
|
|
DEFAULT_UA = f'HTTPie/{__version__}'
|
2012-08-18 00:23:02 +03:00
|
|
|
|
|
|
|
|
2019-08-29 09:39:19 +02:00
|
|
|
# noinspection PyProtectedMember
|
|
|
|
@contextmanager
|
|
|
|
def max_headers(limit):
|
|
|
|
# <https://github.com/jakubroztocil/httpie/issues/802>
|
|
|
|
orig = http.client._MAXHEADERS
|
|
|
|
http.client._MAXHEADERS = limit or float('Inf')
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
http.client._MAXHEADERS = orig
|
|
|
|
|
|
|
|
|
2016-03-02 06:12:05 +02:00
|
|
|
class HTTPieHTTPAdapter(HTTPAdapter):
|
|
|
|
|
2019-09-01 11:38:14 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
ssl_version=None,
|
|
|
|
compression_enabled=False,
|
|
|
|
compress_always=False,
|
|
|
|
**kwargs,
|
|
|
|
):
|
2016-03-02 06:12:05 +02:00
|
|
|
self._ssl_version = ssl_version
|
2019-09-01 11:38:14 +02:00
|
|
|
self._compression_enabled = compression_enabled
|
|
|
|
self._compress_always = compress_always
|
2019-08-30 11:32:14 +02:00
|
|
|
super().__init__(**kwargs)
|
2016-03-02 06:12:05 +02:00
|
|
|
|
|
|
|
def init_poolmanager(self, *args, **kwargs):
|
|
|
|
kwargs['ssl_version'] = self._ssl_version
|
2019-08-30 11:32:14 +02:00
|
|
|
super().init_poolmanager(*args, **kwargs)
|
2016-03-02 06:12:05 +02:00
|
|
|
|
2019-08-31 17:52:56 +02:00
|
|
|
def send(self, request: requests.PreparedRequest, **kwargs):
|
2019-09-01 11:38:14 +02:00
|
|
|
if request.body and self._compression_enabled:
|
|
|
|
self._compress_body(request, always=self._compress_always)
|
2019-08-30 11:32:14 +02:00
|
|
|
return super().send(request, **kwargs)
|
2019-08-29 10:44:59 +02:00
|
|
|
|
2019-08-31 17:52:56 +02:00
|
|
|
@staticmethod
|
2019-09-01 11:38:14 +02:00
|
|
|
def _compress_body(request: requests.PreparedRequest, always: bool):
|
2019-08-31 17:52:56 +02:00
|
|
|
deflater = zlib.compressobj()
|
2019-09-01 11:38:14 +02:00
|
|
|
body_bytes = (
|
|
|
|
request.body
|
|
|
|
if isinstance(request.body, bytes)
|
|
|
|
else request.body.encode()
|
|
|
|
)
|
|
|
|
deflated_data = deflater.compress(body_bytes)
|
2019-08-31 17:52:56 +02:00
|
|
|
deflated_data += deflater.flush()
|
2019-09-01 11:38:14 +02:00
|
|
|
is_economical = len(deflated_data) < len(body_bytes)
|
|
|
|
if is_economical or always:
|
2019-08-31 17:52:56 +02:00
|
|
|
request.body = deflated_data
|
|
|
|
request.headers['Content-Encoding'] = 'deflate'
|
|
|
|
request.headers['Content-Length'] = str(len(deflated_data))
|
|
|
|
|
2019-08-29 10:44:59 +02:00
|
|
|
|
2019-09-01 11:38:14 +02:00
|
|
|
def build_requests_session(
|
|
|
|
ssl_version: str,
|
|
|
|
compress_arg: int,
|
|
|
|
) -> requests.Session:
|
2015-02-05 16:25:00 +02:00
|
|
|
requests_session = requests.Session()
|
2019-09-01 11:38:14 +02:00
|
|
|
|
|
|
|
# Install our adapter.
|
|
|
|
adapter = HTTPieHTTPAdapter(
|
|
|
|
ssl_version=ssl_version,
|
|
|
|
compression_enabled=compress_arg > 0,
|
|
|
|
compress_always=compress_arg > 1,
|
|
|
|
)
|
|
|
|
requests_session.mount('http://', adapter)
|
|
|
|
requests_session.mount('https://', adapter)
|
|
|
|
|
|
|
|
# Install adapters from plugins.
|
|
|
|
for plugin_cls in plugin_manager.get_transport_plugins():
|
|
|
|
transport_plugin = plugin_cls()
|
|
|
|
requests_session.mount(
|
|
|
|
prefix=transport_plugin.prefix,
|
|
|
|
adapter=transport_plugin.get_adapter(),
|
|
|
|
)
|
|
|
|
|
2015-02-05 16:25:00 +02:00
|
|
|
return requests_session
|
|
|
|
|
|
|
|
|
2019-08-31 17:52:56 +02:00
|
|
|
def get_response(
|
|
|
|
args: argparse.Namespace,
|
|
|
|
config_dir: Path
|
|
|
|
) -> requests.Response:
|
2012-09-07 13:38:52 +03:00
|
|
|
"""Send the request and return a `request.Response`."""
|
2016-03-02 06:12:05 +02:00
|
|
|
|
|
|
|
ssl_version = None
|
|
|
|
if args.ssl_version:
|
|
|
|
ssl_version = SSL_VERSION_ARG_MAPPING[args.ssl_version]
|
|
|
|
|
2019-09-01 11:38:14 +02:00
|
|
|
requests_session = build_requests_session(
|
|
|
|
ssl_version=ssl_version,
|
|
|
|
compress_arg=args.compress
|
|
|
|
)
|
2016-02-29 08:21:25 +02:00
|
|
|
requests_session.max_redirects = args.max_redirects
|
2015-02-05 16:25:00 +02:00
|
|
|
|
2019-08-29 09:39:19 +02:00
|
|
|
with max_headers(args.max_headers):
|
|
|
|
if not args.session and not args.session_read_only:
|
2019-09-01 11:38:14 +02:00
|
|
|
kwargs = make_requests_kwargs(args)
|
2019-08-29 09:39:19 +02:00
|
|
|
if args.debug:
|
|
|
|
dump_request(kwargs)
|
|
|
|
response = requests_session.request(**kwargs)
|
|
|
|
else:
|
|
|
|
response = sessions.get_response(
|
|
|
|
requests_session=requests_session,
|
|
|
|
args=args,
|
|
|
|
config_dir=config_dir,
|
|
|
|
session_name=args.session or args.session_read_only,
|
|
|
|
read_only=bool(args.session_read_only),
|
|
|
|
)
|
2012-08-18 00:23:02 +03:00
|
|
|
|
2012-12-01 17:55:58 +03:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-08-31 17:52:56 +02:00
|
|
|
def dump_request(kwargs: dict):
|
2019-08-31 18:00:03 +02:00
|
|
|
sys.stderr.write(
|
|
|
|
f'\n>>> requests.request(**{repr_dict(kwargs)})\n\n')
|
2014-05-08 14:27:50 +03:00
|
|
|
|
|
|
|
|
2019-08-31 17:52:56 +02:00
|
|
|
def finalize_headers(headers: RequestHeadersDict) -> RequestHeadersDict:
|
|
|
|
final_headers = RequestHeadersDict()
|
2016-08-13 22:40:01 +02:00
|
|
|
for name, value in headers.items():
|
|
|
|
if value is not None:
|
|
|
|
# >leading or trailing LWS MAY be removed without
|
|
|
|
# >changing the semantics of the field value"
|
|
|
|
# -https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
|
|
|
|
# Also, requests raises `InvalidHeader` for leading spaces.
|
|
|
|
value = value.strip()
|
|
|
|
if isinstance(value, str):
|
2017-03-10 12:27:38 +02:00
|
|
|
# See: https://github.com/jakubroztocil/httpie/issues/212
|
2016-08-13 22:40:01 +02:00
|
|
|
value = value.encode('utf8')
|
|
|
|
final_headers[name] = value
|
|
|
|
return final_headers
|
2014-04-26 19:16:30 +03:00
|
|
|
|
|
|
|
|
2019-09-01 11:38:14 +02:00
|
|
|
def make_default_headers(args: argparse.Namespace) -> RequestHeadersDict:
|
2019-08-31 17:52:56 +02:00
|
|
|
default_headers = RequestHeadersDict({
|
2012-12-17 19:02:27 +03:00
|
|
|
'User-Agent': DEFAULT_UA
|
2018-02-22 13:52:57 +02:00
|
|
|
})
|
2012-08-19 00:03:31 +03:00
|
|
|
|
2012-08-18 00:23:02 +03:00
|
|
|
auto_json = args.data and not args.form
|
|
|
|
if args.json or auto_json:
|
2016-07-02 14:18:36 +02:00
|
|
|
default_headers['Accept'] = JSON_ACCEPT
|
2013-04-02 17:07:14 +03:00
|
|
|
if args.json or (auto_json and args.data):
|
2016-07-02 14:18:36 +02:00
|
|
|
default_headers['Content-Type'] = JSON_CONTENT_TYPE
|
2012-08-18 00:23:02 +03:00
|
|
|
|
2012-08-19 00:03:31 +03:00
|
|
|
elif args.form and not args.files:
|
2012-09-07 13:48:59 +03:00
|
|
|
# If sending files, `requests` will set
|
|
|
|
# the `Content-Type` for us.
|
2016-07-02 14:18:36 +02:00
|
|
|
default_headers['Content-Type'] = FORM_CONTENT_TYPE
|
2014-05-08 14:27:50 +03:00
|
|
|
return default_headers
|
|
|
|
|
|
|
|
|
2019-09-01 11:38:14 +02:00
|
|
|
def make_requests_kwargs(args: argparse.Namespace, base_headers=None) -> dict:
|
2014-05-08 14:27:50 +03:00
|
|
|
"""
|
|
|
|
Translate our `args` into `requests.request` keyword arguments.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Serialize JSON data, if needed.
|
|
|
|
data = args.data
|
|
|
|
auto_json = data and not args.form
|
2015-04-11 03:11:22 +02:00
|
|
|
if (args.json or auto_json) and isinstance(data, dict):
|
2014-05-08 14:27:50 +03:00
|
|
|
if data:
|
|
|
|
data = json.dumps(data)
|
|
|
|
else:
|
|
|
|
# We need to set data to an empty string to prevent requests
|
|
|
|
# from assigning an empty list to `response.request.data`.
|
|
|
|
data = ''
|
|
|
|
|
|
|
|
# Finalize headers.
|
2019-09-01 11:38:14 +02:00
|
|
|
headers = make_default_headers(args)
|
2014-05-08 14:27:50 +03:00
|
|
|
if base_headers:
|
|
|
|
headers.update(base_headers)
|
|
|
|
headers.update(args.headers)
|
2016-08-13 22:40:01 +02:00
|
|
|
headers = finalize_headers(headers)
|
2012-08-18 00:23:02 +03:00
|
|
|
|
2014-01-28 18:16:48 +03:00
|
|
|
cert = None
|
2014-02-05 14:50:40 +03:00
|
|
|
if args.cert:
|
|
|
|
cert = args.cert
|
2015-01-24 00:54:27 +02:00
|
|
|
if args.cert_key:
|
|
|
|
cert = cert, args.cert_key
|
2014-01-28 18:16:48 +03:00
|
|
|
|
2012-08-18 00:23:02 +03:00
|
|
|
kwargs = {
|
2012-12-17 19:02:27 +03:00
|
|
|
'stream': True,
|
2012-08-18 00:23:02 +03:00
|
|
|
'method': args.method.lower(),
|
|
|
|
'url': args.url,
|
2014-05-08 14:27:50 +03:00
|
|
|
'headers': headers,
|
|
|
|
'data': data,
|
2012-08-18 00:23:02 +03:00
|
|
|
'verify': {
|
|
|
|
'yes': True,
|
2017-02-10 02:51:23 +02:00
|
|
|
'true': True,
|
|
|
|
'no': False,
|
|
|
|
'false': False,
|
|
|
|
}.get(args.verify.lower(), args.verify),
|
2014-01-28 18:16:48 +03:00
|
|
|
'cert': cert,
|
2019-08-29 10:04:49 +02:00
|
|
|
'timeout': args.timeout or None,
|
2016-11-23 23:01:58 +02:00
|
|
|
'auth': args.auth,
|
2017-12-28 19:15:17 +02:00
|
|
|
'proxies': {p.key: p.value for p in args.proxy},
|
2012-08-18 00:23:02 +03:00
|
|
|
'files': args.files,
|
2012-09-07 12:58:39 +03:00
|
|
|
'allow_redirects': args.follow,
|
2012-08-19 00:03:31 +03:00
|
|
|
'params': args.params,
|
2012-08-18 00:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return kwargs
|