From 12cb863083f13ccc896dc274f380e9f64bdcca56 Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Mon, 17 Jul 2023 12:18:03 +0100 Subject: [PATCH] Make http1::Http1Request use Url. --- Cargo.toml | 6 +++--- src/http1.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0ce94e3..db2fb25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "packeteer" description = "An attempt at a Rust library that can be used to assist in programmatically analysing, serving and handling received protocol packets." -version = "0.4.3" +version = "0.5.0" edition = "2021" authors = ["Celeste "] license = "MPL-2.0" -homepage = "https://git.colean.cc/packeteer/about/" -repository = "https://git.colean.cc/packeteer/" +homepage = "https://git.colean.cc/threeoh6000/packeteer/" +repository = "https://git.colean.cc/threeoh6000/packeteer/" readme = "README.md" keywords = ["packets", "protocol", "http", "assistance", "networking"] categories = ["data-structures", "value-formatting", "parsing", "network-programming"] diff --git a/src/http1.rs b/src/http1.rs index ed8187c..7adf597 100644 --- a/src/http1.rs +++ b/src/http1.rs @@ -1,7 +1,7 @@ //! The module handling HTTP/1.0 and HTTP/1.1 operations. //! It contains structures for HTTP/1.x requests and responses as well as functions for generating, unpacking and constructing them. //! It can also upgrade and downgrade HTTP/1.x requests. -use super::{KVHeader, generate_kvheader}; +use super::{KVHeader, generate_kvheader, Url, wrap_url, unwrap_url}; use std::str::FromStr; /// A structure for HTTP/1.x requests. @@ -9,7 +9,7 @@ use std::str::FromStr; #[derive(Debug)] pub struct Http1Request { pub method: String, - pub location: String, + pub location: Url, pub version: String, pub headers: Vec, pub body: String, @@ -29,7 +29,7 @@ pub struct Http1Response { /// By default, packeteer generates HTTP/1.1 requests. pub fn generate_request(method: &str, host: &str, location: &str, body: &str) -> Http1Request { let hostkv = generate_kvheader("Host", host); - let request = Http1Request { method: method.to_string(), location: location.to_string(), version: "1.1".to_string(), headers: vec![hostkv], body: body.to_string() }; + let request = Http1Request { method: method.to_string(), location: wrap_url(&format!("https://root/{}", location)), version: "1.1".to_string(), headers: vec![hostkv], body: body.to_string() }; return request } /// A function for generating Http1Response structures. @@ -47,19 +47,19 @@ pub fn generate_response(code: i32, body: &str) -> Http1Response { /// Construction functions aim to be as transparent as possible so apart from turning it into a structure, the request stays relatively unchanged. pub fn construct_request(request: &str) -> Http1Request { let split = request.split("\r\n"); - let mut request = Http1Request { method: "".to_string(), location: "".to_string(), version: "".to_string(), headers: vec![], body: "".to_string() }; + let mut request = Http1Request { method: "".to_string(), location: wrap_url("https://example.com"), version: "".to_string(), headers: vec![], body: "".to_string() }; let mut reachedbody = false; for v in split { if reachedbody != true { if v.contains("HTTP/1.1") { let split1: Vec<&str> = v.split(" ").collect(); request.method = split1[0].to_string(); - request.location = split1[1].to_string(); + request.location = wrap_url(&format!("https://root/{}", split1[1].to_string())); request.version = "1.1".to_string(); } else if v.contains("HTTP/1.0") { let split1: Vec<&str> = v.split(" ").collect(); request.method = split1[0].to_string(); - request.location = split1[1].to_string(); + request.location = wrap_url(&format!("https://root/{}", split1[1].to_string())); request.version = "1.0".to_string(); } else if v == "" { reachedbody = true; @@ -113,7 +113,7 @@ pub fn construct_response(response: &str) -> Http1Response { /// A function for converting Http1Request structures into HTTP/1.x request strings. /// Unpacking functions are simply taking the data stored in the structure and concatenating it into a string so nothing changes apart from the conversion. pub fn unpack_request(request: Http1Request) -> String { - let mut unpacked = format!("{} {} HTTP/{}\r\n", request.method, request.location, request.version); + let mut unpacked = format!("{} {} HTTP/{}\r\n", request.method, unwrap_url(request.location).replace("https://root/","/"), request.version); for header in request.headers { unpacked = format!("{}{}: {}\r\n", unpacked, header.key, header.value); }