Skip to content

Commit

Permalink
Parse URI queries in to key value pairs
Browse files Browse the repository at this point in the history
- Adds `queries` dict to request uri object containing the uri queries
  as key value pairs
  • Loading branch information
izo0x90 committed Jan 26, 2025
1 parent 997774c commit 3815b38
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions lightbug_http/uri.mojo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Dict
from utils import Variant
from lightbug_http.io.bytes import Bytes, bytes
from lightbug_http.strings import (
Expand All @@ -10,13 +11,28 @@ from lightbug_http.strings import (
https,
)

alias QueryMap = Dict[String, String]


struct QueryDelimiters:
alias STRING_START = "?"
alias ITEM = "&"
alias ITEM_ASSIGN = "="


struct URIDelimiters:
alias SCHEMA = "://"
alias PATH = strSlash
alias ROOT_PATH = strSlash


@value
struct URI(Writable, Stringable, Representable):
var _original_path: String
var scheme: String
var path: String
var query_string: String
var queries: QueryMap
var _hash: String
var host: String

Expand All @@ -27,11 +43,11 @@ struct URI(Writable, Stringable, Representable):
var password: String

@staticmethod
fn parse(uri: String) -> URI:
fn parse(uri: String) raises -> URI:
var proto_str = String(strHttp11)
var is_https = False

var proto_end = uri.find("://")
var proto_end = uri.find(URIDelimiters.SCHEMA)
var remainder_uri: String
if proto_end >= 0:
proto_str = uri[:proto_end]
Expand All @@ -41,7 +57,7 @@ struct URI(Writable, Stringable, Representable):
else:
remainder_uri = uri

var path_start = remainder_uri.find("/")
var path_start = remainder_uri.find(URIDelimiters.PATH)
var host_and_port: String
var request_uri: String
var host: String
Expand All @@ -60,7 +76,7 @@ struct URI(Writable, Stringable, Representable):
else:
scheme = http

var n = request_uri.find("?")
var n = request_uri.find(QueryDelimiters.STRING_START)
var original_path: String
var query_string: String
if n >= 0:
Expand All @@ -70,11 +86,24 @@ struct URI(Writable, Stringable, Representable):
original_path = request_uri
query_string = ""

var queries = QueryMap()
if query_string:
var query_items = query_string.split(QueryDelimiters.ITEM)

for item in query_items:
var key_val = item[].split(QueryDelimiters.ITEM_ASSIGN, 1)

if key_val[0]:
queries[key_val[0]] = ""
if len(key_val) == 2:
queries[key_val[0]] = key_val[1]

return URI(
_original_path=original_path,
scheme=scheme,
path=original_path,
query_string=query_string,
queries=queries,
_hash="",
host=host,
full_uri=uri,
Expand All @@ -84,9 +113,9 @@ struct URI(Writable, Stringable, Representable):
)

fn __str__(self) -> String:
var result = String.write(self.scheme, "://", self.host, self.path)
var result = String.write(self.scheme, URIDelimiters.SCHEMA, self.host, self.path)
if len(self.query_string) > 0:
result.write("?", self.query_string)
result.write(QueryDelimiters.STRING_START, self.query_string)
return result^

fn __repr__(self) -> String:
Expand Down

0 comments on commit 3815b38

Please sign in to comment.