🚧 Working on directory indexes

main
_ 2020-10-29 13:19:14 +00:00
parent 6f31c93c1f
commit af226cb36c
2 changed files with 92 additions and 42 deletions

View File

@ -11,7 +11,6 @@ use std::{
use hyper::{
StatusCode,
Uri,
};
use lazy_static::*;
use regex::Regex;
@ -20,7 +19,11 @@ use reqwest::{
Client,
};
use tokio::{
fs::File,
fs::{
File,
read_dir,
ReadDir,
},
io::AsyncReadExt,
sync::mpsc::{
channel,
@ -52,14 +55,19 @@ fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
const SERVER_URL: &str = "http://127.0.0.1:4000";
async fn serve_response (
client: &Client,
req_id: String,
struct ResponseHandle <'a> {
client: &'a Client,
req_id: &'a str,
}
impl <'a> ResponseHandle <'a> {
async fn respond (
self,
resp_parts: http_serde::ResponseParts,
body: Option <Body>
) {
let mut resp_req = client
.post (&format! ("{}/http_response/{}", SERVER_URL, req_id))
) {
let mut resp_req = self.client
.post (&format! ("{}/http_response/{}", SERVER_URL, self.req_id))
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()));
if let Some (body) = body {
@ -70,12 +78,46 @@ async fn serve_response (
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
}
}
}
async fn serve_dir (
response_handle: ResponseHandle <'_>,
dir: ReadDir
) {
let (tx, rx) = channel (2);
tokio::spawn (async move {
let mut tx = tx;
tx.send (Ok::<_, Infallible> (String::from ("<style>body {font-family:sans;}</style><ul>").into_bytes ())).await.unwrap ();
for i in 0u8..10 {
let s = format! ("<li><a href=\"{}\">Dir entry {}</a></li>\n", i, i);
if tx.send (Ok::<_, Infallible> (s.into_bytes ())).await.is_err ()
{
break;
}
}
tx.send (Ok::<_, Infallible> (String::from ("</ul>").into_bytes ())).await.unwrap ();
});
let mut headers: HashMap <String, Vec <u8>> = Default::default ();
headers.insert ("content-type".into (), String::from ("text/html").into_bytes ());
let resp_parts = http_serde::ResponseParts {
status_code: http_serde::StatusCode::Ok,
headers,
};
response_handle.respond (resp_parts, Some (Body::wrap_stream (rx))).await;
}
async fn serve_file (
client: &Client,
response_handle: ResponseHandle <'_>,
mut f: File,
req_id: String,
should_send_body: bool,
range_start: Option <u64>,
range_end: Option <u64>
@ -133,7 +175,7 @@ async fn serve_file (
//bytes_sent += bytes_read;
//println! ("Sent {} bytes", bytes_sent);
delay_for (Duration::from_millis (50)).await;
//delay_for (Duration::from_millis (50)).await;
}
});
}
@ -157,13 +199,12 @@ async fn serve_file (
headers,
};
serve_response (client, req_id, resp_parts, body).await;
response_handle.respond (resp_parts, body).await;
}
async fn serve_error (
client: &Client,
req_id: String,
status_code: ptth::http_serde::StatusCode
response_handle: ResponseHandle <'_>,
status_code: http_serde::StatusCode
) {
let headers: HashMap <String, Vec <u8>> = Default::default ();
@ -172,11 +213,11 @@ async fn serve_error (
headers,
};
serve_response (client, req_id, resp_parts, Some ("404 Not Found".into ())).await;
response_handle.respond (resp_parts, Some ("404 Not Found".into ())).await;
}
async fn handle_all (
client: Arc <Client>,
client: &Client,
req_resp: reqwest::Response
) -> Result <(), Box <dyn Error>> {
//println! ("Step 1");
@ -222,22 +263,28 @@ async fn handle_all (
let mut path = PathBuf::from ("/home/user");
path.push (&uri [1..]);
match File::open (path).await {
Ok (f) => serve_file (
&client,
f,
req_id,
let response_handle = ResponseHandle {
client,
req_id: &req_id,
};
if let Ok (dir) = read_dir (&path).await {
serve_dir (response_handle, dir).await;
}
else if let Ok (file) = File::open (&path).await {
serve_file (
response_handle,
file,
should_send_body,
range_start,
range_end
).await,
Err (_) => {
serve_error (
&client,
req_id,
ptth::http_serde::StatusCode::NotFound
).await;
},
}
else {
serve_error (
response_handle,
http_serde::StatusCode::NotFound
).await;
}
Ok (())
@ -273,7 +320,9 @@ async fn main () -> Result <(), Box <dyn Error>> {
let client = client.clone ();
tokio::spawn (async move {
handle_all (client, req_resp).await;
match handle_all (&client, req_resp).await {
_ => (),
}
});
}
}

View File

@ -1,4 +1,5 @@
- Index directories
- Handle trailing slash problem for directories
- Set up tokens or something so clients can't trivially
impersonate servers
- Fix possible timing gap when refreshing http_listen (Just have client wait a few seconds?)