main
_ 2020-10-29 12:45:35 +00:00
parent 9dd2155d7a
commit 6f31c93c1f
2 changed files with 128 additions and 110 deletions

View File

@ -50,8 +50,30 @@ fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
(start, end)
}
const SERVER_URL: &str = "http://127.0.0.1:4000";
async fn serve_response (
client: &Client,
req_id: String,
resp_parts: http_serde::ResponseParts,
body: Option <Body>
) {
let mut resp_req = client
.post (&format! ("{}/http_response/{}", SERVER_URL, req_id))
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()));
if let Some (body) = body {
resp_req = resp_req.body (body);
}
//println! ("Step 6");
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
}
}
async fn serve_file (
client: Arc <Client>,
client: &Client,
mut f: File,
req_id: String,
should_send_body: bool,
@ -135,22 +157,11 @@ async fn serve_file (
headers,
};
let mut resp_req = client
.post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id))
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()));
if let Some (body) = body {
resp_req = resp_req.body (body);
}
//println! ("Step 6");
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
}
serve_response (client, req_id, resp_parts, body).await;
}
async fn serve_error (
client: Arc <Client>,
client: &Client,
req_id: String,
status_code: ptth::http_serde::StatusCode
) {
@ -161,52 +172,18 @@ async fn serve_error (
headers,
};
let resp_req = client
.post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id))
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()))
.body ("404 Not Found")
;
//println! ("Step 6");
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
}
serve_response (client, req_id, resp_parts, Some ("404 Not Found".into ())).await;
}
#[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");
async fn handle_all (
client: Arc <Client>,
req_resp: reqwest::Response
) -> Result <(), Box <dyn Error>> {
//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;
// TODO: Error handling
return Ok (());
}
println! ("Step 3");
@ -215,7 +192,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
let parts: http_serde::RequestParts = match rmp_serde::from_read_ref (&body)
{
Ok (x) => x,
_ => continue,
_ => return Ok (()),
};
let (req_id, uri) = (parts.id, parts.uri);
@ -241,14 +218,13 @@ async fn main () -> Result <(), Box <dyn Error>> {
};
//println! ("Step 6");
let client = client.clone ();
tokio::spawn (async move {
let mut path = PathBuf::from ("/home/user");
path.push (&uri [1..]);
match File::open (path).await {
Ok (f) => serve_file (
client,
&client,
f,
req_id,
should_send_body,
@ -256,9 +232,48 @@ async fn main () -> Result <(), Box <dyn Error>> {
range_end
).await,
Err (_) => {
serve_error (client, req_id, ptth::http_serde::StatusCode::NotFound).await;
serve_error (
&client,
req_id,
ptth::http_serde::StatusCode::NotFound
).await;
},
}
Ok (())
}
#[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 req_req = client.get ("http://127.0.0.1:4000/http_listen/alien_wildlands");
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
},
};
// Spawn another task for each request so we can immediately listen
// for the next connection
let client = client.clone ();
tokio::spawn (async move {
handle_all (client, req_resp).await;
});
}
}

View File

@ -2,3 +2,6 @@
- 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?)
- Parameter for server URL
- Parameter for static file serve path
- Error handling