2019-09-03 17:14:39 +02:00
|
|
|
import errno
|
2022-03-07 23:34:04 +01:00
|
|
|
import requests
|
|
|
|
from typing import Any, Dict, IO, Optional, TextIO, Tuple, Type, Union
|
2019-09-03 17:14:39 +02:00
|
|
|
|
2021-12-08 16:49:12 +01:00
|
|
|
from ..cli.dicts import HTTPHeadersDict
|
2021-05-05 14:13:39 +02:00
|
|
|
from ..context import Environment
|
2021-11-25 00:45:39 +01:00
|
|
|
from ..models import (
|
|
|
|
HTTPRequest,
|
|
|
|
HTTPResponse,
|
|
|
|
HTTPMessage,
|
|
|
|
RequestsMessage,
|
|
|
|
RequestsMessageKind,
|
2022-03-07 23:34:04 +01:00
|
|
|
OutputOptions,
|
2021-11-25 00:45:39 +01:00
|
|
|
)
|
2022-03-07 23:34:04 +01:00
|
|
|
from .models import ProcessingOptions
|
2021-05-05 14:13:39 +02:00
|
|
|
from .processing import Conversion, Formatting
|
|
|
|
from .streams import (
|
2020-09-25 13:44:28 +02:00
|
|
|
BaseStream, BufferedPrettyStream, EncodedStream, PrettyStream, RawStream,
|
2019-09-03 17:14:39 +02:00
|
|
|
)
|
2022-05-06 08:59:22 +02:00
|
|
|
from ..utils import parse_content_type_header
|
2019-09-03 17:14:39 +02:00
|
|
|
|
|
|
|
|
2021-01-30 22:14:57 +01:00
|
|
|
MESSAGE_SEPARATOR = '\n\n'
|
|
|
|
MESSAGE_SEPARATOR_BYTES = MESSAGE_SEPARATOR.encode()
|
|
|
|
|
|
|
|
|
2019-09-03 17:14:39 +02:00
|
|
|
def write_message(
|
2021-11-25 00:45:39 +01:00
|
|
|
requests_message: RequestsMessage,
|
2019-09-03 17:14:39 +02:00
|
|
|
env: Environment,
|
2021-12-23 21:13:25 +01:00
|
|
|
output_options: OutputOptions,
|
2022-03-07 23:34:04 +01:00
|
|
|
processing_options: ProcessingOptions,
|
|
|
|
extra_stream_kwargs: Optional[Dict[str, Any]] = None
|
2019-09-03 17:14:39 +02:00
|
|
|
):
|
2021-12-23 21:13:25 +01:00
|
|
|
if not output_options.any():
|
2019-09-03 17:14:39 +02:00
|
|
|
return
|
|
|
|
write_stream_kwargs = {
|
|
|
|
'stream': build_output_stream_for_message(
|
|
|
|
env=env,
|
|
|
|
requests_message=requests_message,
|
2021-12-23 21:13:25 +01:00
|
|
|
output_options=output_options,
|
2022-03-07 23:34:04 +01:00
|
|
|
processing_options=processing_options,
|
|
|
|
extra_stream_kwargs=extra_stream_kwargs
|
2019-09-03 17:14:39 +02:00
|
|
|
),
|
|
|
|
# NOTE: `env.stdout` will in fact be `stderr` with `--download`
|
|
|
|
'outfile': env.stdout,
|
2022-03-07 23:34:04 +01:00
|
|
|
'flush': env.stdout_isatty or processing_options.stream
|
2019-09-03 17:14:39 +02:00
|
|
|
}
|
|
|
|
try:
|
2022-03-07 23:34:04 +01:00
|
|
|
if env.is_windows and 'colors' in processing_options.get_prettify(env):
|
2021-07-26 23:56:38 +02:00
|
|
|
write_stream_with_colors_win(**write_stream_kwargs)
|
2019-09-03 17:14:39 +02:00
|
|
|
else:
|
|
|
|
write_stream(**write_stream_kwargs)
|
2021-05-31 10:10:41 +02:00
|
|
|
except OSError as e:
|
2022-03-07 23:34:04 +01:00
|
|
|
if processing_options.show_traceback and e.errno == errno.EPIPE:
|
2019-09-03 17:14:39 +02:00
|
|
|
# Ignore broken pipes unless --traceback.
|
|
|
|
env.stderr.write('\n')
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
def write_stream(
|
|
|
|
stream: BaseStream,
|
|
|
|
outfile: Union[IO, TextIO],
|
|
|
|
flush: bool
|
|
|
|
):
|
|
|
|
"""Write the output stream."""
|
|
|
|
try:
|
2021-05-27 13:05:41 +02:00
|
|
|
# Writing bytes so we use the buffer interface.
|
2019-09-03 17:14:39 +02:00
|
|
|
buf = outfile.buffer
|
|
|
|
except AttributeError:
|
|
|
|
buf = outfile
|
|
|
|
|
|
|
|
for chunk in stream:
|
|
|
|
buf.write(chunk)
|
|
|
|
if flush:
|
|
|
|
outfile.flush()
|
|
|
|
|
|
|
|
|
2021-07-26 23:56:38 +02:00
|
|
|
def write_stream_with_colors_win(
|
2019-09-03 17:14:39 +02:00
|
|
|
stream: 'BaseStream',
|
|
|
|
outfile: TextIO,
|
|
|
|
flush: bool
|
|
|
|
):
|
|
|
|
"""Like `write`, but colorized chunks are written as text
|
|
|
|
directly to `outfile` to ensure it gets processed by colorama.
|
2021-05-27 13:05:41 +02:00
|
|
|
Applies only to Windows and colorized terminal output.
|
2019-09-03 17:14:39 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
color = b'\x1b['
|
|
|
|
encoding = outfile.encoding
|
|
|
|
for chunk in stream:
|
|
|
|
if color in chunk:
|
|
|
|
outfile.write(chunk.decode(encoding))
|
|
|
|
else:
|
|
|
|
outfile.buffer.write(chunk)
|
|
|
|
if flush:
|
|
|
|
outfile.flush()
|
|
|
|
|
|
|
|
|
2022-03-07 23:34:04 +01:00
|
|
|
def write_raw_data(
|
|
|
|
env: Environment,
|
|
|
|
data: Any,
|
|
|
|
*,
|
|
|
|
processing_options: Optional[ProcessingOptions] = None,
|
|
|
|
headers: Optional[HTTPHeadersDict] = None,
|
|
|
|
stream_kwargs: Optional[Dict[str, Any]] = None
|
|
|
|
):
|
|
|
|
msg = requests.PreparedRequest()
|
|
|
|
msg.is_body_upload_chunk = True
|
|
|
|
msg.body = data
|
|
|
|
msg.headers = headers or HTTPHeadersDict()
|
|
|
|
msg_output_options = OutputOptions.from_message(msg, body=True, headers=False)
|
|
|
|
return write_message(
|
|
|
|
requests_message=msg,
|
|
|
|
env=env,
|
|
|
|
output_options=msg_output_options,
|
|
|
|
processing_options=processing_options or ProcessingOptions(),
|
|
|
|
extra_stream_kwargs=stream_kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-09-03 17:14:39 +02:00
|
|
|
def build_output_stream_for_message(
|
|
|
|
env: Environment,
|
2021-11-25 00:45:39 +01:00
|
|
|
requests_message: RequestsMessage,
|
2021-12-23 21:13:25 +01:00
|
|
|
output_options: OutputOptions,
|
2022-03-07 23:34:04 +01:00
|
|
|
processing_options: ProcessingOptions,
|
|
|
|
extra_stream_kwargs: Optional[Dict[str, Any]] = None
|
2019-09-03 17:14:39 +02:00
|
|
|
):
|
2021-10-06 17:27:07 +02:00
|
|
|
message_type = {
|
2021-11-25 00:45:39 +01:00
|
|
|
RequestsMessageKind.REQUEST: HTTPRequest,
|
|
|
|
RequestsMessageKind.RESPONSE: HTTPResponse,
|
2021-12-23 21:13:25 +01:00
|
|
|
}[output_options.kind]
|
2019-09-03 17:14:39 +02:00
|
|
|
stream_class, stream_kwargs = get_stream_type_and_kwargs(
|
|
|
|
env=env,
|
2022-03-07 23:34:04 +01:00
|
|
|
processing_options=processing_options,
|
2021-10-06 17:27:07 +02:00
|
|
|
message_type=message_type,
|
2021-12-08 16:49:12 +01:00
|
|
|
headers=requests_message.headers
|
2019-09-03 17:14:39 +02:00
|
|
|
)
|
2022-03-07 23:34:04 +01:00
|
|
|
if extra_stream_kwargs:
|
|
|
|
stream_kwargs.update(extra_stream_kwargs)
|
2019-09-03 17:14:39 +02:00
|
|
|
yield from stream_class(
|
2021-10-06 17:27:07 +02:00
|
|
|
msg=message_type(requests_message),
|
2021-12-23 21:13:25 +01:00
|
|
|
output_options=output_options,
|
2019-09-03 17:14:39 +02:00
|
|
|
**stream_kwargs,
|
|
|
|
)
|
2022-01-13 13:04:44 +01:00
|
|
|
if (env.stdout_isatty and output_options.body and not output_options.meta
|
2020-09-28 12:16:57 +02:00
|
|
|
and not getattr(requests_message, 'is_body_upload_chunk', False)):
|
2019-09-03 17:14:39 +02:00
|
|
|
# Ensure a blank line after the response body.
|
|
|
|
# For terminal output only.
|
2021-01-30 22:14:57 +01:00
|
|
|
yield MESSAGE_SEPARATOR_BYTES
|
2019-09-03 17:14:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_stream_type_and_kwargs(
|
|
|
|
env: Environment,
|
2022-03-07 23:34:04 +01:00
|
|
|
processing_options: ProcessingOptions,
|
2021-10-06 17:27:07 +02:00
|
|
|
message_type: Type[HTTPMessage],
|
2021-12-08 16:49:12 +01:00
|
|
|
headers: HTTPHeadersDict,
|
2019-09-03 17:14:39 +02:00
|
|
|
) -> Tuple[Type['BaseStream'], dict]:
|
|
|
|
"""Pick the right stream type and kwargs for it based on `env` and `args`.
|
|
|
|
|
|
|
|
"""
|
2022-03-07 23:34:04 +01:00
|
|
|
is_stream = processing_options.stream
|
|
|
|
prettify_groups = processing_options.get_prettify(env)
|
2021-12-08 16:49:12 +01:00
|
|
|
if not is_stream and message_type is HTTPResponse:
|
|
|
|
# If this is a response, then check the headers for determining
|
|
|
|
# auto-streaming.
|
2022-05-06 08:59:22 +02:00
|
|
|
raw_content_type_header = headers.get('Content-Type', None)
|
|
|
|
if raw_content_type_header:
|
|
|
|
content_type_header, _ = parse_content_type_header(raw_content_type_header)
|
|
|
|
is_stream = (content_type_header == 'text/event-stream')
|
2021-12-08 16:49:12 +01:00
|
|
|
|
2022-03-07 23:34:04 +01:00
|
|
|
if not env.stdout_isatty and not prettify_groups:
|
2019-09-03 17:14:39 +02:00
|
|
|
stream_class = RawStream
|
|
|
|
stream_kwargs = {
|
|
|
|
'chunk_size': (
|
|
|
|
RawStream.CHUNK_SIZE_BY_LINE
|
2021-12-08 16:49:12 +01:00
|
|
|
if is_stream
|
2019-09-03 17:14:39 +02:00
|
|
|
else RawStream.CHUNK_SIZE
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
stream_class = EncodedStream
|
|
|
|
stream_kwargs = {
|
2021-10-06 17:27:07 +02:00
|
|
|
'env': env,
|
2019-09-03 17:14:39 +02:00
|
|
|
}
|
2021-10-06 17:27:07 +02:00
|
|
|
if message_type is HTTPResponse:
|
|
|
|
stream_kwargs.update({
|
2022-03-07 23:34:04 +01:00
|
|
|
'mime_overwrite': processing_options.response_mime,
|
|
|
|
'encoding_overwrite': processing_options.response_charset,
|
2021-10-06 17:27:07 +02:00
|
|
|
})
|
2022-03-07 23:34:04 +01:00
|
|
|
if prettify_groups:
|
2021-12-08 16:49:12 +01:00
|
|
|
stream_class = PrettyStream if is_stream else BufferedPrettyStream
|
2021-10-06 17:27:07 +02:00
|
|
|
stream_kwargs.update({
|
|
|
|
'conversion': Conversion(),
|
|
|
|
'formatting': Formatting(
|
|
|
|
env=env,
|
2022-03-07 23:34:04 +01:00
|
|
|
groups=prettify_groups,
|
|
|
|
color_scheme=processing_options.style,
|
|
|
|
explicit_json=processing_options.json,
|
|
|
|
format_options=processing_options.format_options,
|
2021-10-06 17:27:07 +02:00
|
|
|
)
|
|
|
|
})
|
2019-09-03 17:14:39 +02:00
|
|
|
|
|
|
|
return stream_class, stream_kwargs
|