From 290745f6cf30ce466c2fbab462649ff4284456df Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 27 Oct 2020 20:31:38 -0500 Subject: [PATCH] Pass the right part of the URL to the server. Also remove dead code --- src/bin/relay.rs | 72 ++++++++++++++++++----------------------------- src/http_serde.rs | 42 +-------------------------- 2 files changed, 29 insertions(+), 85 deletions(-) diff --git a/src/bin/relay.rs b/src/bin/relay.rs index 2918cc1..39e36f3 100644 --- a/src/bin/relay.rs +++ b/src/bin/relay.rs @@ -3,14 +3,14 @@ use std::{ error::Error, convert::{ Infallible, - TryInto, + TryFrom, }, + iter::FromIterator, net::SocketAddr, - str::FromStr, sync::{ Arc }, - time::{Duration, Instant}, + time::{Duration}, }; use futures::channel::oneshot; @@ -113,12 +113,33 @@ async fn handle_http_response ( } async fn handle_http_request ( - parts: RequestParts, + req: http::request::Parts, + uri: String, state: Arc , watcher_code: String ) -> Response { + let parts = { + let id = ulid::Ulid::new ().to_string (); + let method = match ptth::http_serde::Method::try_from (req.method) { + Ok (x) => x, + _ => return status_reply (StatusCode::BAD_REQUEST, "Method not supported"), + }; + let headers = HashMap::from_iter ( + req.headers.into_iter () + .filter_map (|(k, v)| k.map (|k| (k, v))) + .map (|(k, v)| (String::from (k.as_str ()), v.as_bytes ().to_vec ())) + ); + + RequestParts { + id, + method, + uri, + headers, + } + }; + println! ("Step 2 {}", parts.id); let (s, r) = oneshot::channel (); @@ -158,32 +179,6 @@ async fn handle_http_request ( } } -async fn handle_udp_send () -> Response -{ - use tokio::net::UdpSocket; - - let mut sock = UdpSocket::bind (SocketAddr::from (([0,0,0,0], 0))).await.unwrap (); - - sock.send_to (b"handle_udp_send\n", SocketAddr::from (([127,0,0,1], 9000u16))).await.unwrap (); - - status_reply (StatusCode::OK, "ok\n") -} - -async fn handle_udp_recv () -> Response -{ - use tokio::net::UdpSocket; - - let mut sock = UdpSocket::bind (SocketAddr::from (([0,0,0,0], 4001))).await.unwrap (); - - let mut buffer = vec! [0u8; 4096]; - - let (bytes_received, _addr) = sock.recv_from (&mut buffer [..]).await.unwrap (); - - buffer.truncate (bytes_received); - - status_reply (StatusCode::OK, buffer) -} - fn prefix_match <'a> (hay: &'a str, needle: &str) -> Option <&'a str> { if hay.starts_with (needle) { @@ -222,26 +217,15 @@ async fn handle_all (req: Request , state: Arc ) else if let Some (rest) = prefix_match (path, "/http_request/") { if let Some (idx) = rest.find ('/') { let listen_code = String::from (&rest [0..idx]); - let path = String::from (&rest [idx + 1..]); + let path = String::from (&rest [idx..]); let (parts, _) = req.into_parts (); - let parts = match parts.try_into () { - Ok (x) => x, - _ => return Ok (status_reply (StatusCode::BAD_REQUEST, "Couldn't convert request")), - }; - Ok (handle_http_request (parts, state, listen_code).await) + + Ok (handle_http_request (parts, path, state, listen_code).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) - } - else if let Some (name) = prefix_match (path, "/udp_recv/") { - Ok (handle_udp_recv ().await) - } - */ else { Ok (status_reply (StatusCode::OK, "Hi\n")) } diff --git a/src/http_serde.rs b/src/http_serde.rs index e52a2fa..953cd49 100644 --- a/src/http_serde.rs +++ b/src/http_serde.rs @@ -12,7 +12,7 @@ pub enum Error { } impl From for Error { - fn from (x: hyper::header::InvalidHeaderName) -> Self { + fn from (_x: hyper::header::InvalidHeaderName) -> Self { Self::InvalidHeaderName } } @@ -58,46 +58,6 @@ pub struct RequestParts { pub headers: HashMap >, } -impl TryFrom for RequestParts { - type Error = Error; - - fn try_from (x: http::request::Parts) -> Result { - let method = x.method.try_into ()?; - let uri = x.uri.to_string (); - let headers = HashMap::from_iter ( - x.headers.into_iter () - .filter_map (|(k, v)| k.map (|k| (k, v))) - .map (|(k, v)| (String::from (k.as_str ()), v.as_bytes ().to_vec ())) - ); - - Ok (Self { - id: ulid::Ulid::new ().to_string (), - method, - uri, - headers, - }) - } -} - -impl TryFrom for http::request::Builder { - type Error = Error; - - fn try_from (x: RequestParts) -> Result { - let method = Method::from (x.method); - - let mut result = Self::new () - .method (method) - .uri (x.uri) - ; - - for (k, v) in x.headers.into_iter () { - result = result.header (http::header::HeaderName::try_from (&k)?, v); - } - - Ok (result) - } -} - #[derive (Deserialize, Serialize)] pub enum StatusCode { Ok,