Make http1::Http1Request use Url.
This commit is contained in:
parent
4d2e355011
commit
12cb863083
2 changed files with 10 additions and 10 deletions
|
@ -1,12 +1,12 @@
|
||||||
[package]
|
[package]
|
||||||
name = "packeteer"
|
name = "packeteer"
|
||||||
description = "An attempt at a Rust library that can be used to assist in programmatically analysing, serving and handling received protocol packets."
|
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"
|
edition = "2021"
|
||||||
authors = ["Celeste <colean@colean.cc>"]
|
authors = ["Celeste <colean@colean.cc>"]
|
||||||
license = "MPL-2.0"
|
license = "MPL-2.0"
|
||||||
homepage = "https://git.colean.cc/packeteer/about/"
|
homepage = "https://git.colean.cc/threeoh6000/packeteer/"
|
||||||
repository = "https://git.colean.cc/packeteer/"
|
repository = "https://git.colean.cc/threeoh6000/packeteer/"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["packets", "protocol", "http", "assistance", "networking"]
|
keywords = ["packets", "protocol", "http", "assistance", "networking"]
|
||||||
categories = ["data-structures", "value-formatting", "parsing", "network-programming"]
|
categories = ["data-structures", "value-formatting", "parsing", "network-programming"]
|
||||||
|
|
14
src/http1.rs
14
src/http1.rs
|
@ -1,7 +1,7 @@
|
||||||
//! The module handling HTTP/1.0 and HTTP/1.1 operations.
|
//! 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 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.
|
//! 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;
|
use std::str::FromStr;
|
||||||
|
|
||||||
/// A structure for HTTP/1.x requests.
|
/// A structure for HTTP/1.x requests.
|
||||||
|
@ -9,7 +9,7 @@ use std::str::FromStr;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Http1Request {
|
pub struct Http1Request {
|
||||||
pub method: String,
|
pub method: String,
|
||||||
pub location: String,
|
pub location: Url,
|
||||||
pub version: String,
|
pub version: String,
|
||||||
pub headers: Vec<KVHeader>,
|
pub headers: Vec<KVHeader>,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
|
@ -29,7 +29,7 @@ pub struct Http1Response {
|
||||||
/// By default, packeteer generates HTTP/1.1 requests.
|
/// By default, packeteer generates HTTP/1.1 requests.
|
||||||
pub fn generate_request(method: &str, host: &str, location: &str, body: &str) -> Http1Request {
|
pub fn generate_request(method: &str, host: &str, location: &str, body: &str) -> Http1Request {
|
||||||
let hostkv = generate_kvheader("Host", host);
|
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
|
return request
|
||||||
}
|
}
|
||||||
/// A function for generating Http1Response structures.
|
/// 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.
|
/// 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 {
|
pub fn construct_request(request: &str) -> Http1Request {
|
||||||
let split = request.split("\r\n");
|
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;
|
let mut reachedbody = false;
|
||||||
for v in split {
|
for v in split {
|
||||||
if reachedbody != true {
|
if reachedbody != true {
|
||||||
if v.contains("HTTP/1.1") {
|
if v.contains("HTTP/1.1") {
|
||||||
let split1: Vec<&str> = v.split(" ").collect();
|
let split1: Vec<&str> = v.split(" ").collect();
|
||||||
request.method = split1[0].to_string();
|
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();
|
request.version = "1.1".to_string();
|
||||||
} else if v.contains("HTTP/1.0") {
|
} else if v.contains("HTTP/1.0") {
|
||||||
let split1: Vec<&str> = v.split(" ").collect();
|
let split1: Vec<&str> = v.split(" ").collect();
|
||||||
request.method = split1[0].to_string();
|
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();
|
request.version = "1.0".to_string();
|
||||||
} else if v == "" {
|
} else if v == "" {
|
||||||
reachedbody = true;
|
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.
|
/// 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.
|
/// 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 {
|
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 {
|
for header in request.headers {
|
||||||
unpacked = format!("{}{}: {}\r\n", unpacked, header.key, header.value);
|
unpacked = format!("{}{}: {}\r\n", unpacked, header.key, header.value);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue