Skip to content

Commit 7ca20d3

Browse files
committed
fix request uri parsing
1 parent 037d74e commit 7ca20d3

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

client.mojo

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from lightbug_http.http import HTTPRequest
2+
from lightbug_http.uri import URI
3+
from lightbug_http.sys.client import MojoClient
4+
5+
fn test_request_simple_url(inout client: MojoClient) raises -> None:
6+
"""
7+
Test making a simple GET request without parameters.
8+
Validate that we get a 200 OK response.
9+
"""
10+
var uri = URI("http://httpbin.org/")
11+
var request = HTTPRequest(uri)
12+
var response = client.do(request)
13+
14+
# print status code
15+
print("Response:", response.header.status_code())
16+
17+
# print various parsed headers
18+
print("Header", response.header.content_length())
19+
20+
# print body
21+
# print(String(response.get_body()))
22+
23+
24+
fn main() raises -> None:
25+
var client = MojoClient()
26+
print("Testing URL request")
27+
test_request_simple_url(client)
28+
print("Done")

lightbug_http/http.mojo

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ fn encode(req: HTTPRequest, uri: URI) raises -> Bytes:
241241

242242
_ = builder.write(req.header.method())
243243
_ = builder.write_string(String(" "))
244-
_ = builder.write(uri.request_uri())
244+
if len(uri.request_uri()) > 1:
245+
_ = builder.write_string(uri.request_uri())
246+
else:
247+
_ = builder.write_string("/")
245248
_ = builder.write_string(String(" "))
246249
_ = builder.write(protocol)
247250
_ = builder.write_string(String("\r\n"))

lightbug_http/uri.mojo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ struct URI:
206206
if path_start >= 0:
207207
host_and_port = remainder_uri[:path_start]
208208
request_uri = remainder_uri[path_start:]
209+
self.__host = host_and_port[:path_start]._buffer
209210
else:
210211
host_and_port = remainder_uri
211-
request_uri = strSlash # Assume root if no path is provided
212-
213-
self.__host = host_and_port[:path_start]._buffer
212+
request_uri = strSlash
213+
self.__host = host_and_port._buffer
214214

215215
if is_https:
216216
_ = self.set_scheme(https)

0 commit comments

Comments
 (0)