-
Notifications
You must be signed in to change notification settings - Fork 978
Add VirtualHost support for random ports (#6410) #6603
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
Open
junjunclub
wants to merge
6
commits into
line:main
Choose a base branch
from
junjunclub:feature/virtualhost-random-port-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
941415c
Add VirtualHost support for random ports (#6410)
junjunclub 80d4811
Add newline at end of file
junjunclub 4c4f766
Apply review feedback: Add @UnstableApi and fix ServerPort validation
junjunclub 8a3b41a
Optimize findVirtualHost for ServerPort-based VirtualHosts with O(1) …
junjunclub 4786e0e
Add @UnstableApi to actualPort() and add fixed port VirtualHost test
junjunclub 3004ed6
Unify VH mapping and cache hostnames
junjunclub 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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 |
|---|---|---|
|
|
@@ -84,6 +84,12 @@ public final class VirtualHost { | |
| private final String hostnamePattern; | ||
| private final int port; | ||
| @Nullable | ||
| private final ServerPort serverPort; | ||
| @Nullable | ||
| private volatile String cachedDefaultHostname; | ||
| @Nullable | ||
| private volatile String cachedHostnamePattern; | ||
| @Nullable | ||
| private final SslContext sslContext; | ||
| @Nullable | ||
| private final TlsProvider tlsProvider; | ||
|
|
@@ -112,6 +118,7 @@ public final class VirtualHost { | |
| private final Function<RoutingContext, RequestId> requestIdGenerator; | ||
|
|
||
| VirtualHost(String defaultHostname, String hostnamePattern, int port, | ||
| @Nullable ServerPort serverPort, | ||
|
Contributor
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. This change seems to introduce a compilation error in |
||
| @Nullable SslContext sslContext, | ||
| @Nullable TlsProvider tlsProvider, | ||
| @Nullable TlsEngineType tlsEngineType, | ||
|
|
@@ -142,6 +149,7 @@ public final class VirtualHost { | |
| this.hostnamePattern = hostnamePattern; | ||
| } | ||
| this.port = port; | ||
| this.serverPort = serverPort; | ||
| this.sslContext = sslContext; | ||
| this.tlsProvider = tlsProvider; | ||
| this.tlsEngineType = tlsEngineType; | ||
|
|
@@ -182,7 +190,8 @@ VirtualHost withNewSslContext(SslContext sslContext) { | |
| ReferenceCountUtil.release(sslContext); | ||
| throw new IllegalStateException("Cannot set a new SslContext when TlsProvider is set."); | ||
| } | ||
| return new VirtualHost(originalDefaultHostname, originalHostnamePattern, port, sslContext, null, | ||
| return new VirtualHost(originalDefaultHostname, originalHostnamePattern, port, serverPort, | ||
| sslContext, null, | ||
| tlsEngineType, serviceConfigs, fallbackServiceConfig, | ||
| RejectedRouteHandler.DISABLED, host -> accessLogger, defaultServiceNaming, | ||
| defaultLogName, requestTimeoutMillis, maxRequestLength, verboseResponses, | ||
|
|
@@ -309,9 +318,21 @@ void setServerConfig(ServerConfig serverConfig) { | |
| } | ||
|
|
||
| /** | ||
| * Returns the default hostname of this virtual host. | ||
| * Returns the name of the default host. | ||
| */ | ||
| public String defaultHostname() { | ||
| if (serverPort != null) { | ||
| String cached = cachedDefaultHostname; | ||
| if (cached != null) { | ||
| return cached; | ||
| } | ||
| final int actualPort = serverPort.actualPort(); | ||
| if (actualPort > 0) { | ||
| cached = originalDefaultHostname + ':' + actualPort; | ||
| cachedDefaultHostname = cached; | ||
| return cached; | ||
| } | ||
| } | ||
| return defaultHostname; | ||
| } | ||
|
|
||
|
|
@@ -320,6 +341,18 @@ public String defaultHostname() { | |
| * <a href="https://datatracker.ietf.org/doc/html/rfc2818#section-3.1">the section 3.1 of RFC2818</a>. | ||
| */ | ||
| public String hostnamePattern() { | ||
| if (serverPort != null) { | ||
| String cached = cachedHostnamePattern; | ||
| if (cached != null) { | ||
| return cached; | ||
| } | ||
| final int actualPort = serverPort.actualPort(); | ||
| if (actualPort > 0) { | ||
| cached = originalHostnamePattern + ':' + actualPort; | ||
| cachedHostnamePattern = cached; | ||
| return cached; | ||
| } | ||
| } | ||
| return hostnamePattern; | ||
| } | ||
|
|
||
|
|
@@ -331,6 +364,15 @@ public int port() { | |
| return port; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the {@link ServerPort} that this virtual host is bound to, | ||
| * or {@code null} if this virtual host is not based on a {@link ServerPort}. | ||
| */ | ||
| @Nullable | ||
| ServerPort serverPort() { | ||
| return serverPort; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the {@link SslContext} of this virtual host. | ||
| */ | ||
|
|
@@ -602,7 +644,8 @@ VirtualHost decorate(@Nullable Function<? super HttpService, ? extends HttpServi | |
| final ServiceConfig fallbackServiceConfig = | ||
| this.fallbackServiceConfig.withDecoratedService(decorator); | ||
|
|
||
| return new VirtualHost(originalDefaultHostname, originalHostnamePattern, port, sslContext, tlsProvider, | ||
| return new VirtualHost(originalDefaultHostname, originalHostnamePattern, port, serverPort, | ||
| sslContext, tlsProvider, | ||
| tlsEngineType, serviceConfigs, fallbackServiceConfig, | ||
| RejectedRouteHandler.DISABLED, host -> accessLogger, defaultServiceNaming, | ||
| defaultLogName, requestTimeoutMillis, maxRequestLength, verboseResponses, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why was the motivation for this refactor? It seems to cause a regression. A unknown host no longer fall back to the default port-based virtual host. The following test passes in the main branch but fails with this PR.