Skip to content

Commit 34aebde

Browse files
committed
remove constants from constant struct
1 parent 7bec608 commit 34aebde

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

lightbug_http/io/bytes.mojo

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ alias Bytes = List[Byte, True]
88

99
struct Constant:
1010
alias WHITESPACE: UInt8 = ord(" ")
11-
alias COLON: UInt8 = ord(":")
12-
alias AT: UInt8 = ord("@")
1311
alias CR: UInt8 = ord("\r")
1412
alias LF: UInt8 = ord("\n")
15-
alias SLASH: UInt8 = ord("/")
16-
alias QUESTION: UInt8 = ord("?")
17-
alias ZERO: UInt8 = ord("0")
18-
alias NINE: UInt8 = ord("9")
1913

2014

2115
@always_inline

lightbug_http/uri.mojo

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from utils import Variant, StringSlice
22
from memory import Span
33
from collections import Optional, Dict
4-
from lightbug_http.io.bytes import Bytes, bytes, ByteReader, Constant
4+
from lightbug_http.io.bytes import Bytes, bytes, ByteReader
55
from lightbug_http.strings import (
66
find_all,
77
strSlash,
@@ -88,6 +88,15 @@ struct URIDelimiters:
8888
alias PATH = strSlash
8989
alias ROOT_PATH = strSlash
9090
alias CHAR_ESCAPE = "%"
91+
alias AUTHORITY = "@"
92+
alias QUERY = "?"
93+
alias SCHEME = ":"
94+
95+
96+
struct PortBounds:
97+
# For port parsing
98+
alias NINE: UInt8 = ord("9")
99+
alias ZERO: UInt8 = ord("0")
91100

92101

93102
@value
@@ -143,32 +152,32 @@ struct URI(Writable, Stringable, Representable):
143152
# Parse the scheme, if exists.
144153
# Assume http if no scheme is provided, fairly safe given the context of lightbug.
145154
var scheme: String = "http"
146-
if Constant.COLON in reader:
147-
scheme = str(reader.read_until(Constant.COLON))
155+
if ord(URIDelimiters.SCHEME) in reader:
156+
scheme = str(reader.read_until(ord(URIDelimiters.SCHEME)))
148157
if reader.read_bytes(3) != "://".as_bytes():
149158
raise Error("URI.parse: Invalid URI format, scheme should be followed by `://`. Received: " + uri)
150159

151160
# Parse the user info, if exists.
152161
var user_info: String = ""
153-
if Constant.AT in reader:
154-
user_info = str(reader.read_until(Constant.AT))
162+
if ord(URIDelimiters.AUTHORITY) in reader:
163+
user_info = str(reader.read_until(ord(URIDelimiters.AUTHORITY)))
155164
reader.increment(1)
156165

157166
# TODOs (@thatstoasty)
158167
# Handle ipv4 and ipv6 literal
159168
# Handle string host
160169
# A query right after the domain is a valid uri, but it's equivalent to example.com/?query
161170
# so we should add the normalization of paths
162-
var host_and_port = reader.read_until(Constant.SLASH)
163-
colon = host_and_port.find(Constant.COLON)
171+
var host_and_port = reader.read_until(ord(URIDelimiters.PATH))
172+
colon = host_and_port.find(ord(URIDelimiters.SCHEME))
164173
var host: String
165174
var port: Optional[UInt16] = None
166175
if colon != -1:
167176
host = str(host_and_port[:colon])
168177
var port_end = colon + 1
169178
# loop through the post colon chunk until we find a non-digit character
170179
for b in host_and_port[colon + 1 :]:
171-
if b[] < Constant.ZERO or b[] > Constant.NINE:
180+
if b[] < PortBounds.ZERO or b[] > PortBounds.NINE:
172181
break
173182
port_end += 1
174183
port = UInt16(atol(str(host_and_port[colon + 1 : port_end])))
@@ -177,7 +186,7 @@ struct URI(Writable, Stringable, Representable):
177186

178187
# Reads until either the start of the query string, or the end of the uri.
179188
var unquote_reader = reader.copy()
180-
var original_path_bytes = unquote_reader.read_until(Constant.QUESTION)
189+
var original_path_bytes = unquote_reader.read_until(ord(URIDelimiters.QUERY))
181190
var original_path: String
182191
if not original_path_bytes:
183192
original_path = "/"
@@ -187,16 +196,16 @@ struct URI(Writable, Stringable, Representable):
187196
# Parse the path
188197
var path: String = "/"
189198
var request_uri: String = "/"
190-
if reader.available() and reader.peek() == Constant.SLASH:
199+
if reader.available() and reader.peek() == ord(URIDelimiters.PATH):
191200
# Copy the remaining bytes to read the request uri.
192201
var request_uri_reader = reader.copy()
193202
request_uri = str(request_uri_reader.read_bytes())
194203
# Read until the query string, or the end if there is none.
195-
path = unquote(str(reader.read_until(Constant.QUESTION)), disallowed_escapes=List(str("/")))
204+
path = unquote(str(reader.read_until(ord(URIDelimiters.QUERY))), disallowed_escapes=List(str("/")))
196205

197206
# Parse query
198207
var query: String = ""
199-
if reader.available() and reader.peek() == Constant.QUESTION:
208+
if reader.available() and reader.peek() == ord(URIDelimiters.QUERY):
200209
# TODO: Handle fragments for anchors
201210
query = str(reader.read_bytes()[1:])
202211

0 commit comments

Comments
 (0)