Skip to content

Commit

Permalink
Tidy up the formatting of HTTP/2 requests (#1860)
Browse files Browse the repository at this point in the history
* Tidy up the formatting of HTTP/2 requests

* Black linting
  • Loading branch information
tomchristie committed Sep 14, 2021
1 parent d41840e commit e1abaf1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions httpx/_main.py
@@ -1,3 +1,4 @@
import functools
import json
import sys
import typing
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit e1abaf1

Please sign in to comment.