use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testUpstreamTransformationBufferedGzipped.
@Test
public void testUpstreamTransformationBufferedGzipped() throws Exception {
startServer(new EchoHttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
super.service(request, response);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new GZIPContentTransformer(new BufferingContentTransformer());
}
});
startClient();
DeferredContentProvider content = new DeferredContentProvider();
Request request = client.newRequest("localhost", serverConnector.getLocalPort());
FutureResponseListener listener = new FutureResponseListener(request);
request.header(HttpHeader.CONTENT_ENCODING, "gzip").content(content).send(listener);
byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
content.offer(ByteBuffer.wrap(gzip(bytes)));
sleep(1000);
content.close();
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testUpstreamTransformationThrowsAfterCommittingProxyRequest.
@Test
public void testUpstreamTransformationThrowsAfterCommittingProxyRequest() throws Exception {
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
startServer(new EchoHttpServlet());
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new ContentTransformer() {
private int count;
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
if (++count < 2)
output.add(input);
else
throw new NullPointerException("explicitly_thrown_by_test");
}
};
}
});
startClient();
final CountDownLatch latch = new CountDownLatch(1);
DeferredContentProvider content = new DeferredContentProvider();
client.newRequest("localhost", serverConnector.getLocalPort()).content(content).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded() && result.getResponse().getStatus() == 502)
latch.countDown();
}
});
content.offer(ByteBuffer.allocate(512));
sleep(1000);
content.offer(ByteBuffer.allocate(512));
content.close();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testProxyRequestHeadersNotSentUntilFirstContent.
@Test
public void testProxyRequestHeadersNotSentUntilFirstContent() throws Exception {
startServer(new EchoHttpServlet());
final CountDownLatch proxyRequestLatch = new CountDownLatch(1);
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new ContentTransformer() {
private ByteBuffer buffer;
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
// Buffer only the first chunk.
if (buffer == null) {
buffer = ByteBuffer.allocate(input.remaining());
buffer.put(input).flip();
} else if (buffer.hasRemaining()) {
output.add(buffer);
output.add(input);
} else {
output.add(input);
}
}
};
}
@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
proxyRequestLatch.countDown();
super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
});
startClient();
DeferredContentProvider content = new DeferredContentProvider();
Request request = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).content(content);
FutureResponseListener listener = new FutureResponseListener(request);
request.send(listener);
// Send one chunk of content, the proxy request must not be sent.
ByteBuffer chunk1 = ByteBuffer.allocate(1024);
content.offer(chunk1);
Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
// Send another chunk of content, the proxy request must be sent.
ByteBuffer chunk2 = ByteBuffer.allocate(512);
content.offer(chunk2);
Assert.assertTrue(proxyRequestLatch.await(5, TimeUnit.SECONDS));
// Finish the content.
content.close();
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(chunk1.capacity() + chunk2.capacity(), response.getContent().length);
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class Usage method testProxyUsage.
@Test
public void testProxyUsage() throws Exception {
// In proxies, we receive the headers but not the content, so we must be able to send the request with
// a lazy content provider that does not block request.send(...)
HttpClient client = new HttpClient();
client.start();
final AtomicBoolean sendContent = new AtomicBoolean(true);
DeferredContentProvider async = new DeferredContentProvider(ByteBuffer.wrap(new byte[] { 0, 1, 2 }));
client.newRequest("localhost", 8080).content(async).send(new Response.Listener.Adapter() {
@Override
public void onBegin(Response response) {
if (response.getStatus() == 404)
sendContent.set(false);
}
});
Thread.sleep(100);
if (sendContent.get())
async.offer(ByteBuffer.wrap(new byte[] { 0 }));
Thread.sleep(100);
if (sendContent.get())
async.offer(ByteBuffer.wrap(new byte[] { 0 }));
Thread.sleep(100);
async.close();
}
use of org.eclipse.jetty.client.util.DeferredContentProvider in project jetty.project by eclipse.
the class ProxyServletFailureTest method testProxyRequestStallsContentServerIdlesTimeout.
@Test
public void testProxyRequestStallsContentServerIdlesTimeout() throws Exception {
final byte[] content = new byte[] { 'C', '0', 'F', 'F', 'E', 'E' };
int expected;
if (proxyServlet instanceof AsyncProxyServlet) {
// TODO should this be a 502 also???
expected = 500;
proxyServlet = new AsyncProxyServlet() {
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
DeferredContentProvider provider = new DeferredContentProvider() {
@Override
public boolean offer(ByteBuffer buffer, Callback callback) {
// Send less content to trigger the test condition.
buffer.limit(buffer.limit() - 1);
return super.offer(buffer.slice(), callback);
}
};
request.getInputStream().setReadListener(newReadListener(request, response, proxyRequest, provider));
return provider;
}
};
} else {
expected = 502;
proxyServlet = new ProxyServlet() {
@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
return new BytesContentProvider(content) {
@Override
public long getLength() {
// Increase the content length to trigger the test condition.
return content.length + 1;
}
};
}
};
}
prepareProxy();
prepareServer(new EchoHttpServlet());
long idleTimeout = 1000;
serverConnector.setIdleTimeout(idleTimeout);
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(content)).send();
Assert.assertEquals(expected, response.getStatus());
}
}
Aggregations