Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Tidy up the formatting of HTTP/2 requests * Black linting

authored by Tom Christie · committed by GitHub · parent d41840e · Browse files · 3 files+13 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+13 added−9 removed

  • other:+13 added−9 removed

3 files+13 added−9 removed

modified:docs/index.md+1 added-1 removed
modified:httpx/_main.py+11 added-7 removed
1import functools2import json3import sys4import typing5
95 unmodified lines
101 pass102 return "" # pragma: nocover103104104def format_request_headers(request: Request) -> str:105def 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.raw109 ]110 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.raw111 lines = [f"{request.method} {target} {version}"] + [112 f"{name.decode('ascii')}: {value.decode('ascii')}" for name, value in headers113 ]114 return "\n".join(lines)115116
6 unmodified lines
123 ]124 return "\n".join(lines)125126123def print_request_headers(request: Request) -> None:127def print_request_headers(request: Request, http2: bool = False) -> None:128 console = rich.console.Console()125 http_text = format_request_headers(request)129 http_text = format_request_headers(request, http2=http2)130 syntax = rich.syntax.Syntax(http_text, "http", theme="ansi_dark", word_wrap=True)131 console.print(syntax)132 syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True)133 console.print(syntax)
264 unmodified lines
398 method = "POST" if content or data or files or json else "GET"399400 event_hooks: typing.Dict[str, typing.List[typing.Callable]] = {}401 if verbose:398 event_hooks["request"] = [print_request_headers]402 event_hooks["request"] = [functools.partial(print_request_headers, http2=http2)]403 if follow_redirects:404 event_hooks["response"] = [print_redirects]405406 try:
36 unmodified lines
modified:README.md+1 added-1 removed
135 unmodified lines
136 * `idna` - Internationalized domain name support.137* `sniffio` - Async library autodetection.138* `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*139* `click` - Command line client support. *(Optional, with `httpx[cli]`)*140* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*140* `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)*142143A huge amount of credit is due to `requests` for the API layout that144much of this work follows, as well as to `urllib3` for plenty of design145inspiration around the lower-level networking details.
3 unmodified lines