Skip to content

Improve link fallback #582

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

Closed
wants to merge 3 commits into from
Closed

Conversation

Zexbe
Copy link
Contributor

@Zexbe Zexbe commented Feb 2, 2020

Improve handling of urls that are missing a /
Ex: http://127.0.0.1:3000/hyper/0.13.2/hyper/client/conn

Fix #288

@jyn514 jyn514 self-assigned this Feb 2, 2020
@@ -111,7 +111,7 @@ impl CratesfyiHandler {

let shared_resources = Self::chain(&pool_factory, rustdoc::SharedResourceHandler);
let router_chain = Self::chain(&pool_factory, routes.iron_router());
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").unwrap()).join("public_html");
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("CRATESFYI_PREFIX environment variable does not exists")).join("public_html");
Copy link
Member

Choose a reason for hiding this comment

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

What do you think, is this overkill?

Suggested change
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("CRATESFYI_PREFIX environment variable does not exists")).join("public_html");
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX")
.expect("the CRATESFYI_PREFIX environment variable is not set, \
see https://github.com/rust-lang/docs.rs/wiki/Developing-without-docker-compose \
for instructions on developing locally")
).join("public_html");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue I feel with putting the link on the expect is that it is weird for this one to have it, and not the other ones, but if all of them have it, it gets pretty ugly, so maybe it needs to just check all the environment variables at the start, and print an error message, but I don't think it should be put into this pull request.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("CRATESFYI_PREFIX environment variable does not exists")).join("public_html");
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("the CRATESFYI_PREFIX environment variable is not set")).join("public_html");

req_path.push("index.html");
} else if !end.contains('.') {
Copy link
Member

Choose a reason for hiding this comment

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

This is not the solution I would like to see. If there is a file (not a directory) called /:crate/:version/name, this will prevent it from being seen, instead always redirecting to the directory. I would rather fallback to the directory in

let file = match File::from_path(&conn, &path) {
only if the file does not already exist.

Also, this is a bit of a fragile part of the code and I'd rather not change it unless absolutely necessary.

//If it is a directory, we default to looking
if path.ends_with("/") {
path.push_str(DIR_DEFAULT_FILE);
req_path.push(DIR_DEFAULT_FILE);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the same behavior, it will turn /crate/version/ into /crate/version//index.html.

This will mess up the redirect to latest version, which unfortunately only has unit tests in #570.

Since that's the case, I'm not comfortable merging until either #570 is merged or I get a chance to test locally which will probably be a few days.

If you instead change only the from_path block (line 251), I would be more comfortable with the changes.

@@ -315,7 +318,7 @@ fn path_for_version(req_path: &[&str], target_name: &str, conn: &Connection) ->
return req_path[3..].join("/");
}
// this page doesn't exist in the latest version
let search_item = if *req_path.last().unwrap() == "index.html" {
let search_item = if *req_path.last().unwrap() == DIR_DEFAULT_FILE {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason you made this change?

.rustdoc_file("all.html", b"some data 2")
.rustdoc_file("file_without_ext", b"some data 3")
.rustdoc_file("some_module/directory/index.html", b"some data 3")
.rustdoc_file("some_module/file_without_ext", b"some data 3")
Copy link
Member

Choose a reason for hiding this comment

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

Please also test /directory.html/index.html, which I don't think will work currently.

@@ -111,7 +111,7 @@ impl CratesfyiHandler {

let shared_resources = Self::chain(&pool_factory, rustdoc::SharedResourceHandler);
let router_chain = Self::chain(&pool_factory, routes.iron_router());
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").unwrap()).join("public_html");
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("CRATESFYI_PREFIX environment variable does not exists")).join("public_html");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("CRATESFYI_PREFIX environment variable does not exists")).join("public_html");
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("the CRATESFYI_PREFIX environment variable is not set")).join("public_html");

@Zexbe Zexbe closed this Feb 2, 2020
@Zexbe Zexbe deleted the improve-link-fallback branch February 2, 2020 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Try to serve index.html if link points to directory
2 participants