Fix it up so the response body goes through a POST body instead

main
_ 2020-10-27 03:36:52 +00:00
parent 0cc61796c0
commit eaca19f6ba
2 changed files with 24 additions and 16 deletions

View File

@ -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 <ServerState>, watcher_code: String)
}
async fn handle_http_response (
req: Request <Body>,
state: Arc <ServerState>,
req_id: String,
response: String
)
-> Response <Body>
{
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 <Body>, state: Arc <ServerState>)
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 <Body>, state: Arc <ServerState>)
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)

View File

@ -46,7 +46,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
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 <dyn Error>> {
.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 {