Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking headers to determine auto-streaming #1383

Merged
merged 18 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion httpie/output/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .streams import (
BaseStream, BufferedPrettyStream, EncodedStream, PrettyStream, RawStream,
)
from ..utils import parse_content_type_header


MESSAGE_SEPARATOR = '\n\n'
Expand Down Expand Up @@ -163,7 +164,10 @@ def get_stream_type_and_kwargs(
if not is_stream and message_type is HTTPResponse:
# If this is a response, then check the headers for determining
# auto-streaming.
is_stream = headers.get('Content-Type') == 'text/event-stream'
ct_raw = headers.get('Content-Type', None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we reference this (and text/event-stream) string in different places now, it might make sense to hoist them to the global scope as a constant (just to the top of the file). E.g.:

CONTENT_TYPE_HEADER_NAME = 'Content-Type'
EVENT_STREAM_TYPE = 'text/event-stream'

if ct_raw:
ct, _ = parse_content_type_header(ct_raw)
is_stream = ct == 'text/event-stream'
egleston marked this conversation as resolved.
Show resolved Hide resolved

if not env.stdout_isatty and not prettify_groups:
stream_class = RawStream
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def chunked_drip(handler):
handler.send_response(200)
accept = handler.headers.get('Accept')
if accept is not None:
handler.send_header('Content-Type', accept)
handler.send_header('Content-Type', accept + '; charset=utf-8')
handler.send_header('Transfer-Encoding', 'chunked')
handler.end_headers()

Expand Down