Red Hat

Using Server Push with Undertow

The Undertow 1.2 branch supports both HTTP2 and SPDY, both of which support the concept of server push. If you are unfamiliar with server push this basically gives the server the ability to push resources cachable resources to a client before a client has requested them. For example if a client requests a HTML page and the server knows this page contains an image, the server could push the image to the client immediately, rather than waiting for the client to receive the page, parse the HTML and then send the request.

Caveats

Before I go into detail about how to use push, there are some pitfalls that you should be aware of. First you can only push resources that are allowed to be cached (think of pushed responses as pre-loading the browser cache).

You should also be aware that in general there is no reliable way to know if a browser already has a resource cached. If you push a resource that a browser has cached this is not a big deal, as the browser will just reset the stream, however it does waste some bandwidth and other server resources.

It is also important to note that if SPDY is in use then it is not possible to set the request headers that correspond to the pushed response, instead the relevant request headers from the current request are assumed by the browser.

Pushing is only allowed when a request is in progress, as all pushed responses correspond to an existing browser initiated request (you cannot do websocket style full duplex communication, at least not without some kind of long polling style hack).

The API

At the moment the API is experimental and somewhat subject to change. At the moment there is no standard API for server push (this is coming in Servlet 4.0), so it is necessary to use Undertow’s native API.

The Undertow API to support push is provided by the ServerConnection object, which can be obtained by calling HttpServerExchange.getConnection() (if you are using the Servlet API and don’t have access to the exchange you can get it by calling io.undertow.servlet.handlers.ServletRequestContext.requireCurrent().getExchange().

The API is as follows:

    /**
     * Attempts to push a resource if this connection supports server push. Otherwise the request is ignored.
     *
     * Note that push is always done on a best effort basis, even if this method returns true it is possible that
     * the remote endpoint will reset the stream.
     *
     * @param path The path of the resource
     * @param method The request method
     * @param requestHeaders The request headers
     * @return <code>true</code> if the server attempted the push, false otherwise
     */
    public boolean pushResource(final String path, final HttpString method, final HeaderMap requestHeaders);

    /**
     * Attempts to push a resource if this connection supports server push. Otherwise the request is ignored.
     *
     * Note that push is always done on a best effort basis, even if this method returns true it is possible that
     * the remote endpoint will reset the stream.
     *
     * The {@link io.undertow.server.HttpHandler} passed in will be used to generate the pushed response.
     *
     * @param path The path of the resource
     * @param method The request method
     * @param requestHeaders The request headers
     * @return <code>true</code> if the server attempted the push, false otherwise
     */
    public boolean pushResource(final String path, final HttpString method, final HeaderMap requestHeaders, HttpHandler handler);

    /**
     * Returns true if push is supported
     *
     * @return <code>true</code> if push is supported on this connection
     */
    public boolean isPushSupported() {
        return false;
    }

For the most part the API is fairly self explanatory, isPushSupported() can be used to determine if the connection supports push, and pushResource() is used to push the specified resource. Note that as specified above the requestHeaders parameter is ignored if SPDY is in use, it is only applicable for HTTP2.

The Learning Push Handler

Undertow provides a built in handler that provides some out of the box server push functionality. This handler is the io.undertow.server.handlers.LearningPushHandler. If you include this in your handler chain it will attempt to learn which resources browsers request soon after requesting specific pages, and uses this knowledge to initiate a server push.

This works by examining the referrer header on requests, and using a heuristic to decide which resources are a candidate to be pushed.

Undertow 1.1.0.Final has been released

Undertow 1.1.0.Final has been released, and is available from maven central. This release brings a number of improvements including:

  • Websockets 1.1

  • SPDY support

  • Many bug fixes

More details on websocket improvements and SPDY support will be coming in new blog posts in the coming weeks.

Undertow now supports HTTP2 Draft 15

The Undertow 1.2 branch now supports the latest draft of the HTTP2 spec. We have a demo site up and running at https://http2.undertow.io.

So far, we have verified HTTP/2 interop with the following browsers:

  • Chrome Canary (41.0.2217.0)

  • Firefox (Nightly build of 36.01a)

  • Internet Explorer (Windows 10 Preview)

The demo site contains instructions for testing it out, including getting it running locally. The HTTP2 code is still quite new, and will be evolving a lot over the coming months. For now most features of HTTP2 are supported, with the exception of priority, which is ignored.

Undertow 1.0.0.Beta1 Released

Undertow is a high performance non-blocking webserver that is the new web server in Wildfly.

Some notable features include:

  • HTTP upgrade support - This enables management and EJB invocations to be multiplexed over the web port without any performance loss

  • Websocket support, including JSR-356

  • Servlet 3.1 support

  • Ability to mix non-blocking handlers with Servlet

Undertow is completely standalone, starts in milliseconds, and has a fluent builder API for building deployments. This all makes it well suited for use in unit tests. An example of setting up an embedded servlet deployment is shown below:

One of the goals of Undertow was to have great performance and I am happy to say so far we have surpassed our own expectations. Against the competition Undertow comes out ahead on every internal test we have run, and this is also represented in a third party benchmark by Tech Empower (the benchmarks that were posted to the core a while back), where it comes in as the fastest Java web server (and in many of the tests as the fastest server overall).

The results are listed here:

If anyone want to participate the dev list is undertow-dev@lists.jboss.org and we hang out on #undertow on freenode.

Some examples

Embedded Servlet Deployment
DeploymentInfo servletBuilder = deployment()
        .setClassLoader(ServletServer.class.getClassLoader())
        .setContextPath("/myapp")
        .setDeploymentName("test.war")
        .addServlets(
                servlet("MessageServlet", MessageServlet.class)
                        .addInitParam("message", "Hello World")
                        .addMapping("/*"),
                servlet("MyServlet", MessageServlet.class)
                        .addInitParam("message", "MyServlet")
                        .addMapping("/myservlet"));

DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
manager.deploy();

Undertow server = Undertow.builder()
        .addListener(8080, "localhost")
        .setHandler(manager.start())
        .build();
server.start();
Simple non-blocking handler
Undertow server = Undertow.builder()
        .addListener(8080, "localhost")
        .setHandler(new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send("Hello World");
            }
        }).build();
server.start();