Skip to content

Fix case-sensitive detection of streaming event-stream responses - #1955

Closed
harsh-thakkar7 wants to merge 1 commit into
httpie:masterfrom
harsh-thakkar7:fix/case-insensitive-sse-detection
Closed

harsh-thakkar7 wants to merge 1 commit into
httpie:masterfrom
harsh-thakkar7:fix/case-insensitive-sse-detection

Conversation

@harsh-thakkar7

Copy link
Copy Markdown

Summary

Servers may send the Content-Type header for Server-Sent Events with any casing (RFC 2045 §5.1: "media types are case-insensitive"). HTTPie's auto-streaming detection compared the parsed media type literally:

is_stream = (content_type_header == 'text/event-stream')

so a response with e.g. Content-Type: Text/Event-Stream silently disabled the documented SSE auto-streaming (falling back to buffered output with chunk_size=102400 instead of line-by-line chunk_size=1).

>>> get_stream_type_and_kwargs(env, opts, HTTPResponse,
...                            {"Content-Type": "Text/Event-Stream"})
(RawStream, {"chunk_size": 102400})   # before: buffered

Fix

Compare the media type case-insensitively:

is_stream = (content_type_header.lower() == 'text/event-stream')

Tests

Added a parametrized case to test_auto_streaming (a Text/Event-Stream response is now streamed line-by-line, yielding 3 writes of test instead of 1 buffered write). The new case fails before this change (1 failed) and passes after (4 passed full suite for the test module).

pytest -q tests/test_stream.py   # 17 passed

Media types are case-insensitive per RFC 2045 section 5.1, but the
auto-streaming check for Server-Sent Events compared the parsed
Content-Type header literally, so a server responding with e.g.
'Text/Event-Stream' silently fell back to buffered output.
@harsh-thakkar7

Copy link
Copy Markdown
Author

Closing this — I don't think this change is mature enough to land right now. Thanks for the project!

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.

1 participant