Skip to content

Commit c7235f4

Browse files
committed
Merge branch 'main' into feature/parse-localhost
2 parents 1f38c87 + 0cbb77b commit c7235f4

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,22 @@ Once you have a Mojo project set up locally,
9696
For example, to make a `Printer` service that prints some details about the request to console:
9797

9898
```mojo
99-
from lightbug_http import *
99+
from lightbug_http.http import HTTPRequest, HTTPResponse, OK
100+
from lightbug_http.strings import to_string
101+
from lightbug_http.header import HeaderKey
100102
101103
@value
102104
struct Printer(HTTPService):
103105
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
104-
var uri = req.uri
105-
print("Request URI: ", to_string(uri.request_uri))
106-
107-
var header = req.headers
108-
print("Request protocol: ", req.protocol)
109-
print("Request method: ", req.method)
110-
print(
111-
"Request Content-Type: ", to_string(header[HeaderKey.CONTENT_TYPE])
112-
)
113-
114-
var body = req.body_raw
115-
print("Request Body: ", to_string(body))
116-
117-
return OK(body)
106+
print("Request URI:", req.uri.request_uri)
107+
print("Request protocol:", req.protocol)
108+
print("Request method:", req.method)
109+
if HeaderKey.CONTENT_TYPE in req.headers:
110+
print("Request Content-Type:", req.headers[HeaderKey.CONTENT_TYPE])
111+
if req.body_raw:
112+
print("Request Body:", to_string(req.body_raw))
113+
114+
return OK(req.body_raw)
118115
```
119116

120117
6. Start a server listening on a port with your service like so.

lightbug_http/http/request.mojo

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ struct HTTPRequest(Writable, Stringable):
5656
var request = HTTPRequest(
5757
URI.parse(addr + uri), headers=headers, method=method, protocol=protocol, cookies=cookies
5858
)
59-
try:
60-
request.read_body(reader, content_length, max_body_size)
61-
except e:
62-
raise Error("HTTPRequest.from_bytes: Failed to read request body: " + str(e))
59+
60+
if content_length > 0:
61+
try:
62+
reader.skip_carriage_return()
63+
request.read_body(reader, content_length, max_body_size)
64+
except e:
65+
raise Error("HTTPRequest.from_bytes: Failed to read request body: " + str(e))
6366

6467
return request
6568

lightbug_http/server.mojo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,15 @@ struct Server(Movable):
161161
var response: HTTPResponse
162162
try:
163163
response = handler.func(request)
164-
except:
164+
except e:
165+
logger.error("Unexpected error in the handler:", e)
166+
165167
if not conn.is_closed():
166168
# Try to send back an internal server error, but always attempt to teardown the connection.
167169
try:
168170
# TODO: Move InternalError response to an alias when Mojo can support Dict operations at compile time. (@thatstoasty)
169171
_ = conn.write(encode(InternalError()))
170172
except e:
171-
logger.error(e)
172173
raise Error("Failed to send InternalError response")
173174
finally:
174175
conn.teardown()

lightbug_http/service.mojo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ struct Printer(HTTPService):
1515
print("Request URI:", req.uri.request_uri)
1616
print("Request protocol:", req.protocol)
1717
print("Request method:", req.method)
18-
print("Request Content-Type:", req.headers[HeaderKey.CONTENT_TYPE])
19-
print("Request Body:", to_string(req.body_raw))
18+
if HeaderKey.CONTENT_TYPE in req.headers:
19+
print("Request Content-Type:", req.headers[HeaderKey.CONTENT_TYPE])
20+
if req.body_raw:
21+
print("Request Body:", to_string(req.body_raw))
2022

2123
return OK(req.body_raw)
2224

tests/lightbug_http/http/test_request.mojo

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ def test_request_from_bytes():
1818

1919

2020
def test_read_body():
21-
...
21+
alias data = "GET /redirect HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nUser-Agent: python-requests/2.32.3\r\nAccept-Encoding: gzip, deflate, br, zstd\r\nAccept: */\r\nContent-Length: 17\r\nconnection: keep-alive\r\n\r\nThis is the body!"
22+
var request = HTTPRequest.from_bytes("127.0.0.1", 4096, data.as_bytes())
23+
testing.assert_equal(request.protocol, "HTTP/1.1")
24+
testing.assert_equal(request.method, "GET")
25+
testing.assert_equal(request.uri.request_uri, "/redirect")
26+
testing.assert_equal(request.headers["Host"], "127.0.0.1:8080")
27+
testing.assert_equal(request.headers["User-Agent"], "python-requests/2.32.3")
28+
29+
testing.assert_equal(request.get_body(), "This is the body!")
2230

2331

2432
def test_encode():

0 commit comments

Comments
 (0)