Skip to content

Commit 6102aa1

Browse files
committed
fix up request uri
1 parent 16fcc12 commit 6102aa1

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

lightbug_http/io/bytes.mojo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ struct ByteReader[origin: Origin]:
182182
self._inner = b
183183
self.read_pos = 0
184184

185+
fn copy(self) -> Self:
186+
return ByteReader(self._inner[self.read_pos :])
187+
185188
fn __contains__(self, b: Byte) -> Bool:
186189
for i in range(self.read_pos, len(self._inner)):
187190
if self._inner[i] == b:

lightbug_http/uri.mojo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ struct URI(Writable, Stringable, Representable):
117117

118118
# Parse the path
119119
var path: String = "/"
120+
var request_uri: String = "/"
120121
if reader.available() and reader.peek() == Constant.SLASH:
122+
# Copy the remaining bytes to read the request uri.
123+
var request_uri_reader = reader.copy()
124+
request_uri = str(request_uri_reader.read_bytes())
121125
# Read until the query string, or the end if there is none.
122126
path = str(reader.read_until(Constant.QUESTION))
123127

@@ -136,7 +140,7 @@ struct URI(Writable, Stringable, Representable):
136140
host=host,
137141
port=port,
138142
full_uri=uri,
139-
request_uri=uri,
143+
request_uri=request_uri,
140144
username="",
141145
password="",
142146
)

tests/lightbug_http/http/test_request.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_request_from_bytes():
88
var request = HTTPRequest.from_bytes("127.0.0.1", 4096, data.as_bytes())
99
testing.assert_equal(request.protocol, "HTTP/1.1")
1010
testing.assert_equal(request.method, "GET")
11-
testing.assert_equal(request.uri.request_uri, "127.0.0.1/redirect")
11+
testing.assert_equal(request.uri.request_uri, "/redirect")
1212
testing.assert_equal(request.headers["Host"], "127.0.0.1:8080")
1313
testing.assert_equal(request.headers["User-Agent"], "python-requests/2.32.3")
1414

tests/lightbug_http/io/test_byte_writer.mojo

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@ from lightbug_http.io.bytes import Bytes, ByteWriter
55
def test_write_byte():
66
var w = ByteWriter()
77
w.write_byte(0x01)
8-
testing.assert_equal(w.consume(), Bytes(0x01))
8+
testing.assert_equal(w^.consume(), Bytes(0x01))
9+
10+
w = ByteWriter()
911
w.write_byte(2)
10-
testing.assert_equal(w.consume(), Bytes(2))
12+
testing.assert_equal(w^.consume(), Bytes(2))
1113

1214

1315
def test_consuming_write():
1416
var w = ByteWriter()
1517
var my_string: String = "World"
1618
w.consuming_write("Hello ")
1719
w.consuming_write(my_string^)
18-
testing.assert_equal(w.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100))
20+
testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100))
1921

22+
w = ByteWriter()
2023
var my_bytes = Bytes(72, 101, 108, 108, 111, 32)
2124
w.consuming_write(my_bytes^)
2225
w.consuming_write(Bytes(87, 111, 114, 108, 10))
23-
testing.assert_equal(w.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100))
26+
testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100))
2427

2528

2629
def test_write():
2730
var w = ByteWriter()
2831
w.write("Hello", ", ")
2932
w.write_bytes("World!".as_bytes())
30-
testing.assert_equal(w.consume(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))
33+
testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))

