1
0
mirror of https://github.com/httpie/cli.git synced 2025-08-10 22:42:05 +02:00

Add support for streamed uploads, --chunked, finish --multipart, etc.

Close #201
Close #753
Close #684
Close #903
Related: #452
This commit is contained in:
Jakub Roztocil
2020-09-28 12:16:57 +02:00
parent b7754f92ce
commit 6925d930da
22 changed files with 498 additions and 328 deletions

View File

@@ -2,7 +2,6 @@ import argparse
import http.client
import json
import sys
import zlib
from contextlib import contextmanager
from pathlib import Path
from typing import Callable, Iterable, Union
@@ -17,7 +16,7 @@ from httpie.plugins.registry import plugin_manager
from httpie.sessions import get_httpie_session
from httpie.ssl import AVAILABLE_SSL_VERSION_ARG_MAPPING, HTTPieHTTPSAdapter
from httpie.uploads import (
wrap_request_data,
compress_request, prepare_request_body,
get_multipart_data_and_content_type,
)
from httpie.utils import get_expired_cookies, repr_dict
@@ -34,7 +33,7 @@ DEFAULT_UA = f'HTTPie/{__version__}'
def collect_messages(
args: argparse.Namespace,
config_dir: Path,
body_chunk_sent_callback: Callable[[bytes], None]=None,
request_body_read_callback: Callable[[bytes], None] = None,
) -> Iterable[Union[requests.PreparedRequest, requests.Response]]:
httpie_session = None
httpie_session_headers = None
@@ -50,7 +49,7 @@ def collect_messages(
request_kwargs = make_request_kwargs(
args=args,
base_headers=httpie_session_headers,
callback=body_chunk_sent_callback
request_body_read_callback=request_body_read_callback
)
send_kwargs = make_send_kwargs(args)
send_kwargs_mergeable_from_env = make_send_kwargs_mergeable_from_env(args)
@@ -85,7 +84,10 @@ def collect_messages(
prepped_url=prepared_request.url,
)
if args.compress and prepared_request.body:
compress_body(prepared_request, always=args.compress > 1)
compress_request(
request=prepared_request,
always=args.compress > 1,
)
response_count = 0
expired_cookies = []
while prepared_request:
@@ -142,23 +144,6 @@ def max_headers(limit):
http.client._MAXHEADERS = orig
def compress_body(request: requests.PreparedRequest, always: bool):
deflater = zlib.compressobj()
if isinstance(request.body, str):
body_bytes = request.body.encode()
elif hasattr(request.body, 'read'):
body_bytes = request.body.read()
else:
body_bytes = request.body
deflated_data = deflater.compress(body_bytes)
deflated_data += deflater.flush()
is_economical = len(deflated_data) < len(body_bytes)
if is_economical or always:
request.body = deflated_data
request.headers['Content-Encoding'] = 'deflate'
request.headers['Content-Length'] = str(len(deflated_data))
def build_requests_session(
verify: bool,
ssl_version: str = None,
@@ -258,7 +243,7 @@ def make_send_kwargs_mergeable_from_env(args: argparse.Namespace) -> dict:
def make_request_kwargs(
args: argparse.Namespace,
base_headers: RequestHeadersDict = None,
callback=lambda chunk: chunk
request_body_read_callback=lambda chunk: chunk
) -> dict:
"""
Translate our `args` into `requests.Request` keyword arguments.
@@ -285,21 +270,23 @@ def make_request_kwargs(
if (args.form and files) or args.multipart:
data, headers['Content-Type'] = get_multipart_data_and_content_type(
data=data,
files=files,
data=args.multipart_data,
boundary=args.boundary,
content_type=args.headers.get('Content-Type'),
)
files = None
kwargs = {
'method': args.method.lower(),
'url': args.url,
'headers': headers,
'data': wrap_request_data(data, callback=callback),
'data': prepare_request_body(
body=data,
body_read_callback=request_body_read_callback,
chunked=args.chunked,
content_length_header_value=headers.get('Content-Length')
),
'auth': args.auth,
'params': args.params,
'files': files,
'params': args.params.items(),
}
return kwargs