hip 0.3__tar.gz → 0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hip-0.3 → hip-0.4}/PKG-INFO +6 -18
- {hip-0.3 → hip-0.4}/docs/README.md +1 -5
- hip-0.4/pyproject.toml +28 -0
- hip-0.4/pyproject.toml.orig +30 -0
- hip-0.4/src/hip/__init__.py +35 -0
- hip-0.4/src/hip/backends/__init__.py +3 -0
- hip-0.3/src/hip/_network.py → hip-0.4/src/hip/backends/sync.py +50 -37
- hip-0.4/src/hip/client/__init__.py +14 -0
- hip-0.3/src/hip/_client.py → hip-0.4/src/hip/client/base.py +22 -28
- hip-0.4/src/hip/client/connection.py +230 -0
- hip-0.3/src/hip/_pool.py → hip-0.4/src/hip/client/pool.py +10 -8
- hip-0.4/src/hip/client/transport.py +44 -0
- hip-0.4/src/hip/core/__init__.py +29 -0
- hip-0.4/src/hip/core/content.py +223 -0
- hip-0.3/src/hip/_content.py → hip-0.4/src/hip/core/forms.py +9 -228
- hip-0.4/src/hip/core/queryparams.py +221 -0
- hip-0.3/src/hip/_request.py → hip-0.4/src/hip/core/request.py +4 -45
- hip-0.3/src/hip/_response.py → hip-0.4/src/hip/core/response.py +3 -37
- hip-0.3/src/hip/_streams.py → hip-0.4/src/hip/core/streams.py +14 -58
- hip-0.3/src/hip/_urls.py → hip-0.4/src/hip/core/urls.py +4 -217
- hip-0.4/src/hip/parsers/__init__.py +6 -0
- hip-0.4/src/hip/parsers/exceptions.py +2 -0
- hip-0.3/src/hip/_parsers.py → hip-0.4/src/hip/parsers/http.py +17 -96
- hip-0.4/src/hip/parsers/readahead.py +81 -0
- hip-0.4/src/hip/server/__init__.py +3 -0
- hip-0.3/src/hip/_server.py → hip-0.4/src/hip/server/base.py +58 -14
- hip-0.3/.gitignore +0 -15
- hip-0.3/pyproject.toml +0 -43
- hip-0.3/src/hip/__init__.py +0 -46
- hip-0.3/src/hip/_body.py +0 -118
- hip-0.3/src/hip/_connection.py +0 -181
- /hip-0.3/src/hip/_headers.py → /hip-0.4/src/hip/core/headers.py +0 -0
- /hip-0.3/src/hip/_urlencode.py → /hip-0.4/src/hip/core/urlencode.py +0 -0
- /hip-0.3/src/hip/_urlparse.py → /hip-0.4/src/hip/core/urlparse.py +0 -0
{hip-0.3 → hip-0.4}/PKG-INFO
RENAMED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: hip
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: HTTP, for
|
|
5
|
-
Author
|
|
6
|
-
Classifier: Development Status :: 4 - Beta
|
|
7
|
-
Classifier: Environment :: Web Environment
|
|
8
|
-
Classifier: Intended Audience :: Developers
|
|
3
|
+
Version: 0.4
|
|
4
|
+
Summary: HTTP, for everyone.
|
|
5
|
+
Author: the hip community
|
|
9
6
|
Classifier: Operating System :: OS Independent
|
|
10
7
|
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
-
Classifier: Topic :: Internet :: WWW/HTTP
|
|
16
|
-
Requires-Python: >=3.10
|
|
17
8
|
Requires-Dist: truststore
|
|
9
|
+
Requires-Python: >=3.10
|
|
18
10
|
Description-Content-Type: text/markdown
|
|
19
11
|
|
|
20
12
|
<p align="center">
|
|
@@ -97,11 +89,7 @@ requests v2.33.1 (63.4KiB)
|
|
|
97
89
|
|
|
98
90
|
## Contribution
|
|
99
91
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
We are currently [seeking sponsorship](https://github.com/sponsors/encode), and looking to expand our team.
|
|
103
|
-
|
|
104
|
-
If you'd like to discuss getting involved, please [book in some time](https://calendly.com/encode-oss).
|
|
92
|
+
See [Contributing](CONTRIBUTING.md) to get involved.
|
|
105
93
|
|
|
106
94
|
---
|
|
107
95
|
|
|
@@ -78,11 +78,7 @@ requests v2.33.1 (63.4KiB)
|
|
|
78
78
|
|
|
79
79
|
## Contribution
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
We are currently [seeking sponsorship](https://github.com/sponsors/encode), and looking to expand our team.
|
|
84
|
-
|
|
85
|
-
If you'd like to discuss getting involved, please [book in some time](https://calendly.com/encode-oss).
|
|
81
|
+
See [Contributing](CONTRIBUTING.md) to get involved.
|
|
86
82
|
|
|
87
83
|
---
|
|
88
84
|
|
hip-0.4/pyproject.toml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "hip"
|
|
3
|
+
version = "0.4"
|
|
4
|
+
description = "HTTP, for everyone."
|
|
5
|
+
readme = "docs/README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
classifiers = [
|
|
8
|
+
"Operating System :: OS Independent",
|
|
9
|
+
"Programming Language :: Python :: 3",
|
|
10
|
+
]
|
|
11
|
+
dependencies = ["truststore"]
|
|
12
|
+
|
|
13
|
+
[[project.authors]]
|
|
14
|
+
name = "the hip community"
|
|
15
|
+
|
|
16
|
+
[dependency-groups]
|
|
17
|
+
dev = [
|
|
18
|
+
"mkdocs==2.0.dev6",
|
|
19
|
+
"mypy==2.3.1",
|
|
20
|
+
"pytest==8.3.5",
|
|
21
|
+
"pytest-cov==7.1.0",
|
|
22
|
+
"ruff",
|
|
23
|
+
"typing-extensions>=4.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["uv_build>=0.13.0,<0.14.0"]
|
|
28
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "hip"
|
|
3
|
+
version = "0.4"
|
|
4
|
+
description = "HTTP, for everyone."
|
|
5
|
+
readme = "docs/README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "the hip community" },
|
|
9
|
+
]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Operating System :: OS Independent",
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
]
|
|
14
|
+
dependencies = [
|
|
15
|
+
"truststore",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[dependency-groups]
|
|
19
|
+
dev = [
|
|
20
|
+
"mkdocs==2.0.dev6",
|
|
21
|
+
"mypy==2.3.1",
|
|
22
|
+
"pytest==8.3.5",
|
|
23
|
+
"pytest-cov==7.1.0",
|
|
24
|
+
"ruff",
|
|
25
|
+
"typing-extensions>=4.0",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["uv_build>=0.13.0,<0.14.0"]
|
|
30
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from . import backends, parsers
|
|
2
|
+
from .client import Client
|
|
3
|
+
from .core.content import HTML, JSON, Binary, Content, Text
|
|
4
|
+
from .core.forms import File, Files, Form, MultiPart
|
|
5
|
+
from .core.headers import Headers
|
|
6
|
+
from .core.queryparams import QueryParams
|
|
7
|
+
from .core.request import Method, Request
|
|
8
|
+
from .core.response import Response, StatusCode
|
|
9
|
+
from .core.streams import Stream
|
|
10
|
+
from .core.urls import URL
|
|
11
|
+
from .server import Server
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"HTML",
|
|
15
|
+
"JSON",
|
|
16
|
+
"URL",
|
|
17
|
+
"Binary",
|
|
18
|
+
"Client",
|
|
19
|
+
"Content",
|
|
20
|
+
"File",
|
|
21
|
+
"Files",
|
|
22
|
+
"Form",
|
|
23
|
+
"Headers",
|
|
24
|
+
"Method",
|
|
25
|
+
"MultiPart",
|
|
26
|
+
"QueryParams",
|
|
27
|
+
"Request",
|
|
28
|
+
"Response",
|
|
29
|
+
"Server",
|
|
30
|
+
"StatusCode",
|
|
31
|
+
"Stream",
|
|
32
|
+
"Text",
|
|
33
|
+
"backends",
|
|
34
|
+
"parsers",
|
|
35
|
+
]
|
|
@@ -15,43 +15,9 @@ if typing.TYPE_CHECKING:
|
|
|
15
15
|
# Once we drop 3.10 support, we can use 'typing.Self'.
|
|
16
16
|
from typing_extensions import Self
|
|
17
17
|
|
|
18
|
-
from .
|
|
18
|
+
from ..core.streams import Stream
|
|
19
19
|
|
|
20
|
-
__all__ = ["NetworkBackend", "NetworkStream", "timeout"]
|
|
21
|
-
|
|
22
|
-
_timeout_stack: contextvars.ContextVar[tuple[float, ...]] = contextvars.ContextVar(
|
|
23
|
-
"timeout_context", default=()
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
@contextlib.contextmanager
|
|
28
|
-
def timeout(duration: float) -> typing.Iterator[None]:
|
|
29
|
-
"""
|
|
30
|
-
A context managed timeout API.
|
|
31
|
-
|
|
32
|
-
with timeout(1.0):
|
|
33
|
-
...
|
|
34
|
-
"""
|
|
35
|
-
now = time.monotonic()
|
|
36
|
-
until = now + duration
|
|
37
|
-
stack = (until, *_timeout_stack.get())
|
|
38
|
-
token = _timeout_stack.set(stack)
|
|
39
|
-
try:
|
|
40
|
-
yield
|
|
41
|
-
finally:
|
|
42
|
-
_timeout_stack.reset(token)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def get_current_timeout() -> float | None:
|
|
46
|
-
stack = _timeout_stack.get()
|
|
47
|
-
if not stack:
|
|
48
|
-
return None
|
|
49
|
-
soonest = min(stack)
|
|
50
|
-
now = time.monotonic()
|
|
51
|
-
remaining = soonest - now
|
|
52
|
-
if remaining <= 0.0:
|
|
53
|
-
raise TimeoutError()
|
|
54
|
-
return remaining
|
|
20
|
+
__all__ = ["NetworkBackend", "NetworkStream", "sleep", "timeout"]
|
|
55
21
|
|
|
56
22
|
|
|
57
23
|
class NetworkStream(Stream):
|
|
@@ -249,4 +215,51 @@ class NetworkBackend:
|
|
|
249
215
|
|
|
250
216
|
Semaphore = threading.Semaphore
|
|
251
217
|
Lock = threading.Lock
|
|
252
|
-
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# sleep / timeout
|
|
221
|
+
|
|
222
|
+
_timeout_stack: contextvars.ContextVar[tuple[float, ...]] = contextvars.ContextVar(
|
|
223
|
+
"timeout_context", default=()
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
@contextlib.contextmanager
|
|
228
|
+
def timeout(duration: float) -> typing.Iterator[None]:
|
|
229
|
+
"""
|
|
230
|
+
A context managed timeout API.
|
|
231
|
+
|
|
232
|
+
with timeout(1.0):
|
|
233
|
+
...
|
|
234
|
+
"""
|
|
235
|
+
now = time.monotonic()
|
|
236
|
+
until = now + duration
|
|
237
|
+
stack = (until, *_timeout_stack.get())
|
|
238
|
+
token = _timeout_stack.set(stack)
|
|
239
|
+
try:
|
|
240
|
+
yield
|
|
241
|
+
finally:
|
|
242
|
+
_timeout_stack.reset(token)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def get_current_timeout() -> float | None:
|
|
246
|
+
stack = _timeout_stack.get()
|
|
247
|
+
if not stack:
|
|
248
|
+
return None
|
|
249
|
+
soonest = min(stack)
|
|
250
|
+
now = time.monotonic()
|
|
251
|
+
remaining = soonest - now
|
|
252
|
+
if remaining <= 0.0:
|
|
253
|
+
raise TimeoutError()
|
|
254
|
+
return remaining
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def sleep(dur: float):
|
|
258
|
+
"""
|
|
259
|
+
A `sleep()` implementation that is aware of the current timeout context.
|
|
260
|
+
"""
|
|
261
|
+
remaining = get_current_timeout()
|
|
262
|
+
if remaining is not None and remaining <= dur: # pragma: nocover
|
|
263
|
+
time.sleep(remaining)
|
|
264
|
+
raise TimeoutError()
|
|
265
|
+
time.sleep(dur)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .base import Client
|
|
2
|
+
from .connection import Connection, GZipStream, open_connection
|
|
3
|
+
from .pool import ConnectionPool
|
|
4
|
+
from .transport import SendContext, Transport
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"Client",
|
|
8
|
+
"Connection",
|
|
9
|
+
"ConnectionPool",
|
|
10
|
+
"GZipStream",
|
|
11
|
+
"SendContext",
|
|
12
|
+
"Transport",
|
|
13
|
+
"open_connection",
|
|
14
|
+
]
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import types
|
|
2
2
|
import typing
|
|
3
3
|
|
|
4
|
-
from .
|
|
5
|
-
from .
|
|
6
|
-
from .
|
|
7
|
-
from .
|
|
8
|
-
from .
|
|
9
|
-
from .
|
|
4
|
+
from ..core.content import HTML, JSON, Binary, Content, Text, select_media
|
|
5
|
+
from ..core.headers import Headers
|
|
6
|
+
from ..core.request import Method, Request
|
|
7
|
+
from ..core.response import Response
|
|
8
|
+
from ..core.urls import URL
|
|
9
|
+
from .pool import ConnectionPool
|
|
10
|
+
from .transport import SendContext, Transport
|
|
10
11
|
|
|
11
12
|
__all__ = ["Client"]
|
|
12
13
|
|
|
@@ -57,26 +58,16 @@ class Client:
|
|
|
57
58
|
content: Content | bytes | None = None,
|
|
58
59
|
) -> Response:
|
|
59
60
|
request = self.build_request(method, url, headers=headers, content=content)
|
|
60
|
-
with self.via.send(request) as stream:
|
|
61
|
-
ct =
|
|
61
|
+
with self.via.send(request) as (status_code, resp_headers, stream):
|
|
62
|
+
ct = resp_headers.get("Content-Type", "application/octet-stream")
|
|
62
63
|
cls = select_media(self.media, ct)
|
|
63
64
|
content = cls.parse(stream)
|
|
64
65
|
return Response(
|
|
65
|
-
status_code=
|
|
66
|
-
headers=
|
|
66
|
+
status_code=status_code,
|
|
67
|
+
headers=resp_headers,
|
|
67
68
|
content=content,
|
|
68
69
|
)
|
|
69
70
|
|
|
70
|
-
def stream(
|
|
71
|
-
self,
|
|
72
|
-
method: Method | str,
|
|
73
|
-
url: URL | str,
|
|
74
|
-
headers: Headers | dict[str, str] | None = None,
|
|
75
|
-
content: Content | bytes | None = None,
|
|
76
|
-
) -> Response:
|
|
77
|
-
request = self.build_request(method, url, headers=headers, content=content)
|
|
78
|
-
return self.via.send(request)
|
|
79
|
-
|
|
80
71
|
def get(
|
|
81
72
|
self,
|
|
82
73
|
url: URL | str,
|
|
@@ -138,15 +129,17 @@ class RedirectMiddleware(Transport):
|
|
|
138
129
|
self._transport = transport
|
|
139
130
|
|
|
140
131
|
def build_redirect_request(
|
|
141
|
-
self, request: Request,
|
|
132
|
+
self, request: Request, ctx: SendContext
|
|
142
133
|
) -> Request | None:
|
|
134
|
+
status, headers, _ = ctx
|
|
135
|
+
|
|
143
136
|
# Redirect status codes...
|
|
144
|
-
if
|
|
137
|
+
if status not in (301, 302, 303, 307, 308):
|
|
145
138
|
return None
|
|
146
139
|
|
|
147
140
|
# Redirects need a valid location header...
|
|
148
141
|
try:
|
|
149
|
-
location = URL(
|
|
142
|
+
location = URL(headers["Location"])
|
|
150
143
|
except (KeyError, ValueError):
|
|
151
144
|
return None
|
|
152
145
|
|
|
@@ -158,21 +151,22 @@ class RedirectMiddleware(Transport):
|
|
|
158
151
|
|
|
159
152
|
return Request(method, url, headers, content)
|
|
160
153
|
|
|
161
|
-
def send(self, request: Request) ->
|
|
154
|
+
def send(self, request: Request) -> SendContext:
|
|
162
155
|
while True:
|
|
163
|
-
|
|
156
|
+
ctx = self._transport.send(request)
|
|
164
157
|
|
|
165
158
|
# Determine if we have a redirect or not.
|
|
166
|
-
redirect = self.build_redirect_request(request,
|
|
159
|
+
redirect = self.build_redirect_request(request, ctx)
|
|
167
160
|
|
|
168
161
|
# If we don't have a redirect, we're done.
|
|
169
162
|
if redirect is None:
|
|
170
|
-
return
|
|
163
|
+
return ctx
|
|
171
164
|
|
|
172
165
|
# If we have a redirect, then we read the body of the response.
|
|
173
166
|
# Ensures that the HTTP connection is available for a new
|
|
174
167
|
# request/response cycle.
|
|
175
|
-
with
|
|
168
|
+
with ctx:
|
|
169
|
+
_, _, stream = ctx
|
|
176
170
|
stream.read()
|
|
177
171
|
|
|
178
172
|
# Make the next request
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import types
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
if typing.TYPE_CHECKING:
|
|
6
|
+
# Once we drop 3.10 support, we can use 'typing.Self'.
|
|
7
|
+
from typing_extensions import Self
|
|
8
|
+
|
|
9
|
+
import zlib
|
|
10
|
+
|
|
11
|
+
from ..backends.sync import Lock, NetworkBackend
|
|
12
|
+
from ..core.content import Content
|
|
13
|
+
from ..core.headers import Headers
|
|
14
|
+
from ..core.request import Method, Request
|
|
15
|
+
from ..core.response import Response, StatusCode
|
|
16
|
+
from ..core.streams import Stream
|
|
17
|
+
from ..core.urls import URL
|
|
18
|
+
from ..parsers import HTTPParser
|
|
19
|
+
from .transport import SendContext, Transport
|
|
20
|
+
|
|
21
|
+
__all__ = ["Connection", "GZipStream", "open_connection"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Connection(Transport):
|
|
25
|
+
def __init__(self, stream: Stream, origin: URL | str):
|
|
26
|
+
self._stream = stream
|
|
27
|
+
self._origin = URL(origin) if not isinstance(origin, URL) else origin
|
|
28
|
+
self._request_lock = Lock()
|
|
29
|
+
self._parser = HTTPParser(stream, mode="CLIENT")
|
|
30
|
+
|
|
31
|
+
# API for connection pool management...
|
|
32
|
+
def origin(self) -> URL:
|
|
33
|
+
return self._origin
|
|
34
|
+
|
|
35
|
+
def is_idle(self) -> bool:
|
|
36
|
+
return self._parser.is_idle()
|
|
37
|
+
|
|
38
|
+
def is_expired(self) -> bool:
|
|
39
|
+
return self._parser.is_idle() and self._parser.keepalive_expired()
|
|
40
|
+
|
|
41
|
+
def is_closed(self) -> bool:
|
|
42
|
+
return self._parser.is_closed()
|
|
43
|
+
|
|
44
|
+
def description(self) -> str:
|
|
45
|
+
return self._parser.description()
|
|
46
|
+
|
|
47
|
+
# API entry points...
|
|
48
|
+
def send(self, request: Request) -> SendContext:
|
|
49
|
+
# async with self._request_lock:
|
|
50
|
+
# try:
|
|
51
|
+
self._send_head(request)
|
|
52
|
+
self._send_body(request)
|
|
53
|
+
code, headers = self._recv_head()
|
|
54
|
+
stream = self._recv_body(headers)
|
|
55
|
+
return SendContext((code, headers, stream))
|
|
56
|
+
|
|
57
|
+
def close(self) -> None:
|
|
58
|
+
with self._request_lock:
|
|
59
|
+
self._close()
|
|
60
|
+
|
|
61
|
+
# Top-level API for working directly with a connection.
|
|
62
|
+
def request(
|
|
63
|
+
self,
|
|
64
|
+
method: Method | str,
|
|
65
|
+
url: URL | str,
|
|
66
|
+
headers: Headers | dict[str, str] | None = None,
|
|
67
|
+
content: Content | bytes | None = None,
|
|
68
|
+
) -> Response:
|
|
69
|
+
url = self._origin.join(url)
|
|
70
|
+
request = Request(method, url, headers=headers, content=content)
|
|
71
|
+
with self.send(request) as (status_code, resp_headers, stream):
|
|
72
|
+
body = stream.read()
|
|
73
|
+
return Response(status_code, headers=resp_headers, content=body)
|
|
74
|
+
|
|
75
|
+
# Send the request...
|
|
76
|
+
def _send_head(self, request: Request) -> None:
|
|
77
|
+
method = bytes(request.method)
|
|
78
|
+
target = request.url.target.encode("ascii")
|
|
79
|
+
protocol = b"HTTP/1.1"
|
|
80
|
+
self._parser.send_method_line(method, target, protocol)
|
|
81
|
+
headers = request.headers.as_byte_pairs()
|
|
82
|
+
self._parser.send_headers(headers)
|
|
83
|
+
|
|
84
|
+
def _send_body(self, request: Request) -> None:
|
|
85
|
+
with request.content.open() as stream:
|
|
86
|
+
while data := stream.read(64 * 1024):
|
|
87
|
+
self._parser.send_body(data)
|
|
88
|
+
self._parser.send_body(b"")
|
|
89
|
+
|
|
90
|
+
# Receive the response...
|
|
91
|
+
def _recv_head(self) -> tuple[StatusCode, Headers]:
|
|
92
|
+
_, code, _ = self._parser.recv_status_line()
|
|
93
|
+
h = self._parser.recv_headers()
|
|
94
|
+
status_code = StatusCode(code)
|
|
95
|
+
headers = Headers([(k.decode("ascii"), v.decode("ascii")) for k, v in h])
|
|
96
|
+
return status_code, headers
|
|
97
|
+
|
|
98
|
+
def _recv_body(self, headers) -> Stream:
|
|
99
|
+
stream: Stream = ResponseStream(self._parser)
|
|
100
|
+
content_encoding = headers.get("Content-Encoding", "")
|
|
101
|
+
if content_encoding.lower() == "gzip":
|
|
102
|
+
stream = GZipStream(stream)
|
|
103
|
+
return stream
|
|
104
|
+
|
|
105
|
+
# Request/response cycle complete...
|
|
106
|
+
def _close(self) -> None:
|
|
107
|
+
self._parser.close()
|
|
108
|
+
|
|
109
|
+
# Builtins...
|
|
110
|
+
def __repr__(self) -> str:
|
|
111
|
+
return f"<Connection [{self._origin} {self.description()}]>"
|
|
112
|
+
|
|
113
|
+
def __enter__(self) -> "Self":
|
|
114
|
+
return self
|
|
115
|
+
|
|
116
|
+
def __exit__(
|
|
117
|
+
self,
|
|
118
|
+
exc_type: type[BaseException] | None = None,
|
|
119
|
+
exc_value: BaseException | None = None,
|
|
120
|
+
traceback: types.TracebackType | None = None,
|
|
121
|
+
):
|
|
122
|
+
self.close()
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def open_connection(
|
|
126
|
+
url: URL | str,
|
|
127
|
+
hostname: str = "",
|
|
128
|
+
backend: NetworkBackend | None = None,
|
|
129
|
+
) -> Connection:
|
|
130
|
+
|
|
131
|
+
if isinstance(url, str):
|
|
132
|
+
url = URL(url)
|
|
133
|
+
|
|
134
|
+
if url.scheme not in ("http", "https"):
|
|
135
|
+
raise ValueError("URL scheme must be 'http://' or 'https://'.")
|
|
136
|
+
if backend is None:
|
|
137
|
+
backend = NetworkBackend()
|
|
138
|
+
|
|
139
|
+
host = url.host
|
|
140
|
+
port = url.port or {"http": 80, "https": 443}[url.scheme]
|
|
141
|
+
|
|
142
|
+
if url.scheme == "https":
|
|
143
|
+
stream = backend.connect_tls(host, port, hostname)
|
|
144
|
+
else:
|
|
145
|
+
stream = backend.connect(host, port)
|
|
146
|
+
|
|
147
|
+
return Connection(stream, url)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class GZipStream(Stream):
|
|
151
|
+
def __init__(self, stream: Stream):
|
|
152
|
+
self._stream = stream
|
|
153
|
+
# Adding 16 enables gzip header and trailer handling.
|
|
154
|
+
self._decoder = zlib.decompressobj(wbits=16 + zlib.MAX_WBITS)
|
|
155
|
+
self._finished = False
|
|
156
|
+
self._buffer = bytearray()
|
|
157
|
+
|
|
158
|
+
def read(self, size=-1) -> bytes:
|
|
159
|
+
while (size < 0 or len(self._buffer) < size) and not self._finished:
|
|
160
|
+
compressed = self._stream.read(8192)
|
|
161
|
+
|
|
162
|
+
try:
|
|
163
|
+
if compressed:
|
|
164
|
+
decoded = self._decoder.decompress(compressed)
|
|
165
|
+
else:
|
|
166
|
+
decoded = self._decoder.flush()
|
|
167
|
+
except zlib.error as exc:
|
|
168
|
+
raise ValueError("Invalid gzip response content.") from exc
|
|
169
|
+
|
|
170
|
+
if not compressed:
|
|
171
|
+
if not self._decoder.eof:
|
|
172
|
+
raise ValueError("Invalid gzip response content.")
|
|
173
|
+
self._finished = True
|
|
174
|
+
|
|
175
|
+
self._buffer.extend(decoded)
|
|
176
|
+
if size < 0:
|
|
177
|
+
output = bytes(self._buffer)
|
|
178
|
+
self._buffer.clear()
|
|
179
|
+
else:
|
|
180
|
+
output = bytes(self._buffer[:size])
|
|
181
|
+
del self._buffer[:size]
|
|
182
|
+
|
|
183
|
+
return output
|
|
184
|
+
|
|
185
|
+
def close(self) -> None:
|
|
186
|
+
self._stream.close()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class ResponseStream(Stream):
|
|
190
|
+
def __init__(self, parser: HTTPParser):
|
|
191
|
+
self._parser = parser
|
|
192
|
+
self._buffer = io.BytesIO()
|
|
193
|
+
self._finished = False
|
|
194
|
+
|
|
195
|
+
def read(self, size=-1) -> bytes:
|
|
196
|
+
if self._finished:
|
|
197
|
+
return b""
|
|
198
|
+
|
|
199
|
+
sections = []
|
|
200
|
+
length = 0
|
|
201
|
+
|
|
202
|
+
# If we have any data in the buffer read that and clear the buffer.
|
|
203
|
+
buffered = self._buffer.read()
|
|
204
|
+
if buffered:
|
|
205
|
+
sections.append(buffered)
|
|
206
|
+
length += len(buffered)
|
|
207
|
+
self._buffer.seek(0)
|
|
208
|
+
self._buffer.truncate(0)
|
|
209
|
+
|
|
210
|
+
# Read each chunk in turn.
|
|
211
|
+
while (size < 0) or (length < size):
|
|
212
|
+
section = self._parser.recv_body()
|
|
213
|
+
sections.append(section)
|
|
214
|
+
length += len(section)
|
|
215
|
+
if section == b"":
|
|
216
|
+
self._finished = True
|
|
217
|
+
break
|
|
218
|
+
|
|
219
|
+
# If we've more data than requested, then push some back into the buffer.
|
|
220
|
+
output = b"".join(sections)
|
|
221
|
+
if size > -1 and len(output) > size:
|
|
222
|
+
output, remainder = output[:size], output[size:]
|
|
223
|
+
self._buffer.write(remainder)
|
|
224
|
+
self._buffer.seek(0)
|
|
225
|
+
|
|
226
|
+
return output
|
|
227
|
+
|
|
228
|
+
def close(self) -> None:
|
|
229
|
+
self._buffer.close()
|
|
230
|
+
self._parser.complete()
|
|
@@ -6,11 +6,14 @@ if typing.TYPE_CHECKING:
|
|
|
6
6
|
# Once we drop 3.10 support, we can use 'typing.Self'.
|
|
7
7
|
from typing_extensions import Self
|
|
8
8
|
|
|
9
|
-
from .
|
|
10
|
-
from .
|
|
11
|
-
from .
|
|
12
|
-
from .
|
|
13
|
-
|
|
9
|
+
from ..backends.sync import NetworkBackend, Semaphore
|
|
10
|
+
from ..core.request import Request
|
|
11
|
+
from ..core.urls import URL
|
|
12
|
+
from .connection import (
|
|
13
|
+
Connection,
|
|
14
|
+
open_connection,
|
|
15
|
+
)
|
|
16
|
+
from .transport import SendContext, Transport
|
|
14
17
|
|
|
15
18
|
__all__ = [
|
|
16
19
|
"ConnectionPool",
|
|
@@ -28,15 +31,14 @@ class ConnectionPool(Transport):
|
|
|
28
31
|
self._closed = False
|
|
29
32
|
|
|
30
33
|
# Public API...
|
|
31
|
-
def send(self, request: Request) ->
|
|
34
|
+
def send(self, request: Request) -> SendContext:
|
|
32
35
|
if self._closed:
|
|
33
36
|
raise RuntimeError("ConnectionPool is closed.")
|
|
34
37
|
|
|
35
38
|
# TODO: concurrency limiting
|
|
36
39
|
self._cleanup()
|
|
37
40
|
connection = self._get_connection(request)
|
|
38
|
-
|
|
39
|
-
return response
|
|
41
|
+
return connection.send(request)
|
|
40
42
|
|
|
41
43
|
def close(self):
|
|
42
44
|
self._closed = True
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import types
|
|
2
|
+
|
|
3
|
+
from ..core.content import Content
|
|
4
|
+
from ..core.headers import Headers
|
|
5
|
+
from ..core.request import Method, Request
|
|
6
|
+
from ..core.response import Response, StatusCode
|
|
7
|
+
from ..core.streams import Stream
|
|
8
|
+
from ..core.urls import URL
|
|
9
|
+
|
|
10
|
+
__all__ = ["SendContext", "Transport"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SendContext(tuple[StatusCode, Headers, Stream]):
|
|
14
|
+
def __enter__(self):
|
|
15
|
+
return self
|
|
16
|
+
|
|
17
|
+
def __exit__(
|
|
18
|
+
self,
|
|
19
|
+
exc_type: type[BaseException] | None = None,
|
|
20
|
+
exc_value: BaseException | None = None,
|
|
21
|
+
traceback: types.TracebackType | None = None,
|
|
22
|
+
) -> None:
|
|
23
|
+
_, _, stream = self
|
|
24
|
+
stream.close()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Transport:
|
|
28
|
+
def send(self, request: Request) -> SendContext:
|
|
29
|
+
raise NotImplementedError()
|
|
30
|
+
|
|
31
|
+
def close(self):
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
def request(
|
|
35
|
+
self,
|
|
36
|
+
method: Method | str,
|
|
37
|
+
url: URL | str,
|
|
38
|
+
headers: Headers | dict[str, str] | None = None,
|
|
39
|
+
content: Content | bytes | None = None,
|
|
40
|
+
) -> Response:
|
|
41
|
+
request = Request(method, url, headers=headers, content=content)
|
|
42
|
+
with self.send(request) as (status, resp_headers, stream):
|
|
43
|
+
body = stream.read()
|
|
44
|
+
return Response(status, headers=resp_headers, content=body)
|