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

53 lines
1.2 KiB
Rust
Raw Normal View History

2026-08-18 13:39:36 +08:00
use std::str::FromStr;
#[derive(Debug)]
2026-08-20 00:44:24 +08:00
pub struct ReqLine {
pub method: String,
pub route: String,
pub version: String,
2026-08-18 13:39:36 +08:00
}
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)]
2026-08-20 00:44:24 +08:00
pub struct Header {
pub key: String,
pub value: String,
2026-08-18 13:39:36 +08:00
}
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(),
})
}
}
2026-08-20 00:44:24 +08:00
#[derive(Debug)]
pub struct Request {
pub req_line: ReqLine,
pub headers: Vec<Header>,
}