Skip to content

Commit

Permalink
Fix #10306 getServerHost
Browse files Browse the repository at this point in the history
work in progress
  • Loading branch information
gregw committed Aug 15, 2023
1 parent b2f3b68 commit 009e050
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ static String getServerName(Request request)
if (uri.hasAuthority())
return HostPort.normalizeHost(uri.getHost());

HostPort authority = request.getConnectionMetaData().getServerAuthority();
HostPort authority = request.getConnectionMetaData().getHttpConfiguration().getServerAuthority();
if (authority != null)
return HostPort.normalizeHost(authority.getHost());

Expand All @@ -423,10 +423,6 @@ static String getServerName(Request request)
return local == null ? null : local.toString();
}

/**
* @param request
* @return
*/
static int getServerPort(Request request)
{
if (request == null)
Expand All @@ -438,7 +434,7 @@ static int getServerPort(Request request)
return uri.getPort();

// Is there a server authority that forces a port?
HostPort authority = request.getConnectionMetaData().getServerAuthority();
HostPort authority = request.getConnectionMetaData().getHttpConfiguration().getServerAuthority();
if (authority != null && authority.getPort() > 0)
return authority.getPort();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public HostPort getServerAuthority()
{
HostPort authority = ConnectionMetaData.getServerAuthority(getHttpConfiguration(), this);
if (authority == null)
authority = new HostPort(getLocalSocketAddress().toString(), -1);
authority = new HostPort(getLocalSocketAddress().toString(), -1); // TODO WRONG!!!
return authority;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.jetty.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
Expand All @@ -22,6 +24,9 @@

import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.internal.HttpConnection;
import org.eclipse.jetty.util.Callback;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -75,7 +80,25 @@ public void init() throws Exception
server.addConnector(connector);

// Alternate behavior Connector
HttpConnectionFactory httpAlt = new HttpConnectionFactory();
HttpConnectionFactory httpAlt = new HttpConnectionFactory()
{
@Override
public Connection newConnection(Connector connector, EndPoint endPoint)
{
HttpConnection connection = new HttpConnection(getHttpConfiguration(), connector, endPoint, isRecordHttpComplianceViolations())
{
@Override
public SocketAddress getLocalSocketAddress()
{
return InetSocketAddress.createUnresolved("test", 42);
}
};
connection.setUseInputDirectByteBuffers(isUseInputDirectByteBuffers());
connection.setUseOutputDirectByteBuffers(isUseOutputDirectByteBuffers());
return configure(connection, connector, endPoint);
}
};

httpAlt.getHttpConfiguration().setSecurePort(8443);
httpAlt.getHttpConfiguration().setHttpCompliance(mismatchedAuthorityHttpCompliance);
customizerAlt = new ForwardedRequestCustomizer();
Expand Down Expand Up @@ -128,27 +151,6 @@ public void destroy() throws Exception
server.stop();
}

public static Stream<Arguments> cases2()
{
return Stream.of(
Arguments.of(new TestRequest("https initial authority, X-Forwarded-Proto on http, Proxy-Ssl-Id exists (setSslIsSecure==false)")
.configureCustomizer((customizer) -> customizer.setSslIsSecure(false))
.headers(
"GET https://alt.example.net/foo HTTP/1.1",
"Host: alt.example.net",
"X-Forwarded-Proto: http",
"Proxy-Ssl-Id: Wibble"
),
new Expectations()
.scheme("http").serverName("alt.example.net").serverPort(80)
.secure(false)
.requestURL("http://alt.example.net/foo")
.remoteAddr("0.0.0.0").remotePort(0)
.sslSession("Wibble")
)
);
}

public static Stream<Arguments> cases()
{
return Stream.of(
Expand Down Expand Up @@ -1063,6 +1065,29 @@ public void testConfiguredBehavior(TestRequest request, Expectations expectation
public static Stream<Arguments> nonStandardPortCases()
{
return Stream.of(
// HTTP 1.0
Arguments.of(
new TestRequest("HTTP/1.0 - no Host header")
.headers(
"GET /example HTTP/1.0"
),
new Expectations()
.scheme("http").serverName("test").serverPort(42)
.secure(false)
.requestURL("http://test:42/example")
),
Arguments.of(
new TestRequest("HTTP/1.0 - Empty Host header")
.headers(
"GET scheme:///example HTTP/1.0",
"Host:"
),
new Expectations()
.scheme("scheme").serverName(null).serverPort(42)
.secure(false)
.requestURL("scheme:///example")
),

// RFC7239 Tests with https.
Arguments.of(new TestRequest("RFC7239 with https and h2")
.headers(
Expand Down Expand Up @@ -1106,7 +1131,7 @@ public static Stream<Arguments> nonStandardPortCases()

/**
* Tests against a Connector with a HttpConfiguration on non-standard ports.
* HttpConfiguration is set to securePort of 8443
* HttpConfiguration is set to securePort of 8443 and the local port is 42.
*/
@ParameterizedTest(name = "{0}")
@MethodSource("nonStandardPortCases")
Expand Down

0 comments on commit 009e050

Please sign in to comment.