Files
rust-http-server/src/request.rs
T

53 lines
1.2 KiB
Rust

use std::str::FromStr;
#[derive(Debug)]
pub struct ReqLine {
pub method: String,
pub route: String,
pub version: String,
}
impl FromStr for ReqLine {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (method, rest) = s
.split_once(" ")
.ok_or(std::io::Error::other("Missing route"))?;
let (route, version) = rest
.split_once(" ")
.ok_or(std::io::Error::other("Missing version"))?;
Ok(Self {
method: method.trim().to_string(),
route: route.trim().to_string(),
version: version.trim().to_string(),
})
}
}
#[derive(Debug)]
pub struct Header {
pub key: String,
pub value: String,
}
impl FromStr for Header {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (key, value) = s
.split_once(":")
.ok_or(std::io::Error::other("Missing value"))?;
Ok(Self {
key: key.trim().to_string(),
value: value.trim().to_string(),
})
}
}
#[derive(Debug)]
pub struct Request {
pub req_line: ReqLine,
pub headers: Vec<Header>,
}