From 5a859e5c337b5286e8fb7e9267d6074789c68563 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Tue, 5 Sep 2017 12:05:56 +0200 Subject: [PATCH] Dispatch PUT to ArticleResource --- src/site.rs | 16 ++++++++++++++-- src/web/resource.rs | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/site.rs b/src/site.rs index 7ac430d..e7e5b0f 100644 --- a/src/site.rs +++ b/src/site.rs @@ -111,7 +111,7 @@ impl ArticleResource { impl Resource for ArticleResource { fn allow(&self) -> Vec { use hyper::Method::*; - vec![Options, Head, Get] + vec![Options, Head, Get, Put] } fn head(&self) -> futures::BoxFuture> { @@ -151,6 +151,10 @@ impl Resource for ArticleResource { }.to_string()) ).boxed() } + + fn put(self, body: &[u8]) -> futures::BoxFuture> { + unimplemented!() + } } @@ -195,7 +199,7 @@ impl Service for Site { type Future = futures::BoxFuture; fn call(&self, req: Request) -> Self::Future { - let (method, uri, _http_version, _headers, _body) = req.deconstruct(); + let (method, uri, _http_version, _headers, body) = req.deconstruct(); println!("{} {}", method, uri); self.root.lookup(uri.path(), uri.query(), None /*uri.fragment()*/) @@ -206,6 +210,14 @@ impl Service for Site { Options => futures::finished(resource.options()).boxed(), Head => resource.head(), Get => resource.get(), + Put => { + use futures::Stream; + body + .concat2() + .map_err(|x| Box::new(x) as Box<::std::error::Error + Send>) + .and_then(move |body| resource.put(&body)) + .boxed() + }, _ => futures::finished(resource.method_not_allowed()).boxed() } }, diff --git a/src/web/resource.rs b/src/web/resource.rs index 65587f8..92da2ce 100644 --- a/src/web/resource.rs +++ b/src/web/resource.rs @@ -13,6 +13,7 @@ pub trait Resource { fn allow(&self) -> Vec; fn head(&self) -> futures::BoxFuture; fn get(self) -> futures::BoxFuture; + fn put(self, body: &[u8]) -> futures::BoxFuture; fn options(&self) -> Response { Response::new()