Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BaseHTTPResponse a base class of HTTP2Response #3311

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/urllib3/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import urllib3.connection
import urllib3.util.ssl_
from urllib3.response import BaseHTTPResponse

from ._collections import HTTPHeaderDict
from .connection import HTTPSConnection
Expand Down Expand Up @@ -54,6 +55,7 @@ def putrequest(
skip_accept_encoding: bool = False,
) -> None:
with self._lock_h2_conn() as h2_conn:
self._request_url = url
self._h2_stream = h2_conn.get_next_available_stream_id()

if ":" in self.host:
Expand Down Expand Up @@ -134,7 +136,12 @@ def getresponse( # type: ignore[override]
self.close()

assert status is not None
return HTTP2Response(status=status, headers=headers, data=bytes(data))
return HTTP2Response(
status=status,
headers=headers,
request_url=self._request_url,
data=bytes(data),
)

def close(self) -> None:
with self._lock_h2_conn() as h2_conn:
Expand All @@ -155,20 +162,39 @@ def close(self) -> None:
super().close()


class HTTP2Response:
class HTTP2Response(BaseHTTPResponse):
# TODO: This is a woefully incomplete response object, but works for non-streaming.
def __init__(self, status: int, headers: HTTPHeaderDict, data: bytes) -> None:
self.status = status
self.headers = headers
self.data = data
def __init__(
self,
status: int,
headers: HTTPHeaderDict,
request_url: str,
data: bytes,
decode_content: bool = False, # TODO: support decoding
) -> None:
super().__init__(
status=status,
headers=headers,
# Following CPython, we map HTTP versions to major * 10 + minor integers
version=20,
# No reason phrase in HTTP/2
reason=None,
decode_content=decode_content,
request_url=request_url,
)
self._data = data
self.length_remaining = 0

@property
def data(self) -> bytes:
return self._data

def get_redirect_location(self) -> None:
return None


def inject_into_urllib3() -> None:
HTTPSConnectionPool.ConnectionCls = HTTP2Connection # type: ignore[assignment]
HTTPSConnectionPool.ConnectionCls = HTTP2Connection
urllib3.connection.HTTPSConnection = HTTP2Connection # type: ignore[misc]

# TODO: Offer 'http/1.1' as well, but for testing purposes this is handy.
Expand Down
1 change: 1 addition & 0 deletions test/with_dummyserver/test_https.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def test_simple(self, http_version: str) -> None:
r = https_pool.request("GET", "/")
assert r.status == 200, r.data
assert r.headers["server"] == f"hypercorn-{http_version}"
assert r.data == b"Dummy server!"

@resolvesLocalhostFQDN()
def test_dotted_fqdn(self) -> None:
Expand Down
Loading