Skip to content

Commit e1abaf1

Browse files
authored
Tidy up the formatting of HTTP/2 requests (#1860)
* Tidy up the formatting of HTTP/2 requests * Black linting
1 parent d41840e commit e1abaf1

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ The HTTPX project relies on these excellent libraries:
137137
* `sniffio` - Async library autodetection.
138138
* `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*
139139
* `click` - Command line client support. *(Optional, with `httpx[cli]`)*
140-
* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
141140
* `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*
141+
* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
142142

143143
A huge amount of credit is due to `requests` for the API layout that
144144
much of this work follows, as well as to `urllib3` for plenty of design

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ The HTTPX project relies on these excellent libraries:
122122
* `sniffio` - Async library autodetection.
123123
* `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*
124124
* `click` - Command line client support. *(Optional, with `httpx[cli]`)*
125-
* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
126125
* `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*
126+
* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
127127

128128
A huge amount of credit is due to `requests` for the API layout that
129129
much of this work follows, as well as to `urllib3` for plenty of design

httpx/_main.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import json
23
import sys
34
import typing
@@ -101,11 +102,14 @@ def get_lexer_for_response(response: Response) -> str:
101102
return "" # pragma: nocover
102103

103104

104-
def format_request_headers(request: Request) -> str:
105+
def format_request_headers(request: Request, http2: bool = False) -> str:
106+
version = "HTTP/2" if http2 else "HTTP/1.1"
107+
headers = [
108+
(name.lower() if http2 else name, value) for name, value in request.headers.raw
109+
]
105110
target = request.url.raw[-1].decode("ascii")
106-
lines = [f"{request.method} {target} HTTP/1.1"] + [
107-
f"{name.decode('ascii')}: {value.decode('ascii')}"
108-
for name, value in request.headers.raw
111+
lines = [f"{request.method} {target} {version}"] + [
112+
f"{name.decode('ascii')}: {value.decode('ascii')}" for name, value in headers
109113
]
110114
return "\n".join(lines)
111115

@@ -120,9 +124,9 @@ def format_response_headers(response: Response) -> str:
120124
return "\n".join(lines)
121125

122126

123-
def print_request_headers(request: Request) -> None:
127+
def print_request_headers(request: Request, http2: bool = False) -> None:
124128
console = rich.console.Console()
125-
http_text = format_request_headers(request)
129+
http_text = format_request_headers(request, http2=http2)
126130
syntax = rich.syntax.Syntax(http_text, "http", theme="ansi_dark", word_wrap=True)
127131
console.print(syntax)
128132
syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True)
@@ -395,7 +399,7 @@ def main(
395399

396400
event_hooks: typing.Dict[str, typing.List[typing.Callable]] = {}
397401
if verbose:
398-
event_hooks["request"] = [print_request_headers]
402+
event_hooks["request"] = [functools.partial(print_request_headers, http2=http2)]
399403
if follow_redirects:
400404
event_hooks["response"] = [print_redirects]
401405

0 commit comments

Comments
 (0)