]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Tidy up the formatting of HTTP/2 requests (#1860)
authorTom Christie <tom@tomchristie.com>
Tue, 14 Sep 2021 12:36:22 +0000 (13:36 +0100)
committerGitHub <noreply@github.com>
Tue, 14 Sep 2021 12:36:22 +0000 (13:36 +0100)
* Tidy up the formatting of HTTP/2 requests

* Black linting

README.md
docs/index.md
httpx/_main.py

index 8ace165bf4b2ada9bf33ae7fe00a03104026852c..3f7b2ef9c1b38eb4b9731997bf291ada57917a65 100644 (file)
--- a/README.md
+++ b/README.md
@@ -137,8 +137,8 @@ The HTTPX project relies on these excellent libraries:
 * `sniffio` - Async library autodetection.
 * `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*
 * `click` - Command line client support. *(Optional, with `httpx[cli]`)*
-* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
 * `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*
+* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
 
 A huge amount of credit is due to `requests` for the API layout that
 much of this work follows, as well as to `urllib3` for plenty of design
index 4445c016a5263e7f01ac7496327f53cba8593b27..ca09a85a1fb455af73fcb6c9f4ddb24b887c16eb 100644 (file)
@@ -122,8 +122,8 @@ The HTTPX project relies on these excellent libraries:
 * `sniffio` - Async library autodetection.
 * `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*
 * `click` - Command line client support. *(Optional, with `httpx[cli]`)*
-* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
 * `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*
+* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
 
 A huge amount of credit is due to `requests` for the API layout that
 much of this work follows, as well as to `urllib3` for plenty of design
index cbc877621daead38bb048f663ece11beabf5b874..6c299ba050c5f310f8fa6a86746be5d0ccc02eb0 100644 (file)
@@ -1,3 +1,4 @@
+import functools
 import json
 import sys
 import typing
@@ -101,11 +102,14 @@ def get_lexer_for_response(response: Response) -> str:
     return ""  # pragma: nocover
 
 
-def format_request_headers(request: Request) -> str:
+def format_request_headers(request: Request, http2: bool = False) -> str:
+    version = "HTTP/2" if http2 else "HTTP/1.1"
+    headers = [
+        (name.lower() if http2 else name, value) for name, value in request.headers.raw
+    ]
     target = request.url.raw[-1].decode("ascii")
-    lines = [f"{request.method} {target} HTTP/1.1"] + [
-        f"{name.decode('ascii')}: {value.decode('ascii')}"
-        for name, value in request.headers.raw
+    lines = [f"{request.method} {target} {version}"] + [
+        f"{name.decode('ascii')}: {value.decode('ascii')}" for name, value in headers
     ]
     return "\n".join(lines)
 
@@ -120,9 +124,9 @@ def format_response_headers(response: Response) -> str:
     return "\n".join(lines)
 
 
-def print_request_headers(request: Request) -> None:
+def print_request_headers(request: Request, http2: bool = False) -> None:
     console = rich.console.Console()
-    http_text = format_request_headers(request)
+    http_text = format_request_headers(request, http2=http2)
     syntax = rich.syntax.Syntax(http_text, "http", theme="ansi_dark", word_wrap=True)
     console.print(syntax)
     syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True)
@@ -395,7 +399,7 @@ def main(
 
     event_hooks: typing.Dict[str, typing.List[typing.Callable]] = {}
     if verbose:
-        event_hooks["request"] = [print_request_headers]
+        event_hooks["request"] = [functools.partial(print_request_headers, http2=http2)]
     if follow_redirects:
         event_hooks["response"] = [print_redirects]