diff options
author | Melody Horn <melody@boringcactus.com> | 2021-12-24 16:25:51 -0700 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-12-24 16:25:51 -0700 |
commit | 3817ec811d76b91d3d4924003a4d48d0cd43a576 (patch) | |
tree | 93cd9ac73e08c9d4414bb312f7a01f104581a160 /src/utils | |
parent | c6d12d80babd9f4bd5692cf74ab9d6e9278859d1 (diff) | |
download | narchttpd-3817ec811d76b91d3d4924003a4d48d0cd43a576.tar.gz narchttpd-3817ec811d76b91d3d4924003a4d48d0cd43a576.zip |
rust is a myth. it doesn't existconfig-toml
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/proxy_child.rs | 69 | ||||
-rw-r--r-- | src/utils/serve_static.rs | 102 |
2 files changed, 74 insertions, 97 deletions
diff --git a/src/utils/proxy_child.rs b/src/utils/proxy_child.rs index d7a021d..cc35298 100644 --- a/src/utils/proxy_child.rs +++ b/src/utils/proxy_child.rs @@ -1,9 +1,12 @@ +use std::path::PathBuf; use std::process::{Child as ChildProcess, Command}; use std::rc::Rc; +use std::sync::Arc; use hyper::http::uri::Scheme; use hyper::{header, Body, Client, Request, Response}; -use rhai::{Dynamic, FnPtr, Map}; + +use super::{async_trait, HttpHandler}; pub struct KillOnDrop(ChildProcess); @@ -15,60 +18,48 @@ impl Drop for KillOnDrop { #[derive(Clone)] pub struct ProxyChild { - process: Rc<KillOnDrop>, + process: Arc<KillOnDrop>, port: u16, } impl ProxyChild { - fn new(params: Map) -> Self { - let command_line = params["command"].clone().into_immutable_string().unwrap(); - let port = params["port"].as_int().unwrap(); + pub fn new(command_line: String, in_dir: Option<PathBuf>, port: u16) -> Self { let mut command_line = command_line.split(" "); let command = command_line.next().unwrap(); let mut child = Command::new(command); child.args(command_line); - if let Some(cwd) = params.get("in_dir") { - let cwd = cwd.clone().into_immutable_string().unwrap(); - let cwd: &str = cwd.as_ref(); + if let Some(cwd) = in_dir { child.current_dir(cwd); } let child = child.spawn().unwrap(); Self { - process: Rc::new(KillOnDrop(child)), + process: Arc::new(KillOnDrop(child)), port: port as u16, } } } -pub fn handle_request(child: &mut ProxyChild, request: Rc<Request<Body>>) -> Rc<Response<Body>> { - let ProxyChild { port, .. } = child; - let mut request_uri = request.uri().clone().into_parts(); - // TODO ipv6 loopback? - request_uri.authority = Some(format!("127.0.0.1:{}", port).parse().unwrap()); - request_uri.scheme = Some(Scheme::HTTP); - let mut proxy_request = Request::builder() - .method(request.method()) - .uri(request_uri) - .header(header::HOST, request.headers()[header::HOST].clone()); - proxy_request.headers_mut().unwrap().extend( - request - .headers() - .iter() - .map(|(x, y)| (x.clone(), y.clone())), - ); - // TODO handle nonempty body - let proxy_request = proxy_request.body(Body::empty()).unwrap(); - let response = async { +#[async_trait] +impl HttpHandler for ProxyChild { + async fn handle(&self, request: Request<Body>) -> Response<Body> { + let ProxyChild { port, .. } = self; + let mut request_uri = request.uri().clone().into_parts(); + // TODO ipv6 loopback? + request_uri.authority = Some(format!("127.0.0.1:{}", port).parse().unwrap()); + request_uri.scheme = Some(Scheme::HTTP); + let mut proxy_request = Request::builder() + .method(request.method()) + .uri(request_uri) + .header(header::HOST, request.headers()[header::HOST].clone()); + proxy_request.headers_mut().unwrap().extend( + request + .headers() + .iter() + .map(|(x, y)| (x.clone(), y.clone())), + ); + // TODO handle nonempty body + let proxy_request = proxy_request.body(Body::empty()).unwrap(); let client = Client::new(); - Rc::new(client.request(proxy_request).await.unwrap()) - }; - let runtime = tokio::runtime::Handle::current(); - runtime.block_on(response) -} - -pub fn proxy_child(params: Map) -> FnPtr { - let child = ProxyChild::new(params); - let mut result = FnPtr::new("handle_request_proxy_child").unwrap(); - result.add_curry(Dynamic::from(child)); - result + client.request(proxy_request).await.unwrap() + } } diff --git a/src/utils/serve_static.rs b/src/utils/serve_static.rs index 2ec0a90..c087c21 100644 --- a/src/utils/serve_static.rs +++ b/src/utils/serve_static.rs @@ -1,75 +1,61 @@ 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}; + +use super::{async_trait, HttpHandler}; #[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(); +impl Params { + pub fn new(root: PathBuf) -> Self { let root = root.canonicalize().unwrap(); - Ok(Self { root }) + 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(""))) +#[async_trait] +impl HttpHandler for Params { + async fn handle(&self, request: Request<Body>) -> Response<Body> { + let Params { root } = self; + 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 + }; + response.unwrap() + } } |