tests/lightbug_http/test_uri.mojo

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_uri_parse_http_with_port():
1919
testing.assert_equal(uri.port.value(), 8080)
2020
testing.assert_equal(uri.path, "/index.html")
2121
testing.assert_equal(uri._original_path, "/index.html")
22-
# testing.assert_equal(uri.request_uri, "http://example.com:8080/index.html")
22+
testing.assert_equal(uri.request_uri, "/index.html")
2323
testing.assert_equal(uri.is_https(), False)
2424
testing.assert_equal(uri.is_http(), True)
2525
testing.assert_equal(uri.query_string, empty_string)
@@ -32,7 +32,7 @@ def test_uri_parse_https_with_port():
3232
testing.assert_equal(uri.port.value(), 8080)
3333
testing.assert_equal(uri.path, "/index.html")
3434
testing.assert_equal(uri._original_path, "/index.html")
35-
# testing.assert_equal(uri.request_uri, "https://example.com:8080/index.html")
35+
testing.assert_equal(uri.request_uri, "/index.html")
3636
testing.assert_equal(uri.is_https(), True)
3737
testing.assert_equal(uri.is_http(), False)
3838
testing.assert_equal(uri.query_string, empty_string)
@@ -44,7 +44,7 @@ def test_uri_parse_http_with_path():
4444
testing.assert_equal(uri.host, "example.com")
4545
testing.assert_equal(uri.path, "/index.html")
4646
testing.assert_equal(uri._original_path, "/index.html")
47-
# testing.assert_equal(uri.request_uri, "http://example.com/index.html")
47+
testing.assert_equal(uri.request_uri, "/index.html")
4848
testing.assert_equal(uri.is_https(), False)
4949
testing.assert_equal(uri.is_http(), True)
5050
testing.assert_equal(uri.query_string, empty_string)
@@ -56,7 +56,7 @@ def test_uri_parse_https_with_path():
5656
testing.assert_equal(uri.host, "example.com")
5757
testing.assert_equal(uri.path, "/index.html")
5858
testing.assert_equal(uri._original_path, "/index.html")
59-
# testing.assert_equal(uri.request_uri, "https://example.com/index.html")
59+
testing.assert_equal(uri.request_uri, "/index.html")
6060
testing.assert_equal(uri.is_https(), True)
6161
testing.assert_equal(uri.is_http(), False)
6262
testing.assert_equal(uri.query_string, empty_string)
@@ -68,7 +68,7 @@ def test_uri_parse_http_basic():
6868
testing.assert_equal(uri.host, "example.com")
6969
testing.assert_equal(uri.path, "/")
7070
testing.assert_equal(uri._original_path, "/")
71-
# testing.assert_equal(uri.request_uri, "/")
71+
testing.assert_equal(uri.request_uri, "/")
7272
testing.assert_equal(uri.query_string, empty_string)
7373

7474

@@ -78,7 +78,7 @@ def test_uri_parse_http_basic_www():
7878
testing.assert_equal(uri.host, "www.example.com")
7979
testing.assert_equal(uri.path, "/")
8080
testing.assert_equal(uri._original_path, "/")
81-
# testing.assert_equal(uri.request_uri, "/")
81+
testing.assert_equal(uri.request_uri, "/")
8282
testing.assert_equal(uri.query_string, empty_string)
8383

8484

@@ -88,7 +88,7 @@ def test_uri_parse_http_with_query_string():
8888
testing.assert_equal(uri.host, "www.example.com")
8989
testing.assert_equal(uri.path, "/job")
9090
testing.assert_equal(uri._original_path, "/job")
91-
# testing.assert_equal(uri.request_uri, "/job?title=engineer")
91+
testing.assert_equal(uri.request_uri, "/job?title=engineer")
9292
testing.assert_equal(uri.query_string, "title=engineer")
9393

9494

@@ -98,5 +98,20 @@ def test_uri_parse_no_scheme():
9898
testing.assert_equal(uri.host, "www.example.com")
9999

100100

101+
def test_uri_ip_address_no_scheme():
102+
var uri = URI.parse("168.22.0.1/path/to/favicon.ico")
103+
testing.assert_equal(uri.scheme, "http")
104+
testing.assert_equal(uri.host, "168.22.0.1")
105+
testing.assert_equal(uri.path, "/path/to/favicon.ico")
106+
107+
108+
def test_uri_ip_address():
109+
var uri = URI.parse("http://168.22.0.1:8080/path/to/favicon.ico")
110+
testing.assert_equal(uri.scheme, "http")
111+
testing.assert_equal(uri.host, "168.22.0.1")
112+
testing.assert_equal(uri.path, "/path/to/favicon.ico")
113+
testing.assert_equal(uri.port.value(), 8080)
114+
115+
101116
# def test_uri_parse_http_with_hash():
102117
# ...

0 commit comments

Comments
 (0)