Introduced default AsyncConnectionFactory in SPDYServerConnector, to fallback

to a default protocol in case of A) non-SSL connection, and B) no NPN.
This commit is contained in:
Simone Bordet 2012-02-24 14:17:47 +01:00
parent 94d5bccbd3
commit 2640e80101
5 changed files with 56 additions and 20 deletions

View File

@ -12,6 +12,19 @@
<artifactId>spdy-jetty-http</artifactId>
<name>SPDY :: Jetty HTTP Layer</name>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Xbootclasspath/p:${settings.localRepository}/org/eclipse/jetty/npn-boot/${npn.version}/npn-boot-${npn.version}.jar
</argLine>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>

View File

@ -16,15 +16,34 @@
package org.eclipse.jetty.spdy.http;
import org.eclipse.jetty.spdy.AsyncConnectionFactory;
import org.eclipse.jetty.spdy.SPDYServerConnector;
import org.eclipse.jetty.spdy.api.SPDY;
import org.eclipse.jetty.util.ssl.SslContextFactory;
public class HTTPSPDYServerConnector extends SPDYServerConnector
{
private final AsyncConnectionFactory defaultConnectionFactory;
public HTTPSPDYServerConnector()
{
super(null);
this(null);
}
public HTTPSPDYServerConnector(SslContextFactory sslContextFactory)
{
super(null, sslContextFactory);
// Override the "spdy/2" protocol by handling HTTP over SPDY
putAsyncConnectionFactory("spdy/2", new ServerHTTPSPDYAsyncConnectionFactory(SPDY.V2, this));
setDefaultProtocol("http/1.1");
// Add the "http/1.1" protocol for browsers that do not support NPN
putAsyncConnectionFactory("http/1.1", new ServerHTTPAsyncConnectionFactory(this));
// Override the default connection factory for non-SSL connections
defaultConnectionFactory = new ServerHTTPAsyncConnectionFactory(this);
}
@Override
protected AsyncConnectionFactory getDefaultAsyncConnectionFactory()
{
return defaultConnectionFactory;
}
}

View File

@ -27,8 +27,8 @@ import javax.net.ssl.SSLSocket;
import org.eclipse.jetty.npn.NextProtoNego;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.spdy.AsyncConnectionFactory;
import org.eclipse.jetty.spdy.SPDYServerConnector;
import org.eclipse.jetty.spdy.api.server.ServerSessionFrameListener;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.Assert;
import org.junit.Rule;
@ -54,12 +54,13 @@ public class ProtocolNegotiationTest
protected Server server;
protected SPDYServerConnector connector;
protected InetSocketAddress startServer(ServerSessionFrameListener listener) throws Exception
protected InetSocketAddress startServer(SPDYServerConnector connector) throws Exception
{
server = new Server();
SslContextFactory sslContextFactory = newSslContextFactory();
connector = new SPDYServerConnector(listener, sslContextFactory);
if (connector == null)
connector = new SPDYServerConnector(null, newSslContextFactory());
connector.setPort(0);
this.connector = connector;
server.addConnector(connector);
server.start();
return new InetSocketAddress("localhost", connector.getLocalPort());
@ -197,9 +198,15 @@ public class ProtocolNegotiationTest
@Test
public void testServerAdvertisingSPDYAndHTTPSpeaksDefaultProtocolWhenNPNMissing() throws Exception
{
InetSocketAddress address = startServer(null);
InetSocketAddress address = startServer(new SPDYServerConnector(null, newSslContextFactory())
{
@Override
protected AsyncConnectionFactory getDefaultAsyncConnectionFactory()
{
return new ServerHTTPAsyncConnectionFactory(connector);
}
});
connector.putAsyncConnectionFactory("http/1.1", new ServerHTTPAsyncConnectionFactory(connector));
connector.setDefaultProtocol("http/1.1");
SslContextFactory sslContextFactory = newSslContextFactory();
sslContextFactory.start();

View File

@ -16,7 +16,6 @@
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<argLine>
-Xbootclasspath/p:${settings.localRepository}/org/eclipse/jetty/npn-boot/${npn.version}/npn-boot-${npn.version}.jar

View File

@ -38,7 +38,7 @@ public class SPDYServerConnector extends SelectChannelConnector
// Order is important on server side, so we use a LinkedHashMap
private final Map<String, AsyncConnectionFactory> factories = new LinkedHashMap<>();
private final SslContextFactory sslContextFactory;
private volatile String defaultProtocol = "spdy/2";
private final AsyncConnectionFactory defaultConnectionFactory;
public SPDYServerConnector(ServerSessionFrameListener listener)
{
@ -50,7 +50,8 @@ public class SPDYServerConnector extends SelectChannelConnector
this.sslContextFactory = sslContextFactory;
if (sslContextFactory != null)
addBean(sslContextFactory);
putAsyncConnectionFactory("spdy/2", new ServerSPDYAsyncConnectionFactory(SPDY.V2, listener));
defaultConnectionFactory = new ServerSPDYAsyncConnectionFactory(SPDY.V2, listener);
putAsyncConnectionFactory("spdy/2", defaultConnectionFactory);
}
public AsyncConnectionFactory getAsyncConnectionFactory(String protocol)
@ -93,14 +94,9 @@ public class SPDYServerConnector extends SelectChannelConnector
}
}
public String getDefaultProtocol()
protected AsyncConnectionFactory getDefaultAsyncConnectionFactory()
{
return defaultProtocol;
}
public void setDefaultProtocol(String defaultProtocol)
{
this.defaultProtocol = defaultProtocol;
return defaultConnectionFactory;
}
@Override
@ -118,7 +114,9 @@ public class SPDYServerConnector extends SelectChannelConnector
@Override
public void unsupported()
{
protocolSelected(getDefaultProtocol());
AsyncConnectionFactory connectionFactory = getDefaultAsyncConnectionFactory();
AsyncConnection connection = connectionFactory.newAsyncConnection(channel, sslEndPoint, null);
sslEndPoint.setConnection(connection);
}
@Override
@ -145,7 +143,7 @@ public class SPDYServerConnector extends SelectChannelConnector
}
else
{
AsyncConnectionFactory connectionFactory = getAsyncConnectionFactory("spdy/2");
AsyncConnectionFactory connectionFactory = getDefaultAsyncConnectionFactory();
AsyncConnection connection = connectionFactory.newAsyncConnection(channel, endPoint, null);
endPoint.setConnection(connection);
return connection;