Search in sources :

Example 21 with SettingsFrame

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

the class CloseTest method testClientAbruptlyClosesConnection.

@Test
public void testClientAbruptlyClosesConnection() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final AtomicReference<Session> sessionRef = new AtomicReference<>();
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            try {
                sessionRef.set(stream.getSession());
                MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
                // Reply with HEADERS.
                stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                closeLatch.await(5, TimeUnit.SECONDS);
                return null;
            } catch (InterruptedException x) {
                return null;
            }
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                try {
                    // Close the connection just after
                    // receiving the response headers.
                    client.close();
                    closeLatch.countDown();
                } catch (IOException x) {
                    throw new RuntimeIOException(x);
                }
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        // We need to give some time to the server to receive and process the TCP FIN.
        Thread.sleep(1000);
        Session session = sessionRef.get();
        Assert.assertTrue(session.isClosed());
        Assert.assertTrue(((HTTP2Session) session).isDisconnected());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 22 with SettingsFrame

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

the class CloseTest method testClientSendsGoAwayButDoesNotCloseConnectionServerCloses.

@Test
public void testClientSendsGoAwayButDoesNotCloseConnectionServerCloses() throws Exception {
    final AtomicReference<Session> sessionRef = new AtomicReference<>();
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            sessionRef.set(stream.getSession());
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            return null;
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    generator.control(lease, new GoAwayFrame(1, ErrorCode.NO_ERROR.code, "OK".getBytes("UTF-8")));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        // Don't close the connection; the server should close.
        final CountDownLatch responseLatch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                // Even if we sent the GO_AWAY immediately after the
                // HEADERS, the server is able to send us the response.
                responseLatch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
        // Wait for the server to close.
        Thread.sleep(1000);
        // Client received the TCP FIN from server.
        Assert.assertEquals(-1, client.getInputStream().read());
        // Server is closed.
        Session session = sessionRef.get();
        Assert.assertTrue(session.isClosed());
        Assert.assertTrue(((HTTP2Session) session).isDisconnected());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 23 with SettingsFrame

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

the class StreamCountTest method testServerAllowsOneStreamEnforcedByClient.

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

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
            return settings;
        }

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    if (frame.isEndStream()) {
                        HttpFields fields = new HttpFields();
                        MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
                        stream.headers(new HeadersFrame(stream.getId(), metaData, null, true), callback);
                    } else {
                        callback.succeeded();
                    }
                }
            };
        }
    });
    final CountDownLatch settingsLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onSettings(Session session, SettingsFrame frame) {
            settingsLatch.countDown();
        }
    });
    Assert.assertTrue(settingsLatch.await(5, TimeUnit.SECONDS));
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame1 = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise1 = new FuturePromise<>();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    session.newStream(frame1, streamPromise1, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                responseLatch.countDown();
        }
    });
    Stream stream1 = streamPromise1.get(5, TimeUnit.SECONDS);
    HeadersFrame frame2 = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise2 = new FuturePromise<>();
    session.newStream(frame2, streamPromise2, new Stream.Listener.Adapter());
    try {
        streamPromise2.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected
    }
    stream1.data(new DataFrame(stream1.getId(), BufferUtil.EMPTY_BUFFER, true), Callback.NOOP);
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) 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) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) 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)

Example 24 with SettingsFrame

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

the class HTTP2Session method onSettings.

public void onSettings(SettingsFrame frame, boolean reply) {
    if (LOG.isDebugEnabled())
        LOG.debug("Received {}", frame);
    if (frame.isReply())
        return;
    // Iterate over all settings
    for (Map.Entry<Integer, Integer> entry : frame.getSettings().entrySet()) {
        int key = entry.getKey();
        int value = entry.getValue();
        switch(key) {
            case SettingsFrame.HEADER_TABLE_SIZE:
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Update HPACK header table size to {} for {}", value, this);
                    generator.setHeaderTableSize(value);
                    break;
                }
            case SettingsFrame.ENABLE_PUSH:
                {
                    // SPEC: check the value is sane.
                    if (value != 0 && value != 1) {
                        onConnectionFailure(ErrorCode.PROTOCOL_ERROR.code, "invalid_settings_enable_push");
                        return;
                    }
                    pushEnabled = value == 1;
                    if (LOG.isDebugEnabled())
                        LOG.debug("{} push for {}", pushEnabled ? "Enable" : "Disable", this);
                    break;
                }
            case SettingsFrame.MAX_CONCURRENT_STREAMS:
                {
                    maxLocalStreams = value;
                    if (LOG.isDebugEnabled())
                        LOG.debug("Update max local concurrent streams to {} for {}", maxLocalStreams, this);
                    break;
                }
            case SettingsFrame.INITIAL_WINDOW_SIZE:
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Update initial window size to {} for {}", value, this);
                    flowControl.updateInitialStreamWindow(this, value, false);
                    break;
                }
            case SettingsFrame.MAX_FRAME_SIZE:
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Update max frame size to {} for {}", value, this);
                    // SPEC: check the max frame size is sane.
                    if (value < Frame.DEFAULT_MAX_LENGTH || value > Frame.MAX_MAX_LENGTH) {
                        onConnectionFailure(ErrorCode.PROTOCOL_ERROR.code, "invalid_settings_max_frame_size");
                        return;
                    }
                    generator.setMaxFrameSize(value);
                    break;
                }
            case SettingsFrame.MAX_HEADER_LIST_SIZE:
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Update max header list size to {} for {}", value, this);
                    generator.setMaxHeaderListSize(value);
                    break;
                }
            default:
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Unknown setting {}:{} for {}", key, value, this);
                    break;
                }
        }
    }
    notifySettings(this, frame);
    if (reply) {
        SettingsFrame replyFrame = new SettingsFrame(Collections.emptyMap(), true);
        settings(replyFrame, Callback.NOOP);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 25 with SettingsFrame

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

the class SettingsBodyParser method onSettings.

protected boolean onSettings(Map<Integer, Integer> settings) {
    SettingsFrame frame = new SettingsFrame(settings, hasFlag(Flags.ACK));
    reset();
    notifySettings(frame);
    return true;
}
Also used : SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame)

Aggregations

SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)33 HashMap (java.util.HashMap)29 Test (org.junit.Test)29 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)26 HttpFields (org.eclipse.jetty.http.HttpFields)25 MetaData (org.eclipse.jetty.http.MetaData)25 ByteBuffer (java.nio.ByteBuffer)22 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)22 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)20 PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 Parser (org.eclipse.jetty.http2.parser.Parser)15 Session (org.eclipse.jetty.http2.api.Session)14 OutputStream (java.io.OutputStream)12 Socket (java.net.Socket)12 Stream (org.eclipse.jetty.http2.api.Stream)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 Map (java.util.Map)10 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)10 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)9