11from utils import Variant, StringSlice
22from memory import Span
33from 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
55from 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