From e1abaf146fe1b66c0d5ae10a24106865efe2d79e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 14 Sep 2021 13:36:22 +0100 Subject: [PATCH] Tidy up the formatting of HTTP/2 requests (#1860) * Tidy up the formatting of HTTP/2 requests * Black linting --- README.md | 2 +- docs/index.md | 2 +- httpx/_main.py | 18 +++++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8ace165bf4..3f7b2ef9c1 100644 --- 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 diff --git a/docs/index.md b/docs/index.md index 4445c016a5..ca09a85a1f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/httpx/_main.py b/httpx/_main.py index cbc877621d..6c299ba050 100644 --- a/httpx/_main.py +++ b/httpx/_main.py @@ -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]