2014-04-27 01:07:13 +03:00
|
|
|
from itertools import chain
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from httpie.context import Environment
|
|
|
|
from httpie.models import HTTPRequest, HTTPResponse
|
|
|
|
from httpie.input import (OUT_REQ_BODY, OUT_REQ_HEAD,
|
|
|
|
OUT_RESP_HEAD, OUT_RESP_BODY)
|
2014-04-29 00:33:30 +03:00
|
|
|
from httpie.output.processing import Formatting, Conversion
|
2014-04-27 01:07:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
BINARY_SUPPRESSED_NOTICE = (
|
|
|
|
b'\n'
|
|
|
|
b'+-----------------------------------------+\n'
|
|
|
|
b'| NOTE: binary data not shown in terminal |\n'
|
|
|
|
b'+-----------------------------------------+'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BinarySuppressedError(Exception):
|
|
|
|
"""An error indicating that the body is binary and won't be written,
|
|
|
|
e.g., for terminal output)."""
|
|
|
|
|
|
|
|
message = BINARY_SUPPRESSED_NOTICE
|
|
|
|
|
|
|
|
|
2016-02-29 09:00:17 +02:00
|
|
|
def write_stream(stream, outfile, flush):
|
2014-04-27 01:07:13 +03:00
|
|
|
"""Write the output stream."""
|
|
|
|
try:
|
|
|
|
# Writing bytes so we use the buffer interface (Python 3).
|
|
|
|
buf = outfile.buffer
|
|
|
|
except AttributeError:
|
|
|
|
buf = outfile
|
|
|
|
|
|
|
|
for chunk in stream:
|
|
|
|
buf.write(chunk)
|
|
|
|
if flush:
|
|
|
|
outfile.flush()
|
|
|
|
|
|
|
|
|
2016-02-29 09:00:17 +02:00
|
|
|
def write_stream_with_colors_win_py3(stream, outfile, flush):
|
2014-04-27 01:07:13 +03:00
|
|
|
"""Like `write`, but colorized chunks are written as text
|
|
|
|
directly to `outfile` to ensure it gets processed by colorama.
|
|
|
|
Applies only to Windows with Python 3 and colorized terminal output.
|
|
|
|
|
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2016-03-05 20:09:52 +02:00
|
|
|
def build_output_stream(args, env, request, response, output_options):
|
2014-04-27 01:07:13 +03:00
|
|
|
"""Build and return a chain of iterators over the `request`-`response`
|
|
|
|
exchange each of which yields `bytes` chunks.
|
|
|
|
|
|
|
|
"""
|
2016-03-05 20:09:52 +02:00
|
|
|
req_h = OUT_REQ_HEAD in output_options
|
|
|
|
req_b = OUT_REQ_BODY in output_options
|
|
|
|
resp_h = OUT_RESP_HEAD in output_options
|
|
|
|
resp_b = OUT_RESP_BODY in output_options
|
2014-04-27 01:07:13 +03:00
|
|
|
req = req_h or req_b
|
|
|
|
resp = resp_h or resp_b
|
|
|
|
|
|
|
|
output = []
|
|
|
|
Stream = get_stream_type(env, args)
|
|
|
|
|
|
|
|
if req:
|
|
|
|
output.append(Stream(
|
|
|
|
msg=HTTPRequest(request),
|
|
|
|
with_headers=req_h,
|
|
|
|
with_body=req_b))
|
|
|
|
|
|
|
|
if req_b and resp:
|
|
|
|
# Request/Response separator.
|
|
|
|
output.append([b'\n\n'])
|
|
|
|
|
|
|
|
if resp:
|
|
|
|
output.append(Stream(
|
|
|
|
msg=HTTPResponse(response),
|
|
|
|
with_headers=resp_h,
|
|
|
|
with_body=resp_b))
|
|
|
|
|
|
|
|
if env.stdout_isatty and resp_b:
|
|
|
|
# Ensure a blank line after the response body.
|
|
|
|
# For terminal output only.
|
|
|
|
output.append([b'\n\n'])
|
|
|
|
|
|
|
|
return chain(*output)
|
|
|
|
|
|
|
|
|
|
|
|
def get_stream_type(env, args):
|
|
|
|
"""Pick the right stream type based on `env` and `args`.
|
|
|
|
Wrap it in a partial with the type-specific args so that
|
|
|
|
we don't need to think what stream we are dealing with.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not env.stdout_isatty and not args.prettify:
|
|
|
|
Stream = partial(
|
|
|
|
RawStream,
|
|
|
|
chunk_size=RawStream.CHUNK_SIZE_BY_LINE
|
|
|
|
if args.stream
|
|
|
|
else RawStream.CHUNK_SIZE
|
|
|
|
)
|
|
|
|
elif args.prettify:
|
|
|
|
Stream = partial(
|
|
|
|
PrettyStream if args.stream else BufferedPrettyStream,
|
|
|
|
env=env,
|
2014-04-29 00:33:30 +03:00
|
|
|
conversion=Conversion(),
|
2016-03-01 08:57:15 +02:00
|
|
|
formatting=Formatting(
|
|
|
|
env=env,
|
|
|
|
groups=args.prettify,
|
|
|
|
color_scheme=args.style,
|
|
|
|
explicit_json=args.json,
|
|
|
|
),
|
2014-04-27 01:07:13 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
Stream = partial(EncodedStream, env=env)
|
|
|
|
|
|
|
|
return Stream
|
|
|
|
|
|
|
|
|
|
|
|
class BaseStream(object):
|
|
|
|
"""Base HTTP message output stream class."""
|
|
|
|
|
|
|
|
def __init__(self, msg, with_headers=True, with_body=True,
|
|
|
|
on_body_chunk_downloaded=None):
|
|
|
|
"""
|
|
|
|
:param msg: a :class:`models.HTTPMessage` subclass
|
|
|
|
:param with_headers: if `True`, headers will be included
|
|
|
|
:param with_body: if `True`, body will be included
|
|
|
|
|
|
|
|
"""
|
|
|
|
assert with_headers or with_body
|
|
|
|
self.msg = msg
|
|
|
|
self.with_headers = with_headers
|
|
|
|
self.with_body = with_body
|
|
|
|
self.on_body_chunk_downloaded = on_body_chunk_downloaded
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def get_headers(self):
|
2014-04-27 01:07:13 +03:00
|
|
|
"""Return the headers' bytes."""
|
|
|
|
return self.msg.headers.encode('utf8')
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def iter_body(self):
|
2014-04-27 01:07:13 +03:00
|
|
|
"""Return an iterator over the message body."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""Return an iterator over `self.msg`."""
|
|
|
|
if self.with_headers:
|
2014-04-29 00:33:30 +03:00
|
|
|
yield self.get_headers()
|
2014-04-27 01:07:13 +03:00
|
|
|
yield b'\r\n\r\n'
|
|
|
|
|
|
|
|
if self.with_body:
|
|
|
|
try:
|
2014-04-29 00:33:30 +03:00
|
|
|
for chunk in self.iter_body():
|
2014-04-27 01:07:13 +03:00
|
|
|
yield chunk
|
|
|
|
if self.on_body_chunk_downloaded:
|
|
|
|
self.on_body_chunk_downloaded(chunk)
|
|
|
|
except BinarySuppressedError as e:
|
|
|
|
if self.with_headers:
|
|
|
|
yield b'\n'
|
|
|
|
yield e.message
|
|
|
|
|
|
|
|
|
|
|
|
class RawStream(BaseStream):
|
|
|
|
"""The message is streamed in chunks with no processing."""
|
|
|
|
|
|
|
|
CHUNK_SIZE = 1024 * 100
|
|
|
|
CHUNK_SIZE_BY_LINE = 1
|
|
|
|
|
|
|
|
def __init__(self, chunk_size=CHUNK_SIZE, **kwargs):
|
|
|
|
super(RawStream, self).__init__(**kwargs)
|
|
|
|
self.chunk_size = chunk_size
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def iter_body(self):
|
2014-04-27 01:07:13 +03:00
|
|
|
return self.msg.iter_body(self.chunk_size)
|
|
|
|
|
|
|
|
|
|
|
|
class EncodedStream(BaseStream):
|
|
|
|
"""Encoded HTTP message stream.
|
|
|
|
|
|
|
|
The message bytes are converted to an encoding suitable for
|
|
|
|
`self.env.stdout`. Unicode errors are replaced and binary data
|
|
|
|
is suppressed. The body is always streamed by line.
|
|
|
|
|
|
|
|
"""
|
|
|
|
CHUNK_SIZE = 1
|
|
|
|
|
|
|
|
def __init__(self, env=Environment(), **kwargs):
|
|
|
|
|
|
|
|
super(EncodedStream, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
if env.stdout_isatty:
|
|
|
|
# Use the encoding supported by the terminal.
|
|
|
|
output_encoding = env.stdout_encoding
|
|
|
|
else:
|
|
|
|
# Preserve the message encoding.
|
|
|
|
output_encoding = self.msg.encoding
|
|
|
|
|
|
|
|
# Default to utf8 when unsure.
|
|
|
|
self.output_encoding = output_encoding or 'utf8'
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def iter_body(self):
|
2014-04-27 01:07:13 +03:00
|
|
|
|
|
|
|
for line, lf in self.msg.iter_lines(self.CHUNK_SIZE):
|
|
|
|
|
|
|
|
if b'\0' in line:
|
|
|
|
raise BinarySuppressedError()
|
|
|
|
|
|
|
|
yield line.decode(self.msg.encoding) \
|
|
|
|
.encode(self.output_encoding, 'replace') + lf
|
|
|
|
|
|
|
|
|
|
|
|
class PrettyStream(EncodedStream):
|
|
|
|
"""In addition to :class:`EncodedStream` behaviour, this stream applies
|
|
|
|
content processing.
|
|
|
|
|
|
|
|
Useful for long-lived HTTP responses that stream by lines
|
|
|
|
such as the Twitter streaming API.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
CHUNK_SIZE = 1
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def __init__(self, conversion, formatting, **kwargs):
|
2014-04-27 01:07:13 +03:00
|
|
|
super(PrettyStream, self).__init__(**kwargs)
|
2014-04-29 00:33:30 +03:00
|
|
|
self.formatting = formatting
|
|
|
|
self.conversion = conversion
|
|
|
|
self.mime = self.msg.content_type.split(';')[0]
|
2014-04-27 01:07:13 +03:00
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def get_headers(self):
|
|
|
|
return self.formatting.format_headers(
|
2014-04-27 01:07:13 +03:00
|
|
|
self.msg.headers).encode(self.output_encoding)
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def iter_body(self):
|
|
|
|
first_chunk = True
|
|
|
|
iter_lines = self.msg.iter_lines(self.CHUNK_SIZE)
|
|
|
|
for line, lf in iter_lines:
|
2014-04-27 01:07:13 +03:00
|
|
|
if b'\0' in line:
|
2014-04-29 00:33:30 +03:00
|
|
|
if first_chunk:
|
|
|
|
converter = self.conversion.get_converter(self.mime)
|
|
|
|
if converter:
|
|
|
|
body = bytearray()
|
|
|
|
# noinspection PyAssignmentToLoopOrWithParameter
|
|
|
|
for line, lf in chain([(line, lf)], iter_lines):
|
|
|
|
body.extend(line)
|
|
|
|
body.extend(lf)
|
|
|
|
self.mime, body = converter.convert(body)
|
|
|
|
assert isinstance(body, str)
|
|
|
|
yield self.process_body(body)
|
|
|
|
return
|
2014-04-27 01:07:13 +03:00
|
|
|
raise BinarySuppressedError()
|
2014-04-29 00:33:30 +03:00
|
|
|
yield self.process_body(line) + lf
|
|
|
|
first_chunk = False
|
2014-04-27 01:07:13 +03:00
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def process_body(self, chunk):
|
|
|
|
if not isinstance(chunk, str):
|
|
|
|
# Text when a converter has been used,
|
|
|
|
# otherwise it will always be bytes.
|
|
|
|
chunk = chunk.decode(self.msg.encoding, 'replace')
|
|
|
|
chunk = self.formatting.format_body(content=chunk, mime=self.mime)
|
|
|
|
return chunk.encode(self.output_encoding, 'replace')
|
2014-04-27 01:07:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BufferedPrettyStream(PrettyStream):
|
|
|
|
"""The same as :class:`PrettyStream` except that the body is fully
|
|
|
|
fetched before it's processed.
|
|
|
|
|
|
|
|
Suitable regular HTTP responses.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
CHUNK_SIZE = 1024 * 10
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
def iter_body(self):
|
2014-04-27 01:07:13 +03:00
|
|
|
# Read the whole body before prettifying it,
|
|
|
|
# but bail out immediately if the body is binary.
|
2014-04-29 00:33:30 +03:00
|
|
|
converter = None
|
2014-04-27 01:07:13 +03:00
|
|
|
body = bytearray()
|
2014-04-29 00:33:30 +03:00
|
|
|
|
2014-04-27 01:07:13 +03:00
|
|
|
for chunk in self.msg.iter_body(self.CHUNK_SIZE):
|
2014-04-29 00:33:30 +03:00
|
|
|
if not converter and b'\0' in chunk:
|
|
|
|
converter = self.conversion.get_converter(self.mime)
|
|
|
|
if not converter:
|
|
|
|
raise BinarySuppressedError()
|
2014-04-27 01:07:13 +03:00
|
|
|
body.extend(chunk)
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
if converter:
|
|
|
|
self.mime, body = converter.convert(body)
|
|
|
|
|
|
|
|
yield self.process_body(body)
|