From 131e661427facc2c37cd48cf62dd237559196568 Mon Sep 17 00:00:00 2001 From: Ondro Mihalyi Date: Fri, 12 Jul 2024 22:57:57 +0200 Subject: [PATCH 1/2] GH #24918: Allow scheme in server name --- .../help/en/help/ref-httplisteneredit.html | 2 +- .../help/en/help/ref-httplistenernew.html | 2 +- .../main/help/en/help/ref-protocolnew.html | 2 +- .../help/en/help/task-httplisteneredit.html | 2 +- .../help/en/help/task-httplistenernew.html | 2 +- .../main/help/en/help/task-protocolnew.html | 2 +- .../glassfish/web/admingui/Strings.properties | 2 +- .../web/admin/cli/create-http-listener.1 | 2 +- .../org/glassfish/web/admin/cli/create-http.1 | 2 +- .../java/org/apache/catalina/Connector.java | 8 +++ .../apache/catalina/connector/Connector.java | 28 ++++++++- .../apache/catalina/connector/Response.java | 13 ++-- .../connector/coyote/PECoyoteConnector.java | 61 +++++++++++-------- .../main/asciidoc/create-http-listener.adoc | 2 +- .../src/main/asciidoc/create-http.adoc | 2 +- 15 files changed, 90 insertions(+), 42 deletions(-) diff --git a/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html b/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html index 219f6d92d50..413c3ad1c2e 100644 --- a/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html +++ b/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html @@ -69,7 +69,7 @@

Edit HTTP Listener

Server Name

The host name to be used in the URLs the server sends to the client. This name is the alias name if your server uses an alias. If your server does not use an alias, leave this field blank.

-

This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client.

+

This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs.

diff --git a/appserver/admingui/web/src/main/help/en/help/ref-httplistenernew.html b/appserver/admingui/web/src/main/help/en/help/ref-httplistenernew.html index 8a26ba18fca..504f7350387 100644 --- a/appserver/admingui/web/src/main/help/en/help/ref-httplistenernew.html +++ b/appserver/admingui/web/src/main/help/en/help/ref-httplistenernew.html @@ -65,7 +65,7 @@

New HTTP Listener

Server Name

The host name to be used in the URLs the server sends to the client. This name is the alias name if your server uses an alias. If your server does not use an alias, leave this field blank.

-

This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client.

+

This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs.

diff --git a/appserver/admingui/web/src/main/help/en/help/ref-protocolnew.html b/appserver/admingui/web/src/main/help/en/help/ref-protocolnew.html index 3474d11a7e3..f9b6ea262ca 100644 --- a/appserver/admingui/web/src/main/help/en/help/ref-protocolnew.html +++ b/appserver/admingui/web/src/main/help/en/help/ref-protocolnew.html @@ -60,7 +60,7 @@

New Protocol

Server Name

The host name to be used in the URLs the server sends to the client. This name is the alias name if your server uses an alias. If your server does not use an alias, leave this field blank.

-

This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client.

+

This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs.

Default Virtual Server
diff --git a/appserver/admingui/web/src/main/help/en/help/task-httplisteneredit.html b/appserver/admingui/web/src/main/help/en/help/task-httplisteneredit.html index 9514dcac3d7..b41121d5761 100644 --- a/appserver/admingui/web/src/main/help/en/help/task-httplisteneredit.html +++ b/appserver/admingui/web/src/main/help/en/help/task-httplisteneredit.html @@ -67,7 +67,7 @@

