Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Jetty/Feature/SPDY"
m |
|||
Line 2: | Line 2: | ||
| introduction = | | introduction = | ||
− | + | Jetty supports both a client and a server implementation for the [http://www.chromium.org/spdy SPDY] protocol, beginning with versions 7.6.2 and 8.1.2. To provide the best support possible for SPDY, the Jetty project also provides an [[Jetty/Feature/NPN|implementation for NPN]]. Both the SPDY and the NPN implementation require OpenJDK 1.7 or greater. | |
− | + | A server deployed over TLS normally advertises the SPDY protocol, via the Next Protocol Negotiation TLS Extension (NPN). | |
− | + | ||
− | + | ||
− | + | ||
| body = | | body = | ||
− | ===SPDY Modules=== | + | ===Introducing SPDY Modules=== |
− | Jetty's SPDY implementation consists of | + | Jetty's SPDY implementation consists of four modules: |
− | * | + | * <code>spdy-core</code> – contains the SPDY API and a partial implementation. This module is independent of Jetty (the servlet container), and you can reuse it with other Java SPDY implementations. One of the goals of this module is to standardize the SPDY Java API. |
− | * | + | * <code>spdy-jetty</code> module – binds the <code>spdy-core</code> module to Jetty's NIO framework to provide asynchronous socket I/O. This module uses Jetty internals, but Java SPDY client applications can also use it to communicate with a SPDY server. |
− | * | + | * <code>spdy-jetty-http</code> module – provides a server-side layering of HTTP over SPDY. This module allows any SPDY compliant browser, such as Chromium/Chrome, to talk SPDY to a Jetty server that deploys a standard web application made of servlets, filters and JSPs. This module performs the conversion SPDY to HTTP and vice versa so that for the web application it is as if a normal HTTP request has arrived, and a normal HTTP response is returned. |
− | * | + | * <code>spdy-jetty-http-webapp</code> module – provides a demo application for the a <code>spdy-jetty-http</code> module. |
===Transparently Enabling HTTP over SPDY in Jetty=== | ===Transparently Enabling HTTP over SPDY in Jetty=== | ||
− | The <code>spdy-jetty-http</code> module provides an out-of-the-box [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/spdy/http/HTTPSPDYServerConnector.html server connector] that performs the SPDY to HTTP conversion and | + | The <code>spdy-jetty-http</code> module provides an out-of-the-box [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/spdy/http/HTTPSPDYServerConnector.html server connector] that performs the SPDY to HTTP conversion and vice versa (HTTP over SPDY). |
− | + | You can use this connector instead of Jetty's <code>SslSelectChannelConnector</code> (that only speaks HTTP), and it falls back to HTTPS if SPDY is not negotiated. | |
− | + | An example <code>jetty-spdy.xml</code> file that you can use instead of <code>jetty-ssl.xml</code> follows: | |
<source lang="xml"> | <source lang="xml"> | ||
Line 53: | Line 50: | ||
</source> | </source> | ||
− | This is sufficient to enable your Jetty server to speak SPDY to browsers that | + | This is sufficient to enable your Jetty server to speak SPDY to browsers that support it. |
− | ===SPDY Client Applications=== | + | ===Using SPDY Client Applications=== |
− | The <code>spdy-jetty</code> module provides a SPDY client (that speaks pure SPDY, and not HTTP over SPDY), which can | + | The <code>spdy-jetty</code> module provides a SPDY client (that speaks pure SPDY, and not HTTP over SPDY), which you can use in this way: |
<source lang="java"> | <source lang="java"> | ||
Line 75: | Line 72: | ||
</source> | </source> | ||
− | To listen to SPDY frames | + | To listen to SPDY frames the server sends, you need to pass a <code>StreamFrameListener</code> upon stream creation: |
<source lang="java"> | <source lang="java"> | ||
Line 100: | Line 97: | ||
</source> | </source> | ||
− | + | Become familiar with the [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/spdy/api/package-summary.html SPDY API] to understand how to use the API. | |
− | ===SPDY Server Applications=== | + | ===Using SPDY Server Applications=== |
− | The <code>spdy-jetty</code> module provides a server connector that speaks pure SPDY, and that can | + | The <code>spdy-jetty</code> module provides a server connector that speaks pure SPDY, and that you can use in conjunction with the SPDY client (see above). |
− | + | You can start the <code>SPDYServerConnector</code> in this way: | |
<source lang="java"> | <source lang="java"> | ||
Line 143: | Line 140: | ||
The <code>ServerSessionFrameListener</code> can inspect the incoming <code>SynInfo</code> and provide different execution paths depending on headers and/or data content. | The <code>ServerSessionFrameListener</code> can inspect the incoming <code>SynInfo</code> and provide different execution paths depending on headers and/or data content. | ||
− | + | Become familiar with the [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/spdy/api/package-summary.html SPDY API] to understand how to use the API. | |
}} | }} |
Revision as of 16:17, 12 March 2012
Contents
Introduction
Jetty supports both a client and a server implementation for the SPDY protocol, beginning with versions 7.6.2 and 8.1.2. To provide the best support possible for SPDY, the Jetty project also provides an implementation for NPN. Both the SPDY and the NPN implementation require OpenJDK 1.7 or greater.
A server deployed over TLS normally advertises the SPDY protocol, via the Next Protocol Negotiation TLS Extension (NPN).
Feature
Introducing SPDY Modules
Jetty's SPDY implementation consists of four modules:
-
spdy-core
– contains the SPDY API and a partial implementation. This module is independent of Jetty (the servlet container), and you can reuse it with other Java SPDY implementations. One of the goals of this module is to standardize the SPDY Java API. -
spdy-jetty
module – binds thespdy-core
module to Jetty's NIO framework to provide asynchronous socket I/O. This module uses Jetty internals, but Java SPDY client applications can also use it to communicate with a SPDY server. -
spdy-jetty-http
module – provides a server-side layering of HTTP over SPDY. This module allows any SPDY compliant browser, such as Chromium/Chrome, to talk SPDY to a Jetty server that deploys a standard web application made of servlets, filters and JSPs. This module performs the conversion SPDY to HTTP and vice versa so that for the web application it is as if a normal HTTP request has arrived, and a normal HTTP response is returned. -
spdy-jetty-http-webapp
module – provides a demo application for the aspdy-jetty-http
module.
Transparently Enabling HTTP over SPDY in Jetty
The spdy-jetty-http
module provides an out-of-the-box server connector that performs the SPDY to HTTP conversion and vice versa (HTTP over SPDY).
You can use this connector instead of Jetty's SslSelectChannelConnector
(that only speaks HTTP), and it falls back to HTTPS if SPDY is not negotiated.
An example jetty-spdy.xml
file that you can use instead of jetty-ssl.xml
follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <New id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory"> <Set name="keyStorePath">your_keystore.jks</Set> <Set name="keyStorePassword">storepwd</Set> <Set name="protocol">TLSv1</Set> </New> <Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.spdy.http.HTTPSPDYServerConnector"> <Arg> <Ref id="sslContextFactory" /> </Arg> <Set name="Port">8443</Set> </New> </Arg> </Call> </Configure>
This is sufficient to enable your Jetty server to speak SPDY to browsers that support it.
Using SPDY Client Applications
The spdy-jetty
module provides a SPDY client (that speaks pure SPDY, and not HTTP over SPDY), which you can use in this way:
// Start a SPDYClient factory shared among all SPDYClient instances SPDYClient.Factory clientFactory = new SPDYClient.Factory(); clientFactory.start(); // Create one SPDYClient instance SPDYClient client = clientFactory.newSPDYClient(SPDY.V2); // Obtain a Session instance to send data to the server that listens on port 8181 Session session = client.connect(new InetSocketAddress("localhost", 8181), null).get(5, TimeUnit.SECONDS); // Sends SYN_STREAM and DATA to the server Stream stream = session.syn(new SynInfo(false), null).get(5, TimeUnit.SECONDS); stream.data(new StringDataInfo("Hello, World", true));
To listen to SPDY frames the server sends, you need to pass a StreamFrameListener
upon stream creation:
StreamFrameListener streamListener = new StreamFrameListener.Adapter() { public void onReply(Stream stream, ReplyInfo replyInfo) { // Reply received from server, send DATA to the server stream.data(new StringDataInfo("Hello, World", true)); } public void onData(Stream stream, DataInfo dataInfo) { // Data received from server String content = dataInfo.asString("UTF-8", true); System.err.printf("SPDY content: %s%n", content); } }; // Sends SYN_STREAM to the server, adding headers Headers headers = new Headers(); headers.put("url", "/echo"); Stream stream = session.syn(new SynInfo(headers, false), null).get(5, TimeUnit.SECONDS);
Become familiar with the SPDY API to understand how to use the API.
Using SPDY Server Applications
The spdy-jetty
module provides a server connector that speaks pure SPDY, and that you can use in conjunction with the SPDY client (see above).
You can start the SPDYServerConnector
in this way:
// The application code that handles incoming SYN_STREAMS ServerSessionFrameListener application = new ServerSessionFrameListener.Adapter() { public StreamFrameListener onSyn(Stream stream, SynInfo synInfo) { // Reply upon receiving a SYN_STREAM stream.reply(new ReplyInfo(false)); // Inspect the headers Headers headers = synInfo.getHeaders(); if ("/echo".equals(headers.get("url").value())) { return new StreamFrameListener.Adapter() { public void onData(Stream stream, DataInfo dataInfo) { // Upon receiving hello data from the client, echo it back String clientData = dataInfo.asString("UTF-8", true); stream.data(new StringDataInfo("Echo for " + clientData, true)); } }; } return null; } }; // Wire up and start the connector org.eclipse.jetty.server.Server server = new Server(); server.addConnector(new SPDYServerConnector(application)); server.start();
The ServerSessionFrameListener
can inspect the incoming SynInfo
and provide different execution paths depending on headers and/or data content.
Become familiar with the SPDY API to understand how to use the API.