Search in sources :

Example 36 with DataFrame

use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.

the class HttpChannelOverHTTP2 method onRequestContent.

public Runnable onRequestContent(DataFrame frame, final Callback callback) {
    Stream stream = getStream();
    if (stream.isReset()) {
        // Consume previously queued content to
        // enlarge the session flow control window.
        consumeInput();
        // Consume immediately this content.
        callback.succeeded();
        return null;
    }
    // We must copy the data since we do not know when the
    // application will consume the bytes (we queue them by
    // calling onContent()), and the parsing will continue
    // as soon as this method returns, eventually leading
    // to reusing the underlying buffer for more reads.
    final ByteBufferPool byteBufferPool = getByteBufferPool();
    ByteBuffer original = frame.getData();
    int length = original.remaining();
    final ByteBuffer copy = byteBufferPool.acquire(length, original.isDirect());
    BufferUtil.clearToFill(copy);
    copy.put(original);
    BufferUtil.flipToFlush(copy, 0);
    boolean handle = onContent(new HttpInput.Content(copy) {

        @Override
        public InvocationType getInvocationType() {
            return callback.getInvocationType();
        }

        @Override
        public void succeeded() {
            byteBufferPool.release(copy);
            callback.succeeded();
        }

        @Override
        public void failed(Throwable x) {
            byteBufferPool.release(copy);
            callback.failed(x);
        }
    });
    boolean endStream = frame.isEndStream();
    if (endStream) {
        boolean handle_content = onContentComplete();
        boolean handle_request = onRequestComplete();
        handle |= handle_content | handle_request;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("HTTP2 Request #{}/{}: {} bytes of {} content, handle: {}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), length, endStream ? "last" : "some", handle);
    }
    boolean wasDelayed = _delayedUntilContent;
    _delayedUntilContent = false;
    if (wasDelayed)
        _handled = true;
    return handle || wasDelayed ? this : null;
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) HttpInput(org.eclipse.jetty.server.HttpInput) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 37 with DataFrame

use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.

the class HttpTransportOverHTTP2 method send.

private void send(ByteBuffer content, boolean lastContent, Callback callback) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("HTTP2 Response #{}/{}: {} content bytes{}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), content.remaining(), lastContent ? " (last chunk)" : "");
    }
    DataFrame frame = new DataFrame(stream.getId(), content, lastContent);
    stream.data(frame, callback);
}
Also used : DataFrame(org.eclipse.jetty.http2.frames.DataFrame)

Example 38 with DataFrame

use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.

the class HttpSenderOverHTTP2 method sendHeaders.

@Override
protected void sendHeaders(HttpExchange exchange, final HttpContent content, final Callback callback) {
    Request request = exchange.getRequest();
    String path = relativize(request.getPath());
    HttpURI uri = new HttpURI(request.getScheme(), request.getHost(), request.getPort(), path, null, request.getQuery(), null);
    MetaData.Request metaData = new MetaData.Request(request.getMethod(), uri, HttpVersion.HTTP_2, request.getHeaders());
    HeadersFrame headersFrame = new HeadersFrame(metaData, null, !content.hasContent());
    HttpChannelOverHTTP2 channel = getHttpChannel();
    Promise<Stream> promise = new Promise<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            getHttpChannel().setStream(stream);
            stream.setIdleTimeout(request.getIdleTimeout());
            if (content.hasContent() && !expects100Continue(request)) {
                boolean advanced = content.advance();
                boolean lastContent = content.isLast();
                if (advanced || lastContent) {
                    DataFrame dataFrame = new DataFrame(stream.getId(), content.getByteBuffer(), lastContent);
                    stream.data(dataFrame, callback);
                    return;
                }
            }
            callback.succeeded();
        }

        @Override
        public void failed(Throwable failure) {
            callback.failed(failure);
        }
    };
    // TODO optimize the send of HEADERS and DATA frames.
    channel.getSession().newStream(headersFrame, promise, channel.getStreamListener());
}
Also used : Request(org.eclipse.jetty.client.api.Request) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HttpURI(org.eclipse.jetty.http.HttpURI) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) MetaData(org.eclipse.jetty.http.MetaData) Stream(org.eclipse.jetty.http2.api.Stream)

Example 39 with DataFrame

use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.

the class HttpSenderOverHTTP2 method sendContent.

@Override
protected void sendContent(HttpExchange exchange, HttpContent content, Callback callback) {
    if (content.isConsumed()) {
        callback.succeeded();
    } else {
        Stream stream = getHttpChannel().getStream();
        DataFrame frame = new DataFrame(stream.getId(), content.getByteBuffer(), content.isLast());
        stream.data(frame, callback);
    }
}
Also used : Stream(org.eclipse.jetty.http2.api.Stream) DataFrame(org.eclipse.jetty.http2.frames.DataFrame)

Example 40 with DataFrame

use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.

the class StreamCloseTest method testRequestDataClosedResponseDataClosedClosesStream.

@Test
public void testRequestDataClosedResponseDataClosedClosesStream() throws Exception {
    final CountDownLatch serverDataLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame response = new HeadersFrame(stream.getId(), metaData, null, false);
            Callback.Completable completable = new Callback.Completable();
            stream.headers(response, completable);
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(final Stream stream, DataFrame frame, final Callback callback) {
                    Assert.assertTrue(((HTTP2Stream) stream).isRemotelyClosed());
                    // We must copy the data that we send asynchronously.
                    ByteBuffer data = frame.getData();
                    ByteBuffer copy = ByteBuffer.allocate(data.remaining());
                    copy.put(data).flip();
                    completable.thenRun(() -> stream.data(new DataFrame(stream.getId(), copy, frame.isEndStream()), new Callback() {

                        @Override
                        public void succeeded() {
                            Assert.assertTrue(stream.isClosed());
                            Assert.assertEquals(0, stream.getSession().getStreams().size());
                            callback.succeeded();
                            serverDataLatch.countDown();
                        }
                    }));
                }
            };
        }
    });
    final CountDownLatch completeLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            // The sent data callback may not be notified yet here.
            callback.succeeded();
            completeLatch.countDown();
        }
    });
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertFalse(stream.isClosed());
    Assert.assertFalse(((HTTP2Stream) stream).isLocallyClosed());
    final CountDownLatch clientDataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.wrap(new byte[512]), true), new Callback() {

        @Override
        public void succeeded() {
            // Here the stream may be just locally closed or fully closed.
            clientDataLatch.countDown();
        }
    });
    Assert.assertTrue(clientDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(serverDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(stream.isClosed());
    Assert.assertEquals(0, stream.getSession().getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

DataFrame (org.eclipse.jetty.http2.frames.DataFrame)59 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)56 HttpFields (org.eclipse.jetty.http.HttpFields)55 MetaData (org.eclipse.jetty.http.MetaData)55 Stream (org.eclipse.jetty.http2.api.Stream)55 CountDownLatch (java.util.concurrent.CountDownLatch)54 Test (org.junit.Test)53 Session (org.eclipse.jetty.http2.api.Session)51 Callback (org.eclipse.jetty.util.Callback)47 FuturePromise (org.eclipse.jetty.util.FuturePromise)42 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)39 Promise (org.eclipse.jetty.util.Promise)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)29 IOException (java.io.IOException)25 ServletException (javax.servlet.ServletException)24 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 ByteBuffer (java.nio.ByteBuffer)23 HttpServlet (javax.servlet.http.HttpServlet)23 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)20 ISession (org.eclipse.jetty.http2.ISession)18