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

Conversation

egleston
Copy link
Contributor

@egleston egleston commented May 3, 2022

The header-check for auto-streaming should parse out the type/sub-type from optional parameters.
FastAPI/Starlette modifies the Content-Type header for SSE responses (actually, for all text/* media) by adding the charset as an "optional parameter": Content-Type: text/event-stream; charset=utf-8

@codecov-commenter
Copy link

codecov-commenter commented May 3, 2022

Codecov Report

Merging #1383 (fbcff30) into master (4d7d6b6) will decrease coverage by 2.69%.
The diff coverage is 91.32%.

@@            Coverage Diff             @@
##           master    #1383      +/-   ##
==========================================
- Coverage   97.28%   94.59%   -2.70%     
==========================================
  Files          67      109      +42     
  Lines        4235     7655    +3420     
==========================================
+ Hits         4120     7241    +3121     
- Misses        115      414     +299     
Impacted Files Coverage Δ
httpie/output/ui/man_pages.py 0.00% <0.00%> (ø)
httpie/output/ui/rich_utils.py 0.00% <0.00%> (ø)
tests/test_binary.py 100.00% <ø> (ø)
tests/test_ssl.py 92.59% <ø> (-2.35%) ⬇️
tests/test_stream.py 100.00% <ø> (ø)
tests/test_tokens.py 100.00% <ø> (ø)
tests/test_update_warnings.py 99.14% <ø> (ø)
tests/test_uploads.py 96.70% <ø> (-3.30%) ⬇️
tests/test_xml.py 97.56% <ø> (-0.06%) ⬇️
tests/utils/__init__.py 92.37% <ø> (+3.69%) ⬆️
... and 101 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e76ebc...fbcff30. Read the comment docs.

httpie/output/writer.py Outdated Show resolved Hide resolved
@@ -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'

@isidentical
Copy link
Contributor

Overall, it looks amazing. Thanks @egleston for the PR (and finding the bug). Beside those 2 minor comments, I wouldn't touch to the HTTP server code but just rather change the test case (which should be easily adaptable):

diff --git a/tests/test_stream.py b/tests/test_stream.py
index a8950d2..8df2af2 100644
--- a/tests/test_stream.py
+++ b/tests/test_stream.py
@@ -124,6 +124,10 @@ def test_redirected_stream(httpbin):
         ['Accept:text/event-stream'],
         3
     ),
+    (
+        ['Accept:text/event-stream; charset=utf-8'],
+        3
+    ),
     (
         ['Accept:text/plain'],
         1
@@ -132,7 +136,7 @@ def test_redirected_stream(httpbin):
 def test_auto_streaming(http_server, extras, expected):
     env = MockEnvironment()
     env.stdout.write = Mock()
     http(http_server + '/drip', *extras, env=env)
     assert len([
         call_arg
         for call_arg in env.stdout.write.call_args_list

and add a changelog entry!

@isidentical isidentical self-assigned this May 3, 2022
egleston and others added 15 commits May 5, 2022 16:14
Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
This reverts commit fe50bc0.

Accomplished through additional test case.
* Refactor palette

* Modifiers / change static strings to colors

* Colors...

* Error-based tests

* Styling linting

Co-authored-by: Jakub Roztocil <jakub@roztocil.co>
- Highlighting for options (-x, --x) now doesn't strip the prefix (may be whitespace).
- Escape sequences are now cross-platform compatible (directly taken by groff/troff [man's renderer])
- Now we check for the section before displaying the man pages.
- On MacOS, there is HTTP(n) which is different from our HTTP(1). This used to conflict with it, and we showed the wrong page. Now we specifically ask foir HTTP(1).
- Errors that might happen (e.g non executable man command) is now suppressed. So in the worst case (if anything regarding man execution goes wrong), we'll always display the manual.
- Docs for man pages.
- HTTPie man pages.
- Epilog for the man pages (see also)
- Auto-generated comments.
* Hide pretty help

* Automatic release update warnings.

* `httpie cli check-updates`

* adapt to the new loglevel construct

* Don't make the pie-colors the bold

* Apply review feedback.

Co-authored-by: Jakub Roztocil <jakub@roztocil.co>
Copy link
Contributor

@isidentical isidentical left a comment

Choose a reason for hiding this comment

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

LGTM

@isidentical isidentical merged commit 9f7612c into httpie:master May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants