Refactor to reuse TemporaryRedirectResource instead of ArticleRedirectResource
This commit is contained in:
parent
1bdc21039b
commit
975661048e
4 changed files with 13 additions and 55 deletions
|
@ -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<hyper::Method> {
|
|
||||||
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<Self>) -> ResponseFuture {
|
|
||||||
Box::new(self.head()
|
|
||||||
.and_then(move |head| {
|
|
||||||
Ok(head
|
|
||||||
.with_body(format!("Moved to {}", self.slug)))
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn put(self: Box<Self>, _body: hyper::Body, _identity: Option<String>) -> ResponseFuture {
|
|
||||||
Box::new(self.head()
|
|
||||||
.and_then(move |head| {
|
|
||||||
Ok(head
|
|
||||||
.with_body(format!("Moved to {}", self.slug)))
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,5 @@
|
||||||
pub mod pagination;
|
pub mod pagination;
|
||||||
|
|
||||||
mod article_redirect_resource;
|
|
||||||
mod article_revision_resource;
|
mod article_revision_resource;
|
||||||
mod article_resource;
|
mod article_resource;
|
||||||
mod changes_resource;
|
mod changes_resource;
|
||||||
|
@ -9,7 +8,6 @@ mod search_resource;
|
||||||
mod sitemap_resource;
|
mod sitemap_resource;
|
||||||
mod temporary_redirect_resource;
|
mod temporary_redirect_resource;
|
||||||
|
|
||||||
pub use self::article_redirect_resource::ArticleRedirectResource;
|
|
||||||
pub use self::article_revision_resource::ArticleRevisionResource;
|
pub use self::article_revision_resource::ArticleRevisionResource;
|
||||||
pub use self::article_resource::ArticleResource;
|
pub use self::article_resource::ArticleResource;
|
||||||
pub use self::changes_resource::{ChangesLookup, ChangesResource};
|
pub use self::changes_resource::{ChangesLookup, ChangesResource};
|
||||||
|
|
|
@ -13,6 +13,17 @@ impl TemporaryRedirectResource {
|
||||||
pub fn new(location: String) -> Self {
|
pub fn new(location: String) -> Self {
|
||||||
Self { location }
|
Self { location }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_slug<S: AsRef<str>>(slug: S) -> Self {
|
||||||
|
Self {
|
||||||
|
location:
|
||||||
|
if slug.as_ref().is_empty() {
|
||||||
|
"."
|
||||||
|
} else {
|
||||||
|
slug.as_ref()
|
||||||
|
}.to_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for TemporaryRedirectResource {
|
impl Resource for TemporaryRedirectResource {
|
||||||
|
|
|
@ -170,7 +170,7 @@ impl WikiLookup {
|
||||||
let slugified_slug = slugify(&slug);
|
let slugified_slug = slugify(&slug);
|
||||||
if slugified_slug != slug {
|
if slugified_slug != slug {
|
||||||
return Box::new(finished(Some(
|
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 } =>
|
SlugLookup::Hit { article_id, revision } =>
|
||||||
Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource,
|
Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource,
|
||||||
SlugLookup::Redirect(slug) =>
|
SlugLookup::Redirect(slug) =>
|
||||||
Box::new(ArticleRedirectResource::new(slug)) as BoxResource,
|
Box::new(TemporaryRedirectResource::from_slug(slug)) as BoxResource,
|
||||||
})))
|
})))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue