Skip to content

Commit 0cbb77b

Browse files
authored
Merge pull request #241 from thatstoasty/fix-body-reader
Fix reader not skipping over all CRLF
2 parents 0d320e7 + dfcf13d commit 0cbb77b

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

README.md

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

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

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

lightbug_http/http/request.mojo

+7-4
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

+3-2
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

+4-2
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

+9-1
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)