CCCLL opened a new pull request, #16564:
URL: https://github.com/apache/dubbo/pull/16564
## What is the purpose of the change?
Fixes #16563.
Servlet code running in Triple REST (`dubbo-triple-servlet`) sent wrong
`Location` headers when it redirected the usual ways:
- `ServletHttpRequestAdapter.getContextPath()` and
`DummyServletContext.getContextPath()` returned `"/"`, so
`sendRedirect(getContextPath() + "/login")` sent `//login`. The servlet API
returns `""` for the root context. They now return `""`.
- `ServletHttpResponseAdapter.encodeURL`, `encodeRedirectURL`, `encodeUrl`
and `encodeRedirectUrl` returned `URLEncoder.encode(url)`, so
`sendRedirect(encodeRedirectURL("/login?next=/orders"))` sent
`%2Flogin%3Fnext%3D%2Forders`. In the servlet API these methods only add a
session id for URL rewriting and otherwise return the URL unchanged. Dubbo does
not rewrite URLs for sessions, so they now return the URL as it is.
Both are needed for code like Spring Security's `DefaultRedirectStrategy`,
which calls `encodeRedirectURL(getContextPath() + url)`: with only the second
change it would send `//login?next=/orders`. The PR has one commit for each,
the context path first.
## Behavior change
- `encodeURL`, `encodeRedirectURL`, `encodeUrl` and `encodeRedirectUrl`
return their argument unchanged instead of a form-encoded copy.
- `HttpServletRequest#getContextPath()` and
`ServletContext#getContextPath()` return `""` instead of `"/"`.
- Code that used `encodeURL` to percent-escape a value now gets the value
back unescaped, as with any servlet container.
- The generated `jakarta` adapters change the same way, since they are
copied from these sources at build time.
## Not covered
- A non-root context: when the protocol has a `contextpath`, or
`TripleFilter` runs in a webapp that is not deployed at the root,
`getContextPath()` now returns `""` instead of `"/"` (a redirect to
`getContextPath() + "/login"` goes to `/login` instead of `//login`), but a
servlet container would return that context path and leave it out of
`getServletPath()`. Doing that needs the context path of the matched service or
of the container request, so I left it out of this PR.
## Verification
- New `ServletHttpRequestAdapterTest` (empty context path for the request
and the servlet context) and `ServletHttpResponseAdapterTest` (the four methods
return `/login?next=/orders`, `http://example.com/a b?x=1#y` and `""`
unchanged). They fail before the change (`expected: <> but was: </>`,
`expected: </login?next=/orders> but was: <%2Flogin%3Fnext%3D%2Forders>`) and
pass after it. Putting the old code back into any one of the six methods makes
a test fail. The tests use the `javax` classes because the `jakarta` copies are
only generated on JDK 17+.
- `mvn clean test verify` of dubbo-triple-servlet and dubbo-rest-spring with
the options of the CI unit test job, on JDK 8, 11, 17, 21 and 25: 61 tests, 0
failures each time (11 skipped, the existing `@Disabled`
`SpringMvcRestProtocolTest`); checkstyle and spotless are clean.
- End to end, the three redirects from the issue, compared with the same
servlet code in Tomcat 9.0.121 (Spring Security 5.8.16 for `/security`):
| Request | 3.3.6 | this PR | Tomcat 9.0.121 |
|---|---|---|---|
| `/redirect` | `%2Flogin%3Fnext%3D%2Forders` | `/login?next=/orders` |
`/login?next=/orders` |
| `/context` | `//login?next=/orders` | `/login?next=/orders` |
`/login?next=/orders` |
| `/security` | `%2F%2Flogin%3Fnext%3D%2Forders` | `/login?next=/orders` |
`/login?next=/orders` |
## Checklist
- [x] Make sure there is a
[GitHub_issue](https://github.com/apache/dubbo/issues) field for the change.
- [x] Write a pull request description that is detailed enough to understand
what the pull request does, how, and why.
- [x] Write necessary unit-test to verify your logic correction. If the new
feature or significant change is committed, please remember to add sample in
[dubbo samples](https://github.com/apache/dubbo-samples) project.
- [ ] Make sure gitHub actions can pass. [Why the workflow is failing and
how to fix it?](../CONTRIBUTING.md)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]