Skip to content

fix(triple): fix getRemoteHost, getRequestURL port check, and HttpInputStream.read in ServletHttpRequestAdapter - #16599

Open
flush921 wants to merge 3 commits into
apache:3.3from
flush921:taiyi/fix-servlet-request-adapter
Open

flush921 wants to merge 3 commits into
apache:3.3from
flush921:taiyi/fix-servlet-request-adapter

Conversation

@flush921

Copy link
Copy Markdown
Contributor

What is the purpose of the change?

Fix three bugs in ServletHttpRequestAdapter (and its generated Jakarta Servlet counterpart in dubbo-triple-servlet):

  1. getRemoteHost() returned the remote port instead of the host: getRemoteHost() called String.valueOf(remotePort()) instead of remoteHost(), returning e.g. "54321" instead of the peer host name/address.
  2. getRequestURL() appended : -1 when no port is specified in Host / :authority: DefaultHttpRequest.serverPort() returns -1 when the Host header omits a port (as verified in DefaultHttpRequestTest.parsesHostAuthority), so port != 80 / port != 443 evaluated to true and produced URLs like http://example.com:-1/orders/1. Guarding with port > 0 omits unspecified ports while keeping non-default ports.
  3. HttpInputStream overrode readLine(byte[], int, int) instead of read(byte[], int, int): Overriding ServletInputStream.readLine(byte[], int, int) with is.read(b, off, len) bypassed the Servlet specification's newline-delimited readLine() implementation (reading across multiple lines) and left bulk offset reads (read(byte[], int, int)) falling back to InputStream's single-byte loop. Replacing the override with read(byte[], int, int) fixes both.

Verification

  • Confirmed all three issues fail with the new tests in ServletHttpRequestAdapterTest before the fix.
  • Ran ./mvnw test -pl dubbo-plugin/dubbo-triple-servlet -am -Dtest=ServletHttpRequestAdapterTest,ServletHttpResponseAdapterTest -Dsurefire.failIfNoSpecifiedTests=false (including spotless:check, checkstyle, and JDK 17 Jakarta source generation): 13/13 tests pass.

Checklist

  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction.
  • Make sure GitHub Actions can pass.

…utStream.read in ServletHttpRequestAdapter

Fix three bugs in ServletHttpRequestAdapter (and its generated Jakarta Servlet counterpart):

1. getRemoteHost() returned String.valueOf(remotePort()) instead of remoteHost().
2. getRequestURL() appended ":-1" when the Host/:authority header omitted an explicit port (DefaultHttpRequest.serverPort() returns -1). Guard port appending with port > 0.
3. HttpInputStream overrode ServletInputStream.readLine(byte[], int, int) with is.read(b, off, len) instead of overriding read(byte[], int, int), breaking ServletInputStream's newline-delimited readLine() behavior and leaving offset reads un-delegated.

Add unit tests in ServletHttpRequestAdapterTest covering all three fixes.

Signed-off-by: flush921 <1366412595@qq.com>
@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.28%. Comparing base (09a5747) to head (3d122cc).

Files with missing lines Patch % Lines
...est/support/servlet/ServletHttpRequestAdapter.java 81.81% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                3.3   #16599      +/-   ##
============================================
- Coverage     61.24%   59.28%   -1.96%     
+ Complexity    11763       15   -11748     
============================================
  Files          1953     1953              
  Lines         89411    89418       +7     
  Branches      13502    13505       +3     
============================================
- Hits          54757    53009    -1748     
- Misses        29031    30668    +1637     
- Partials       5623     5741     +118     
Flag Coverage Δ
integration-tests-java21 ?
integration-tests-java8 ?
samples-tests-java21 ?
samples-tests-java8 ?
unit-tests-java11 59.52% <81.81%> (-0.03%) ⬇️
unit-tests-java17 ?
unit-tests-java21 58.98% <81.81%> (+<0.01%) ⬆️
unit-tests-java25 58.93% <81.81%> (-0.02%) ⬇️
unit-tests-java8 59.54% <81.81%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@EarthChen EarthChen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verified locally — the three fixes are real and correct. On base d809bb1, 5 of 10 tests fail with exactly the claimed mechanisms (getRemoteHost() returned "54321"; getRequestURL() produced "http://example.com:-1/orders/1"; readLine() consumed "first\nsecond\n" as 13 bytes and read(byte[], int, int) fell back to InputStream's single-byte loop). On this PR head all 10 pass, and the JDK-17-generated Jakarta variant in the same module carries the same fixes. Also confirmed that javax.servlet-api 4.0.1's ServletInputStream.readLine(byte[], int, int) has a concrete spec-default implementation (byte-by-byte until '\n'), so removing the wrong override restores spec behavior rather than breaking anything.

One remaining spec deviation in the same adapter — could we fix it in this PR too?

getServerPort() still returns -1 when the Host header omits the port. Within dubbo's http12 core, -1 is an established "unspecified" sentinel — CorsHeaderFilter.getPort() normalizes it to 80/443 by scheme — but this adapter is the Servlet spec boundary: downstream servlet code calling getServerPort() has no visibility into that internal convention, and the Servlet spec says the port defaults to 80/443. Suggest mirroring CorsHeaderFilter's normalization in ServletHttpRequestAdapter.getServerPort() (return 80 for http / 443 for https when serverPort() < 0). That leaves dubbo-remoting-http12 untouched, keeps the CORS comparison identical (getPort(-1) == getPort(80)), and makes the adapter spec-conformant end to end — at which point the port > 0 guard added in getRequestURL() becomes unreachable for http/https and can be simplified accordingly. A getServerPort() column in the existing @CsvSource test would pin it down.

…er.getServerPort()

Mirror CorsHeaderFilter.getPort() in ServletHttpRequestAdapter.getServerPort() by returning 80 for http and 443 for https when serverPort() < 0, simplify the now-redundant port > 0 check in getRequestURL(), and assert getServerPort() in the parameterized test.

Signed-off-by: flush921 <1366412595@qq.com>
@flush921

Copy link
Copy Markdown
Contributor Author

Thanks @EarthChen for the thorough review and great suggestion! Updated in the latest commit:

  • Normalized serverPort() < 0 to 80 (http) / 443 (https) in ServletHttpRequestAdapter.getServerPort() (mirroring CorsHeaderFilter.getPort() while leaving dubbo-remoting-http12 untouched).
  • Simplified the now-redundant port > 0 check in getRequestURL().
  • Added the expected getServerPort() column to the @CsvSource test in ServletHttpRequestAdapterTest.

Spotless, checkstyle, Jakarta source generation, and all 10 tests pass locally.

This branch has not been deployed

No deployments
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.

3 participants