diff --git a/src/bin/server.rs b/src/bin/server.rs index bd575d8..5be1ed0 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -109,10 +109,21 @@ async fn main () -> Result <(), Box > { } } + let should_send_body = match &parts.method { + http_serde::Method::Get => true, + _ => false, + }; + println! ("Step 6"); let client = client.clone (); tokio::spawn (async move { let (tx, rx) = channel (2); + let body = if should_send_body { + Some (Body::wrap_stream (rx)) + } + else { + None + }; let mut path = PathBuf::from ("/home/user"); path.push (&uri [1..]); @@ -124,6 +135,7 @@ async fn main () -> Result <(), Box > { let file_md = f.metadata ().await.unwrap (); + if should_send_body { tokio::spawn (async move { //println! ("Opening file {:?}", path); @@ -150,6 +162,7 @@ async fn main () -> Result <(), Box > { delay_for (Duration::from_millis (50)).await; } }); + } let mut headers: HashMap > = Default::default (); //headers.insert (String::from ("x-its-a-header"), Vec::from (&b"wow"[..])); @@ -163,10 +176,13 @@ async fn main () -> Result <(), Box > { headers, }; - let resp_req = client + let mut resp_req = client .post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id)) - .header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ())) - .body (Body::wrap_stream (rx)); + .header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ())); + + if let Some (body) = body { + resp_req = resp_req.body (body); + } println! ("Step 6"); if let Err (e) = resp_req.send ().await { diff --git a/src/http_serde.rs b/src/http_serde.rs index 8b822fe..7a52fd4 100644 --- a/src/http_serde.rs +++ b/src/http_serde.rs @@ -18,8 +18,8 @@ impl From for Error { #[derive (Deserialize, Serialize)] pub enum Method { - // Only GET is supported for now Get, + Head, } impl TryFrom for Method { @@ -28,19 +28,12 @@ impl TryFrom for Method { fn try_from (x: hyper::Method) -> Result { match x { hyper::Method::GET => Ok (Self::Get), + hyper::Method::HEAD => Ok (Self::Head), _ => Err (Error::Unsupported), } } } -impl From for hyper::Method { - fn from (x: Method) -> Self { - match x { - Method::Get => Self::GET, - } - } -} - #[derive (Deserialize, Serialize)] pub struct RequestParts { pub id: String, @@ -61,25 +54,15 @@ pub struct RequestParts { pub enum StatusCode { Ok, NotFound, + PartialContent, } -/* -impl TryFrom for StatusCode { - type Error = Error; - - fn try_from (x: hyper::StatusCode) -> Result { - match x { - hyper::StatusCode::OK => Ok (Self::Ok), - hyper::StatusCode::NOT_FOUND => Ok (Self::NotFound), - _ => Err (Error::Unsupported), - } - } -} -*/ + impl From for hyper::StatusCode { fn from (x: StatusCode) -> Self { match x { StatusCode::Ok => Self::OK, StatusCode::NotFound => Self::NOT_FOUND, + StatusCode::PartialContent => Self::PARTIAL_CONTENT, } } }