Search in sources :

Example 16 with StreamSinkChannel

use of org.xnio.channels.StreamSinkChannel 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 17 with StreamSinkChannel

use of org.xnio.channels.StreamSinkChannel in project undertow by undertow-io.

the class HttpReadListener method sendBadRequestAndClose.

private void sendBadRequestAndClose(final StreamConnection connection, final Exception exception) {
    UndertowLogger.REQUEST_IO_LOGGER.failedToParseRequest(exception);
    connection.getSourceChannel().suspendReads();
    new StringWriteChannelListener(BAD_REQUEST) {

        @Override
        protected void writeDone(final StreamSinkChannel c) {
            super.writeDone(c);
            c.suspendWrites();
            IoUtils.safeClose(connection);
        }

        @Override
        protected void handleError(StreamSinkChannel channel, IOException e) {
            IoUtils.safeClose(connection);
        }
    }.setup(connection.getSinkChannel());
}
Also used : StreamSinkChannel(org.xnio.channels.StreamSinkChannel) ConduitStreamSinkChannel(org.xnio.conduits.ConduitStreamSinkChannel) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException)

Example 18 with StreamSinkChannel

use of org.xnio.channels.StreamSinkChannel in project undertow by undertow-io.

the class ReadTimeoutTestCase method testReadTimeout.

@Test
public void testReadTimeout() throws IOException, InterruptedException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final StreamSinkChannel response = exchange.getResponseChannel();
            final StreamSourceChannel request = exchange.getRequestChannel();
            try {
                request.setOption(Options.READ_TIMEOUT, 100);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            request.getReadSetter().set(ChannelListeners.drainListener(Long.MAX_VALUE, new ChannelListener<Channel>() {

                @Override
                public void handleEvent(final Channel channel) {
                    new StringWriteChannelListener("COMPLETED") {

                        @Override
                        protected void writeDone(final StreamSinkChannel channel) {
                            exchange.endExchange();
                        }
                    }.setup(response);
                }
            }, new ChannelExceptionHandler<StreamSourceChannel>() {

                @Override
                public void handleException(final StreamSourceChannel channel, final IOException e) {
                    exchange.endExchange();
                    exception = e;
                    errorLatch.countDown();
                }
            }));
            request.wakeupReads();
        }
    });
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL());
        post.setEntity(new AbstractHttpEntity() {

            @Override
            public InputStream getContent() throws IOException, IllegalStateException {
                return null;
            }

            @Override
            public void writeTo(final OutputStream outstream) throws IOException {
                for (int i = 0; i < 5; ++i) {
                    outstream.write('*');
                    outstream.flush();
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            @Override
            public boolean isStreaming() {
                return true;
            }

            @Override
            public boolean isRepeatable() {
                return false;
            }

            @Override
            public long getContentLength() {
                return 5;
            }
        });
        post.addHeader(Headers.CONNECTION_STRING, "close");
        try {
            client.execute(post);
        } catch (IOException e) {
        }
        if (errorLatch.await(5, TimeUnit.SECONDS)) {
            Assert.assertEquals(ReadTimeoutException.class, exception.getClass());
        } else {
            Assert.fail("Read did not time out");
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) StreamSourceChannel(org.xnio.channels.StreamSourceChannel) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) OutputStream(java.io.OutputStream) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IOException(java.io.IOException) IOException(java.io.IOException) ReadTimeoutException(org.xnio.channels.ReadTimeoutException) TestHttpClient(io.undertow.testutils.TestHttpClient) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) Test(org.junit.Test)

Example 19 with StreamSinkChannel

use of org.xnio.channels.StreamSinkChannel in project undertow by undertow-io.

the class WriteTimeoutTestCase method testWriteTimeout.

@Test
public void testWriteTimeout() throws IOException, InterruptedException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final StreamSinkChannel response = exchange.getResponseChannel();
            try {
                response.setOption(Options.WRITE_TIMEOUT, 10);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //1mb
            final int capacity = 1 * 1024 * 1024;
            final ByteBuffer originalBuffer = ByteBuffer.allocateDirect(capacity);
            for (int i = 0; i < capacity; ++i) {
                originalBuffer.put((byte) '*');
            }
            originalBuffer.flip();
            response.getWriteSetter().set(new ChannelListener<Channel>() {

                private ByteBuffer buffer = originalBuffer.duplicate();

                int count = 0;

                @Override
                public void handleEvent(final Channel channel) {
                    do {
                        try {
                            int res = response.write(buffer);
                            if (res == 0) {
                                return;
                            }
                        } catch (IOException e) {
                            exception = e;
                            errorLatch.countDown();
                        }
                        if (!buffer.hasRemaining()) {
                            count++;
                            buffer = originalBuffer.duplicate();
                        }
                    } while (count < 1000);
                    exchange.endExchange();
                }
            });
            response.wakeupWrites();
        }
    });
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
        try {
            HttpResponse result = client.execute(get);
            InputStream content = result.getEntity().getContent();
            byte[] buffer = new byte[512];
            int r = 0;
            while ((r = content.read(buffer)) > 0) {
                Thread.sleep(200);
                if (exception != null) {
                    Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
                    return;
                }
            }
            Assert.fail("Write did not time out");
        } catch (IOException e) {
            if (errorLatch.await(5, TimeUnit.SECONDS)) {
                Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
            } else {
                Assert.fail("Write did not time out");
            }
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) InputStream(java.io.InputStream) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Channel(java.nio.channels.Channel) HttpGet(org.apache.http.client.methods.HttpGet) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) WriteTimeoutException(org.xnio.channels.WriteTimeoutException) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) WriteTimeoutException(org.xnio.channels.WriteTimeoutException) Test(org.junit.Test)

Aggregations

StreamSinkChannel (org.xnio.channels.StreamSinkChannel)19 IOException (java.io.IOException)14 ChannelListener (org.xnio.ChannelListener)5 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)4 HttpServerExchange (io.undertow.server.HttpServerExchange)4 ByteBuffer (java.nio.ByteBuffer)4 ChannelExceptionHandler (org.xnio.ChannelExceptionHandler)4 Channel (java.nio.channels.Channel)3 ByteBufferPool (io.undertow.connector.ByteBufferPool)2 TestHttpClient (io.undertow.testutils.TestHttpClient)2 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)2 ClientCallback (io.undertow.client.ClientCallback)1 ClientExchange (io.undertow.client.ClientExchange)1 ClientResponse (io.undertow.client.ClientResponse)1 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)1