❇️ Sending the ResponseParts back through a header.

Do you feel like a hero yet?
main
_ 2020-10-27 21:29:15 -05:00
parent 1e86e9735e
commit 4c03e6d32a
3 changed files with 36 additions and 18 deletions

View File

@ -30,14 +30,14 @@ use tokio::{
};
use ptth::{
http_serde::*,
http_serde,
watcher::Watchers,
};
enum Message {
Meow,
HttpRequestResponse (RequestParts),
HttpResponseResponseStream (Body),
HttpRequestResponse (http_serde::RequestParts),
HttpResponseResponseStream ((http_serde::ResponseParts, Body)),
}
#[derive (Default)]
@ -94,13 +94,14 @@ async fn handle_http_response (
-> Response <Body>
{
println! ("Step 6");
let body = req.into_body ();
let (parts, body) = req.into_parts ();
let resp_parts: http_serde::ResponseParts = rmp_serde::from_read_ref (&base64::decode (parts.headers.get ("X-PTTH-2LJYXWC4").unwrap ()).unwrap ()).unwrap ();
{
let mut watchers = state.watchers.lock ().await;
println! ("Step 7");
if ! watchers.wake_one (Message::HttpResponseResponseStream (body), &req_id)
if ! watchers.wake_one (Message::HttpResponseResponseStream ((resp_parts, body)), &req_id)
{
println! ("Step 8 (bad thing)");
status_reply (StatusCode::BAD_REQUEST, "A bad thing happened.\n")
@ -132,7 +133,7 @@ async fn handle_http_request (
.map (|(k, v)| (String::from (k.as_str ()), v.as_bytes ().to_vec ()))
);
RequestParts {
http_serde::RequestParts {
id,
method,
uri,
@ -171,12 +172,17 @@ async fn handle_http_request (
});
match r.await {
Ok (Message::HttpResponseResponseStream (body)) => {
Ok (Message::HttpResponseResponseStream ((resp_parts, body))) => {
println! ("Step 7");
Response::builder ()
.status (StatusCode::OK)
.header ("Accept-Ranges", "bytes")
let mut resp = Response::builder ()
.status (hyper::StatusCode::from (resp_parts.status_code));
for (k, v) in resp_parts.headers.into_iter () {
resp = resp.header (&k, v);
}
resp
.body (body)
.unwrap ()
},

View File

@ -1,4 +1,5 @@
use std::{
collections::*,
convert::Infallible,
error::Error,
io::SeekFrom,
@ -26,7 +27,7 @@ use tokio::{
time::delay_for,
};
use ptth::http_serde::*;
use ptth::http_serde;
fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
lazy_static! {
@ -85,7 +86,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
println! ("Step 3");
let body = req_resp.bytes ().await?;
let parts: RequestParts = match rmp_serde::from_read_ref (&body)
let parts: http_serde::RequestParts = match rmp_serde::from_read_ref (&body)
{
Ok (x) => x,
_ => continue,
@ -108,8 +109,6 @@ async fn main () -> Result <(), Box <dyn Error>> {
}
}
println! ("Step 4/5");
println! ("Step 6");
let client = client.clone ();
tokio::spawn (async move {
@ -150,7 +149,18 @@ async fn main () -> Result <(), Box <dyn Error>> {
}
});
let resp_req = client.post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id)).body (Body::wrap_stream (rx));
let mut headers: HashMap <String, Vec <u8>> = Default::default ();
headers.insert (String::from ("x-its-a-header"), Vec::from (&b"wow"[..]));
let resp_parts = http_serde::ResponseParts {
status_code: http_serde::StatusCode::Ok,
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 (Body::wrap_stream (rx));
println! ("Step 6");
if let Err (e) = resp_req.send ().await {

View File

@ -85,10 +85,12 @@ impl From <StatusCode> for hyper::StatusCode {
}
#[derive (Deserialize, Serialize)]
pub struct Response {
pub status_code: String,
pub struct ResponseParts {
pub status_code: StatusCode,
// Technically Hyper has headers in a multi-map
// but I don't feel like doing that right now.
pub headers: HashMap <String, String>,
// We promise not to need this feature.
pub headers: HashMap <String, Vec <u8>>,
}