🚧 Working on directory indexes
parent
6f31c93c1f
commit
af226cb36c
|
@ -11,7 +11,6 @@ use std::{
|
||||||
|
|
||||||
use hyper::{
|
use hyper::{
|
||||||
StatusCode,
|
StatusCode,
|
||||||
Uri,
|
|
||||||
};
|
};
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
@ -20,7 +19,11 @@ use reqwest::{
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::{
|
||||||
|
File,
|
||||||
|
read_dir,
|
||||||
|
ReadDir,
|
||||||
|
},
|
||||||
io::AsyncReadExt,
|
io::AsyncReadExt,
|
||||||
sync::mpsc::{
|
sync::mpsc::{
|
||||||
channel,
|
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";
|
const SERVER_URL: &str = "http://127.0.0.1:4000";
|
||||||
|
|
||||||
async fn serve_response (
|
struct ResponseHandle <'a> {
|
||||||
client: &Client,
|
client: &'a Client,
|
||||||
req_id: String,
|
req_id: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <'a> ResponseHandle <'a> {
|
||||||
|
async fn respond (
|
||||||
|
self,
|
||||||
resp_parts: http_serde::ResponseParts,
|
resp_parts: http_serde::ResponseParts,
|
||||||
body: Option <Body>
|
body: Option <Body>
|
||||||
) {
|
) {
|
||||||
let mut resp_req = client
|
let mut resp_req = self.client
|
||||||
.post (&format! ("{}/http_response/{}", SERVER_URL, req_id))
|
.post (&format! ("{}/http_response/{}", SERVER_URL, self.req_id))
|
||||||
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()));
|
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()));
|
||||||
|
|
||||||
if let Some (body) = body {
|
if let Some (body) = body {
|
||||||
|
@ -70,12 +78,46 @@ async fn serve_response (
|
||||||
if let Err (e) = resp_req.send ().await {
|
if let Err (e) = resp_req.send ().await {
|
||||||
println! ("Err: {:?}", e);
|
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 (
|
async fn serve_file (
|
||||||
client: &Client,
|
response_handle: ResponseHandle <'_>,
|
||||||
mut f: File,
|
mut f: File,
|
||||||
req_id: String,
|
|
||||||
should_send_body: bool,
|
should_send_body: bool,
|
||||||
range_start: Option <u64>,
|
range_start: Option <u64>,
|
||||||
range_end: Option <u64>
|
range_end: Option <u64>
|
||||||
|
@ -133,7 +175,7 @@ async fn serve_file (
|
||||||
//bytes_sent += bytes_read;
|
//bytes_sent += bytes_read;
|
||||||
//println! ("Sent {} bytes", bytes_sent);
|
//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,
|
headers,
|
||||||
};
|
};
|
||||||
|
|
||||||
serve_response (client, req_id, resp_parts, body).await;
|
response_handle.respond (resp_parts, body).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn serve_error (
|
async fn serve_error (
|
||||||
client: &Client,
|
response_handle: ResponseHandle <'_>,
|
||||||
req_id: String,
|
status_code: http_serde::StatusCode
|
||||||
status_code: ptth::http_serde::StatusCode
|
|
||||||
) {
|
) {
|
||||||
let headers: HashMap <String, Vec <u8>> = Default::default ();
|
let headers: HashMap <String, Vec <u8>> = Default::default ();
|
||||||
|
|
||||||
|
@ -172,11 +213,11 @@ async fn serve_error (
|
||||||
headers,
|
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 (
|
async fn handle_all (
|
||||||
client: Arc <Client>,
|
client: &Client,
|
||||||
req_resp: reqwest::Response
|
req_resp: reqwest::Response
|
||||||
) -> Result <(), Box <dyn Error>> {
|
) -> Result <(), Box <dyn Error>> {
|
||||||
//println! ("Step 1");
|
//println! ("Step 1");
|
||||||
|
@ -222,22 +263,28 @@ async fn handle_all (
|
||||||
let mut path = PathBuf::from ("/home/user");
|
let mut path = PathBuf::from ("/home/user");
|
||||||
path.push (&uri [1..]);
|
path.push (&uri [1..]);
|
||||||
|
|
||||||
match File::open (path).await {
|
let response_handle = ResponseHandle {
|
||||||
Ok (f) => serve_file (
|
client,
|
||||||
&client,
|
req_id: &req_id,
|
||||||
f,
|
};
|
||||||
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,
|
should_send_body,
|
||||||
range_start,
|
range_start,
|
||||||
range_end
|
range_end
|
||||||
).await,
|
|
||||||
Err (_) => {
|
|
||||||
serve_error (
|
|
||||||
&client,
|
|
||||||
req_id,
|
|
||||||
ptth::http_serde::StatusCode::NotFound
|
|
||||||
).await;
|
).await;
|
||||||
},
|
}
|
||||||
|
else {
|
||||||
|
serve_error (
|
||||||
|
response_handle,
|
||||||
|
http_serde::StatusCode::NotFound
|
||||||
|
).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok (())
|
Ok (())
|
||||||
|
@ -273,7 +320,9 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
|
|
||||||
let client = client.clone ();
|
let client = client.clone ();
|
||||||
tokio::spawn (async move {
|
tokio::spawn (async move {
|
||||||
handle_all (client, req_resp).await;
|
match handle_all (&client, req_resp).await {
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
todo.md
1
todo.md
|
@ -1,4 +1,5 @@
|
||||||
- Index directories
|
- Index directories
|
||||||
|
- Handle trailing slash problem for directories
|
||||||
- Set up tokens or something so clients can't trivially
|
- Set up tokens or something so clients can't trivially
|
||||||
impersonate servers
|
impersonate servers
|
||||||
- Fix possible timing gap when refreshing http_listen (Just have client wait a few seconds?)
|
- Fix possible timing gap when refreshing http_listen (Just have client wait a few seconds?)
|
||||||
|
|
Loading…
Reference in New Issue