Search in sources :

Example 1 with HttpInput

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;
}
Also used : HttpInput(org.eclipse.jetty.server.HttpInput)

Example 2 with HttpInput

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));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) ReadListener(javax.servlet.ReadListener) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpInput(org.eclipse.jetty.server.HttpInput) ServletInputStream(javax.servlet.ServletInputStream) Content(org.eclipse.jetty.server.HttpInput.Content) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Aggregations

HttpInput (org.eclipse.jetty.server.HttpInput)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AsyncContext (javax.servlet.AsyncContext)1 ReadListener (javax.servlet.ReadListener)1 ServletException (javax.servlet.ServletException)1 ServletInputStream (javax.servlet.ServletInputStream)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Response (org.eclipse.jetty.client.api.Response)1 Result (org.eclipse.jetty.client.api.Result)1 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)1 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)1 Content (org.eclipse.jetty.server.HttpInput.Content)1