Search in sources :

Example 1 with HeadersFrame

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

the class JDK9HTTP2Client method main.

public static void main(String[] args) throws Exception {
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    client.addBean(sslContextFactory);
    client.start();
    String host = "localhost";
    int port = 8443;
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new Session.Listener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(555, TimeUnit.SECONDS);
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            System.err.println(frame);
            if (frame.isEndStream())
                latch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            System.err.println(frame);
            callback.succeeded();
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    client.stop();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session)

Example 2 with HeadersFrame

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

the class TrailersTest method testTrailersSentByServer.

@Test
public void testTrailersSentByServer() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpFields responseFields = new HttpFields();
            responseFields.put("X-Response", "true");
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, responseFields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, false);
            stream.headers(responseFrame, new Callback() {

                @Override
                public void succeeded() {
                    HttpFields trailerFields = new HttpFields();
                    trailerFields.put("X-Trailer", "true");
                    MetaData trailer = new MetaData(HttpVersion.HTTP_2, trailerFields);
                    HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailer, null, true);
                    stream.headers(trailerFrame, NOOP);
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(request, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        private boolean responded;

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (!responded) {
                MetaData.Response response = (MetaData.Response) frame.getMetaData();
                Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
                Assert.assertTrue(response.getFields().containsKey("X-Response"));
                Assert.assertFalse(frame.isEndStream());
                responded = true;
            } else {
                MetaData trailer = frame.getMetaData();
                Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                Assert.assertTrue(frame.isEndStream());
                latch.countDown();
            }
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 3 with HeadersFrame

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

the class TrailersTest method testTrailersSentByClient.

@Test
public void testTrailersSentByClient() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            Assert.assertFalse(frame.isEndStream());
            Assert.assertTrue(request.getFields().containsKey("X-Request"));
            return new Stream.Listener.Adapter() {

                @Override
                public void onHeaders(Stream stream, HeadersFrame frame) {
                    MetaData trailer = frame.getMetaData();
                    Assert.assertTrue(frame.isEndStream());
                    Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                    latch.countDown();
                }
            };
        }
    });
    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<>();
    session.newStream(requestFrame, streamPromise, new Stream.Listener.Adapter());
    Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
    // Send the trailers.
    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));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 4 with HeadersFrame

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

the class ContinuationBodyParser method onHeaders.

private void onHeaders() {
    ByteBuffer headerBlock = headerBlockFragments.complete();
    MetaData metaData = headerBlockParser.parse(headerBlock, headerBlock.remaining());
    HeadersFrame frame = new HeadersFrame(getStreamId(), metaData, headerBlockFragments.getPriorityFrame(), headerBlockFragments.isEndStream());
    notifyHeaders(frame);
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) ByteBuffer(java.nio.ByteBuffer) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame)

Example 5 with HeadersFrame

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

the class HeadersBodyParser method onHeaders.

private void onHeaders(int parentStreamId, int weight, boolean exclusive, MetaData metaData) {
    PriorityFrame priorityFrame = null;
    if (hasFlag(Flags.PRIORITY))
        priorityFrame = new PriorityFrame(getStreamId(), parentStreamId, weight, exclusive);
    HeadersFrame frame = new HeadersFrame(getStreamId(), metaData, priorityFrame, isEndStream());
    notifyHeaders(frame);
}
Also used : PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame)

Aggregations

HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)114 HttpFields (org.eclipse.jetty.http.HttpFields)111 MetaData (org.eclipse.jetty.http.MetaData)111 Test (org.junit.Test)106 CountDownLatch (java.util.concurrent.CountDownLatch)98 Stream (org.eclipse.jetty.http2.api.Stream)98 Session (org.eclipse.jetty.http2.api.Session)90 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)82 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)57 Callback (org.eclipse.jetty.util.Callback)54 HttpServletResponse (javax.servlet.http.HttpServletResponse)53 Promise (org.eclipse.jetty.util.Promise)50 ByteBuffer (java.nio.ByteBuffer)40 HttpServletRequest (javax.servlet.http.HttpServletRequest)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)38 IOException (java.io.IOException)37 ServletException (javax.servlet.ServletException)36 HashMap (java.util.HashMap)33 HttpServlet (javax.servlet.http.HttpServlet)33