Skip to content

Commit e3b022e

Browse files
Merge pull request #340 from QuietMisdreavus/favicon
redirect all *.ico paths to /favicon.ico
2 parents 62680b0 + f3848a3 commit e3b022e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/web/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! ctry {
99
($result:expr) => (match $result {
1010
Ok(v) => v,
1111
Err(e) => {
12-
return super::page::Page::new(format!("{:?}", e)).title("An error has occured")
12+
return $crate::web::page::Page::new(format!("{:?}", e)).title("An error has occured")
1313
.set_status(::iron::status::BadRequest).to_resp("resp");
1414
}
1515
})
@@ -21,7 +21,7 @@ macro_rules! cexpect {
2121
($option:expr) => (match $option {
2222
Some(v) => v,
2323
None => {
24-
return super::page::Page::new("Resource not found".to_owned())
24+
return $crate::web::page::Page::new("Resource not found".to_owned())
2525
.title("An error has occured")
2626
.set_status(::iron::status::BadRequest).to_resp("resp");
2727
}
@@ -485,6 +485,25 @@ fn opensearch_xml_handler(_: &mut Request) -> IronResult<Response> {
485485
Ok(response)
486486
}
487487

488+
fn ico_handler(req: &mut Request) -> IronResult<Response> {
489+
use iron::Url;
490+
491+
if let Some(&"favicon.ico") = req.url.path().last() {
492+
// if we're looking for exactly "favicon.ico", we need to defer to the handler that loads
493+
// from `public_html`, so return a 404 here to make the main handler carry on
494+
Err(IronError::new(error::Nope::ResourceNotFound, status::NotFound))
495+
} else {
496+
// if we're looking for something like "favicon-20190317-1.35.0-nightly-c82834e2b.ico",
497+
// redirect to the plain one so that the above branch can trigger with the correct filename
498+
let url = ctry!(Url::parse(&format!("{}://{}:{}/favicon.ico",
499+
req.url.scheme(),
500+
req.url.host(),
501+
req.url.port())[..]));
502+
503+
Ok(redirect(url))
504+
}
505+
}
506+
488507
/// MetaData used in header
489508
#[derive(Debug)]
490509
pub struct MetaData {

src/web/rustdoc.rs

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
112112
// javascript files should be handled by the file server instead of erroneously
113113
// redirecting to the crate root page
114114
return rustdoc_html_server_handler(req);
115+
} else if req.url.as_ref().path_segments().unwrap().last().map_or(false, |s| s.ends_with(".ico")) {
116+
// route .ico files into their dedicated handler so that docs.rs's favicon is always
117+
// displayed
118+
return super::ico_handler(req);
115119
}
116120

117121
let router = extension!(req, Router);

0 commit comments

Comments
 (0)