main
parent
9dd2155d7a
commit
6f31c93c1f
|
@ -50,8 +50,30 @@ fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
|
||||||
(start, end)
|
(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 (
|
async fn serve_file (
|
||||||
client: Arc <Client>,
|
client: &Client,
|
||||||
mut f: File,
|
mut f: File,
|
||||||
req_id: String,
|
req_id: String,
|
||||||
should_send_body: bool,
|
should_send_body: bool,
|
||||||
|
@ -135,22 +157,11 @@ async fn serve_file (
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut resp_req = client
|
serve_response (client, req_id, resp_parts, body).await;
|
||||||
.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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn serve_error (
|
async fn serve_error (
|
||||||
client: Arc <Client>,
|
client: &Client,
|
||||||
req_id: String,
|
req_id: String,
|
||||||
status_code: ptth::http_serde::StatusCode
|
status_code: ptth::http_serde::StatusCode
|
||||||
) {
|
) {
|
||||||
|
@ -161,52 +172,18 @@ async fn serve_error (
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
|
|
||||||
let resp_req = client
|
serve_response (client, req_id, resp_parts, Some ("404 Not Found".into ())).await;
|
||||||
.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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
async fn handle_all (
|
||||||
async fn main () -> Result <(), Box <dyn Error>> {
|
client: Arc <Client>,
|
||||||
let client = Arc::new (Client::new ());
|
req_resp: reqwest::Response
|
||||||
|
) -> Result <(), Box <dyn Error>> {
|
||||||
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");
|
//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 {
|
if req_resp.status () != StatusCode::OK {
|
||||||
continue;
|
// TODO: Error handling
|
||||||
|
return Ok (());
|
||||||
}
|
}
|
||||||
|
|
||||||
println! ("Step 3");
|
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)
|
let parts: http_serde::RequestParts = match rmp_serde::from_read_ref (&body)
|
||||||
{
|
{
|
||||||
Ok (x) => x,
|
Ok (x) => x,
|
||||||
_ => continue,
|
_ => return Ok (()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (req_id, uri) = (parts.id, parts.uri);
|
let (req_id, uri) = (parts.id, parts.uri);
|
||||||
|
@ -241,14 +218,13 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
};
|
};
|
||||||
|
|
||||||
//println! ("Step 6");
|
//println! ("Step 6");
|
||||||
let client = client.clone ();
|
|
||||||
tokio::spawn (async move {
|
|
||||||
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 {
|
match File::open (path).await {
|
||||||
Ok (f) => serve_file (
|
Ok (f) => serve_file (
|
||||||
client,
|
&client,
|
||||||
f,
|
f,
|
||||||
req_id,
|
req_id,
|
||||||
should_send_body,
|
should_send_body,
|
||||||
|
@ -256,9 +232,48 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
range_end
|
range_end
|
||||||
).await,
|
).await,
|
||||||
Err (_) => {
|
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;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
3
todo.md
3
todo.md
|
@ -2,3 +2,6 @@
|
||||||
- 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?)
|
||||||
|
- Parameter for server URL
|
||||||
|
- Parameter for static file serve path
|
||||||
|
- Error handling
|
||||||
|
|
Loading…
Reference in New Issue