Search in sources :

Example 1 with Frame

use of org.eclipse.jetty.http2.frames.Frame 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 Frame

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

the class HttpReceiverOverHTTP2 method onReset.

@Override
public void onReset(Stream stream, ResetFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return;
    ErrorCode error = ErrorCode.from(frame.getError());
    String reason = error == null ? "reset" : error.name().toLowerCase(Locale.ENGLISH);
    exchange.getRequest().abort(new IOException(reason));
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) ErrorCode(org.eclipse.jetty.http2.ErrorCode) IOException(java.io.IOException)

Example 3 with Frame

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

the class HttpReceiverOverHTTP2 method onPush.

@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return null;
    HttpRequest request = exchange.getRequest();
    MetaData.Request metaData = (MetaData.Request) frame.getMetaData();
    HttpRequest pushRequest = (HttpRequest) getHttpDestination().getHttpClient().newRequest(metaData.getURIString());
    BiFunction<Request, Request, Response.CompleteListener> pushListener = request.getPushListener();
    if (pushListener != null) {
        Response.CompleteListener listener = pushListener.apply(request, pushRequest);
        if (listener != null) {
            HttpChannelOverHTTP2 pushChannel = getHttpChannel().getHttpConnection().newHttpChannel(true);
            List<Response.ResponseListener> listeners = Collections.singletonList(listener);
            HttpExchange pushExchange = new HttpExchange(getHttpDestination(), pushRequest, listeners);
            pushChannel.associate(pushExchange);
            pushChannel.setStream(stream);
            // TODO: idle timeout ?
            pushExchange.requestComplete(null);
            pushExchange.terminateRequest();
            return pushChannel.getStreamListener();
        }
    }
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
    return null;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpExchange(org.eclipse.jetty.client.HttpExchange) Request(org.eclipse.jetty.client.api.Request) HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame)

Example 4 with Frame

use of org.eclipse.jetty.http2.frames.Frame 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 5 with Frame

use of org.eclipse.jetty.http2.frames.Frame 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)

Aggregations

Test (org.junit.Test)124 HttpFields (org.eclipse.jetty.http.HttpFields)107 MetaData (org.eclipse.jetty.http.MetaData)107 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)107 CountDownLatch (java.util.concurrent.CountDownLatch)104 Stream (org.eclipse.jetty.http2.api.Stream)99 Session (org.eclipse.jetty.http2.api.Session)94 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)85 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)60 ByteBuffer (java.nio.ByteBuffer)59 Callback (org.eclipse.jetty.util.Callback)53 HttpServletResponse (javax.servlet.http.HttpServletResponse)51 Promise (org.eclipse.jetty.util.Promise)49 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)43 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)39 IOException (java.io.IOException)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)38 ServletException (javax.servlet.ServletException)35 HashMap (java.util.HashMap)34