Skip to content

Handle 404s and semver in source file viewer #1350

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
Apr 14, 2021
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
7 changes: 7 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ pub(crate) fn assert_success(path: &str, web: &TestFrontend) -> Result<(), Error
Ok(())
}

/// Make sure that a URL returns a 404
pub(crate) fn assert_not_found(path: &str, web: &TestFrontend) -> Result<(), Error> {
let status = web.get(path).send()?.status();
assert_eq!(status, 404, "GET {} should have been a 404", path);
Ok(())
}

/// Make sure that a URL redirects to a specific page
pub(crate) fn assert_redirect(
path: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! ctry {
let request: &::iron::Request = $req;

::log::error!(
"called ctry!() on an `Err` value: {}\nnote: while attempting to fetch the route {:?}\n{:?}",
"called ctry!() on an `Err` value: {:?}\nnote: while attempting to fetch the route {:?}\n{:?}",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this it only shows the name of the template page that failed to render, not what the error actually was.

error,
request.url,
::backtrace::Backtrace::new(),
Expand Down
88 changes: 73 additions & 15 deletions src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use crate::{
db::Pool,
impl_webpage,
web::{error::Nope, file::File as DbFile, page::WebPage, MetaData},
web::{
error::Nope, file::File as DbFile, match_version, page::WebPage, redirect_base,
MatchSemver, MetaData, Url,
},
Config, Storage,
};
use iron::{IronResult, Request, Response};
Expand Down Expand Up @@ -160,17 +163,42 @@ impl_webpage! {

pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
let router = extension!(req, Router);
let name = cexpect!(req, router.find("name"));
let version = cexpect!(req, router.find("version"));
let mut crate_name = cexpect!(req, router.find("name"));
let req_version = cexpect!(req, router.find("version"));
let pool = extension!(req, Pool);
let mut conn = pool.get()?;

let mut req_path = req.url.path();
// remove first elements from path which is /crate/:name/:version/source
req_path.drain(0..4);

let v = match_version(&mut conn, &crate_name, Some(req_version))?;
if let Some(new_name) = &v.corrected_name {
// `match_version` checked against -/_ typos, so if we have a name here we should
// use that instead
crate_name = new_name;
}
let version = match v.version {
MatchSemver::Exact((version, _)) => version,
MatchSemver::Semver((version, _)) => {
let url = ctry!(
req,
Url::parse(&format!(
"{}/crate/{}/{}/source/{}",
redirect_base(req),
crate_name,
version,
req_path.join("/"),
)),
);

return Ok(super::redirect(url));
}
};

// get path (req_path) for FileList::from_path and actual path for super::file::File::from_path
let (req_path, file_path) = {
let mut req_path = req.url.path();
// remove first elements from path which is /crate/:name/:version/source
for _ in 0..4 {
req_path.remove(0);
}
let file_path = format!("sources/{}/{}/{}", name, version, req_path.join("/"));
let file_path = format!("sources/{}/{}/{}", crate_name, version, req_path.join("/"));

// FileList::from_path is only working for directories
// remove file name if it's not a directory
Expand All @@ -183,13 +211,11 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
// remove crate name and version from req_path
let path = req_path
.join("/")
.replace(&format!("{}/{}/", name, version), "");
.replace(&format!("{}/{}/", crate_name, version), "");

(path, file_path)
};

let pool = extension!(req, Pool);
let mut conn = pool.get()?;
let storage = extension!(req, Storage);
let config = extension!(req, Config);

Expand Down Expand Up @@ -217,8 +243,8 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
(None, false)
};

let file_list =
FileList::from_path(&mut conn, &name, &version, &req_path).ok_or(Nope::NoResults)?;
let file_list = FileList::from_path(&mut conn, &crate_name, &version, &req_path)
.ok_or(Nope::ResourceNotFound)?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what fixed the 500 error; before it was treating this as a search page and panicking because variables weren't set.


SourcePage {
file_list,
Expand All @@ -231,7 +257,7 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {

#[cfg(test)]
mod tests {
use crate::test::{assert_success, wrapper};
use crate::test::*;

#[test]
fn cargo_ok_not_skipped() {
Expand All @@ -247,4 +273,36 @@ mod tests {
Ok(())
});
}

#[test]
fn directory_not_found() {
wrapper(|env| {
env.fake_release()
.name("mbedtls")
.version("0.2.0")
.create()?;
let web = env.frontend();
assert_not_found("/crate/mbedtls/0.2.0/source/test/", web)?;
Ok(())
})
}

#[test]
fn semver_handled() {
wrapper(|env| {
env.fake_release()
.name("mbedtls")
.version("0.2.0")
.source_file("README.md", b"hello")
.create()?;
let web = env.frontend();
assert_success("/crate/mbedtls/0.2.0/source/", web)?;
assert_redirect(
"/crate/mbedtls/*/source/",
"/crate/mbedtls/0.2.0/source/",
web,
)?;
Ok(())
})
}
}