Tidy up the formatting of HTTP/2 requests (#1860)

* Tidy up the formatting of HTTP/2 requests

* Black linting
This commit is contained in:
Tom Christie 2021-09-14 13:36:22 +01:00 committed by GitHub
parent d41840e18d
commit e1abaf146f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View File

@ -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

View 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

View 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]