use of org.eclipse.jetty.server.HttpInput in project jetty.project by eclipse.
the class HttpChannelOverHTTP2 method onStreamTimeout.
public boolean onStreamTimeout(Throwable failure) {
if (!_handled)
return true;
HttpInput input = getRequest().getHttpInput();
boolean readFailed = input.failed(failure);
if (readFailed)
handle();
boolean writeFailed = getHttpTransport().onStreamTimeout(failure);
return readFailed || writeFailed;
}
use of org.eclipse.jetty.server.HttpInput in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncIntercepted.
@Test
public void testAsyncIntercepted() throws Exception {
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.err.println("Service " + request);
final HttpInput httpInput = ((Request) request).getHttpInput();
httpInput.addInterceptor(new HttpInput.Interceptor() {
int state = 0;
Content saved;
@Override
public Content readFrom(Content content) {
// System.err.printf("readFrom s=%d saved=%b %s%n",state,saved!=null,content);
switch(state) {
case 0:
// null transform
if (content.isEmpty())
state++;
return null;
case 1:
{
// copy transform
if (content.isEmpty()) {
state++;
return content;
}
ByteBuffer copy = wrap(toArray(content.getByteBuffer()));
content.skip(copy.remaining());
return new Content(copy);
}
case 2:
// byte by byte
if (content.isEmpty()) {
state++;
return content;
}
byte[] b = new byte[1];
int l = content.get(b, 0, 1);
return new Content(wrap(b, 0, l));
case 3:
{
// double vision
if (content.isEmpty()) {
if (saved == null) {
state++;
return content;
}
Content copy = saved;
saved = null;
return copy;
}
byte[] data = toArray(content.getByteBuffer());
content.skip(data.length);
saved = new Content(wrap(data));
return new Content(wrap(data));
}
default:
return null;
}
}
});
AsyncContext asyncContext = request.startAsync();
ServletInputStream input = request.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
input.setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
while (input.isReady()) {
int b = input.read();
if (b > 0) {
// System.err.printf("0x%2x %s %n", b, Character.isISOControl(b)?"?":(""+(char)b));
out.write(b);
} else if (b < 0)
return;
}
}
@Override
public void onAllDataRead() throws IOException {
response.getOutputStream().write(out.toByteArray());
asyncContext.complete();
}
@Override
public void onError(Throwable x) {
}
});
}
});
DeferredContentProvider contentProvider = new DeferredContentProvider();
CountDownLatch clientLatch = new CountDownLatch(1);
String expected = "S0" + "S1" + "S2" + "S3S3" + "S4" + "S5" + "S6";
client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
Response response = result.getResponse();
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertThat(getContentAsString(), Matchers.equalTo(expected));
clientLatch.countDown();
}
}
});
contentProvider.offer(BufferUtil.toBuffer("S0"));
contentProvider.flush();
contentProvider.offer(BufferUtil.toBuffer("S1"));
contentProvider.flush();
contentProvider.offer(BufferUtil.toBuffer("S2"));
contentProvider.flush();
contentProvider.offer(BufferUtil.toBuffer("S3"));
contentProvider.flush();
contentProvider.offer(BufferUtil.toBuffer("S4"));
contentProvider.flush();
contentProvider.offer(BufferUtil.toBuffer("S5"));
contentProvider.flush();
contentProvider.offer(BufferUtil.toBuffer("S6"));
contentProvider.close();
Assert.assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
}
Aggregations