@@ -186,54 +186,39 @@ struct URI:
186186
187187 # Defaults to HTTP/1.1
188188 var proto_str = String(strHttp11)
189-
190- # Parse requestURI
191- var n = raw_uri.rfind(" " )
192- if n < 0 :
193- n = len (raw_uri)
194- elif n == 0 :
195- raise Error(" Request URI cannot be empty" )
196- else :
197- var proto = raw_uri[n + 1 :]
198- if proto != strHttp11:
199- proto_str = proto
200-
201- var request_uri = raw_uri[:n]
202-
203- # Parse host from requestURI
204- n = request_uri.find(" ://" )
205-
206189 var is_https = False
207190
208- if n >= 0 :
209- var host_and_port = request_uri[n + 3 :]
210-
211- if request_uri[:n] == https:
191+ # Parse the protocol
192+ var proto_end = raw_uri.find(" ://" )
193+ var remainder_uri : String
194+ if proto_end >= 0 :
195+ proto_str = raw_uri[:proto_end]
196+ if proto_str == https:
212197 is_https = True
213-
214- n = host_and_port.find(" /" )
215- if n >= 0 :
216- self .__host = host_and_port[:n]._buffer
217- request_uri = request_uri[n + 3 :]
218- else :
219- self .__host = host_and_port._buffer
220- request_uri = strSlash
198+ remainder_uri = raw_uri[proto_end + 3 :]
199+ else :
200+ raise Error(" Invalid URI: Missing protocol" )
201+
202+ # Parse the host and optional port
203+ var path_start = remainder_uri.find(" /" )
204+ var host_and_port : String
205+ var request_uri : String
206+ if path_start >= 0 :
207+ host_and_port = remainder_uri[:path_start]
208+ request_uri = remainder_uri[path_start:]
221209 else :
222- n = request_uri.find(" /" )
223- if n >= 0 :
224- self .__host = request_uri[:n]._buffer
225- request_uri = request_uri[n:]
226- else :
227- self .__host = request_uri._buffer
228- request_uri = strSlash
210+ 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
229214
230215 if is_https:
231216 _ = self .set_scheme(https)
232217 else :
233218 _ = self .set_scheme(http)
234-
219+
235220 # Parse path
236- n = request_uri.find(" ?" )
221+ var n = request_uri.find(" ?" )
237222 if n >= 0 :
238223 self .__path_original = request_uri[:n]._buffer
239224 self .__query_string = request_uri[n + 1 :]._buffer
0 commit comments