Skip to content
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

fs: Fix serving precompressed files without an extension #507

Merged
merged 5 commits into from
Sep 19, 2024

Conversation

SvizelPritula
Copy link
Contributor

Motivation

When trying to locate the precompressed version of a file that doesn't have a file extension (say /downloads/foobar-cli), the ServeDir service passes the relevant extension with a leading dot to PathBuf::set_extension, which doesn't expect a leading dot. This means it will end up with an filename that looks like this: /downloads/foobar-cli..gz, which probably doesn't exist. Worse still, it also fails to reconstruct the original path when it doesn't find anything at the corrupted path, which leads to a 404 error.

Solution

This change makes the function responsible for constructing the modified path directly modify the filename, instead of using the extension API, since an extension might not exist.

Fixes #504

I feel that #504 is clearly a bug, but someone might for some reason depend on the broken behavior somewhere.

let mut decoder = GzDecoder::new(&body[..]);
let mut decompressed = String::new();
decoder.read_to_string(&mut decompressed).unwrap();
assert!(decompressed.starts_with("Content."));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why assert it starts with that? This seems like it should be an assert_eq! - maybe the string literal needs an extra \n in that case, or we could call decompressed.trim().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that this is a weird way to do this. I did it for consistency with the existing precompression tests, which do the same.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Weird. No need to be consistent with that. I would welcome another PR to change the existing tests too, if you feel like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to check the entire response body.

I'm afraid it will break on Windows if git is configured to change line endings to CRLF. This conversion will also only affect the uncompressed test files, which is annoying.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized that I can just remove the trailing newline from the test file, so I did that. That makes it not a valid Unix text file, but there is no reason it should be.

@jplatte
Copy link
Collaborator

jplatte commented Sep 19, 2024

I forgot to say as part of the review, but thanks a lot for the clear issue description and providing a fix complete with tests!

Copy link
Collaborator

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

One more thing, could you please add a changelog entry to tower-http/CHANGELOG.md?

Copy link
Collaborator

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

Thanks!

@jplatte jplatte changed the title Add support for serving precompressed static files without an extension fs: Fix serving precompressed files without an extension Sep 19, 2024
@jplatte jplatte enabled auto-merge (squash) September 19, 2024 15:44
@jplatte jplatte merged commit 95e28bf into tower-rs:main Sep 19, 2024
11 checks passed
@SvizelPritula
Copy link
Contributor Author

SvizelPritula commented Sep 19, 2024

It seems I managed to add a commit to the PR between you approving it and you merging it. I didn't know that's possible and it seems dangerous.

Edit: This can apparently be disabled.

@jplatte
Copy link
Collaborator

jplatte commented Sep 19, 2024

I saw the new commit before pressing merge. I know this can be disabled, we do it at work, but I haven't seen this be a problem in all of the open source projects I work on. Especially because the GitHub web UI frequently craps out when you try to merge if a commit is pushed between opening the page and pressing merge 😂
(maybe it's intentional that it doesn't work then, but if so they should provide a proper message)

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.

ServeDir doesn't serve files without extentions if pre-compression is enabled
2 participants