-
Notifications
You must be signed in to change notification settings - Fork 209
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
||
|
@@ -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)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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() { | ||
|
@@ -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(()) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.