Impl HEAD method

main
_ 2020-10-27 21:43:12 -05:00
parent 9e3470d5b4
commit aa183de15e
2 changed files with 24 additions and 25 deletions

View File

@ -109,10 +109,21 @@ async fn main () -> Result <(), Box <dyn Error>> {
} }
} }
let should_send_body = match &parts.method {
http_serde::Method::Get => true,
_ => false,
};
println! ("Step 6"); println! ("Step 6");
let client = client.clone (); let client = client.clone ();
tokio::spawn (async move { tokio::spawn (async move {
let (tx, rx) = channel (2); 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"); let mut path = PathBuf::from ("/home/user");
path.push (&uri [1..]); path.push (&uri [1..]);
@ -124,6 +135,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
let file_md = f.metadata ().await.unwrap (); let file_md = f.metadata ().await.unwrap ();
if should_send_body {
tokio::spawn (async move { tokio::spawn (async move {
//println! ("Opening file {:?}", path); //println! ("Opening file {:?}", path);
@ -150,6 +162,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
delay_for (Duration::from_millis (50)).await; delay_for (Duration::from_millis (50)).await;
} }
}); });
}
let mut headers: HashMap <String, Vec <u8>> = Default::default (); let mut headers: HashMap <String, Vec <u8>> = Default::default ();
//headers.insert (String::from ("x-its-a-header"), Vec::from (&b"wow"[..])); //headers.insert (String::from ("x-its-a-header"), Vec::from (&b"wow"[..]));
@ -163,10 +176,13 @@ async fn main () -> Result <(), Box <dyn Error>> {
headers, headers,
}; };
let resp_req = client let mut resp_req = client
.post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id)) .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 ())) .header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()));
.body (Body::wrap_stream (rx));
if let Some (body) = body {
resp_req = resp_req.body (body);
}
println! ("Step 6"); println! ("Step 6");
if let Err (e) = resp_req.send ().await { if let Err (e) = resp_req.send ().await {

View File

@ -18,8 +18,8 @@ impl From <hyper::header::InvalidHeaderName> for Error {
#[derive (Deserialize, Serialize)] #[derive (Deserialize, Serialize)]
pub enum Method { pub enum Method {
// Only GET is supported for now
Get, Get,
Head,
} }
impl TryFrom <hyper::Method> for Method { impl TryFrom <hyper::Method> for Method {
@ -28,19 +28,12 @@ impl TryFrom <hyper::Method> for Method {
fn try_from (x: hyper::Method) -> Result <Self, Error> { fn try_from (x: hyper::Method) -> Result <Self, Error> {
match x { match x {
hyper::Method::GET => Ok (Self::Get), hyper::Method::GET => Ok (Self::Get),
hyper::Method::HEAD => Ok (Self::Head),
_ => Err (Error::Unsupported), _ => Err (Error::Unsupported),
} }
} }
} }
impl From <Method> for hyper::Method {
fn from (x: Method) -> Self {
match x {
Method::Get => Self::GET,
}
}
}
#[derive (Deserialize, Serialize)] #[derive (Deserialize, Serialize)]
pub struct RequestParts { pub struct RequestParts {
pub id: String, pub id: String,
@ -61,25 +54,15 @@ pub struct RequestParts {
pub enum StatusCode { pub enum StatusCode {
Ok, Ok,
NotFound, NotFound,
PartialContent,
} }
/*
impl TryFrom <hyper::StatusCode> for StatusCode {
type Error = Error;
fn try_from (x: hyper::StatusCode) -> Result <Self, Error> {
match x {
hyper::StatusCode::OK => Ok (Self::Ok),
hyper::StatusCode::NOT_FOUND => Ok (Self::NotFound),
_ => Err (Error::Unsupported),
}
}
}
*/
impl From <StatusCode> for hyper::StatusCode { impl From <StatusCode> for hyper::StatusCode {
fn from (x: StatusCode) -> Self { fn from (x: StatusCode) -> Self {
match x { match x {
StatusCode::Ok => Self::OK, StatusCode::Ok => Self::OK,
StatusCode::NotFound => Self::NOT_FOUND, StatusCode::NotFound => Self::NOT_FOUND,
StatusCode::PartialContent => Self::PARTIAL_CONTENT,
} }
} }
} }