🐛 Add content-length header when POSTing a response to the relay

main
Trisha Earley 2020-11-06 18:47:04 +00:00
parent 690f07dab6
commit a96c82dea4
3 changed files with 34 additions and 13 deletions

View File

@ -126,6 +126,9 @@ type Body = Receiver <Result <Vec <u8>, std::convert::Infallible>>;
pub struct Response {
pub parts: ResponseParts,
pub body: Option <Body>,
// Needed to make Nginx happy
pub content_length: Option <u64>,
}
impl Response {

View File

@ -120,11 +120,14 @@ async fn serve_dir (
path,
entries,
}).unwrap ();
let body = s.into_bytes ();
let mut resp = http_serde::Response::default ();
resp.content_length = Some (body.len ().try_into ().unwrap ());
resp
.header ("content-type".to_string (), "text/html".to_string ().into_bytes ())
.body_bytes (s.into_bytes ())
.header ("content-length".to_string (), body.len ().to_string ().into_bytes ())
.body_bytes (body)
;
resp
}
@ -161,7 +164,7 @@ async fn serve_file (
//println! ("Opening file {:?}", path);
let mut tx = tx;
//let mut bytes_sent = 0;
let mut bytes_sent = 0;
let mut bytes_left = end - start;
loop {
@ -177,16 +180,18 @@ async fn serve_file (
}
if tx.send (Ok::<_, Infallible> (buffer)).await.is_err () {
eprintln! ("Send failed while streaming file ({} bytes sent)", bytes_sent);
break;
}
bytes_left -= bytes_read;
if bytes_left == 0 {
eprintln! ("Finished streaming file");
break;
}
//bytes_sent += bytes_read;
//println! ("Sent {} bytes", bytes_sent);
bytes_sent += bytes_read;
println! ("Sent {} bytes", bytes_sent);
//delay_for (Duration::from_millis (50)).await;
}
@ -197,13 +202,17 @@ async fn serve_file (
response.header (String::from ("accept-ranges"), b"bytes".to_vec ());
if range_start.is_none () && range_end.is_none () {
response.status_code (http_serde::StatusCode::Ok);
response.header (String::from ("content-length"), end.to_string ().into_bytes ());
}
else {
response.status_code (http_serde::StatusCode::PartialContent);
response.header (String::from ("content-range"), format! ("bytes {}-{}/{}", start, end - 1, end).into_bytes ());
if should_send_body {
if range_start.is_none () && range_end.is_none () {
response.status_code (http_serde::StatusCode::Ok);
response.header (String::from ("content-length"), end.to_string ().into_bytes ());
}
else {
response.status_code (http_serde::StatusCode::PartialContent);
response.header (String::from ("content-range"), format! ("bytes {}-{}/{}", start, end - 1, end).into_bytes ());
}
response.content_length = Some (end - start);
}
if let Some (body) = body {

View File

@ -83,14 +83,23 @@ async fn handle_req_resp <'a> (
.post (&format! ("{}/http_response/{}", state.config.relay_url, req_id))
.header (crate::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).unwrap ()));
if let Some (length) = response.content_length {
resp_req = resp_req.header ("Content-Length", length.to_string ());
}
if let Some (body) = response.body {
resp_req = resp_req.body (reqwest::Body::wrap_stream (body));
}
let req = resp_req.build ().unwrap ();
eprintln! ("{:?}", req.headers ());
//println! ("Step 6");
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
match state.client.execute (req).await {
Ok (r) => eprintln! ("{:?} {:?}", r.status (), r.text ().await.unwrap ()),
Err (e) => eprintln! ("Err: {:?}", e),
}
});
}
}