use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class H2CUpgradeResetTestCase method createClientCallback.
/**
* Create the callback to receive the response and assign it to the list.
* @param responses The list where the response will be added
* @param latch The latch to count down when the response is received
* @param message The message to send if it's a POST message (if null nothing is send)
* @param boolean reset if true a RST is sent for the received the frame ID after completed
* @return The created callback
*/
private static ClientCallback<ClientExchange> createClientCallback(final List<ClientResponse> responses, final CountDownLatch latch, String message, boolean reset) {
return new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
if (message != null) {
new StringWriteChannelListener(message).setup(result.getRequestChannel());
}
result.setResponseListener(new ClientCallback<ClientExchange>() {
@Override
public void completed(final ClientExchange result) {
responses.add(result.getResponse());
new StringReadChannelListener(result.getConnection().getBufferPool()) {
@Override
protected void stringDone(String string) {
result.getResponse().putAttachment(RESPONSE_BODY, string);
if (reset) {
Http2StreamSourceChannel res = (Http2StreamSourceChannel) result.getResponseChannel();
res.getHttp2Channel().sendRstStream(res.getStreamId(), Http2Channel.ERROR_STREAM_CLOSED);
}
latch.countDown();
}
@Override
protected void error(IOException e) {
e.printStackTrace();
latch.countDown();
}
}.setup(result.getResponseChannel());
}
@Override
public void failed(IOException e) {
e.printStackTrace();
latch.countDown();
}
});
}
@Override
public void failed(IOException e) {
e.printStackTrace();
latch.countDown();
}
};
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class ChunkedResponseTrailersTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
if (connection == null) {
connection = exchange.getConnection();
} else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write("Connection not persistent".getBytes());
outputStream.close();
return;
}
HeaderMap trailers = new HeaderMap();
exchange.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers);
trailers.put(HttpString.tryFromString("foo"), "fooVal");
trailers.put(HttpString.tryFromString("bar"), "barVal");
new StringWriteChannelListener(message).setup(exchange.getResponseChannel());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class ReadTimeoutTestCase method testReadTimeout.
@Test
public void testReadTimeout() throws InterruptedException, IOException {
final CountDownLatch errorLatch = new CountDownLatch(1);
DefaultServer.setRootHandler((final HttpServerExchange exchange) -> {
final StreamSinkChannel response = exchange.getResponseChannel();
final StreamSourceChannel request = exchange.getRequestChannel();
request.getReadSetter().set(ChannelListeners.drainListener(Long.MAX_VALUE, (final Channel channel) -> {
new StringWriteChannelListener("COMPLETED") {
@Override
protected void writeDone(final StreamSinkChannel channel) {
exchange.endExchange();
}
}.setup(response);
}, (final StreamSourceChannel channel, final IOException e) -> {
e.printStackTrace();
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 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");
boolean socketFailure = false;
try {
client.execute(post);
} catch (SocketException e) {
Assert.assertTrue(e.getMessage(), e.getMessage().contains("Broken pipe") || e.getMessage().contains("connection abort"));
socketFailure = true;
}
Assert.assertTrue("Test sent request without any exception", socketFailure);
if (errorLatch.await(5, TimeUnit.SECONDS)) {
Assert.assertTrue(getExceptionDescription(exception), exception instanceof ReadTimeoutException || (DefaultServer.isProxy() && exception instanceof IOException));
if (exception.getSuppressed() != null && exception.getSuppressed().length > 0) {
for (Throwable supressed : exception.getSuppressed()) {
Assert.assertEquals(getExceptionDescription(supressed), ReadTimeoutException.class, exception.getClass());
}
}
} else if (!DefaultServer.isProxy()) {
// ignore if proxy, because when we're on proxy, we might not be able to see the exception
Assert.fail("Did not get ReadTimeoutException");
}
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class ChunkedResponseTransferCodingTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
if (connection == null) {
connection = exchange.getConnection();
} else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write("Connection not persistent".getBytes());
outputStream.close();
return;
}
new StringWriteChannelListener(message).setup(exchange.getResponseChannel());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class PreChunkedResponseTransferCodingTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
if (connection == null) {
connection = exchange.getConnection();
} else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write("Connection not persistent".getBytes());
outputStream.close();
return;
}
exchange.getResponseHeaders().put(Headers.TRANSFER_ENCODING, Headers.CHUNKED.toString());
exchange.putAttachment(HttpAttachments.PRE_CHUNKED_RESPONSE, true);
new StringWriteChannelListener(chunkedMessage).setup(exchange.getResponseChannel());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
Aggregations