parent
1e86e9735e
commit
4c03e6d32a
|
@ -30,14 +30,14 @@ use tokio::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use ptth::{
|
use ptth::{
|
||||||
http_serde::*,
|
http_serde,
|
||||||
watcher::Watchers,
|
watcher::Watchers,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
Meow,
|
Meow,
|
||||||
HttpRequestResponse (RequestParts),
|
HttpRequestResponse (http_serde::RequestParts),
|
||||||
HttpResponseResponseStream (Body),
|
HttpResponseResponseStream ((http_serde::ResponseParts, Body)),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Default)]
|
#[derive (Default)]
|
||||||
|
@ -94,13 +94,14 @@ async fn handle_http_response (
|
||||||
-> Response <Body>
|
-> Response <Body>
|
||||||
{
|
{
|
||||||
println! ("Step 6");
|
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;
|
let mut watchers = state.watchers.lock ().await;
|
||||||
|
|
||||||
println! ("Step 7");
|
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)");
|
println! ("Step 8 (bad thing)");
|
||||||
status_reply (StatusCode::BAD_REQUEST, "A bad thing happened.\n")
|
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 ()))
|
.map (|(k, v)| (String::from (k.as_str ()), v.as_bytes ().to_vec ()))
|
||||||
);
|
);
|
||||||
|
|
||||||
RequestParts {
|
http_serde::RequestParts {
|
||||||
id,
|
id,
|
||||||
method,
|
method,
|
||||||
uri,
|
uri,
|
||||||
|
@ -171,12 +172,17 @@ async fn handle_http_request (
|
||||||
});
|
});
|
||||||
|
|
||||||
match r.await {
|
match r.await {
|
||||||
Ok (Message::HttpResponseResponseStream (body)) => {
|
Ok (Message::HttpResponseResponseStream ((resp_parts, body))) => {
|
||||||
println! ("Step 7");
|
println! ("Step 7");
|
||||||
|
|
||||||
Response::builder ()
|
let mut resp = Response::builder ()
|
||||||
.status (StatusCode::OK)
|
.status (hyper::StatusCode::from (resp_parts.status_code));
|
||||||
.header ("Accept-Ranges", "bytes")
|
|
||||||
|
for (k, v) in resp_parts.headers.into_iter () {
|
||||||
|
resp = resp.header (&k, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
resp
|
||||||
.body (body)
|
.body (body)
|
||||||
.unwrap ()
|
.unwrap ()
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::*,
|
||||||
convert::Infallible,
|
convert::Infallible,
|
||||||
error::Error,
|
error::Error,
|
||||||
io::SeekFrom,
|
io::SeekFrom,
|
||||||
|
@ -26,7 +27,7 @@ use tokio::{
|
||||||
time::delay_for,
|
time::delay_for,
|
||||||
};
|
};
|
||||||
|
|
||||||
use ptth::http_serde::*;
|
use ptth::http_serde;
|
||||||
|
|
||||||
fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
|
fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -85,7 +86,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
println! ("Step 3");
|
println! ("Step 3");
|
||||||
|
|
||||||
let body = req_resp.bytes ().await?;
|
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,
|
Ok (x) => x,
|
||||||
_ => continue,
|
_ => continue,
|
||||||
|
@ -108,8 +109,6 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println! ("Step 4/5");
|
|
||||||
|
|
||||||
println! ("Step 6");
|
println! ("Step 6");
|
||||||
let client = client.clone ();
|
let client = client.clone ();
|
||||||
tokio::spawn (async move {
|
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");
|
println! ("Step 6");
|
||||||
if let Err (e) = resp_req.send ().await {
|
if let Err (e) = resp_req.send ().await {
|
||||||
|
|
|
@ -85,10 +85,12 @@ impl From <StatusCode> for hyper::StatusCode {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Deserialize, Serialize)]
|
#[derive (Deserialize, Serialize)]
|
||||||
pub struct Response {
|
pub struct ResponseParts {
|
||||||
pub status_code: String,
|
pub status_code: StatusCode,
|
||||||
|
|
||||||
// Technically Hyper has headers in a multi-map
|
// Technically Hyper has headers in a multi-map
|
||||||
// but I don't feel like doing that right now.
|
// 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>>,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue