Skip to content

Commit 921e036

Browse files
committed
fix serving raw body bytes
1 parent 830334b commit 921e036

File tree

4 files changed

+9
-14
lines changed

4 files changed

+9
-14
lines changed

external/libc.mojo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ fn to_char_ptr(s: String) -> UnsafePointer[c_char]:
8585
ptr[i] = ord(s[i])
8686
return ptr
8787

88-
8988
fn to_char_ptr(s: Bytes) -> UnsafePointer[c_char]:
9089
var ptr = UnsafePointer[c_char]().alloc(len(s))
9190
for i in range(len(s)):

lightbug_http/http.mojo

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ struct HTTPResponse(Response):
211211
fn get_body_bytes(self) -> BytesView:
212212
return BytesView(unsafe_ptr=self.body_raw.unsafe_ptr(), len=self.body_raw.size)
213213

214+
fn get_body(self) -> Bytes:
215+
return self.body_raw
216+
214217
fn set_body_bytes(inout self, body: Bytes) -> Self:
215218
self.body_raw = body
216219
return self
@@ -328,7 +331,7 @@ fn encode(req: HTTPRequest) raises -> StringSlice[False, ImmutableStaticLifetime
328331
return StringSlice[False, ImmutableStaticLifetime](unsafe_from_utf8_ptr=builder.render().unsafe_ptr(), len=builder.size)
329332

330333

331-
fn encode(res: HTTPResponse) raises -> String:
334+
fn encode(res: HTTPResponse) raises -> Bytes:
332335
var current_time = String()
333336
try:
334337
current_time = Morrow.utcnow().__str__()
@@ -392,8 +395,8 @@ fn encode(res: HTTPResponse) raises -> String:
392395

393396
if len(res.body_raw) > 0:
394397
_ = builder.write(res.get_body_bytes())
395-
396-
return StringSlice[False, ImmutableStaticLifetime](unsafe_from_utf8_ptr=builder.render().unsafe_ptr(), len=builder.size)
398+
399+
return builder.render().as_bytes_slice()
397400

398401
fn split_http_string(buf: Bytes) raises -> (String, String, String):
399402
var request = String(buf)

lightbug_http/sys/server.mojo

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ struct SysServer:
219219
except e:
220220
error = Error("Failed to read request body: " + e.__str__())
221221

222-
# var remaining_body = Bytes()
223-
# var remaining_len = header.content_length() - (len(request_body) + 1)
224-
# while remaining_len > 0:
225-
# var read_len = conn.read(remaining_body)
226-
# buf.extend(remaining_body)
227-
# remaining_len -= read_len
228-
229222
var res = handler.func(request)
230223

231224
if not self.tcp_keep_alive:

tests/test_http.mojo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ def test_encode_http_response():
5454
var expected_full = "HTTP/1.1 200 OK\r\nServer: lightbug_http\r\nContent-Type: application/octet-stream\r\nContent-Length: 13\r\nConnection: keep-alive\r\nDate: 2024-06-02T13:41:50.766880+00:00\r\n\r\nHello, World!"
5555

5656
var expected_headers_len = 124
57-
var hello_world_len = len(String("Hello, World!"))
57+
var hello_world_len = len(String("Hello, World!")) - 1 # -1 for the null terminator
5858
var date_header_len = len(String("Date: 2024-06-02T13:41:50.766880+00:00"))
5959

6060
var expected_split = String(expected_full).split("\r\n\r\n")
6161
var expected_headers = expected_split[0]
6262
var expected_body = expected_split[1]
63-
63+
6464
test.assert_equal(res_str[:expected_headers_len], expected_headers[:len(expected_headers) - date_header_len])
65-
test.assert_equal(res_str[(len(res_str) - hello_world_len):len(res_str)], expected_body)
65+
test.assert_equal(res_str[(len(res_str) - hello_world_len):len(res_str) + 1], expected_body)

0 commit comments

Comments
 (0)