style: struct URL -> Url

This commit is contained in:
Compositr 2024-11-06 17:01:18 +11:00
parent 2d6df9f737
commit 4047c5ddb7
2 changed files with 12 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
http::{ http::{
requests::{Request, RequestStatus, URL}, requests::{Request, RequestStatus, Url},
responses::{Body, Response, UnitOrBoxedError}, responses::{Body, Response, UnitOrBoxedError},
}, },
threads::ThreadPool, threads::ThreadPool,
@ -97,7 +97,7 @@ impl Handlers {
Ok(()) Ok(())
} }
fn match_handler<'a>(matchers: &'a HandlersMap, url: &URL) -> Option<&'a Arc<BoxedHandlerFn>> { fn match_handler<'a>(matchers: &'a HandlersMap, url: &Url) -> Option<&'a Arc<BoxedHandlerFn>> {
'matching_loop: for (path, handler) in matchers.iter() { 'matching_loop: for (path, handler) in matchers.iter() {
// Exact match // Exact match
if path == &url.path { if path == &url.path {

View file

@ -4,12 +4,12 @@ use std::{
net::TcpStream, net::TcpStream,
}; };
pub struct URL { pub struct Url {
pub path: String, pub path: String,
pub query: HashMap<String, String>, pub query: HashMap<String, String>,
} }
impl URL { impl Url {
pub fn new(url: String) -> Option<Self> { pub fn new(url: String) -> Option<Self> {
let mut split = url.split('?'); let mut split = url.split('?');
@ -47,7 +47,7 @@ impl URL {
path path
}; };
Some(URL { path, query }) Some(Url { path, query })
} }
/// Utility function to get a path segment by index /// Utility function to get a path segment by index
@ -70,7 +70,7 @@ impl URL {
pub struct Request { pub struct Request {
stream: TcpStream, stream: TcpStream,
pub method: String, pub method: String,
pub url: URL, pub url: Url,
} }
pub enum RequestStatus { pub enum RequestStatus {
@ -99,7 +99,7 @@ impl Request {
request_line_parts[0].to_string().clone(), request_line_parts[0].to_string().clone(),
request_line_parts[1].to_string().clone(), request_line_parts[1].to_string().clone(),
); );
let url = match URL::new(url_str) { let url = match Url::new(url_str) {
Some(path) => path, Some(path) => path,
None => return RequestStatus::MalformedHTTP(stream), None => return RequestStatus::MalformedHTTP(stream),
}; };
@ -122,26 +122,26 @@ mod tests {
#[test] #[test]
fn test_url_new() { fn test_url_new() {
let url = URL::new("/".to_string()).unwrap(); let url = Url::new("/".to_string()).unwrap();
assert_eq!(url.path, "/"); assert_eq!(url.path, "/");
let url = URL::new("/path/to/resource".to_string()).unwrap(); let url = Url::new("/path/to/resource".to_string()).unwrap();
assert_eq!(url.path, "/path/to/resource"); assert_eq!(url.path, "/path/to/resource");
assert_eq!(url.query.len(), 0); assert_eq!(url.query.len(), 0);
let url = URL::new("/path/to/resource?query=string".to_string()).unwrap(); let url = Url::new("/path/to/resource?query=string".to_string()).unwrap();
assert_eq!(url.path, "/path/to/resource"); assert_eq!(url.path, "/path/to/resource");
assert_eq!(url.query.len(), 1); assert_eq!(url.query.len(), 1);
assert_eq!(url.query.get("query").unwrap(), "string"); assert_eq!(url.query.get("query").unwrap(), "string");
let url = URL::new("/path/to/resource?query=string&another=one".to_string()).unwrap(); let url = Url::new("/path/to/resource?query=string&another=one".to_string()).unwrap();
assert_eq!(url.path, "/path/to/resource"); assert_eq!(url.path, "/path/to/resource");
assert_eq!(url.query.len(), 2); assert_eq!(url.query.len(), 2);
assert_eq!(url.query.get("query").unwrap(), "string"); assert_eq!(url.query.get("query").unwrap(), "string");
assert_eq!(url.query.get("another").unwrap(), "one"); assert_eq!(url.query.get("another").unwrap(), "one");
let url = let url =
URL::new("/path/to/resource?query=string&another=one&third=3".to_string()).unwrap(); Url::new("/path/to/resource?query=string&another=one&third=3".to_string()).unwrap();
assert_eq!(url.path, "/path/to/resource"); assert_eq!(url.path, "/path/to/resource");
assert_eq!(url.query.len(), 3); assert_eq!(url.query.len(), 3);
assert_eq!(url.query.get("query").unwrap(), "string"); assert_eq!(url.query.get("query").unwrap(), "string");