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