To Edit an HTTP Listener

  • In the Server Name field, type the host name to be used in the URLs the server sends to the client.

    This name is the alias name if your server uses an alias. If your server does not use an alias, leave this field blank.

    -

    This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client.

    +

    This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs.

  • Click Save.

    diff --git a/appserver/admingui/web/src/main/help/en/help/task-httplistenernew.html b/appserver/admingui/web/src/main/help/en/help/task-httplistenernew.html index d7c83b6c643..6a97ce7b3d1 100644 --- a/appserver/admingui/web/src/main/help/en/help/task-httplistenernew.html +++ b/appserver/admingui/web/src/main/help/en/help/task-httplistenernew.html @@ -72,7 +72,7 @@

    To Create an HTTP Listener

  • In the Server Name field, type the host name to be used in the URLs the server sends to the client.

    This name is the alias name if your server uses an alias. If your server does not use an alias, leave this field blank.

    -

    This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client.

    +

    This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs.

  • Click OK.

    diff --git a/appserver/admingui/web/src/main/help/en/help/task-protocolnew.html b/appserver/admingui/web/src/main/help/en/help/task-protocolnew.html index 49f0d20e85d..1cf95e8d3a6 100644 --- a/appserver/admingui/web/src/main/help/en/help/task-protocolnew.html +++ b/appserver/admingui/web/src/main/help/en/help/task-protocolnew.html @@ -64,7 +64,7 @@

    To Create a Protocol

  • In the Server Name field, type the host name to be used in the URLs the server sends to the client.

    This name is the alias name if your server uses an alias. If your server does not use an alias, leave this field blank.

    -

    This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client.

    +

    This value affects URLs the server automatically generates; it does not affect the URLs for directories and files stored in the server. If your server uses an alias, the server-name should be the alias name. If a colon and port number are appended, that port is used in URLs the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs.

  • From the Default Virtual Server drop-down list, select the virtual server to be associated with this protocol.

    diff --git a/appserver/admingui/web/src/main/resources/org/glassfish/web/admingui/Strings.properties b/appserver/admingui/web/src/main/resources/org/glassfish/web/admingui/Strings.properties index e79ef837bbe..9bbd25cdcd8 100644 --- a/appserver/admingui/web/src/main/resources/org/glassfish/web/admingui/Strings.properties +++ b/appserver/admingui/web/src/main/resources/org/glassfish/web/admingui/Strings.properties @@ -210,7 +210,7 @@ grizzly.httpPageTitle=HTTP grizzly.httpPageTitleHelp=Modify HTTP settings for the protocol. http.serverNameLabel=Server Name: -http.serverNameHelp=Alias name if server uses an alias. If a colon and port number are appended, that port will be used in URLs the server sends to the client. +http.serverNameHelp=Alias name if server uses an alias. A colon and port number can be appended. Scheme and :// can be prepended. They will be used in URLs the server sends to the client. http.Adapter=Adapter: http.AdapterHelp=Class name of the static resources adapter http.defVirtualServerLabel=Default Virtual Server: diff --git a/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http-listener.1 b/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http-listener.1 index 734c19d4e57..624aa3ef6fb 100644 --- a/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http-listener.1 +++ b/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http-listener.1 @@ -69,7 +69,7 @@ OPTIONS generates; it doesn't affect the URLs for directories and files stored in the server. This name should be the alias name if your server uses an alias. If a colon and port number are appended, that - port will be used in URLs that the server sends to the client. + port will be used in URLs that the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs. --acceptorthreads The number of acceptor threads for the listener socket. The diff --git a/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http.1 b/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http.1 index 6f2494fb083..937ea09b627 100644 --- a/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http.1 +++ b/appserver/web/admin/src/main/manpages/org/glassfish/web/admin/cli/create-http.1 @@ -53,7 +53,7 @@ OPTIONS generates; it doesn't affect the URLs for directories and files stored in the server. This name should be the alias name if your server uses an alias. If a colon and port number are appended, that - port will be used in URLs that the server sends to the client. + port will be used in URLs that the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs. --target Creates the set of HTTP parameters only on the specified target. diff --git a/appserver/web/web-core/src/main/java/org/apache/catalina/Connector.java b/appserver/web/web-core/src/main/java/org/apache/catalina/Connector.java index 55891f5a54e..87d891b3076 100644 --- a/appserver/web/web-core/src/main/java/org/apache/catalina/Connector.java +++ b/appserver/web/web-core/src/main/java/org/apache/catalina/Connector.java @@ -335,4 +335,12 @@ void initialize() * during authentication. */ int getMaxSavePostSize(); + + /** + * Return the proxy scheme for this Connector + * @return The scheme or null if not set + */ + default String getProxyScheme() { + return null; + } } diff --git a/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Connector.java b/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Connector.java index 83dc83e22db..f68fbc63c84 100644 --- a/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Connector.java +++ b/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Connector.java @@ -197,18 +197,24 @@ public class Connector implements org.apache.catalina.Connector, Lifecycle { /** * The server name to which we should pretend requests to this Connector were directed. This is useful when operating - * Tomcat behind a proxy server, so that redirects get constructed accurately. If not specified, the server name + * GlassFish behind a proxy server, so that redirects get constructed accurately. If not specified, the server name * included in the Host header is used. */ private String proxyName; /** * The server port to which we should pretend requests to this Connector were directed. This is useful when operating - * Tomcat behind a proxy server, so that redirects get constructed accurately. If not specified, the port number + * GlassFish behind a proxy server, so that redirects get constructed accurately. If not specified, the port number * specified by the port property is used. */ private int proxyPort = 0; + /** + * The HTTP scheme to which we should pretend requests to this Connector were directed. This is useful when operating + * GlassFish behind a proxy server, so that redirects get constructed accurately. + */ + private String proxyScheme = null; + /** * The redirect port for non-SSL to SSL redirects. */ @@ -824,6 +830,24 @@ public void setProxyPort(int proxyPort) { setProperty("proxyPort", String.valueOf(proxyPort)); } + /** + * Return the proxy scheme for this Connector + * @return The scheme or null if not set + */ + public String getProxyScheme() { + return proxyScheme; + } + + /** + * Set the proxy scheme for this Connector. + * + * @param proxyPort The new proxy scheme or null to unset + */ + public void setProxyScheme(String scheme) { + this.proxyScheme = scheme; + setProperty("proxyScheme", String.valueOf(scheme)); + } + /** * Return the port number to which a request should be redirected if it comes in on a non-SSL port and is subject to a * security constraint with a transport guarantee that requires SSL. diff --git a/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Response.java b/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Response.java index 593221adf78..50ce7134237 100644 --- a/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Response.java +++ b/appserver/web/web-core/src/main/java/org/apache/catalina/connector/Response.java @@ -1539,10 +1539,15 @@ public String run() throws IOException { private String getRedirectScheme() { String scheme = connectorRequest.getScheme(); - if (getConnector() != null && getConnector().getAuthPassthroughEnabled()) { - ProxyHandler proxyHandler = getConnector().getProxyHandler(); - if (proxyHandler != null && proxyHandler.getSSLKeysize(connectorRequest) > 0) { - scheme = "https"; + if (getConnector() != null) { + if (getConnector().getProxyScheme() != null) { + scheme = getConnector().getProxyScheme(); + } + if (getConnector().getAuthPassthroughEnabled()) { + ProxyHandler proxyHandler = getConnector().getProxyHandler(); + if (proxyHandler != null && proxyHandler.getSSLKeysize(connectorRequest) > 0) { + scheme = "https"; + } } } diff --git a/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/connector/coyote/PECoyoteConnector.java b/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/connector/coyote/PECoyoteConnector.java index 6affe3d0fe9..a43acdfaa90 100644 --- a/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/connector/coyote/PECoyoteConnector.java +++ b/appserver/web/web-glue/src/main/java/com/sun/enterprise/web/connector/coyote/PECoyoteConnector.java @@ -879,31 +879,7 @@ public void configure(NetworkListener listener, boolean isSecure, // server-name (may contain scheme and colon-separated port number) String serverName = http.getServerName(); - if (serverName != null && serverName.length() > 0) { - // Ignore scheme, which was required for webcore issued redirects - // in 8.x EE - if (serverName.startsWith("http://")) { - serverName = serverName.substring("http://".length()); - } else if (serverName.startsWith("https://")) { - serverName = serverName.substring("https://".length()); - } - int index = serverName.indexOf(':'); - if (index != -1) { - setProxyName(serverName.substring(0, index).trim()); - String serverPort = serverName.substring(index+1).trim(); - if (serverPort.length() > 0) { - try { - setProxyPort(Integer.parseInt(serverPort)); - } catch (NumberFormatException nfe) { - _logger.log(Level.SEVERE, - LogFacade.INVALID_PROXY_PORT, - new Object[] { serverPort, listener.getName() }); - } - } - } else { - setProxyName(serverName); - } - } + setProxyAttributesFromServerName(serverName, listener); // redirect-port String redirectPort = http.getRedirectPort(); @@ -949,6 +925,41 @@ public void configure(NetworkListener listener, boolean isSecure, configureHttpListenerProperties(listener); } + private void setProxyAttributesFromServerName(String serverName, NetworkListener listener) { + if (serverName != null && serverName.length() > 0) { + // Split serverName into scheme, serverName, and port number + String scheme = null; + if (serverName.startsWith("http://")) { + scheme = "http"; + serverName = serverName.substring("http://".length()); + } else if (serverName.startsWith("https://")) { + scheme = "https"; + serverName = serverName.substring("https://".length()); + } + setProxyScheme(scheme); + int index = serverName.indexOf(':'); + if (index != -1) { + setProxyName(serverName.substring(0, index).trim()); + String serverPort = serverName.substring(index+1).trim(); + setProxyPort(serverPort, listener); + } else { + setProxyName(serverName); + } + } + } + + private void setProxyPort(String serverPort, NetworkListener listener) { + if (serverPort.length() > 0) { + try { + setProxyPort(Integer.parseInt(serverPort)); + } catch (NumberFormatException nfe) { + _logger.log(Level.SEVERE, + LogFacade.INVALID_PROXY_PORT, + new Object[] { serverPort, listener.getName() }); + } + } + } + /* * Configures this connector for modjk. diff --git a/docs/reference-manual/src/main/asciidoc/create-http-listener.adoc b/docs/reference-manual/src/main/asciidoc/create-http-listener.adoc index 9adb8af6236..37bdf7743df 100644 --- a/docs/reference-manual/src/main/asciidoc/create-http-listener.adoc +++ b/docs/reference-manual/src/main/asciidoc/create-http-listener.adoc @@ -79,7 +79,7 @@ asadmin-options:: generates; it doesn't affect the URLs for directories and files stored in the server. This name should be the alias name if your server uses an alias. If a colon and port number are appended, that port will be - used in URLs that the server sends to the client. + used in URLs that the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs. `--acceptorthreads`:: The number of acceptor threads for the listener socket. The recommended value is the number of processors in the machine. The diff --git a/docs/reference-manual/src/main/asciidoc/create-http.adoc b/docs/reference-manual/src/main/asciidoc/create-http.adoc index a7b2e6980d4..66fbb387b24 100644 --- a/docs/reference-manual/src/main/asciidoc/create-http.adoc +++ b/docs/reference-manual/src/main/asciidoc/create-http.adoc @@ -66,7 +66,7 @@ asadmin-options:: generates; it doesn't affect the URLs for directories and files stored in the server. This name should be the alias name if your server uses an alias. If a colon and port number are appended, that port will be - used in URLs that the server sends to the client. + used in URLs that the server sends to the client. If a scheme and :// are prepended, the scheme will be used in the URLs. `--target`:: Creates the set of HTTP parameters only on the specified target. Valid values are as follows: From 2746e91604df4ae1b7532bd3c4f34f02a70b972e Mon Sep 17 00:00:00 2001 From: Ondro Mihalyi Date: Sat, 13 Jul 2024 01:22:35 +0200 Subject: [PATCH 2/2] GH #24918: Tests and copyright headers --- .../help/en/help/ref-httplisteneredit.html | 1 + .../help/en/help/ref-httplistenernew.html | 1 + .../main/help/en/help/ref-protocolnew.html | 1 + .../help/en/help/task-httplisteneredit.html | 1 + .../help/en/help/task-httplistenernew.html | 1 + .../main/help/en/help/task-protocolnew.html | 1 + .../glassfish/web/admingui/Strings.properties | 1 + appserver/tests/admin/tests/pom.xml | 5 ++ .../main/admin/test/ClusterITest.java | 71 ++-------------- .../main/admin/test/ConnectionUtils.java | 70 ++++++++++++++++ .../main/admin/test/TestResources.java | 52 ++++++++++++ .../test/webapp/HttpServerNameITest.java | 84 +++++++++++++++++++ .../java/org/apache/catalina/Connector.java | 1 + .../apache/catalina/connector/Connector.java | 2 +- .../apache/catalina/connector/Response.java | 1 + .../connector/coyote/PECoyoteConnector.java | 1 + 16 files changed, 228 insertions(+), 66 deletions(-) create mode 100644 appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/ConnectionUtils.java create mode 100644 appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/TestResources.java create mode 100644 appserver/tests/admin/tests/src/test/java/org/glassfish/main/admin/test/webapp/HttpServerNameITest.java diff --git a/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html b/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html index 413c3ad1c2e..c60f10823b9 100644 --- a/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html +++ b/appserver/admingui/web/src/main/help/en/help/ref-httplisteneredit.html @@ -1,5 +1,6 @@