Skip to content

Commit

Permalink
redirect all *.ico paths to /favicon.ico
Browse files Browse the repository at this point in the history
several rustdoc versions try to use rustdoc's built-in favicon, which
overrides docs.rs's own favicon. instead of modifying these pages to
remove the reference to their favicon, instead redirect them to the
main favicon
  • Loading branch information
QuietMisdreavus committed May 3, 2019
1 parent 62680b0 commit f3848a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit f3848a3

Please sign in to comment.