-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Avoid duplicate URI construction in ReactorServerHttpRequest #30047
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
Closed
yuzawa-san
wants to merge
1
commit into
spring-projects:main
from
yuzawa-san:ReactorServerHttpRequest-uri
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import io.netty5.handler.codec.http.HttpHeaderNames; | ||
import io.netty5.handler.codec.http.headers.HttpCookiePair; | ||
import io.netty5.handler.ssl.SslHandler; | ||
import io.netty5.util.NetUtil; | ||
import org.apache.commons.logging.Log; | ||
import reactor.core.publisher.Flux; | ||
import reactor.netty5.ChannelOperationsId; | ||
|
@@ -75,48 +76,23 @@ public ReactorNetty2ServerHttpRequest(HttpServerRequest request, Netty5DataBuffe | |
|
||
private static URI initUri(HttpServerRequest request) throws URISyntaxException { | ||
Assert.notNull(request, "HttpServerRequest must not be null"); | ||
return new URI(resolveBaseUrl(request) + resolveRequestUri(request)); | ||
return new URI(request.scheme() + "://" + resolveHost(request) + resolveRequestUri(request)); | ||
} | ||
|
||
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException { | ||
String scheme = getScheme(request); | ||
|
||
private static CharSequence resolveHost(HttpServerRequest request) { | ||
InetSocketAddress hostAddress = request.hostAddress(); | ||
if (hostAddress != null) { | ||
return new URI(scheme, null, hostAddress.getHostString(), hostAddress.getPort(), null, null, null); | ||
return NetUtil.toSocketAddressString(hostAddress); | ||
} | ||
|
||
CharSequence charSequence = request.requestHeaders().get(HttpHeaderNames.HOST); | ||
if (charSequence != null) { | ||
String header = charSequence.toString(); | ||
final int portIndex; | ||
if (header.startsWith("[")) { | ||
portIndex = header.indexOf(':', header.indexOf(']')); | ||
} | ||
else { | ||
portIndex = header.indexOf(':'); | ||
} | ||
if (portIndex != -1) { | ||
try { | ||
return new URI(scheme, null, header.substring(0, portIndex), | ||
Integer.parseInt(header, portIndex + 1, header.length(), 10), null, null, null); | ||
} | ||
catch (NumberFormatException ex) { | ||
throw new URISyntaxException(header, "Unable to parse port", portIndex); | ||
} | ||
} | ||
else { | ||
return new URI(scheme, header, null, null); | ||
} | ||
CharSequence header = request.requestHeaders().get(HttpHeaderNames.HOST); | ||
if (header != null) { | ||
return header; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "Host" header can contain host and port, see for example https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host. And its parsing needs to take into account that the host maybe an IPv6 address. This is what the removed code [89-110] does, and that needs to remain. |
||
} | ||
|
||
throw new IllegalStateException("Neither local hostAddress nor HOST header available"); | ||
} | ||
|
||
private static String getScheme(HttpServerRequest request) { | ||
return request.scheme(); | ||
} | ||
|
||
private static String resolveRequestUri(HttpServerRequest request) { | ||
String uri = request.uri(); | ||
for (int i = 0; i < uri.length(); i++) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Before this change, the URI constructor that accepts individual URI components would validate the host and port. After this change, the value returned from
resolveHost
is not validated, the URI constructor with a String (called on line 79) doesn't either.When I tested whether this change would help with spring-projects/spring-boot#34395 where X-Forwarded-HOST is "localhost:3000" and X-Forwarded-Port is "3000", I saw that
resolveHost
returns "localhost:3000:3000", and the resulting URI was "http://localhost:3000:3000/hello". When I test without this change, there is an exception from the URI constructor that was previously used here because "localhost:3000" is not a valid host.