Skip to content

Commit f8d1ae1

Browse files
Fixups (#78)
1 parent 55403e7 commit f8d1ae1

File tree

6 files changed

+36
-30
lines changed

6 files changed

+36
-30
lines changed

scripts/docs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ md.treeprocessors.register(
8080

8181

8282
def not_found():
83-
headers = {'Content-Type': 'text/plain'}
84-
content = b'Not Found'
85-
return httpx.Response(404, headers=headers, content=content)
83+
text = httpx.Text('Not Found')
84+
return httpx.Response(404, content=text)
8685

8786

8887
def web_server(request):
@@ -95,8 +94,8 @@ def web_server(request):
9594
state.file = file
9695
content = md.convert(text)
9796
html = template.render(content=content).encode('utf-8')
98-
headers = {'Content-Type': 'text/html; charset=utf-8'}
99-
return httpx.Response(200, headers=headers, content=html)
97+
content = httpx.HTML(html)
98+
return httpx.Response(200, content=html)
10099

101100

102101
@click.group()

src/ahttpx/_models.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ def __init__(
114114
# The presence of a message-body in a request is signaled by the
115115
# inclusion of a Content-Length or Transfer-Encoding header field in
116116
# the request's message-headers.
117-
if self.stream.size is None:
117+
content_length: int | None = self.stream.size
118+
if content_length is None:
118119
self.headers = self.headers.copy_set("Transfer-Encoding", "chunked")
119-
elif self.stream.size > 0:
120-
self.headers = self.headers.copy_set("Content-Length", str(stream.size))
120+
elif content_length > 0:
121+
self.headers = self.headers.copy_set("Content-Length", str(content_length))
121122

122123
async def read(self):
123124
self.body = b"".join([part async for part in self.stream])
@@ -160,10 +161,15 @@ def __init__(
160161
# MUST NOT include a message-body. All other responses do include a
161162
# message-body, although it MAY be of zero length.
162163
if code >= 200 and code != 204 and code != 304:
163-
if self.stream.size is None:
164+
content_length: int | None = self.stream.size
165+
if content_length is None:
164166
self.headers = self.headers.copy_set("Transfer-Encoding", "chunked")
165167
else:
166-
self.headers = self.headers.copy_set("Content-Length", str(stream.size))
168+
self.headers = self.headers.copy_set("Content-Length", str(content_length))
169+
170+
@property
171+
def reason_phrase(self):
172+
return _codes.get(self.code, "Unknown Status Code")
167173

168174
async def read(self):
169175
self.body = b"".join([part async for part in self.stream])
@@ -173,5 +179,4 @@ def close(self):
173179
self.stream = ClosedStream()
174180

175181
def __repr__(self):
176-
phrase = _codes.get(self.code, "UNKNOWN")
177-
return f"<Response [{self.code} {phrase}]>"
182+
return f"<Response [{self.code} {self.reason_phrase}]>"

src/httpx/_content.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
21
import os
3-
import re
42
import typing
53
import urllib.parse
64

@@ -319,6 +317,8 @@ def __init__(self, data: typing.Any) -> None:
319317
self._data = data
320318

321319
def encode(self) -> tuple[Stream, str]:
320+
import json
321+
322322
content = json.dumps(
323323
self._data,
324324
ensure_ascii=False,

src/httpx/_headers.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import typing
22

3-
4-
__all__ = [
5-
"Headers",
6-
]
3+
__all__ = ["Headers"]
74

85

96
class Headers(typing.Mapping[str, str]):

src/httpx/_models.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ def __init__(
114114
# The presence of a message-body in a request is signaled by the
115115
# inclusion of a Content-Length or Transfer-Encoding header field in
116116
# the request's message-headers.
117-
if self.stream.size is None:
117+
content_length: int | None = self.stream.size
118+
if content_length is None:
118119
self.headers = self.headers.copy_set("Transfer-Encoding", "chunked")
119-
elif self.stream.size > 0:
120-
self.headers = self.headers.copy_set("Content-Length", str(stream.size))
120+
elif content_length > 0:
121+
self.headers = self.headers.copy_set("Content-Length", str(content_length))
121122

122123
def read(self):
123124
self.body = b"".join([part for part in self.stream])
@@ -160,10 +161,15 @@ def __init__(
160161
# MUST NOT include a message-body. All other responses do include a
161162
# message-body, although it MAY be of zero length.
162163
if code >= 200 and code != 204 and code != 304:
163-
if self.stream.size is None:
164+
content_length: int | None = self.stream.size
165+
if content_length is None:
164166
self.headers = self.headers.copy_set("Transfer-Encoding", "chunked")
165167
else:
166-
self.headers = self.headers.copy_set("Content-Length", str(stream.size))
168+
self.headers = self.headers.copy_set("Content-Length", str(content_length))
169+
170+
@property
171+
def reason_phrase(self):
172+
return _codes.get(self.code, "Unknown Status Code")
167173

168174
def read(self):
169175
self.body = b"".join([part for part in self.stream])
@@ -173,5 +179,4 @@ def close(self):
173179
self.stream = ClosedStream()
174180

175181
def __repr__(self):
176-
phrase = _codes.get(self.code, "UNKNOWN")
177-
return f"<Response [{self.code} {phrase}]>"
182+
return f"<Response [{self.code} {self.reason_phrase}]>"

src/httpx/_server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import contextlib
33
import select
44
import socket
5-
import time
65
import threading
6+
import time
7+
import traceback
78

89
import h11
910
import httpx
@@ -32,23 +33,22 @@ def __init__(self, stream, endpoint):
3233
def handle_requests(self):
3334
try:
3435
method, url, headers = self._recv_head()
35-
print(method, url)
3636
stream = IterByteStream(self._recv_body())
37-
print(stream)
3837
# TODO: Handle endpoint exceptions
3938
try:
4039
request = httpx.Request(method, url, headers=headers, content=stream)
4140
response = self._endpoint(request)
4241
except Exception as exc:
43-
print(exc)
4442
content = httpx.Text("Internal Server Error")
4543
response = httpx.Response(code=500, content=content)
4644
self._send_head(response)
4745
self._send_body(response)
4846
else:
49-
print(response)
5047
self._send_head(response)
5148
self._send_body(response)
49+
finally:
50+
status_line = f"{request.method} {request.url.target} [{response.code} {response.reason_phrase}]"
51+
print(status_line)
5252
except ConnectionClosed:
5353
pass
5454
finally:

0 commit comments

Comments
 (0)