Search in sources :

Example 66 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class Http2UpgradeHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final String upgrade = exchange.getRequestHeaders().getFirst(Headers.UPGRADE);
    if (upgrade != null && upgradeStrings.contains(upgrade)) {
        String settings = exchange.getRequestHeaders().getFirst("HTTP2-Settings");
        if (settings != null) {
            //required by spec
            final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings);
            exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade);
            exchange.upgradeChannel(new HttpUpgradeListener() {

                @Override
                public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                    OptionMap undertowOptions = exchange.getConnection().getUndertowOptions();
                    Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions);
                    Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() {

                        @Override
                        public void handleRequest(HttpServerExchange exchange) throws Exception {
                            //as the request was only to create the initial request
                            if (exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) {
                                exchange.endExchange();
                                return;
                            }
                            exchange.setProtocol(Protocols.HTTP_2_0);
                            next.handleRequest(exchange);
                        }
                    }, undertowOptions, exchange.getConnection().getBufferSize(), null);
                    channel.getReceiveSetter().set(receiveListener);
                    receiveListener.handleInitialRequest(exchange, channel);
                    channel.resumeReceives();
                }
            });
            return;
        }
    }
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) OptionMap(org.xnio.OptionMap) Http2Channel(io.undertow.protocols.http2.Http2Channel) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) ByteBuffer(java.nio.ByteBuffer)

Example 67 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class HttpContinue method internalSendContinueResponse.

private static void internalSendContinueResponse(final HttpServerExchange exchange, final IoCallback callback) {
    if (exchange.getAttachment(ALREADY_SENT) != null) {
        callback.onComplete(exchange, null);
        return;
    }
    HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
    exchange.putAttachment(ALREADY_SENT, true);
    newExchange.setStatusCode(StatusCodes.CONTINUE);
    newExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, 0);
    final StreamSinkChannel responseChannel = newExchange.getResponseChannel();
    try {
        responseChannel.shutdownWrites();
        if (!responseChannel.flush()) {
            responseChannel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {

                @Override
                public void handleEvent(StreamSinkChannel channel) {
                    channel.suspendWrites();
                    exchange.dispatch(new HttpHandler() {

                        @Override
                        public void handleRequest(HttpServerExchange exchange) throws Exception {
                            callback.onComplete(exchange, null);
                        }
                    });
                }
            }, new ChannelExceptionHandler<Channel>() {

                @Override
                public void handleException(Channel channel, final IOException e) {
                    exchange.dispatch(new HttpHandler() {

                        @Override
                        public void handleRequest(HttpServerExchange exchange) throws Exception {
                            callback.onException(exchange, null, e);
                        }
                    });
                }
            }));
            responseChannel.resumeWrites();
            exchange.dispatch();
        } else {
            callback.onComplete(exchange, null);
        }
    } catch (IOException e) {
        callback.onException(exchange, null, e);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) ChannelListener(org.xnio.ChannelListener) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) ChannelExceptionHandler(org.xnio.ChannelExceptionHandler) IOException(java.io.IOException) IOException(java.io.IOException)

Example 68 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ChunkedRequestTrailersTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    existing = DefaultServer.getUndertowOptions();
    DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.ALWAYS_SET_DATE, false));
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = (HttpServerConnection) exchange.getConnection();
                } else if (!DefaultServer.isProxy() && connection != exchange.getConnection()) {
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                final OutputStream outputStream = exchange.getOutputStream();
                final InputStream inputStream = exchange.getInputStream();
                String m = HttpClientUtils.readResponse(inputStream);
                Assert.assertEquals("abcdefghi", m);
                HeaderMap headers = exchange.getAttachment(HttpAttachments.REQUEST_TRAILERS);
                for (HeaderValues header : headers) {
                    for (String val : header) {
                        outputStream.write(header.getHeaderName().toString().getBytes());
                        outputStream.write(": ".getBytes());
                        outputStream.write(val.getBytes());
                        outputStream.write("\r\n".getBytes());
                    }
                }
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpServerConnection(io.undertow.server.protocol.http.HttpServerConnection) HeaderMap(io.undertow.util.HeaderMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HeaderValues(io.undertow.util.HeaderValues) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 69 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ChunkedRequestTransferCodingTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = exchange.getConnection();
                } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                final OutputStream outputStream = exchange.getOutputStream();
                final InputStream inputStream = exchange.getInputStream();
                String m = HttpClientUtils.readResponse(inputStream);
                Assert.assertEquals(message.length(), m.length());
                Assert.assertEquals(message, m);
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 70 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class ChunkedResponseTrailersTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = exchange.getConnection();
                } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                HeaderMap trailers = new HeaderMap();
                exchange.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers);
                trailers.put(HttpString.tryFromString("foo"), "fooVal");
                trailers.put(HttpString.tryFromString("bar"), "barVal");
                new StringWriteChannelListener(message).setup(exchange.getResponseChannel());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HeaderMap(io.undertow.util.HeaderMap) OutputStream(java.io.OutputStream) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7