Merge pull request #2902 from eclipse/jetty-9.4.x-issue-2875-websocketclient-redirect
Issues #2875 and #2901 WebSocketClient Redirected Upgrade
This commit is contained in:
commit
b225ebe756
|
@ -108,7 +108,7 @@ public class HttpRequest implements Request
|
||||||
headers.put(userAgentField);
|
headers.put(userAgentField);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected HttpConversation getConversation()
|
public HttpConversation getConversation()
|
||||||
{
|
{
|
||||||
return conversation;
|
return conversation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,16 +110,16 @@ public class HttpChannelOverHTTP extends HttpChannel
|
||||||
|
|
||||||
// Upgrade Response
|
// Upgrade Response
|
||||||
HttpRequest request = exchange.getRequest();
|
HttpRequest request = exchange.getRequest();
|
||||||
if (request instanceof HttpConnectionUpgrader)
|
HttpConnectionUpgrader upgrader = (HttpConnectionUpgrader) request.getConversation().getAttribute(HttpConnectionUpgrader.class.getName());
|
||||||
|
if (upgrader != null)
|
||||||
{
|
{
|
||||||
HttpConnectionUpgrader listener = (HttpConnectionUpgrader)request;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
listener.upgrade(response,getHttpConnection());
|
upgrader.upgrade(response, getHttpConnection());
|
||||||
}
|
}
|
||||||
catch (Throwable x)
|
catch (Throwable x)
|
||||||
{
|
{
|
||||||
return new Result(result,x);
|
return new Result(result, x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -415,6 +415,8 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList
|
||||||
this.localEndpoint = this.wsClient.getEventDriverFactory().wrap(localEndpoint);
|
this.localEndpoint = this.wsClient.getEventDriverFactory().wrap(localEndpoint);
|
||||||
|
|
||||||
this.fut = new CompletableFuture<Session>();
|
this.fut = new CompletableFuture<Session>();
|
||||||
|
|
||||||
|
getConversation().setAttribute(HttpConnectionUpgrader.class.getName(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String genRandomKey()
|
private final String genRandomKey()
|
||||||
|
@ -504,7 +506,7 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList
|
||||||
}
|
}
|
||||||
|
|
||||||
Throwable failure = result.getFailure();
|
Throwable failure = result.getFailure();
|
||||||
if ((failure instanceof java.net.ConnectException) || (failure instanceof UpgradeException))
|
if ((failure instanceof java.io.IOException) || (failure instanceof UpgradeException))
|
||||||
{
|
{
|
||||||
// handle as-is
|
// handle as-is
|
||||||
handleException(failure);
|
handleException(failure);
|
||||||
|
@ -519,7 +521,7 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList
|
||||||
if (responseStatusCode != HttpStatus.SWITCHING_PROTOCOLS_101)
|
if (responseStatusCode != HttpStatus.SWITCHING_PROTOCOLS_101)
|
||||||
{
|
{
|
||||||
// Failed to upgrade (other reason)
|
// Failed to upgrade (other reason)
|
||||||
handleException(new UpgradeException(requestURI,responseStatusCode,responseLine));
|
handleException(new UpgradeException(requestURI,responseStatusCode,"Failed to upgrade to websocket: Unexpected HTTP Response Status Code: " + responseLine));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -620,7 +620,7 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
|
||||||
|
|
||||||
if (LOG.isDebugEnabled())
|
if (LOG.isDebugEnabled())
|
||||||
{
|
{
|
||||||
LOG.debug("open -> {}",dump());
|
LOG.debug("[{}] open -> {}",getPolicy().getBehavior(),dump());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(openFuture != null)
|
if(openFuture != null)
|
||||||
|
|
|
@ -64,6 +64,7 @@ public class BlockheadClientRequest extends HttpRequest implements Response.Comp
|
||||||
super(client, new HttpConversation(), uri);
|
super(client, new HttpConversation(), uri);
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.fut = new CompletableFuture<>();
|
this.fut = new CompletableFuture<>();
|
||||||
|
getConversation().setAttribute(HttpConnectionUpgrader.class.getName(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInitialBytes(ByteBuffer initialBytes)
|
public void setInitialBytes(ByteBuffer initialBytes)
|
||||||
|
|
|
@ -0,0 +1,155 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// All rights reserved. This program and the accompanying materials
|
||||||
|
// are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
// and Apache License v2.0 which accompanies this distribution.
|
||||||
|
//
|
||||||
|
// The Eclipse Public License is available at
|
||||||
|
// http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
//
|
||||||
|
// The Apache License v2.0 is available at
|
||||||
|
// http://www.opensource.org/licenses/apache2.0.php
|
||||||
|
//
|
||||||
|
// You may elect to redistribute this code under either of these licenses.
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
package org.eclipse.jetty.websocket.server;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.server.HttpConfiguration;
|
||||||
|
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.SecureRequestCustomizer;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
|
import org.eclipse.jetty.server.SslConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.handler.DefaultHandler;
|
||||||
|
import org.eclipse.jetty.server.handler.HandlerList;
|
||||||
|
import org.eclipse.jetty.server.handler.SecuredRedirectHandler;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||||
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||||
|
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
|
||||||
|
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||||
|
import org.eclipse.jetty.websocket.server.helper.EchoServlet;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class RedirectWebSocketClientTest
|
||||||
|
{
|
||||||
|
public static Server server;
|
||||||
|
public static URI serverWsUri;
|
||||||
|
public static URI serverWssUri;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void startServer() throws Exception
|
||||||
|
{
|
||||||
|
server = new Server();
|
||||||
|
|
||||||
|
HttpConfiguration http_config = new HttpConfiguration();
|
||||||
|
http_config.setSecureScheme("https");
|
||||||
|
http_config.setSecurePort(0);
|
||||||
|
http_config.setOutputBufferSize(32768);
|
||||||
|
http_config.setRequestHeaderSize(8192);
|
||||||
|
http_config.setResponseHeaderSize(8192);
|
||||||
|
http_config.setSendServerVersion(true);
|
||||||
|
http_config.setSendDateHeader(false);
|
||||||
|
|
||||||
|
SslContextFactory sslContextFactory = newSslContextFactory();
|
||||||
|
|
||||||
|
// SSL HTTP Configuration
|
||||||
|
HttpConfiguration https_config = new HttpConfiguration(http_config);
|
||||||
|
https_config.addCustomizer(new SecureRequestCustomizer());
|
||||||
|
|
||||||
|
// SSL Connector
|
||||||
|
ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
|
||||||
|
sslConnector.setPort(0);
|
||||||
|
server.addConnector(sslConnector);
|
||||||
|
|
||||||
|
// Normal Connector
|
||||||
|
ServerConnector connector = new ServerConnector(server);
|
||||||
|
connector.setPort(0);
|
||||||
|
server.addConnector(connector);
|
||||||
|
|
||||||
|
ServletContextHandler contextHandler = new ServletContextHandler();
|
||||||
|
contextHandler.setContextPath("/");
|
||||||
|
contextHandler.addServlet(EchoServlet.class, "/echo");
|
||||||
|
|
||||||
|
HandlerList handlers = new HandlerList();
|
||||||
|
|
||||||
|
handlers.addHandler(new SecuredRedirectHandler());
|
||||||
|
handlers.addHandler(contextHandler);
|
||||||
|
handlers.addHandler(new DefaultHandler());
|
||||||
|
|
||||||
|
server.setHandler(handlers);
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
|
||||||
|
serverWsUri = URI.create("ws://localhost:" + connector.getLocalPort() + "/");
|
||||||
|
serverWssUri = URI.create("wss://localhost:" + sslConnector.getLocalPort() + "/");
|
||||||
|
|
||||||
|
// adjust HttpConfiguration in connector
|
||||||
|
HttpConnectionFactory connectionFactory = connector.getConnectionFactory(HttpConnectionFactory.class);
|
||||||
|
connectionFactory.getHttpConfiguration().setSecurePort(sslConnector.getLocalPort());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void stopServer() throws Exception
|
||||||
|
{
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SslContextFactory newSslContextFactory()
|
||||||
|
{
|
||||||
|
SslContextFactory ssl = new SslContextFactory();
|
||||||
|
ssl.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
|
||||||
|
ssl.setKeyStorePassword("storepwd");
|
||||||
|
ssl.setKeyManagerPassword("keypwd");
|
||||||
|
return ssl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedirect() throws Exception
|
||||||
|
{
|
||||||
|
SslContextFactory ssl = newSslContextFactory();
|
||||||
|
ssl.setTrustAll(false);
|
||||||
|
ssl.setEndpointIdentificationAlgorithm(null);
|
||||||
|
HttpClient httpClient = new HttpClient(ssl);
|
||||||
|
|
||||||
|
WebSocketClient client = new WebSocketClient(httpClient);
|
||||||
|
client.addBean(httpClient, true);
|
||||||
|
client.start();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
URI wsUri = serverWsUri.resolve("/echo");
|
||||||
|
|
||||||
|
ClientUpgradeRequest request = new ClientUpgradeRequest();
|
||||||
|
Future<Session> sessionFuture = client.connect(new EmptyWebSocket(), wsUri, request);
|
||||||
|
Session session = sessionFuture.get();
|
||||||
|
assertThat(session, is(notNullValue()));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
client.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@WebSocket
|
||||||
|
public static class EmptyWebSocket {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue