Skip to content

Commit c631c44

Browse files
committed
fix reader not skipping over all CRLF
1 parent 0d320e7 commit c631c44

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

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/io/bytes.mojo

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct ByteReader[origin: Origin]:
205205
raise OutOfBoundsError
206206

207207
self.read_pos += count
208+
print(start, start + count)
208209
return self._inner[start : start + count]
209210

210211
fn read_until(mut self, char: Byte) -> ByteView[origin]:

lightbug_http/service.mojo

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from lightbug_http.http import HTTPRequest, HTTPResponse, OK, NotFound
22
from lightbug_http.io.bytes import Bytes, bytes
33
from lightbug_http.strings import to_string
44
from lightbug_http.header import HeaderKey
5+
from utils import StringSlice
56

67

78
trait HTTPService:

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)