Skip to content

Commit 045e8ac

Browse files
committed
fix the client
1 parent 639cd00 commit 045e8ac

File tree

6 files changed

+22
-25
lines changed

6 files changed

+22
-25
lines changed

client.mojo

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ from lightbug_http.sys.client import MojoClient
55

66
fn test_request(inout client: MojoClient) raises -> None:
77
var uri = URI("http://httpbin.org/status/404")
8+
try:
9+
uri.parse()
10+
except e:
11+
print("error parsing uri: " + e.__str__())
12+
13+
814
var request = HTTPRequest(uri)
915
var response = client.do(request)
1016

@@ -17,9 +23,10 @@ fn test_request(inout client: MojoClient) raises -> None:
1723
# print parsed headers (only some are parsed for now)
1824
print("Content-Type:", String(response.header.content_type()))
1925
print("Content-Length", response.header.content_length())
20-
print("Connection:", response.header.connection_close())
2126
print("Server:", String(response.header.server()))
2227

28+
print("Is connection set to connection-close? ", response.header.connection_close())
29+
2330
# print body
2431
print(String(response.get_body_bytes()))
2532

lightbug_http/header.mojo

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -678,21 +678,16 @@ struct ResponseHeader:
678678
raise Error("Could not find HTTP version in response line: " + String(b))
679679

680680
_ = self.set_protocol(b[:first_whitespace])
681+
682+
var end_of_status_code = first_whitespace+5 # status code is always 3 digits, this calculation includes null terminator
681683

682-
var last_whitespace = last_index_byte(b, bytes(whitespace, pop=False)[0]) + 1
683-
684-
if last_whitespace < 0:
685-
raise Error("Could not find status code or in response line: " + String(b))
686-
elif last_whitespace == 0:
687-
raise Error("Response URI is empty: " + String(b))
684+
var status_code = atol(b[first_whitespace+1:end_of_status_code])
685+
_ = self.set_status_code(status_code)
688686

689-
var status_text = b[last_whitespace :]
687+
var status_text = b[end_of_status_code + 1 :]
690688
if len(status_text) > 1:
691689
_ = self.set_status_message(status_text)
692690

693-
var status_code = atol(b[first_whitespace+1:last_whitespace])
694-
_ = self.set_status_code(status_code)
695-
696691
return len(buf) - len(b_next)
697692

698693
fn parse_headers(inout self, buf: Bytes) raises -> None:

lightbug_http/http.mojo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ fn encode(req: HTTPRequest) raises -> StringSlice[False, ImmutableStaticLifetime
282282

283283
_ = builder.write(req.header.method())
284284
_ = builder.write_string(whitespace)
285-
if len(req.header.request_uri()) > 1:
286-
_ = builder.write(req.header.request_uri())
285+
if len(req.uri().path_bytes()) > 1:
286+
_ = builder.write_string(req.uri().path())
287287
else:
288288
_ = builder.write_string(strSlash)
289289
_ = builder.write_string(whitespace)
@@ -324,7 +324,7 @@ fn encode(req: HTTPRequest) raises -> StringSlice[False, ImmutableStaticLifetime
324324

325325
if len(req.body_raw) > 0:
326326
_ = builder.write(req.get_body_bytes())
327-
327+
328328
return StringSlice[False, ImmutableStaticLifetime](unsafe_from_utf8_ptr=builder.render().unsafe_ptr(), len=builder.size)
329329

330330

lightbug_http/io/bytes.mojo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn last_index_byte(buf: Bytes, c: Byte) -> Int:
3737
fn compare_case_insensitive(a: Bytes, b: Bytes) -> Bool:
3838
if len(a) != len(b):
3939
return False
40-
for i in range(len(a)):
41-
if a[i].__xor__(0x20) != b[i].__xor__(0x20):
40+
for i in range(len(a) - 1):
41+
if (a[i] | 0x20) != (b[i] | 0x20):
4242
return False
4343
return True
4444

lightbug_http/sys/client.mojo

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ struct MojoClient(Client):
6565
If there is a failure in sending or receiving the message.
6666
"""
6767
var uri = req.uri()
68-
try:
69-
_ = uri.parse()
70-
except e:
71-
print("error parsing uri: " + e.__str__())
72-
7368
var host = String(uri.host())
7469

7570
if host == "":
@@ -94,7 +89,7 @@ struct MojoClient(Client):
9489
port = 80
9590

9691
var conn = create_connection(self.fd, host_str, port)
97-
92+
9893
var req_encoded = encode(req)
9994

10095
var bytes_sent = conn.write(req_encoded)
@@ -103,6 +98,7 @@ struct MojoClient(Client):
10398

10499
var new_buf = Bytes(capacity=default_buffer_size)
105100
var bytes_recv = conn.read(new_buf)
101+
106102
if bytes_recv == 0:
107103
conn.close()
108104

lightbug_http/uri.mojo

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct URI:
117117
self.__path = normalise_path(bytes(path), self.__path_original)
118118
return self
119119

120-
fn set_path_sbytes(inout self, path: Bytes) -> Self:
120+
fn set_path_bytes(inout self, path: Bytes) -> Self:
121121
self.__path = normalise_path(path, self.__path_original)
122122
return self
123123

@@ -287,8 +287,7 @@ struct URI:
287287
self.__path_original = bytes(request_uri, pop=False)
288288
self.__query_string = Bytes()
289289

290-
_ = self.set_path_sbytes(normalise_path(self.__path_original, self.__path_original))
291-
290+
_ = self.set_path_bytes(normalise_path(self.__path_original, self.__path_original))
292291
_ = self.set_request_uri_bytes(bytes(request_uri, pop=False))
293292

294293

0 commit comments

Comments
 (0)