use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.
the class GzipHttpInputInterceptor method readFrom.
@Override
public Content readFrom(Content content) {
_decoder.decodeChunks(content.getByteBuffer());
final ByteBuffer chunk = _chunk;
if (chunk == null)
return null;
return new Content(chunk) {
@Override
public void succeeded() {
_decoder.release(chunk);
}
};
}
use of org.eclipse.jetty.server.HttpInput.Content 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));
}
use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.
the class HttpInputAsyncStateTest method deliver.
private void deliver(Content... content) {
if (content != null) {
for (Content c : content) {
if (c == EOF_CONTENT) {
_in.eof();
_eof = true;
} else if (c == HttpInput.EARLY_EOF_CONTENT) {
_in.earlyEOF();
_eof = true;
} else {
_in.addContent(c);
BufferUtil.append(_expected, c.getByteBuffer().slice());
}
}
}
}
use of org.eclipse.jetty.server.HttpInput.Content in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncWriteLessThanContentLengthFlushed.
@Test
public void testAsyncWriteLessThanContentLengthFlushed() throws Exception {
CountDownLatch complete = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentLength(10);
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
AtomicInteger state = new AtomicInteger(0);
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (true) {
if (!out.isReady())
return;
switch(state.get()) {
case 0:
state.incrementAndGet();
WriteListener listener = this;
new Thread(() -> {
try {
Thread.sleep(50);
listener.onWritePossible();
} catch (Exception e) {
}
}).start();
return;
case 1:
state.incrementAndGet();
out.flush();
break;
case 2:
state.incrementAndGet();
out.write("12345".getBytes());
break;
case 3:
async.complete();
complete.countDown();
return;
}
}
}
@Override
public void onError(Throwable t) {
}
});
}
});
AtomicBoolean failed = new AtomicBoolean(false);
CountDownLatch clientLatch = new CountDownLatch(3);
client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
if (response.getStatus() == HttpStatus.OK_200)
clientLatch.countDown();
}).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// System.err.println("Content: "+BufferUtil.toDetailString(content));
}
}).onResponseFailure(new Response.FailureListener() {
@Override
public void onFailure(Response response, Throwable failure) {
clientLatch.countDown();
}
}).send(result -> {
failed.set(result.isFailed());
clientLatch.countDown();
clientLatch.countDown();
clientLatch.countDown();
});
assertTrue(complete.await(10, TimeUnit.SECONDS));
assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
assertTrue(failed.get());
}
Aggregations