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,
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 <ServerState>,
watcher_code: String
)
-> 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);
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>
{
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/") {
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"))
}

View File

@ -12,7 +12,7 @@ pub enum Error {
}
impl From <hyper::header::InvalidHeaderName> 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 <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)]
pub enum StatusCode {
Ok,