use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class TrailersTest method testServletRequestTrailers.
@Test
public void testServletRequestTrailers() throws Exception {
CountDownLatch trailerLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Request jettyRequest = (Request) request;
// No trailers yet.
Assert.assertNull(jettyRequest.getTrailers());
trailerLatch.countDown();
// Read the content.
ServletInputStream input = jettyRequest.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
// Now we have the trailers.
HttpFields trailers = jettyRequest.getTrailers();
Assert.assertNotNull(trailers);
Assert.assertNotNull(trailers.get("X-Trailer"));
}
});
Session session = newClient(new Session.Listener.Adapter());
HttpFields requestFields = new HttpFields();
requestFields.put("X-Request", "true");
MetaData.Request request = newRequest("GET", requestFields);
HeadersFrame requestFrame = new HeadersFrame(request, null, false);
FuturePromise<Stream> streamPromise = new FuturePromise<>();
CountDownLatch latch = new CountDownLatch(1);
session.newStream(requestFrame, streamPromise, new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
// Send some data.
Callback.Completable callback = new Callback.Completable();
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(16), false), callback);
Assert.assertTrue(trailerLatch.await(5, TimeUnit.SECONDS));
// Send the trailers.
callback.thenRun(() -> {
HttpFields trailerFields = new HttpFields();
trailerFields.put("X-Trailer", "true");
MetaData trailers = new MetaData(HttpVersion.HTTP_2, trailerFields);
HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailers, null, true);
stream.headers(trailerFrame, Callback.NOOP);
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class ServerTimeoutsTest method testAsyncReadIdleTimeoutFires.
@Test
public void testAsyncReadIdleTimeoutFires() throws Exception {
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
ServletInputStream input = request.getInputStream();
input.setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
Assert.assertEquals(0, input.read());
Assert.assertFalse(input.isReady());
}
@Override
public void onAllDataRead() throws IOException {
}
@Override
public void onError(Throwable failure) {
if (failure instanceof TimeoutException) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
asyncContext.complete();
handlerLatch.countDown();
}
}
});
}
});
long idleTimeout = 2500;
setServerIdleTimeout(idleTimeout);
DeferredContentProvider contentProvider = new DeferredContentProvider(ByteBuffer.allocate(1));
CountDownLatch resultLatch = new CountDownLatch(1);
client.POST(newURI()).content(contentProvider).send(result -> {
if (result.getResponse().getStatus() == HttpStatus.INTERNAL_SERVER_ERROR_500)
resultLatch.countDown();
});
// Async read should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// Complete the request.
contentProvider.close();
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class ServerTimeoutsTest method testBlockingReadWithMinimumDataRateAboveLimit.
@Test
public void testBlockingReadWithMinimumDataRateAboveLimit() throws Exception {
int bytesPerSecond = 20;
httpConfig.setMinRequestDataRate(bytesPerSecond);
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletInputStream input = request.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
handlerLatch.countDown();
}
});
DeferredContentProvider contentProvider = new DeferredContentProvider();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).content(contentProvider).send(result -> {
if (result.getResponse().getStatus() == HttpStatus.OK_200)
resultLatch.countDown();
});
for (int i = 0; i < 3; ++i) {
contentProvider.offer(ByteBuffer.allocate(bytesPerSecond * 2));
Thread.sleep(2500);
}
contentProvider.close();
Assert.assertTrue(handlerLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class HttpClientTest method testUploadWithoutResponseContent.
private void testUploadWithoutResponseContent(int length) throws Exception {
final byte[] bytes = new byte[length];
new Random().nextBytes(bytes);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletInputStream input = request.getInputStream();
for (int i = 0; i < bytes.length; ++i) Assert.assertEquals(bytes[i] & 0xFF, input.read());
Assert.assertEquals(-1, input.read());
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(new BytesContentProvider(bytes)).timeout(15, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(0, response.getContent().length);
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class HttpClientTest method testClientManyWritesSlowServer.
@Test
public void testClientManyWritesSlowServer() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
long sleep = 1024;
long total = 0;
ServletInputStream input = request.getInputStream();
byte[] buffer = new byte[1024];
while (true) {
int read = input.read(buffer);
if (read < 0)
break;
total += read;
if (total >= sleep) {
sleep(250);
sleep += 256;
}
}
response.getOutputStream().print(total);
}
});
int chunks = 256;
int chunkSize = 16;
byte[][] bytes = IntStream.range(0, chunks).mapToObj(x -> new byte[chunkSize]).toArray(byte[][]::new);
BytesContentProvider contentProvider = new BytesContentProvider("application/octet-stream", bytes);
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(contentProvider).timeout(15, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals(chunks * chunkSize, Integer.parseInt(response.getContentAsString()));
}
Aggregations