diff options
author | Melody Horn <melody@boringcactus.com> | 2021-11-12 22:38:39 -0700 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-11-12 22:38:39 -0700 |
commit | f79b75710167088e2031a82a94a8d127fbb16a1f (patch) | |
tree | ffbbd167c34864d92b1d1e0a3b40da77e0b06874 /src/utils/serve_static.rs | |
parent | fad6b7b755b788ca0d9113999faa8fb0d6f89ad0 (diff) | |
download | narchttpd-f79b75710167088e2031a82a94a8d127fbb16a1f.tar.gz narchttpd-f79b75710167088e2031a82a94a8d127fbb16a1f.zip |
god is dead and we have killed him.
Diffstat (limited to 'src/utils/serve_static.rs')
-rw-r--r-- | src/utils/serve_static.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/utils/serve_static.rs b/src/utils/serve_static.rs new file mode 100644 index 0000000..2ec0a90 --- /dev/null +++ b/src/utils/serve_static.rs @@ -0,0 +1,75 @@ +use std::fs::File; +use std::io::{ErrorKind, Read}; +use std::path::PathBuf; +use std::rc::Rc; + +use hyper::{Body, Request, Response, StatusCode}; +use rhai::{Dynamic, FnPtr, Map}; + +#[derive(Clone, Debug)] +pub struct Params { + root: PathBuf, +} + +#[derive(Debug)] +pub struct KeyError(&'static str); + +impl TryFrom<Map> for Params { + type Error = KeyError; + + fn try_from(mut value: Map) -> Result<Self, Self::Error> { + let root = value + .remove("root") + .and_then(|value| value.into_immutable_string().ok()) + .ok_or(KeyError("root"))?; + let root: PathBuf = root.parse().unwrap(); + let root = root.canonicalize().unwrap(); + Ok(Self { root }) + } +} + +pub fn handle_request(params: &mut Params, request: Rc<Request<Body>>) -> Rc<Response<Body>> { + let Params { root } = params; + eprintln!("handling request {:?}", request); + let request_path = request.uri().path(); + let request_path = request_path.trim_start_matches("/"); + let target = root.join(request_path); + let target = if target.is_dir() { + target.join("index.html") + } else { + target + }; + eprintln!("target is {}", target.display()); + let response = if target.ancestors().all(|ancestor| ancestor != root) { + // oops! all directory traversal + Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::empty()) + } else { + let file = File::open(target); + match file { + Ok(mut file) => { + let mut result = Vec::new(); + file.read_to_end(&mut result).unwrap(); + Response::builder().body(Body::from(result)) + } + Err(err) => { + let status_code = match err.kind() { + ErrorKind::NotFound => StatusCode::NOT_FOUND, + _ => StatusCode::BAD_REQUEST, + }; + Response::builder() + .status(status_code) + .body(Body::from(status_code.canonical_reason().unwrap_or(""))) + } + } + }; + Rc::new(response.unwrap()) +} + +pub fn serve_static(params: Map) -> FnPtr { + let params: Params = params.try_into().unwrap(); + let mut result = FnPtr::new("handle_request_serve_static").unwrap(); + result.add_curry(Dynamic::from(params)); + result +} |