Search in sources :

Example 1 with StreamSourceChannel

use of org.xnio.channels.StreamSourceChannel in project camel by apache.

the class DefaultUndertowHttpBindingTest method readEntireDelayedPayload.

@Test(timeout = 1000)
public void readEntireDelayedPayload() throws Exception {
    String[] delayedPayloads = new String[] { "", "chunk" };
    StreamSourceChannel source = source(delayedPayloads);
    DefaultUndertowHttpBinding binding = new DefaultUndertowHttpBinding();
    String result = new String(binding.readFromChannel(source));
    checkResult(result, delayedPayloads);
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) EmptyStreamSourceChannel(org.xnio.channels.EmptyStreamSourceChannel) Test(org.junit.Test)

Example 2 with StreamSourceChannel

use of org.xnio.channels.StreamSourceChannel in project camel by apache.

the class DefaultUndertowHttpBindingTest method readEntireMultiDelayedPayload.

@Test(timeout = 1000)
public void readEntireMultiDelayedPayload() throws Exception {
    String[] delayedPayloads = new String[] { "", "first ", "second" };
    StreamSourceChannel source = source(delayedPayloads);
    DefaultUndertowHttpBinding binding = new DefaultUndertowHttpBinding();
    String result = new String(binding.readFromChannel(source));
    checkResult(result, delayedPayloads);
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) EmptyStreamSourceChannel(org.xnio.channels.EmptyStreamSourceChannel) Test(org.junit.Test)

Example 3 with StreamSourceChannel

use of org.xnio.channels.StreamSourceChannel in project camel by apache.

the class DefaultUndertowHttpBindingTest method readEntireMultiDelayedWithPausePayload.

@Test(timeout = 1000)
public void readEntireMultiDelayedWithPausePayload() throws Exception {
    String[] delayedPayloads = new String[] { "", "first ", "", "second" };
    StreamSourceChannel source = source(delayedPayloads);
    DefaultUndertowHttpBinding binding = new DefaultUndertowHttpBinding();
    String result = new String(binding.readFromChannel(source));
    checkResult(result, delayedPayloads);
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) EmptyStreamSourceChannel(org.xnio.channels.EmptyStreamSourceChannel) Test(org.junit.Test)

Example 4 with StreamSourceChannel

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

the class AsyncWebSocketHttpServerExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    final PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
    final ByteBuffer buffer = pooled.getBuffer();
    final StreamSourceChannel channel = exchange.getRequestChannel();
    int res;
    for (; ; ) {
        try {
            res = channel.read(buffer);
            if (res == -1) {
                return new FinishedIoFuture<>(data.toByteArray());
            } else if (res == 0) {
                //callback
                final FutureResult<byte[]> future = new FutureResult<>();
                channel.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {

                    @Override
                    public void handleEvent(final StreamSourceChannel channel) {
                        int res;
                        try {
                            res = channel.read(buffer);
                            if (res == -1) {
                                future.setResult(data.toByteArray());
                                channel.suspendReads();
                                return;
                            } else if (res == 0) {
                                return;
                            } else {
                                buffer.flip();
                                while (buffer.hasRemaining()) {
                                    data.write(buffer.get());
                                }
                                buffer.clear();
                            }
                        } catch (IOException e) {
                            future.setException(e);
                        }
                    }
                });
                channel.resumeReads();
                return future.getIoFuture();
            } else {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    data.write(buffer.get());
                }
                buffer.clear();
            }
        } catch (IOException e) {
            final FutureResult<byte[]> future = new FutureResult<>();
            future.setException(e);
            return future.getIoFuture();
        }
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) FinishedIoFuture(org.xnio.FinishedIoFuture) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 5 with StreamSourceChannel

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

the class ConnectionSSLSessionInfo method renegotiateBufferRequest.

public void renegotiateBufferRequest(HttpServerExchange exchange, SslClientAuthMode newAuthMode) throws IOException {
    int maxSize = exchange.getConnection().getUndertowOptions().get(UndertowOptions.MAX_BUFFERED_REQUEST_SIZE, 16384);
    if (maxSize <= 0) {
        throw new SSLPeerUnverifiedException("");
    }
    //first we need to read the request
    boolean requestResetRequired = false;
    StreamSourceChannel requestChannel = Connectors.getExistingRequestChannel(exchange);
    if (requestChannel == null) {
        requestChannel = exchange.getRequestChannel();
        requestResetRequired = true;
    }
    PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
    //if the pooled buffer should be freed
    boolean free = true;
    int usedBuffers = 0;
    PooledByteBuffer[] poolArray = null;
    final int bufferSize = pooled.getBuffer().remaining();
    int allowedBuffers = ((maxSize + bufferSize - 1) / bufferSize);
    poolArray = new PooledByteBuffer[allowedBuffers];
    poolArray[usedBuffers++] = pooled;
    try {
        int res;
        do {
            final ByteBuffer buf = pooled.getBuffer();
            res = Channels.readBlocking(requestChannel, buf);
            if (!buf.hasRemaining()) {
                if (usedBuffers == allowedBuffers) {
                    throw new SSLPeerUnverifiedException("");
                } else {
                    buf.flip();
                    pooled = exchange.getConnection().getByteBufferPool().allocate();
                    poolArray[usedBuffers++] = pooled;
                }
            }
        } while (res != -1);
        free = false;
        pooled.getBuffer().flip();
        Connectors.ungetRequestBytes(exchange, poolArray);
        renegotiateNoRequest(exchange, newAuthMode);
    } finally {
        if (free) {
            for (PooledByteBuffer buf : poolArray) {
                if (buf != null) {
                    buf.close();
                }
            }
        }
        if (requestResetRequired) {
            exchange.requestChannel = null;
        }
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Aggregations

StreamSourceChannel (org.xnio.channels.StreamSourceChannel)13 IOException (java.io.IOException)9 ByteBuffer (java.nio.ByteBuffer)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 HttpHandler (io.undertow.server.HttpHandler)4 HttpServerExchange (io.undertow.server.HttpServerExchange)4 Test (org.junit.Test)4 ChannelListener (org.xnio.ChannelListener)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 EmptyStreamSourceChannel (org.xnio.channels.EmptyStreamSourceChannel)3 ALPNProvider (io.undertow.protocols.alpn.ALPNProvider)1 SslConduit (io.undertow.protocols.ssl.SslConduit)1 TestHttpClient (io.undertow.testutils.TestHttpClient)1 ImmediatePooled (io.undertow.util.ImmediatePooled)1 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 CharBuffer (java.nio.CharBuffer)1 Channel (java.nio.channels.Channel)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1