Skip to content

Commit fc9b238

Browse files
committed
fix scheme parsing bug and remove constants
1 parent 34aebde commit fc9b238

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

lightbug_http/client.mojo

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,23 @@ struct Client:
5555
"""
5656
if request.uri.host == "":
5757
raise Error("Client.do: Host must not be empty.")
58-
if not request.uri.port:
59-
raise Error("Client.do: You must specify the port to connect on.")
6058

6159
var is_tls = False
6260
var scheme = Scheme.HTTP
6361
if request.uri.is_https():
6462
is_tls = True
6563
scheme = Scheme.HTTPS
6664

67-
var uri = URI.parse(request.uri.host)
68-
var pool_key = PoolKey(uri.host, uri.port.value(), scheme)
65+
port = request.uri.port.value() if request.uri.port else 80
66+
var pool_key = PoolKey(request.uri.host, port, scheme)
6967
var cached_connection = False
7068
var conn: TCPConnection
7169
try:
7270
conn = self._connections.take(pool_key)
7371
cached_connection = True
7472
except e:
7573
if str(e) == "PoolManager.take: Key not found.":
76-
conn = create_connection(uri.host, uri.port.value())
74+
conn = create_connection(request.uri.host, port)
7775
else:
7876
logger.error(e)
7977
raise Error("Client.do: Failed to create a connection to host.")

lightbug_http/io/bytes.mojo

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
from utils import StringSlice
22
from memory.span import Span, _SpanIter
3+
from lightbug_http.strings import BytesConstant
34
from lightbug_http.net import default_buffer_size
45

56

67
alias Bytes = List[Byte, True]
78

89

9-
struct Constant:
10-
alias WHITESPACE: UInt8 = ord(" ")
11-
alias CR: UInt8 = ord("\r")
12-
alias LF: UInt8 = ord("\n")
13-
14-
1510
@always_inline
1611
fn byte(s: String) -> Byte:
1712
return ord(s)
@@ -24,12 +19,12 @@ fn bytes(s: String) -> Bytes:
2419

2520
@always_inline
2621
fn is_newline(b: Byte) -> Bool:
27-
return b == Constant.LF or b == Constant.CR
22+
return b == BytesConstant.nChar or b == BytesConstant.rChar
2823

2924

3025
@always_inline
3126
fn is_space(b: Byte) -> Bool:
32-
return b == Constant.WHITESPACE
27+
return b == BytesConstant.whitespace
3328

3429

3530
struct ByteWriter(Writer):
@@ -223,7 +218,7 @@ struct ByteReader[origin: Origin]:
223218

224219
@always_inline
225220
fn read_word(mut self) -> ByteView[origin]:
226-
return self.read_until(Constant.WHITESPACE)
221+
return self.read_until(BytesConstant.whitespace)
227222

228223
fn read_line(mut self) -> ByteView[origin]:
229224
var start = self.read_pos
@@ -237,7 +232,7 @@ struct ByteReader[origin: Origin]:
237232
if not self.available():
238233
return ret
239234

240-
if self._inner[self.read_pos] == Constant.CR:
235+
if self._inner[self.read_pos] == BytesConstant.rChar:
241236
self.increment(2)
242237
else:
243238
self.increment()
@@ -254,7 +249,7 @@ struct ByteReader[origin: Origin]:
254249
@always_inline
255250
fn skip_carriage_return(mut self):
256251
for i in range(self.read_pos, len(self._inner)):
257-
if self._inner[i] == Constant.CR:
252+
if self._inner[i] == BytesConstant.rChar:
258253
self.increment(2)
259254
else:
260255
break

lightbug_http/uri.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct URI(Writable, Stringable, Representable):
152152
# Parse the scheme, if exists.
153153
# Assume http if no scheme is provided, fairly safe given the context of lightbug.
154154
var scheme: String = "http"
155-
if ord(URIDelimiters.SCHEME) in reader:
155+
if "://" in uri:
156156
scheme = str(reader.read_until(ord(URIDelimiters.SCHEME)))
157157
if reader.read_bytes(3) != "://".as_bytes():
158158
raise Error("URI.parse: Invalid URI format, scheme should be followed by `://`. Received: " + uri)

0 commit comments

Comments
 (0)