🐛 Add content-length header when POSTing a response to the relay
parent
690f07dab6
commit
a96c82dea4
|
@ -126,6 +126,9 @@ type Body = Receiver <Result <Vec <u8>, std::convert::Infallible>>;
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub parts: ResponseParts,
|
pub parts: ResponseParts,
|
||||||
pub body: Option <Body>,
|
pub body: Option <Body>,
|
||||||
|
|
||||||
|
// Needed to make Nginx happy
|
||||||
|
pub content_length: Option <u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
|
|
|
@ -120,11 +120,14 @@ async fn serve_dir (
|
||||||
path,
|
path,
|
||||||
entries,
|
entries,
|
||||||
}).unwrap ();
|
}).unwrap ();
|
||||||
|
let body = s.into_bytes ();
|
||||||
|
|
||||||
let mut resp = http_serde::Response::default ();
|
let mut resp = http_serde::Response::default ();
|
||||||
|
resp.content_length = Some (body.len ().try_into ().unwrap ());
|
||||||
resp
|
resp
|
||||||
.header ("content-type".to_string (), "text/html".to_string ().into_bytes ())
|
.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
|
resp
|
||||||
}
|
}
|
||||||
|
@ -161,7 +164,7 @@ async fn serve_file (
|
||||||
//println! ("Opening file {:?}", path);
|
//println! ("Opening file {:?}", path);
|
||||||
|
|
||||||
let mut tx = tx;
|
let mut tx = tx;
|
||||||
//let mut bytes_sent = 0;
|
let mut bytes_sent = 0;
|
||||||
let mut bytes_left = end - start;
|
let mut bytes_left = end - start;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -177,16 +180,18 @@ async fn serve_file (
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.send (Ok::<_, Infallible> (buffer)).await.is_err () {
|
if tx.send (Ok::<_, Infallible> (buffer)).await.is_err () {
|
||||||
|
eprintln! ("Send failed while streaming file ({} bytes sent)", bytes_sent);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes_left -= bytes_read;
|
bytes_left -= bytes_read;
|
||||||
if bytes_left == 0 {
|
if bytes_left == 0 {
|
||||||
|
eprintln! ("Finished streaming file");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//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;
|
||||||
}
|
}
|
||||||
|
@ -197,13 +202,17 @@ async fn serve_file (
|
||||||
|
|
||||||
response.header (String::from ("accept-ranges"), b"bytes".to_vec ());
|
response.header (String::from ("accept-ranges"), b"bytes".to_vec ());
|
||||||
|
|
||||||
if range_start.is_none () && range_end.is_none () {
|
if should_send_body {
|
||||||
response.status_code (http_serde::StatusCode::Ok);
|
if range_start.is_none () && range_end.is_none () {
|
||||||
response.header (String::from ("content-length"), end.to_string ().into_bytes ());
|
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);
|
else {
|
||||||
response.header (String::from ("content-range"), format! ("bytes {}-{}/{}", start, end - 1, end).into_bytes ());
|
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 {
|
if let Some (body) = body {
|
||||||
|
|
|
@ -83,14 +83,23 @@ async fn handle_req_resp <'a> (
|
||||||
.post (&format! ("{}/http_response/{}", state.config.relay_url, req_id))
|
.post (&format! ("{}/http_response/{}", state.config.relay_url, req_id))
|
||||||
.header (crate::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).unwrap ()));
|
.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 {
|
if let Some (body) = response.body {
|
||||||
resp_req = resp_req.body (reqwest::Body::wrap_stream (body));
|
resp_req = resp_req.body (reqwest::Body::wrap_stream (body));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let req = resp_req.build ().unwrap ();
|
||||||
|
|
||||||
|
eprintln! ("{:?}", req.headers ());
|
||||||
|
|
||||||
//println! ("Step 6");
|
//println! ("Step 6");
|
||||||
if let Err (e) = resp_req.send ().await {
|
match state.client.execute (req).await {
|
||||||
println! ("Err: {:?}", e);
|
Ok (r) => eprintln! ("{:?} {:?}", r.status (), r.text ().await.unwrap ()),
|
||||||
|
Err (e) => eprintln! ("Err: {:?}", e),
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue