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

47 lines
1.1 KiB
Rust
Raw Normal View History

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