Test the file server by hashing the AGPL license remotely

main
_ 2020-10-30 18:36:32 -05:00
parent 5d14155ba3
commit 0f092f7213
5 changed files with 44 additions and 13 deletions

View File

@ -10,6 +10,7 @@ edition = "2018"
[dependencies] [dependencies]
base64 = "0.12" base64 = "0.12"
blake3 = "0.3"
futures = "0.3" futures = "0.3"
http = "0.2" http = "0.2"
hyper = "0.13" hyper = "0.13"

View File

@ -2,5 +2,9 @@ use std::error::Error;
#[tokio::main] #[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> { async fn main () -> Result <(), Box <dyn Error>> {
ptth::server::main ().await let opt = ptth::server::Opt {
root: "/home/user".into (),
};
ptth::server::main (opt).await
} }

View File

@ -15,16 +15,9 @@ pub mod server;
#[cfg (test)] #[cfg (test)]
mod tests { mod tests {
use std::{
error::Error,
time::Duration,
};
use tokio::{ use tokio::{
prelude::*,
runtime::Runtime, runtime::Runtime,
spawn, spawn,
time::delay_for,
}; };
use super::{ use super::{
@ -44,12 +37,35 @@ mod tests {
relay::main ().await.unwrap (); relay::main ().await.unwrap ();
}); });
spawn (async {
let opt = server::Opt {
root: ".".into (),
};
server::main (opt).await.unwrap ();
});
let client = Client::new (); let client = Client::new ();
let resp = client.get ("http://127.0.0.1:4000/relay_up_check") let resp = client.get ("http://127.0.0.1:4000/relay_up_check")
.send ().await.unwrap ().bytes ().await.unwrap (); .send ().await.unwrap ().bytes ().await.unwrap ();
assert_eq! (resp, "Relay is up\n"); 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,
]));
}); });
} }
} }

View File

@ -4,7 +4,7 @@ use std::{
cmp::{min, max}, cmp::{min, max},
convert::{Infallible, TryInto}, convert::{Infallible, TryInto},
io::SeekFrom, io::SeekFrom,
path::PathBuf, path::{Path, PathBuf},
}; };
// file_server shouldn't depend on reqwest, but for now it // file_server shouldn't depend on reqwest, but for now it
@ -205,6 +205,7 @@ async fn serve_error (
} }
pub async fn serve_all ( pub async fn serve_all (
root: &Path,
parts: http_serde::RequestParts parts: http_serde::RequestParts
) )
-> http_serde::Response -> http_serde::Response
@ -234,7 +235,7 @@ pub async fn serve_all (
let encoded_path = &parts.uri [1..]; let encoded_path = &parts.uri [1..];
let path = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().unwrap (); 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); full_path.push (&*path);
if let Ok (dir) = read_dir (&full_path).await { if let Ok (dir) = read_dir (&full_path).await {

View File

@ -1,5 +1,6 @@
use std::{ use std::{
error::Error, error::Error,
path::PathBuf,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
@ -20,6 +21,7 @@ const RELAY_URL: &str = "http://127.0.0.1:4000";
const SERVER_NAME: &str = "alien_wildlands"; const SERVER_NAME: &str = "alien_wildlands";
async fn handle_req_resp ( async fn handle_req_resp (
opt: &Opt,
client: &Client, client: &Client,
req_resp: reqwest::Response req_resp: reqwest::Response
) -> Result <(), Box <dyn Error>> { ) -> Result <(), Box <dyn Error>> {
@ -39,7 +41,7 @@ async fn handle_req_resp (
let (req_id, parts) = (wrapped_req.id, wrapped_req.req); 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 let mut resp_req = client
.post (&format! ("{}/http_response/{}", RELAY_URL, req_id)) .post (&format! ("{}/http_response/{}", RELAY_URL, req_id))
@ -57,8 +59,13 @@ async fn handle_req_resp (
Ok (()) Ok (())
} }
pub async fn main () -> Result <(), Box <dyn Error>> { pub struct Opt {
pub root: PathBuf,
}
pub async fn main (opt: Opt) -> Result <(), Box <dyn Error>> {
let client = Arc::new (Client::new ()); let client = Arc::new (Client::new ());
let opt = Arc::new (opt);
let mut backoff_delay = 0; let mut backoff_delay = 0;
@ -85,8 +92,10 @@ pub async fn main () -> Result <(), Box <dyn Error>> {
// immediately listen for the next connection // immediately listen for the next connection
let client = client.clone (); let client = client.clone ();
let opt = opt.clone ();
tokio::spawn (async move { tokio::spawn (async move {
match handle_req_resp (&client, req_resp).await { match handle_req_resp (&opt, &client, req_resp).await {
_ => (), _ => (),
} }
}); });