From 975661048e2aea2631939a2420fe3a4939f81515 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Mon, 30 Oct 2017 12:56:28 +0100 Subject: [PATCH] Refactor to reuse TemporaryRedirectResource instead of ArticleRedirectResource --- src/resources/article_redirect_resource.rs | 51 -------------------- src/resources/mod.rs | 2 - src/resources/temporary_redirect_resource.rs | 11 +++++ src/wiki_lookup.rs | 4 +- 4 files changed, 13 insertions(+), 55 deletions(-) delete mode 100644 src/resources/article_redirect_resource.rs diff --git a/src/resources/article_redirect_resource.rs b/src/resources/article_redirect_resource.rs deleted file mode 100644 index 26b5b7b..0000000 --- a/src/resources/article_redirect_resource.rs +++ /dev/null @@ -1,51 +0,0 @@ -use futures::{self, Future}; -use hyper; -use hyper::header::Location; -use hyper::server::*; - -use web::{Resource, ResponseFuture}; - -pub struct ArticleRedirectResource { - slug: String, -} - -impl ArticleRedirectResource { - pub fn new(slug: String) -> Self { - // Hack to let redirects to "" work: - // TODO Calculate absolute Location URLs to conform to spec - // This would also remove the need for this hack - Self { - slug: if slug == "" { ".".to_owned() } else { slug } - } - } -} - -impl Resource for ArticleRedirectResource { - fn allow(&self) -> Vec { - use hyper::Method::*; - vec![Options, Head, Get, Put] - } - - fn head(&self) -> ResponseFuture { - Box::new(futures::finished(Response::new() - .with_status(hyper::StatusCode::TemporaryRedirect) - .with_header(Location::new(self.slug.clone())) - )) - } - - fn get(self: Box) -> ResponseFuture { - Box::new(self.head() - .and_then(move |head| { - Ok(head - .with_body(format!("Moved to {}", self.slug))) - })) - } - - fn put(self: Box, _body: hyper::Body, _identity: Option) -> ResponseFuture { - Box::new(self.head() - .and_then(move |head| { - Ok(head - .with_body(format!("Moved to {}", self.slug))) - })) - } -} diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 010706f..0791e0e 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -1,6 +1,5 @@ pub mod pagination; -mod article_redirect_resource; mod article_revision_resource; mod article_resource; mod changes_resource; @@ -9,7 +8,6 @@ mod search_resource; mod sitemap_resource; mod temporary_redirect_resource; -pub use self::article_redirect_resource::ArticleRedirectResource; pub use self::article_revision_resource::ArticleRevisionResource; pub use self::article_resource::ArticleResource; pub use self::changes_resource::{ChangesLookup, ChangesResource}; diff --git a/src/resources/temporary_redirect_resource.rs b/src/resources/temporary_redirect_resource.rs index 085072e..53b3417 100644 --- a/src/resources/temporary_redirect_resource.rs +++ b/src/resources/temporary_redirect_resource.rs @@ -13,6 +13,17 @@ impl TemporaryRedirectResource { pub fn new(location: String) -> Self { Self { location } } + + pub fn from_slug>(slug: S) -> Self { + Self { + location: + if slug.as_ref().is_empty() { + "." + } else { + slug.as_ref() + }.to_owned() + } + } } impl Resource for TemporaryRedirectResource { diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index 24a916e..4c9736f 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -170,7 +170,7 @@ impl WikiLookup { let slugified_slug = slugify(&slug); if slugified_slug != slug { return Box::new(finished(Some( - Box::new(ArticleRedirectResource::new(slugified_slug)) as BoxResource + Box::new(TemporaryRedirectResource::from_slug(slugified_slug)) as BoxResource ))); } @@ -186,7 +186,7 @@ impl WikiLookup { SlugLookup::Hit { article_id, revision } => Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource, SlugLookup::Redirect(slug) => - Box::new(ArticleRedirectResource::new(slug)) as BoxResource, + Box::new(TemporaryRedirectResource::from_slug(slug)) as BoxResource, }))) ) }