use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDownstreamTransformationKnownContentLengthDroppingLastChunk.
@Test
public void testDownstreamTransformationKnownContentLengthDroppingLastChunk() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] chunk = new byte[1024];
int contentLength = 2 * chunk.length;
response.setContentLength(contentLength);
ServletOutputStream output = response.getOutputStream();
output.write(chunk);
output.flush();
sleep(1000);
output.write(chunk);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new ContentTransformer() {
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
if (!finished)
output.add(input);
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class NetworkTrafficListenerTest method testTrafficWithResponseContentChunkedOnPersistentConnection.
@Test
public void testTrafficWithResponseContentChunkedOnPersistentConnection() throws Exception {
final String responseContent = "response_content";
final String responseChunk1 = "response_content".substring(0, responseContent.length() / 2);
final String responseChunk2 = "response_content".substring(responseContent.length() / 2, responseContent.length());
initConnector(new AbstractHandler() {
@Override
public void handle(String uri, Request request, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException, ServletException {
request.setHandled(true);
ServletOutputStream output = servletResponse.getOutputStream();
output.write(responseChunk1.getBytes(StandardCharsets.UTF_8));
output.flush();
output.write(responseChunk2.getBytes(StandardCharsets.UTF_8));
output.flush();
}
});
final AtomicReference<String> incomingData = new AtomicReference<>();
final CountDownLatch incomingLatch = new CountDownLatch(1);
final AtomicReference<String> outgoingData = new AtomicReference<>("");
final CountDownLatch outgoingLatch = new CountDownLatch(1);
connector.addNetworkTrafficListener(new NetworkTrafficListener.Adapter() {
@Override
public void incoming(Socket socket, ByteBuffer bytes) {
incomingData.set(BufferUtil.toString(bytes, StandardCharsets.UTF_8));
incomingLatch.countDown();
}
@Override
public void outgoing(Socket socket, ByteBuffer bytes) {
outgoingData.set(outgoingData.get() + BufferUtil.toString(bytes, StandardCharsets.UTF_8));
if (outgoingData.get().endsWith("\r\n0\r\n\r\n"))
outgoingLatch.countDown();
}
});
int port = connector.getLocalPort();
String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost:" + port + "\r\n" + "\r\n";
String expectedResponse = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + responseChunk1.length() + "\r\n" + responseChunk1 + "\r\n" + responseChunk2.length() + "\r\n" + responseChunk2 + "\r\n" + "0\r\n" + "\r\n";
Socket socket = new Socket("localhost", port);
OutputStream output = socket.getOutputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
assertTrue(incomingLatch.await(1, TimeUnit.SECONDS));
assertEquals(request, incomingData.get());
assertTrue(outgoingLatch.await(1, TimeUnit.SECONDS));
assertEquals(expectedResponse, outgoingData.get());
byte[] responseBytes = readResponse(socket);
String response = new String(responseBytes, StandardCharsets.UTF_8);
assertEquals(expectedResponse, response);
socket.close();
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class ResponseTest method testFlushAfterFullContent.
@Test
public void testFlushAfterFullContent() throws Exception {
Response response = getResponse();
byte[] data = new byte[] { (byte) 0xCA, (byte) 0xFE };
ServletOutputStream output = response.getOutputStream();
response.setContentLength(data.length);
// Write the whole content
output.write(data);
// Must not throw
output.flush();
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class HttpClientGZIPTest method testGZIPContentSentTwiceInOneWrite.
@Test
public void testGZIPContentSentTwiceInOneWrite() throws Exception {
final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setHeader("Content-Encoding", "gzip");
ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
gzipOutput.write(data);
gzipOutput.finish();
byte[] gzipBytes = gzipData.toByteArray();
byte[] content = Arrays.copyOf(gzipBytes, 2 * gzipBytes.length);
System.arraycopy(gzipBytes, 0, content, gzipBytes.length, gzipBytes.length);
ServletOutputStream output = response.getOutputStream();
output.write(content);
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
Assert.assertEquals(200, response.getStatus());
byte[] expected = Arrays.copyOf(data, 2 * data.length);
System.arraycopy(data, 0, expected, data.length, data.length);
Assert.assertArrayEquals(expected, response.getContent());
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class HttpClientGZIPTest method testGZIPContentFragmented.
private void testGZIPContentFragmented(final int fragment) throws Exception {
final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.setHeader("Content-Encoding", "gzip");
ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
gzipOutput.write(data);
gzipOutput.finish();
byte[] gzipBytes = gzipData.toByteArray();
byte[] chunk1 = Arrays.copyOfRange(gzipBytes, 0, gzipBytes.length - fragment);
byte[] chunk2 = Arrays.copyOfRange(gzipBytes, gzipBytes.length - fragment, gzipBytes.length);
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
output.flush();
sleep(500);
output.write(chunk2);
output.flush();
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(data, response.getContent());
}
Aggregations