use of javax.servlet.ServletException in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testLargeChunkedBufferedDownstreamTransformation.
private void testLargeChunkedBufferedDownstreamTransformation(final boolean gzipped) throws Exception {
// Tests the race between a incomplete write performed from ProxyResponseListener.onSuccess()
// and ProxyResponseListener.onComplete() being called before the write has completed.
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStream output = response.getOutputStream();
if (gzipped) {
output = new GZIPOutputStream(output);
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
}
Random random = new Random();
byte[] chunk = new byte[1024 * 1024];
for (int i = 0; i < 16; ++i) {
random.nextBytes(chunk);
output.write(chunk);
output.flush();
}
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
ContentTransformer transformer = new BufferingContentTransformer();
if (gzipped)
transformer = new GZIPContentTransformer(transformer);
return transformer;
}
});
startClient();
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", serverConnector.getLocalPort()).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// Slow down the reader so that the
// write from the proxy gets congested.
sleep(1);
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
latch.countDown();
}
});
Assert.assertTrue(latch.await(15, TimeUnit.SECONDS));
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDiscardUpstreamAndDownstreamKnownContentLengthGzipped.
@Test
public void testDiscardUpstreamAndDownstreamKnownContentLengthGzipped() throws Exception {
final byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// decode input stream thru gzip
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IO.copy(new GZIPInputStream(request.getInputStream()), bos);
// ensure decompressed is 0 length
Assert.assertEquals(0, bos.toByteArray().length);
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
response.getOutputStream().write(gzip(bytes));
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new GZIPContentTransformer(new DiscardContentTransformer());
}
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(new DiscardContentTransformer());
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(new BytesContentProvider(gzip(bytes))).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(0, response.getContent().length);
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformerClosingFilesOnClientRequestException.
@Test
public void testAfterContentTransformerClosingFilesOnClientRequestException() throws Exception {
final Path targetTestsDir = prepareTargetTestsDir();
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IO.copy(request.getInputStream(), IO.getNullStream());
}
});
final CountDownLatch destroyLatch = new CountDownLatch(1);
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new AfterContentTransformer() {
{
setOverflowDirectory(targetTestsDir);
setMaxInputBufferSize(0);
setMaxOutputBufferSize(0);
}
@Override
public boolean transform(Source source, Sink sink) throws IOException {
IO.copy(source.getInputStream(), sink.getOutputStream());
return true;
}
@Override
public void destroy() {
super.destroy();
destroyLatch.countDown();
}
};
}
});
long idleTimeout = 1000;
proxyConnector.setIdleTimeout(idleTimeout);
startClient();
// Send only part of the content; the proxy will idle timeout.
final byte[] data = new byte[] { 'c', 'a', 'f', 'e' };
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(data) {
@Override
public long getLength() {
return data.length + 1;
}
}).timeout(5 * idleTimeout, TimeUnit.MILLISECONDS).send();
Assert.assertTrue(destroyLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT_408, response.getStatus());
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class ProxyServletFailureTest method testProxyRequestExpired.
@Test
public void testProxyRequestExpired() throws Exception {
prepareProxy();
final long timeout = 1000;
proxyServlet.setTimeout(timeout);
prepareServer(new HttpServlet() {
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
if (request.getHeader("Via") != null)
response.addHeader(PROXIED_HEADER, "true");
try {
TimeUnit.MILLISECONDS.sleep(2 * timeout);
} catch (InterruptedException x) {
throw new ServletException(x);
}
}
});
Response response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(3 * timeout, TimeUnit.MILLISECONDS).send();
Assert.assertEquals(504, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(PROXIED_HEADER));
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class ProxyServletLoadTest method test.
@Test
public void test() throws Exception {
startServer(new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
IO.copy(req.getInputStream(), resp.getOutputStream());
}
});
startProxy();
startClient();
// Number of clients to simulate
int clientCount = Runtime.getRuntime().availableProcessors();
// Latch for number of clients still active (used to terminate test)
final CountDownLatch activeClientLatch = new CountDownLatch(clientCount);
// Atomic Boolean to track that its OK to still continue looping.
// When this goes false, that means one of the client threads has
// encountered an error condition, and should allow all remaining
// client threads to finish cleanly.
final AtomicBoolean success = new AtomicBoolean(true);
int iterations = 1000;
// Start clients
for (int i = 0; i < clientCount; i++) {
ClientLoop r = new ClientLoop(activeClientLatch, success, client, "localhost", serverConnector.getLocalPort(), iterations);
String name = "client-" + i;
Thread thread = new Thread(r, name);
thread.start();
}
Assert.assertTrue(activeClientLatch.await(Math.max(clientCount * iterations * 10, 5000), TimeUnit.MILLISECONDS));
Assert.assertTrue(success.get());
}
Aggregations