-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add TrailingSlash::MergeOnly behavior #1695
Conversation
… with actix-web 2.0 Behavior of `NormalizePath` in actix-web 2.0 would keep the trailing slash's existance as it is, which is different from that in 3.0. This patch add a new option called `MergeOnly` in `TrailingSlash`, which only merge the trailing slashes into one if there exist. This option makes `NormalizePath` able to be compatible with actix-web 2.0 and makes it eaiser for users who want to migrate from 2.0 to 3.0. This will close actix#1694.
Codecov Report
@@ Coverage Diff @@
## master #1695 +/- ##
==========================================
+ Coverage 53.32% 53.41% +0.09%
==========================================
Files 125 125
Lines 11918 11942 +24
==========================================
+ Hits 6355 6379 +24
Misses 5563 5563
Continue to review full report at Codecov.
|
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.
Looks good.
I think it should be discussed whether this should be considered a permanent option or just for migration from v2 (and then removed with the next breaking changes). I personally can't see the use case for wanting the distinction and consider it a foot gun to allow it in this middleware. In my view, either all your routes are defined with trailing slashes or they aren't. |
Browsers are affected by the trailing slash policies when they are trying to find resource via relative paths. Example 1:I've got a site provides <link href="style.css" type="text/css" rel="stylesheet"/> No matter how the route I defined, users' access to To fix the problem above, I'd have to write the routes which response Example 2:Consider I want When I use When I use Only when I use |
It seems most of your problems could be solved, client-side, by not relying so heavily on relative paths. That being said, the case of redirection and relative path resolving in browsers is compelling to try solving anyway. From the perspective of an actix app serving markup, an alternate way to solve this would be an option within this middleware to redirect to the normalized path rather than transparently normalizing and serving that resource. I think this idea has been mentioned before. Prior art for this approach includes Apache and Nginx rewrite rule flags. |
Well, I'm afraid that w/ this approach, I'll not able to deploy actix-web sites directly to 80/tcp or 443/tcp, but have to set a reverse proxy for it. |
I want to try to fully understand your needs here so we can come up with the best solution. It occurs to me that this is a solved problem on the reverse proxy/static page servers like apache and nginx where they have chosen to use the rewrite rules system that can optionally redirect to the normalized page. As far as I can tell, this solves the same problem and is worth considering as an alternative. You've even suggested this will solve the problem:
...so providing a built-in way to redirect to normalized paths would work and be more familiar to folks coming from apache/nginx. Example 1: using a relative link here is dangerous anyway, using Example 2: same idea, you want to redirect to There's lots of things to consider when adding a feature like this so I'd like to be convinced this is solving the right problem. |
Well, what I'm facing is a little troublesome. 0x0. Working backgroundI'm working in an company w/ tens of branches and a huge intranet. The company I'm working for is not a company w/ many professioned server operators, hence many employees are not able to write any config files. In fact, it is a company in traditional industries. As a part of them but not a full-time developer, I often develop some small HTTP services to reduce my pressure. Sometimes, employees in other branches would tell me that they want to deploy my service in theirs branches, bad sadly, they are neither developers nor professioned server operators. In past years, I'd like to provide my HTTP services executables to them but they are unable to prepare the executing environment themselves. (i.e. NodeJS, docker & etc.) What the worse is I'm also wasn't able to access their server becasue the cyber departments in those branches would never allow theirs firewall policy request, in which will aceept the connection from me. We can consider most of them are only able copy my executable to their server's In order to make the deployment as easy as possible, I have use the 0x2. Why I used to face the problems like example 1 & 2In some branches which are financial challenged or far away from big metropolics, they would not able to hire a professional server operator to maintaince their servers. Services which are not provided by parent company have to be deployed by employees who are not familiar with servers. Those branches would provide a hardware load balancer like F5 Big-IP, but cannot provide independent hostnames, because DNS server in our intranet is controlled by parent company, and to applicate a new IP is also a painful work for them. Hence they often use the same hostname but different access folders for different service. Of course I know that's legacy way and not the best practise, but I cannot affect their choice. In my experience, I found that use the relative path is the best way to avoid the troubles for them, because there were no need to config the root path when deploying the app, and compatible with any URL prefix. E.g. In branch A, they may map http://foo.A.intranet/ to I believe that not only me but also other friends may face the similar problem in traditional industries, especially in some developing countries. So I hope this PR will be accept, otherwise I'll have to write a independent external crate for similar requirements😭 |
Thank you for the detailed explanation; it is a compelling reason to add it. |
- Rewrite comments & changes Signed-off-by: 劉安 <liuan@sgcc.com.cn>
Thank you for your suggestion @robjtede . I have appended your suggestions to the new commit. |
Behavior of
NormalizePath
in actix-web 2.0 would keep the trailing slash's existance as it is,which is different from that in 3.0.
This patch add a new option called
MergeOnly
inTrailingSlash
, which only merge the trailingslashes into one if there exist. This option makes
NormalizePath
able to be compatible withactix-web 2.0 and makes it eaiser for users who want to migrate from 2.0 to 3.0.
This will close #1694.
PR Type
Bug Fix
PR Checklist
Check your PR fulfills the following:
Overview
New option in
TrailingSlash
will makes it easy for developers who want to migratefrom 1.0 / 2.0 to 3.0, because the behavior of this option is compatible with legacy versions.
No breaking changes.
close #1694.