You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phd/src/request.rs

62 lines
1.7 KiB
Rust

4 years ago
use crate::Result;
use std::fs;
/// This struct represents a single gopher request.
#[derive(Debug, Clone)]
4 years ago
pub struct Request {
pub selector: String,
pub query: String,
4 years ago
pub root: String,
pub host: String,
pub port: u16,
}
impl Request {
/// Try to create a new request state object.
4 years ago
pub fn from(host: &str, port: u16, root: &str) -> Result<Request> {
Ok(Request {
host: host.into(),
4 years ago
port,
4 years ago
root: fs::canonicalize(root)?.to_string_lossy().into(),
selector: String::new(),
query: String::new(),
4 years ago
})
}
/// Path to the target file on disk requested by this request.
pub fn file_path(&self) -> String {
let mut path = self.root.to_string();
if !path.ends_with('/') {
path.push('/');
}
path.push_str(self.selector.replace("..", ".").trim_start_matches('/'));
path
}
/// Path to the target file relative to the server root.
pub fn relative_file_path(&self) -> String {
self.file_path().replace(&self.root, "")
}
/// Set selector + query based on what the client sent.
pub fn parse_request(&mut self, line: &str) {
self.query.clear();
self.selector.clear();
if let Some(i) = line.find('\t') {
4 years ago
if line.len() > i {
self.query.push_str(&line[i + 1..]);
self.selector.push_str(&line[..i]);
4 years ago
return;
}
}
4 years ago
self.selector.push_str(line);
// strip trailing /
if let Some(last) = self.selector.chars().last() {
if last == '/' {
self.selector.pop();
}
}
}
4 years ago
}