use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithChunkedContent_Respond100Continue.
@Test
public void test_Expect100Continue_WithChunkedContent_Respond100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and copy the content back
ServletInputStream input = request.getInputStream();
// Make sure we chunk the response too
response.flushBuffer();
IO.copy(input, response.getOutputStream());
}
});
byte[] content1 = new byte[10240];
byte[] content2 = new byte[16384];
ContentResponse response = client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content1, content2) {
@Override
public long getLength() {
return -1;
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
int index = 0;
byte[] responseContent = response.getContent();
for (byte b : content1) Assert.assertEquals(b, responseContent[index++]);
for (byte b : content2) Assert.assertEquals(b, responseContent[index++]);
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_During100Continue.
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_During100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and consume the content
IO.copy(request.getInputStream(), new ByteArrayOutputStream());
}
});
client.getProtocolHandlers().clear();
client.getProtocolHandlers().put(new ContinueProtocolHandler() {
@Override
public Response.Listener getResponseListener() {
final Response.Listener listener = super.getResponseListener();
return new Response.Listener.Adapter() {
@Override
public void onBegin(Response response) {
response.abort(new Exception());
}
@Override
public void onFailure(Response response, Throwable failure) {
listener.onFailure(response, failure);
}
};
}
});
byte[] content = new byte[1024];
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isFailed());
Assert.assertNotNull(result.getRequestFailure());
Assert.assertNotNull(result.getResponseFailure());
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project druid by druid-io.
the class AsyncQueryForwardingServlet method sendProxyRequest.
@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
proxyRequest.timeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
proxyRequest.idleTimeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
final Query query = (Query) clientRequest.getAttribute(QUERY_ATTRIBUTE);
if (query != null) {
final ObjectMapper objectMapper = (ObjectMapper) clientRequest.getAttribute(OBJECTMAPPER_ATTRIBUTE);
try {
proxyRequest.content(new BytesContentProvider(objectMapper.writeValueAsBytes(query)));
} catch (JsonProcessingException e) {
Throwables.propagate(e);
}
}
super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testTransformUpstreamAndDownstreamKnownContentLengthGzipped.
@Test
public void testTransformUpstreamAndDownstreamKnownContentLengthGzipped() throws Exception {
String data = "<a href=\"http://google.com\">Google</a>";
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
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 HrefTransformer.Client());
}
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new GZIPContentTransformer(new HrefTransformer.Server());
}
});
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.assertArrayEquals(bytes, response.getContent());
}
use of org.eclipse.jetty.client.util.BytesContentProvider in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testClientRequestContentKnownLengthGzipped.
private void testClientRequestContentKnownLengthGzipped(int length, final boolean expectChunked) throws Exception {
byte[] bytes = new byte[length];
new Random().nextBytes(bytes);
startServer(new EchoHttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String transferEncoding = request.getHeader(HttpHeader.TRANSFER_ENCODING.asString());
if (expectChunked)
Assert.assertNotNull(transferEncoding);
else
Assert.assertNull(transferEncoding);
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(ContentTransformer.IDENTITY);
}
});
startClient();
byte[] gzipBytes = gzip(bytes);
ContentProvider gzipContent = new BytesContentProvider(gzipBytes);
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(gzipContent).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(bytes, response.getContent());
}
Aggregations