Search in sources :

Example 16 with OptionMap

use of org.xnio.OptionMap in project undertow by undertow-io.

the class Http2ClientProvider method connect.

@Override
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    if (ssl == null) {
        listener.failed(UndertowMessages.MESSAGES.sslWasNull());
        return;
    }
    if (bindAddress == null) {
        OptionMap tlsOptions = OptionMap.builder().addAll(options).set(Options.SSL_STARTTLS, true).getMap();
        ssl.openSslConnection(ioThread, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 443 : uri.getPort()), createOpenListener(listener, uri, ssl, bufferPool, tlsOptions), options).addNotifier(createNotifier(listener), null);
    } else {
        ssl.openSslConnection(ioThread, bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 443 : uri.getPort()), createOpenListener(listener, uri, ssl, bufferPool, options), options).addNotifier(createNotifier(listener), null);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) OptionMap(org.xnio.OptionMap)

Example 17 with OptionMap

use of org.xnio.OptionMap 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 18 with OptionMap

use of org.xnio.OptionMap in project undertow by undertow-io.

the class MaxRequestSizeTestCase method testMaxRequestEntitySize.

@Test
public void testMaxRequestEntitySize() throws IOException {
    OptionMap existing = DefaultServer.getUndertowOptions();
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        post.setEntity(new StringEntity(A_MESSAGE));
        post.addHeader(Headers.CONNECTION_STRING, "close");
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        OptionMap maxSize = OptionMap.create(UndertowOptions.MAX_ENTITY_SIZE, (long) A_MESSAGE.length() - 1);
        DefaultServer.setUndertowOptions(maxSize);
        post = new HttpPost(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        post.setEntity(new StringEntity(A_MESSAGE));
        result = client.execute(post);
        Assert.assertEquals(StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        maxSize = OptionMap.create(UndertowOptions.MAX_HEADER_SIZE, 1000);
        DefaultServer.setUndertowOptions(maxSize);
        post = new HttpPost(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        post.setEntity(new StringEntity(A_MESSAGE));
        post.addHeader(Headers.CONNECTION_STRING, "close");
        result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        DefaultServer.setUndertowOptions(existing);
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) OptionMap(org.xnio.OptionMap) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 19 with OptionMap

use of org.xnio.OptionMap in project undertow by undertow-io.

the class MaxRequestSizeTestCase method testMaxRequestHeaderSize.

@Test
public void testMaxRequestHeaderSize() throws IOException {
    OptionMap existing = DefaultServer.getUndertowOptions();
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        post.setEntity(new StringEntity(A_MESSAGE));
        post.addHeader(Headers.CONNECTION_STRING, "close");
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        OptionMap maxSize = OptionMap.create(UndertowOptions.MAX_HEADER_SIZE, 10);
        DefaultServer.setUndertowOptions(maxSize);
        try {
            HttpResponse response = client.execute(post);
            HttpClientUtils.readResponse(response);
            Assert.assertEquals(StatusCodes.BAD_REQUEST, response.getStatusLine().getStatusCode());
        } catch (IOException e) {
        //expected
        }
        maxSize = OptionMap.create(UndertowOptions.MAX_HEADER_SIZE, 1000);
        DefaultServer.setUndertowOptions(maxSize);
        result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        DefaultServer.setUndertowOptions(existing);
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) OptionMap(org.xnio.OptionMap) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 20 with OptionMap

use of org.xnio.OptionMap in project undertow by undertow-io.

the class FixedLengthRequestTestCase method testMaxRequestSizeFixedLengthRequest.

@Test
@Ignore("sometimes the client attempts to re-use the same connection after the failure, but the server has already closed it")
public void testMaxRequestSizeFixedLengthRequest() throws IOException {
    connection = null;
    OptionMap existing = DefaultServer.getUndertowOptions();
    HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
    post.setHeader(HttpHeaders.CONNECTION, "close");
    TestHttpClient client = new TestHttpClient();
    try {
        generateMessage(1);
        post.setEntity(new StringEntity(message));
        DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_ENTITY_SIZE, 3L));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        connection = null;
        DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_ENTITY_SIZE, (long) message.length()));
        result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        DefaultServer.setUndertowOptions(existing);
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) OptionMap(org.xnio.OptionMap) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

OptionMap (org.xnio.OptionMap)34 InetSocketAddress (java.net.InetSocketAddress)10 Test (org.junit.Test)10 TestHttpClient (io.undertow.testutils.TestHttpClient)9 HttpResponse (org.apache.http.HttpResponse)8 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)7 ChannelListener (org.xnio.ChannelListener)7 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)6 IOException (java.io.IOException)6 ModelNode (org.jboss.dmr.ModelNode)5 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)4 HttpHandler (io.undertow.server.HttpHandler)4 HttpGet (org.apache.http.client.methods.HttpGet)4 StreamConnection (org.xnio.StreamConnection)4 XnioSsl (org.xnio.ssl.XnioSsl)4 AjpIgnore (io.undertow.testutils.AjpIgnore)3 HttpPost (org.apache.http.client.methods.HttpPost)3 StringEntity (org.apache.http.entity.StringEntity)3 ServiceName (org.jboss.msc.service.ServiceName)3 XnioWorker (org.xnio.XnioWorker)3