From eaca19f6ba36c11899b032abc338f22f6b7b976d Mon Sep 17 00:00:00 2001 From: _ <> Date: Tue, 27 Oct 2020 03:36:52 +0000 Subject: [PATCH] Fix it up so the response body goes through a POST body instead --- src/bin/relay.rs | 36 ++++++++++++++++++++++-------------- src/bin/server.rs | 4 ++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/bin/relay.rs b/src/bin/relay.rs index 7dc37c1..b5de82a 100644 --- a/src/bin/relay.rs +++ b/src/bin/relay.rs @@ -13,7 +13,14 @@ use std::sync::{ use std::time::{Duration, Instant}; use futures::channel::oneshot; -use hyper::{Body, Request, Response, Server, StatusCode}; +use hyper::{ + Body, + Method, + Request, + Response, + Server, + StatusCode, +}; use hyper::service::{make_service_fn, service_fn}; use tokio::{ @@ -79,19 +86,20 @@ async fn handle_http_listen (state: Arc , watcher_code: String) } async fn handle_http_response ( + req: Request , state: Arc , - req_id: String, - response: String + req_id: String, ) -> Response { println! ("Step 6"); + let payload = String::from (std::str::from_utf8 (&hyper::body::to_bytes (req.into_body ()).await.unwrap ()).unwrap ()); { let mut watchers = state.watchers.lock ().await; println! ("Step 7"); - if ! watchers.wake_one (Message::HttpResponseResponse (response), &req_id) + if ! watchers.wake_one (Message::HttpResponseResponse (payload), &req_id) { println! ("Step 8"); return status_reply (StatusCode::BAD_REQUEST, "A bad thing happened.\n"); @@ -191,6 +199,16 @@ async fn handle_all (req: Request , state: Arc ) let path = req.uri ().path (); println! ("{}", path); + if req.method () == Method::POST { + return Ok (if let Some (request_code) = prefix_match (path, "/http_response/") { + let request_code = request_code.into (); + handle_http_response (req, state, request_code).await + } + else { + status_reply (StatusCode::BAD_REQUEST, "Can't POST this\n") + }); + } + if let Some (watch_code) = prefix_match (path, "/watch/") { Ok (handle_watch (state, watch_code.into ()).await) } @@ -210,16 +228,6 @@ async fn handle_all (req: Request , state: Arc ) Ok (status_reply (StatusCode::BAD_REQUEST, "Bad URI format")) } } - else if let Some (rest) = prefix_match (path, "/http_response/") { - if let Some (idx) = rest.find ('/') { - let request_code = &rest [0..idx]; - let response = &rest [idx + 1..]; - Ok (handle_http_response (state, request_code.into (), response.into ()).await) - } - else { - Ok (status_reply (StatusCode::BAD_REQUEST, "Bad URI format")) - } - } /* else if let Some (name) = prefix_match (path, "/udp_send/") { Ok (handle_udp_send ().await) diff --git a/src/bin/server.rs b/src/bin/server.rs index 081e0c0..80f8cc5 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -46,7 +46,7 @@ async fn main () -> Result <(), Box > { println! ("Client requested {}", body); println! ("Step 4/5"); - let payload = String::from ("Ha ha hue hue it worked."); + let payload = String::from ("Ha ha hue hue it worked.\n"); println! ("Step 6"); let client = client.clone (); @@ -57,7 +57,7 @@ async fn main () -> Result <(), Box > { .path_and_query ("/listen/alien_wildlands") .build ().unwrap (); - let resp_req = client.get (&format! ("http://127.0.0.1:4000/http_response/{}/{}", body, payload)); + let resp_req = client.post (&format! ("http://127.0.0.1:4000/http_response/{}", body)).body (payload); println! ("Step 6"); match resp_req.send ().await {