Skip to content

redirect all *.ico paths to /favicon.ico #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! ctry {
($result:expr) => (match $result {
Ok(v) => v,
Err(e) => {
return super::page::Page::new(format!("{:?}", e)).title("An error has occured")
return $crate::web::page::Page::new(format!("{:?}", e)).title("An error has occured")
.set_status(::iron::status::BadRequest).to_resp("resp");
}
})
Expand All @@ -21,7 +21,7 @@ macro_rules! cexpect {
($option:expr) => (match $option {
Some(v) => v,
None => {
return super::page::Page::new("Resource not found".to_owned())
return $crate::web::page::Page::new("Resource not found".to_owned())
.title("An error has occured")
.set_status(::iron::status::BadRequest).to_resp("resp");
}
Expand Down Expand Up @@ -485,6 +485,25 @@ fn opensearch_xml_handler(_: &mut Request) -> IronResult<Response> {
Ok(response)
}

fn ico_handler(req: &mut Request) -> IronResult<Response> {
use iron::Url;

if let Some(&"favicon.ico") = req.url.path().last() {
// if we're looking for exactly "favicon.ico", we need to defer to the handler that loads
// from `public_html`, so return a 404 here to make the main handler carry on
Err(IronError::new(error::Nope::ResourceNotFound, status::NotFound))
} else {
// if we're looking for something like "favicon-20190317-1.35.0-nightly-c82834e2b.ico",
// redirect to the plain one so that the above branch can trigger with the correct filename
let url = ctry!(Url::parse(&format!("{}://{}:{}/favicon.ico",
req.url.scheme(),
req.url.host(),
req.url.port())[..]));

Ok(redirect(url))
}
}

/// MetaData used in header
#[derive(Debug)]
pub struct MetaData {
Expand Down
4 changes: 4 additions & 0 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
// javascript files should be handled by the file server instead of erroneously
// redirecting to the crate root page
return rustdoc_html_server_handler(req);
} else if req.url.as_ref().path_segments().unwrap().last().map_or(false, |s| s.ends_with(".ico")) {
// route .ico files into their dedicated handler so that docs.rs's favicon is always
// displayed
return super::ico_handler(req);
}

let router = extension!(req, Router);
Expand Down