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

Move all rewrite handling logic into the rewrites module #353

Merged
merged 8 commits into from
Apr 21, 2024

Conversation

palant
Copy link
Contributor

@palant palant commented Apr 20, 2024

Description

This implements the same changes to rewrites as have been implemented for redirects in #348. As suggested, I merged the identical code so that it is only present in the redirects module now.

There are some changes to the handler: request fields can no longer be borrowed because we are now modifying the request. While I meant to implement these changes anyway, this unfortunately introduces a merge conflict with #351.

There are also some behavior changes due to the fact that destination is always being processed as a URL now and not merely in the redirect scenario:

  1. Previously, internal rewrites kept the query string unchanged. Now the query string from the destination overrides the original query string if present. This actually matches the behavior of nginx rewrites. The impact should be limited because the only place where query strings currently matter is directory listings, and existing configurations shouldn’t have destinations with query strings anyway.
  2. It is now possible to do an internal rewrite to a full URL which includes a different host name. IMHO that’s a useful feature because it allows defining virtual hosts that are normally only reachable via rewrite – so you can have a subdirectory mapped to a different root (similar to the nginx alias directive). Configuration example:
[advanced]

[[advanced.rewrites]]
source = "/test/{**}"
destination = "http://internal.local/test/$1"

[[advanced.virtual-hosts]]
host = "internal.local"
root = "/usr/srv/test"

This might be worth documenting. There are caveats however. First, it’s a good idea to rewrite to the same path and merely change the host name like I’ve done above, otherwise redirects when accessing directories will be a mess (this is generally an issue with rewrites). Second, someone who knows the name of the internal virtual host will still be able to access it by mapping this host name to the server’s IP address explicitly. So one should not expect anything under that host’s root to be hidden just because rewrites only map to some of the files there.

Related Issue

This is another step towards fixing #342, also fixes the rest of #347.

How Has This Been Tested?

Integration tests pass, some additional unit tests have been added as well. Functional testing on Linux looks fine.

Copy link

semanticdiff-com bot commented Apr 20, 2024

Review changes with SemanticDiff.

Analyzed 3 of 4 files.

Overall, the semantic diff is 13% smaller than the GitHub diff.

Filename Status
docs/content/features/url-rewrites.md Unsupported file format
✔️ src/handler.rs 1.17% smaller
✔️ src/redirects.rs 46.86% smaller
✔️ src/rewrites.rs 0.52% smaller

@palant palant mentioned this pull request Apr 20, 2024
13 tasks
@joseluisq joseluisq added enhancement New feature or request v2 v2 release codebase Related to the project's source code labels Apr 20, 2024
@joseluisq
Copy link
Collaborator

JFYI @palant I will check the PR in the next hours but maybe you want to join us in Discord to also coordinate better reviews, tasks, time, etc.

Copy link
Collaborator

@joseluisq joseluisq left a comment

Choose a reason for hiding this comment

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

Looks good to me. Just a minor doc to add as mentioned in the comment.

src/rewrites.rs Show resolved Hide resolved
Copy link
Collaborator

@joseluisq joseluisq left a comment

Choose a reason for hiding this comment

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

Thanks!

@joseluisq joseluisq merged commit 207fa4a into static-web-server:master Apr 21, 2024
24 checks passed
@palant palant deleted the separate-rewrites-logic branch April 21, 2024 22:43
@joseluisq joseluisq added this to the v2.30.0 milestone Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codebase Related to the project's source code enhancement New feature or request v2 v2 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants