hip 0.2__tar.gz → 0.3__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.2 → hip-0.3}/PKG-INFO +4 -3
- {hip-0.2 → hip-0.3}/docs/README.md +3 -2
- {hip-0.2 → hip-0.3}/pyproject.toml +1 -6
- {hip-0.2 → hip-0.3}/src/hip/__init__.py +1 -11
- {hip-0.2 → hip-0.3}/src/hip/_body.py +47 -2
- {hip-0.2 → hip-0.3}/src/hip/_client.py +5 -3
- {hip-0.2 → hip-0.3}/src/hip/_connection.py +1 -1
- {hip-0.2 → hip-0.3}/src/hip/_server.py +8 -11
- {hip-0.2 → hip-0.3}/src/hip/_streams.py +0 -16
- {hip-0.2 → hip-0.3}/src/hip/_urlencode.py +0 -3
- hip-0.2/src/hip/__version__.py +0 -2
- {hip-0.2 → hip-0.3}/.gitignore +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_content.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_headers.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_network.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_parsers.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_pool.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_request.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_response.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_urlparse.py +0 -0
- {hip-0.2 → hip-0.3}/src/hip/_urls.py +0 -0
{hip-0.2 → hip-0.3}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: hip
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3
|
|
4
4
|
Summary: HTTP, for Python.
|
|
5
5
|
Author-email: Kim Christie <noreply@lovelydinosaur.com>
|
|
6
6
|
Classifier: Development Status :: 4 - Beta
|
|
@@ -63,8 +63,9 @@ Creating web apps…
|
|
|
63
63
|
... content = hip.HTML('<html><body>¡Hola, mundo!</body></html>')
|
|
64
64
|
... return hip.Response(200, content=content)
|
|
65
65
|
|
|
66
|
-
>>>
|
|
67
|
-
|
|
66
|
+
>>> with hip.Server(app) as server:
|
|
67
|
+
... print(server.url)
|
|
68
|
+
... server.until_interrupt()
|
|
68
69
|
```
|
|
69
70
|
|
|
70
71
|
---
|
|
@@ -44,8 +44,9 @@ Creating web apps…
|
|
|
44
44
|
... content = hip.HTML('<html><body>¡Hola, mundo!</body></html>')
|
|
45
45
|
... return hip.Response(200, content=content)
|
|
46
46
|
|
|
47
|
-
>>>
|
|
48
|
-
|
|
47
|
+
>>> with hip.Server(app) as server:
|
|
48
|
+
... print(server.url)
|
|
49
|
+
... server.until_interrupt()
|
|
49
50
|
```
|
|
50
51
|
|
|
51
52
|
---
|
|
@@ -4,6 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "hip"
|
|
7
|
+
version = "0.3"
|
|
7
8
|
description = "HTTP, for Python."
|
|
8
9
|
readme = "docs/README.md"
|
|
9
10
|
requires-python = ">=3.10"
|
|
@@ -25,18 +26,12 @@ classifiers = [
|
|
|
25
26
|
dependencies = [
|
|
26
27
|
"truststore",
|
|
27
28
|
]
|
|
28
|
-
dynamic = [
|
|
29
|
-
"version"
|
|
30
|
-
]
|
|
31
29
|
|
|
32
30
|
[tool.hatch.build.targets.sdist]
|
|
33
31
|
include = [
|
|
34
32
|
"/src",
|
|
35
33
|
]
|
|
36
34
|
|
|
37
|
-
[tool.hatch.version]
|
|
38
|
-
path = "src/hip/__version__.py"
|
|
39
|
-
|
|
40
35
|
[dependency-groups]
|
|
41
36
|
dev = [
|
|
42
37
|
"mkdocs==2.0.dev6",
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
from .__version__ import __title__, __version__
|
|
2
1
|
from ._client import * # Client
|
|
3
2
|
from ._connection import * # Connection, Transport
|
|
4
3
|
from ._content import * # Binary, Content, File, Files, Form, HTML, JSON, MultiPart, Text
|
|
@@ -8,9 +7,8 @@ from ._parsers import * # HTTPParser, ProtocolError
|
|
|
8
7
|
from ._pool import * # ConnectionPool
|
|
9
8
|
from ._request import * # Method, Request
|
|
10
9
|
from ._response import * # StatusCode, Response
|
|
11
|
-
from ._server import * # Server
|
|
10
|
+
from ._server import * # Server
|
|
12
11
|
from ._streams import * # ByteStream, DuplexStream, FileStream, Stream # ByteStream, DuplexStream, FileStream, Stream
|
|
13
|
-
from ._urlencode import * # quote, unquote, urldecode, urlencode
|
|
14
12
|
from ._urls import * # QueryParams, URL
|
|
15
13
|
|
|
16
14
|
__all__ = [
|
|
@@ -43,14 +41,6 @@ __all__ = [
|
|
|
43
41
|
"Stream",
|
|
44
42
|
"Text",
|
|
45
43
|
"Transport",
|
|
46
|
-
"__title__",
|
|
47
|
-
"__version__",
|
|
48
44
|
"open_connection",
|
|
49
|
-
"quote",
|
|
50
|
-
"run",
|
|
51
|
-
"serve_http",
|
|
52
45
|
"timeout",
|
|
53
|
-
"unquote",
|
|
54
|
-
"urldecode",
|
|
55
|
-
"urlencode",
|
|
56
46
|
]
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import io
|
|
2
|
+
import zlib
|
|
2
3
|
|
|
3
4
|
from ._content import Content
|
|
4
5
|
from ._headers import Headers
|
|
@@ -17,11 +18,16 @@ class RequestContent(Content):
|
|
|
17
18
|
|
|
18
19
|
|
|
19
20
|
class ResponseContent(Content):
|
|
20
|
-
def __init__(self, parser: HTTPParser):
|
|
21
|
+
def __init__(self, parser: HTTPParser, headers: Headers):
|
|
21
22
|
self._parser = parser
|
|
23
|
+
self._headers = headers
|
|
22
24
|
|
|
23
25
|
def open(self) -> Stream:
|
|
24
|
-
|
|
26
|
+
stream = HTTPStream(self._parser, is_response=True)
|
|
27
|
+
content_encoding = self._headers.get("Content-Encoding", "")
|
|
28
|
+
if content_encoding.lower() == "gzip":
|
|
29
|
+
return GZipStream(stream)
|
|
30
|
+
return stream
|
|
25
31
|
|
|
26
32
|
def headers(self) -> Headers:
|
|
27
33
|
return Headers()
|
|
@@ -71,3 +77,42 @@ class HTTPStream(Stream):
|
|
|
71
77
|
self._buffer.close()
|
|
72
78
|
if self._is_response:
|
|
73
79
|
self._parser.complete()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class GZipStream(Stream):
|
|
83
|
+
def __init__(self, stream: Stream):
|
|
84
|
+
self._stream = stream
|
|
85
|
+
# Adding 16 enables gzip header and trailer handling.
|
|
86
|
+
self._decoder = zlib.decompressobj(wbits=16 + zlib.MAX_WBITS)
|
|
87
|
+
self._finished = False
|
|
88
|
+
self._buffer = bytearray()
|
|
89
|
+
|
|
90
|
+
def read(self, size=-1) -> bytes:
|
|
91
|
+
while (size < 0 or len(self._buffer) < size) and not self._finished:
|
|
92
|
+
compressed = self._stream.read(8192)
|
|
93
|
+
|
|
94
|
+
try:
|
|
95
|
+
if compressed:
|
|
96
|
+
decoded = self._decoder.decompress(compressed)
|
|
97
|
+
else:
|
|
98
|
+
decoded = self._decoder.flush()
|
|
99
|
+
except zlib.error as exc:
|
|
100
|
+
raise ValueError("Invalid gzip response content.") from exc
|
|
101
|
+
|
|
102
|
+
if not compressed:
|
|
103
|
+
if not self._decoder.eof:
|
|
104
|
+
raise ValueError("Invalid gzip response content.")
|
|
105
|
+
self._finished = True
|
|
106
|
+
|
|
107
|
+
self._buffer.extend(decoded)
|
|
108
|
+
if size < 0:
|
|
109
|
+
output = bytes(self._buffer)
|
|
110
|
+
self._buffer.clear()
|
|
111
|
+
else:
|
|
112
|
+
output = bytes(self._buffer[:size])
|
|
113
|
+
del self._buffer[:size]
|
|
114
|
+
|
|
115
|
+
return output
|
|
116
|
+
|
|
117
|
+
def close(self) -> None:
|
|
118
|
+
self._stream.close()
|
|
@@ -20,11 +20,13 @@ class Client:
|
|
|
20
20
|
if url is None:
|
|
21
21
|
url = ""
|
|
22
22
|
if headers is None:
|
|
23
|
-
headers = {
|
|
23
|
+
headers = {
|
|
24
|
+
"User-Agent": "dev",
|
|
25
|
+
"Accept-Encoding": "gzip",
|
|
26
|
+
}
|
|
27
|
+
self.headers = Headers(headers)
|
|
24
28
|
self.transport = ConnectionPool()
|
|
25
|
-
|
|
26
29
|
self.url = URL(url)
|
|
27
|
-
self.headers = Headers(headers)
|
|
28
30
|
self.via = RedirectMiddleware(self.transport)
|
|
29
31
|
self.media = {
|
|
30
32
|
"application/json": JSON,
|
|
@@ -82,7 +82,7 @@ class Connection(Transport):
|
|
|
82
82
|
self._send_head(request)
|
|
83
83
|
self._send_body(request)
|
|
84
84
|
code, headers = self._recv_head()
|
|
85
|
-
content = ResponseContent(self._parser)
|
|
85
|
+
content = ResponseContent(self._parser, headers)
|
|
86
86
|
return Response(code, headers=headers, content=content)
|
|
87
87
|
|
|
88
88
|
def close(self) -> None:
|
|
@@ -53,9 +53,6 @@ class HTTPConnection:
|
|
|
53
53
|
except BaseException:
|
|
54
54
|
logger.exception("Internal Server Error")
|
|
55
55
|
|
|
56
|
-
def close(self):
|
|
57
|
-
self._parser.close()
|
|
58
|
-
|
|
59
56
|
# Receive the request...
|
|
60
57
|
def _recv_head(self) -> tuple[str, str, list[tuple[str, str]]]:
|
|
61
58
|
method, target, _ = self._parser.recv_method_line()
|
|
@@ -65,9 +62,6 @@ class HTTPConnection:
|
|
|
65
62
|
h = [(k.decode("latin-1"), v.decode("latin-1")) for k, v in headers]
|
|
66
63
|
return m, t, h
|
|
67
64
|
|
|
68
|
-
def _recv_body(self):
|
|
69
|
-
return self._parser.recv_body()
|
|
70
|
-
|
|
71
65
|
# Return the response...
|
|
72
66
|
def _send_head(self, response: Response):
|
|
73
67
|
protocol = b"HTTP/1.1"
|
|
@@ -113,11 +107,14 @@ class Server:
|
|
|
113
107
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
114
108
|
self._tcp_server.__exit__(exc_type, exc_val, exc_tb)
|
|
115
109
|
|
|
110
|
+
def until_interrupt(self): # pragma: no cover
|
|
111
|
+
"""Keep a context-managed server alive until Ctrl+C is pressed."""
|
|
112
|
+
try:
|
|
113
|
+
while True:
|
|
114
|
+
sleep(1)
|
|
115
|
+
except KeyboardInterrupt:
|
|
116
|
+
pass
|
|
117
|
+
|
|
116
118
|
def handle_stream(self, stream):
|
|
117
119
|
connection = HTTPConnection(stream, self.app)
|
|
118
120
|
connection.handle_requests()
|
|
119
|
-
|
|
120
|
-
def serve(self):
|
|
121
|
-
with self:
|
|
122
|
-
while True:
|
|
123
|
-
sleep(1)
|
|
@@ -15,10 +15,6 @@ class Stream:
|
|
|
15
15
|
def close(self) -> None:
|
|
16
16
|
raise NotImplementedError()
|
|
17
17
|
|
|
18
|
-
@property
|
|
19
|
-
def size(self) -> int | None:
|
|
20
|
-
return None
|
|
21
|
-
|
|
22
18
|
def __enter__(self):
|
|
23
19
|
return self
|
|
24
20
|
|
|
@@ -42,10 +38,6 @@ class ByteStream(Stream):
|
|
|
42
38
|
def close(self) -> None:
|
|
43
39
|
self._buffer.close()
|
|
44
40
|
|
|
45
|
-
@property
|
|
46
|
-
def size(self) -> int | None:
|
|
47
|
-
return self._size
|
|
48
|
-
|
|
49
41
|
|
|
50
42
|
class DuplexStream(Stream):
|
|
51
43
|
"""
|
|
@@ -97,10 +89,6 @@ class FileStream(Stream):
|
|
|
97
89
|
if self._fileobj is not None:
|
|
98
90
|
self._fileobj.close()
|
|
99
91
|
|
|
100
|
-
@property
|
|
101
|
-
def size(self) -> int | None:
|
|
102
|
-
return self._size
|
|
103
|
-
|
|
104
92
|
def __enter__(self):
|
|
105
93
|
self.open()
|
|
106
94
|
return self
|
|
@@ -193,7 +181,3 @@ class MultiPartStream(Stream):
|
|
|
193
181
|
self._filestream.close()
|
|
194
182
|
self._filestream = None
|
|
195
183
|
self._buffer.close()
|
|
196
|
-
|
|
197
|
-
@property
|
|
198
|
-
def size(self) -> int | None:
|
|
199
|
-
return None
|
hip-0.2/src/hip/__version__.py
DELETED
{hip-0.2 → hip-0.3}/.gitignore
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|