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);
}
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);
}
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);
}
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();
}
}
}
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;
}
}
}
Aggregations