♻️ Extract serve_file fn

main
_ 2020-10-29 12:17:40 +00:00
parent 722eac97e8
commit ec5b770ae3
2 changed files with 111 additions and 91 deletions

View File

@ -50,76 +50,14 @@ fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
(start, end)
}
#[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> {
let client = Arc::new (Client::new ());
let mut backoff_delay = 0;
loop {
if backoff_delay > 0 {
delay_for (Duration::from_millis (backoff_delay)).await;
}
let _uri = Uri::builder ()
.scheme ("http")
.authority ("127.0.0.1:4000")
.path_and_query ("/http_listen/alien_wildlands")
.build ().unwrap ();
let req_req = client.get ("http://127.0.0.1:4000/http_listen/alien_wildlands");
//println! ("Step 1");
let req_resp = match req_req.send ().await {
Err (e) => {
println! ("Err: {:?}", e);
backoff_delay = backoff_delay * 2 + 500;
continue;
},
Ok (r) => {
backoff_delay = 0;
r
},
};
if req_resp.status () != StatusCode::OK {
continue;
}
println! ("Step 3");
let body = req_resp.bytes ().await?;
let parts: http_serde::RequestParts = match rmp_serde::from_read_ref (&body)
{
Ok (x) => x,
_ => continue,
};
let (req_id, uri) = (parts.id, parts.uri);
println! ("Client requested {}", uri);
let mut range_start = None;
let mut range_end = None;
for (k, v) in parts.headers.iter () {
let v = std::str::from_utf8 (v).unwrap ();
//println! ("{}: {}", k, v);
if k == "range" {
let (start, end) = parse_range_header (v);
range_start = start;
range_end = end;
}
}
let should_send_body = match &parts.method {
http_serde::Method::Get => true,
_ => false,
};
//println! ("Step 6");
let client = client.clone ();
tokio::spawn (async move {
async fn serve_file (
client: Arc <Client>,
mut f: File,
req_id: String,
should_send_body: bool,
range_start: Option <u64>,
range_end: Option <u64>
) {
let (tx, rx) = channel (2);
let body = if should_send_body {
Some (Body::wrap_stream (rx))
@ -128,10 +66,6 @@ async fn main () -> Result <(), Box <dyn Error>> {
None
};
let mut path = PathBuf::from ("/home/user");
path.push (&uri [1..]);
let mut f = File::open (path).await.unwrap ();
let file_md = f.metadata ().await.unwrap ();
let file_len = file_md.len ();
@ -213,6 +147,90 @@ async fn main () -> Result <(), Box <dyn Error>> {
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
}
}
#[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> {
let client = Arc::new (Client::new ());
let mut backoff_delay = 0;
loop {
if backoff_delay > 0 {
delay_for (Duration::from_millis (backoff_delay)).await;
}
let _uri = Uri::builder ()
.scheme ("http")
.authority ("127.0.0.1:4000")
.path_and_query ("/http_listen/alien_wildlands")
.build ().unwrap ();
let req_req = client.get ("http://127.0.0.1:4000/http_listen/alien_wildlands");
//println! ("Step 1");
let req_resp = match req_req.send ().await {
Err (e) => {
println! ("Err: {:?}", e);
backoff_delay = backoff_delay * 2 + 500;
continue;
},
Ok (r) => {
backoff_delay = 0;
r
},
};
if req_resp.status () != StatusCode::OK {
continue;
}
println! ("Step 3");
let body = req_resp.bytes ().await?;
let parts: http_serde::RequestParts = match rmp_serde::from_read_ref (&body)
{
Ok (x) => x,
_ => continue,
};
let (req_id, uri) = (parts.id, parts.uri);
println! ("Client requested {}", uri);
let mut range_start = None;
let mut range_end = None;
for (k, v) in parts.headers.iter () {
let v = std::str::from_utf8 (v).unwrap ();
//println! ("{}: {}", k, v);
if k == "range" {
let (start, end) = parse_range_header (v);
range_start = start;
range_end = end;
}
}
let should_send_body = match &parts.method {
http_serde::Method::Get => true,
_ => false,
};
//println! ("Step 6");
let client = client.clone ();
tokio::spawn (async move {
let mut path = PathBuf::from ("/home/user");
path.push (&uri [1..]);
let f = File::open (path).await.unwrap ();
serve_file (
client,
f,
req_id,
should_send_body,
range_start,
range_end
).await;
});
}
}

View File

@ -1,2 +1,4 @@
- Index 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?)