Pass the right part of the URL to the server.

Also remove dead code
main
_ 2020-10-27 20:31:38 -05:00
parent 406b13c3b1
commit 290745f6cf
2 changed files with 29 additions and 85 deletions

View File

@ -3,14 +3,14 @@ use std::{
error::Error, error::Error,
convert::{ convert::{
Infallible, Infallible,
TryInto, TryFrom,
}, },
iter::FromIterator,
net::SocketAddr, net::SocketAddr,
str::FromStr,
sync::{ sync::{
Arc Arc
}, },
time::{Duration, Instant}, time::{Duration},
}; };
use futures::channel::oneshot; use futures::channel::oneshot;
@ -113,12 +113,33 @@ async fn handle_http_response (
} }
async fn handle_http_request ( async fn handle_http_request (
parts: RequestParts, req: http::request::Parts,
uri: String,
state: Arc <ServerState>, state: Arc <ServerState>,
watcher_code: String watcher_code: String
) )
-> Response <Body> -> Response <Body>
{ {
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); println! ("Step 2 {}", parts.id);
let (s, r) = oneshot::channel (); let (s, r) = oneshot::channel ();
@ -158,32 +179,6 @@ async fn handle_http_request (
} }
} }
async fn handle_udp_send () -> Response <Body>
{
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 <Body>
{
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> fn prefix_match <'a> (hay: &'a str, needle: &str) -> Option <&'a str>
{ {
if hay.starts_with (needle) { if hay.starts_with (needle) {
@ -222,26 +217,15 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState>)
else if let Some (rest) = prefix_match (path, "/http_request/") { else if let Some (rest) = prefix_match (path, "/http_request/") {
if let Some (idx) = rest.find ('/') { if let Some (idx) = rest.find ('/') {
let listen_code = String::from (&rest [0..idx]); 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, _) = req.into_parts ();
let parts = match parts.try_into () {
Ok (x) => x, Ok (handle_http_request (parts, path, state, listen_code).await)
_ => return Ok (status_reply (StatusCode::BAD_REQUEST, "Couldn't convert request")),
};
Ok (handle_http_request (parts, state, listen_code).await)
} }
else { else {
Ok (status_reply (StatusCode::BAD_REQUEST, "Bad URI format")) 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 { else {
Ok (status_reply (StatusCode::OK, "Hi\n")) Ok (status_reply (StatusCode::OK, "Hi\n"))
} }

View File

@ -12,7 +12,7 @@ pub enum Error {
} }
impl From <hyper::header::InvalidHeaderName> for Error { impl From <hyper::header::InvalidHeaderName> for Error {
fn from (x: hyper::header::InvalidHeaderName) -> Self { fn from (_x: hyper::header::InvalidHeaderName) -> Self {
Self::InvalidHeaderName Self::InvalidHeaderName
} }
} }
@ -58,46 +58,6 @@ pub struct RequestParts {
pub headers: HashMap <String, Vec <u8>>, pub headers: HashMap <String, Vec <u8>>,
} }
impl TryFrom <http::request::Parts> for RequestParts {
type Error = Error;
fn try_from (x: http::request::Parts) -> Result <Self, Error> {
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 <RequestParts> for http::request::Builder {
type Error = Error;
fn try_from (x: RequestParts) -> Result <Self, Error> {
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)] #[derive (Deserialize, Serialize)]
pub enum StatusCode { pub enum StatusCode {
Ok, Ok,