diff --git a/Cargo.toml b/Cargo.toml index e3a6b86..67b5885 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" [dependencies] base64 = "0.12" +blake3 = "0.3" futures = "0.3" http = "0.2" hyper = "0.13" diff --git a/src/bin/server.rs b/src/bin/server.rs index 94b16be..b4aedb4 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -2,5 +2,9 @@ use std::error::Error; #[tokio::main] async fn main () -> Result <(), Box > { - ptth::server::main ().await + let opt = ptth::server::Opt { + root: "/home/user".into (), + }; + + ptth::server::main (opt).await } diff --git a/src/lib.rs b/src/lib.rs index 781be02..e7feeec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,16 +15,9 @@ pub mod server; #[cfg (test)] mod tests { - use std::{ - error::Error, - time::Duration, - }; - use tokio::{ - prelude::*, runtime::Runtime, spawn, - time::delay_for, }; use super::{ @@ -44,12 +37,35 @@ mod tests { relay::main ().await.unwrap (); }); + spawn (async { + let opt = server::Opt { + root: ".".into (), + }; + + server::main (opt).await.unwrap (); + }); + let client = Client::new (); let resp = client.get ("http://127.0.0.1:4000/relay_up_check") .send ().await.unwrap ().bytes ().await.unwrap (); assert_eq! (resp, "Relay is up\n"); + + let resp = client.get ("http://127.0.0.1:4000/http_request/alien_wildlands/COPYING") + .send ().await.unwrap ().bytes ().await.unwrap (); + + assert_eq! (blake3::hash (&resp), blake3::Hash::from ([ + 0xca, 0x02, 0x92, 0x78, + 0x9c, 0x0a, 0x0e, 0xcb, + 0xa7, 0x06, 0xf4, 0xb3, + 0xf3, 0x49, 0x30, 0x07, + + 0xa9, 0x95, 0x17, 0x31, + 0xc1, 0xd4, 0x32, 0xc5, + 0x2c, 0x4a, 0xac, 0x1f, + 0x1a, 0xbb, 0xa8, 0xef, + ])); }); } } diff --git a/src/server/file_server.rs b/src/server/file_server.rs index b76e439..0748f0b 100644 --- a/src/server/file_server.rs +++ b/src/server/file_server.rs @@ -4,7 +4,7 @@ use std::{ cmp::{min, max}, convert::{Infallible, TryInto}, io::SeekFrom, - path::PathBuf, + path::{Path, PathBuf}, }; // file_server shouldn't depend on reqwest, but for now it @@ -205,6 +205,7 @@ async fn serve_error ( } pub async fn serve_all ( + root: &Path, parts: http_serde::RequestParts ) -> http_serde::Response @@ -234,7 +235,7 @@ pub async fn serve_all ( let encoded_path = &parts.uri [1..]; let path = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().unwrap (); - let mut full_path = PathBuf::from ("/home/user"); + let mut full_path = PathBuf::from (root); full_path.push (&*path); if let Ok (dir) = read_dir (&full_path).await { diff --git a/src/server/mod.rs b/src/server/mod.rs index 825058e..8de8fc0 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,5 +1,6 @@ use std::{ error::Error, + path::PathBuf, sync::Arc, time::Duration, }; @@ -20,6 +21,7 @@ const RELAY_URL: &str = "http://127.0.0.1:4000"; const SERVER_NAME: &str = "alien_wildlands"; async fn handle_req_resp ( + opt: &Opt, client: &Client, req_resp: reqwest::Response ) -> Result <(), Box > { @@ -39,7 +41,7 @@ async fn handle_req_resp ( let (req_id, parts) = (wrapped_req.id, wrapped_req.req); - let response = file_server::serve_all (parts).await; + let response = file_server::serve_all (&opt.root, parts).await; let mut resp_req = client .post (&format! ("{}/http_response/{}", RELAY_URL, req_id)) @@ -57,8 +59,13 @@ async fn handle_req_resp ( Ok (()) } -pub async fn main () -> Result <(), Box > { +pub struct Opt { + pub root: PathBuf, +} + +pub async fn main (opt: Opt) -> Result <(), Box > { let client = Arc::new (Client::new ()); + let opt = Arc::new (opt); let mut backoff_delay = 0; @@ -85,8 +92,10 @@ pub async fn main () -> Result <(), Box > { // immediately listen for the next connection let client = client.clone (); + let opt = opt.clone (); + tokio::spawn (async move { - match handle_req_resp (&client, req_resp).await { + match handle_req_resp (&opt, &client, req_resp).await { _ => (), } });