From 4c03e6d32afa468a4b5ae9ec467543082b25b1dd Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 27 Oct 2020 21:29:15 -0500 Subject: [PATCH] :sparkle: Sending the ResponseParts back through a header. Do you feel like a hero yet? --- src/bin/relay.rs | 26 ++++++++++++++++---------- src/bin/server.rs | 20 +++++++++++++++----- src/http_serde.rs | 8 +++++--- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/bin/relay.rs b/src/bin/relay.rs index f98f44f..9084211 100644 --- a/src/bin/relay.rs +++ b/src/bin/relay.rs @@ -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 { 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 () }, diff --git a/src/bin/server.rs b/src/bin/server.rs index bc6d0c3..e529096 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -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 , Option ) { lazy_static! { @@ -85,7 +86,7 @@ async fn main () -> Result <(), Box > { 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 > { } } - println! ("Step 4/5"); - println! ("Step 6"); let client = client.clone (); tokio::spawn (async move { @@ -150,7 +149,18 @@ async fn main () -> Result <(), Box > { } }); - 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 > = 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 { diff --git a/src/http_serde.rs b/src/http_serde.rs index 9799b9d..8b822fe 100644 --- a/src/http_serde.rs +++ b/src/http_serde.rs @@ -85,10 +85,12 @@ impl From 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 , + // We promise not to need this feature. + + pub headers: HashMap >, }