Search in sources :

Example 26 with SettingsFrame

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

the class FlowControlStrategyTest method testFlowControlWithConcurrentSettings.

@Test
public void testFlowControlWithConcurrentSettings() throws Exception {
    // Initial window is 64 KiB. We allow the client to send 1024 B
    // then we change the window to 512 B. At this point, the client
    // must stop sending data (although the initial window allows it).
    final int size = 512;
    // We get 3 data frames: the first of 1024 and 2 of 512 each
    // after the flow control window has been reduced.
    final CountDownLatch dataLatch = new CountDownLatch(3);
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame requestFrame) {
            HttpFields fields = new HttpFields();
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return new Stream.Listener.Adapter() {

                private final AtomicInteger dataFrames = new AtomicInteger();

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    dataLatch.countDown();
                    int dataFrameCount = dataFrames.incrementAndGet();
                    if (dataFrameCount == 1) {
                        callbackRef.set(callback);
                        Map<Integer, Integer> settings = new HashMap<>();
                        settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, size);
                        stream.getSession().settings(new SettingsFrame(settings, false), Callback.NOOP);
                    // Do not succeed the callback here.
                    } else if (dataFrameCount > 1) {
                        // Consume the data.
                        callback.succeeded();
                    }
                }
            };
        }
    });
    // Two SETTINGS frames, the initial one and the one we send from the server.
    final CountDownLatch settingsLatch = new CountDownLatch(2);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onSettings(Session session, SettingsFrame frame) {
            settingsLatch.countDown();
        }
    });
    MetaData.Request request = newRequest("POST", new HttpFields());
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(new HeadersFrame(request, null, false), promise, new Stream.Listener.Adapter());
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    // Send first chunk that exceeds the window.
    Callback.Completable completable = new Callback.Completable();
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(size * 2), false), completable);
    settingsLatch.await(5, TimeUnit.SECONDS);
    completable.thenRun(() -> {
        // Send the second chunk of data, must not arrive since we're flow control stalled on the client.
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(size * 2), true), Callback.NOOP);
    });
    Assert.assertFalse(dataLatch.await(1, TimeUnit.SECONDS));
    // Consume the data arrived to server, this will resume flow control on the client.
    callbackRef.get().succeeded();
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) 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) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 27 with SettingsFrame

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

the class HTTP2ServerConnection method upgrade.

public boolean upgrade(Request request) {
    if (HttpMethod.PRI.is(request.getMethod())) {
        getParser().directUpgrade();
    } else {
        HttpField settingsField = request.getFields().getField(HttpHeader.HTTP2_SETTINGS);
        if (settingsField == null)
            throw new BadMessageException("Missing " + HttpHeader.HTTP2_SETTINGS + " header");
        String value = settingsField.getValue();
        final byte[] settings = B64Code.decodeRFC4648URL(value == null ? "" : value);
        if (LOG.isDebugEnabled())
            LOG.debug("{} settings {}", this, TypeUtil.toHexString(settings));
        SettingsFrame settingsFrame = SettingsBodyParser.parseBody(BufferUtil.toBuffer(settings));
        if (settingsFrame == null) {
            LOG.warn("Invalid {} header value: {}", HttpHeader.HTTP2_SETTINGS, value);
            throw new BadMessageException();
        }
        getParser().standardUpgrade();
        upgradeFrames.add(new PrefaceFrame());
        upgradeFrames.add(settingsFrame);
        // Remember the request to send a response from onOpen().
        upgradeFrames.add(new HeadersFrame(1, new Request(request), null, true));
    }
    return true;
}
Also used : PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HttpField(org.eclipse.jetty.http.HttpField) BadMessageException(org.eclipse.jetty.http.BadMessageException) Request(org.eclipse.jetty.http.MetaData.Request) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame)

Example 28 with SettingsFrame

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

the class HTTP2ServerSession method onPreface.

@Override
public void onPreface() {
    // SPEC: send a SETTINGS frame upon receiving the preface.
    Map<Integer, Integer> settings = notifyPreface(this);
    if (settings == null)
        settings = Collections.emptyMap();
    SettingsFrame settingsFrame = new SettingsFrame(settings, false);
    WindowUpdateFrame windowFrame = null;
    int sessionWindow = getInitialSessionRecvWindow() - FlowControlStrategy.DEFAULT_WINDOW_SIZE;
    if (sessionWindow > 0) {
        updateRecvWindow(sessionWindow);
        windowFrame = new WindowUpdateFrame(0, sessionWindow);
    }
    if (windowFrame == null)
        frames(null, Callback.NOOP, settingsFrame, Frame.EMPTY_ARRAY);
    else
        frames(null, Callback.NOOP, settingsFrame, windowFrame);
}
Also used : SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) WindowUpdateFrame(org.eclipse.jetty.http2.frames.WindowUpdateFrame) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 29 with SettingsFrame

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

the class HTTP2CServerTest method testHTTP_2_0_Direct.

@Test
public void testHTTP_2_0_Direct() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    byteBufferPool = new MappedByteBufferPool();
    generator = new Generator(byteBufferPool);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:" + connector.getLocalPort()), "/test", HttpVersion.HTTP_2, 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));
        }
        final AtomicReference<HeadersFrame> headersRef = new AtomicReference<>();
        final AtomicReference<DataFrame> dataRef = new AtomicReference<>();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                latch.countDown();
            }

            @Override
            public void onHeaders(HeadersFrame frame) {
                headersRef.set(frame);
                latch.countDown();
            }

            @Override
            public void onData(DataFrame frame) {
                dataRef.set(frame);
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        HeadersFrame response = headersRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        DataFrame responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        String s = BufferUtil.toString(responseData.getData());
        assertThat(s, containsString("Hello from Jetty using HTTP/2.0"));
        assertThat(s, containsString("uri=/test"));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) 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) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 30 with SettingsFrame

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

the class HTTP2CServerTest method testHTTP_1_1_Upgrade.

@Test
public void testHTTP_1_1_Upgrade() throws Exception {
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET /one HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: something, else, upgrade, HTTP2-Settings\r\n" + "Upgrade: h2c\r\n" + "HTTP2-Settings: \r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
        output.flush();
        InputStream input = client.getInputStream();
        Utf8StringBuilder upgrade = new Utf8StringBuilder();
        int crlfs = 0;
        while (true) {
            int read = input.read();
            if (read == '\r' || read == '\n')
                ++crlfs;
            else
                crlfs = 0;
            upgrade.append((byte) read);
            if (crlfs == 4)
                break;
        }
        assertTrue(upgrade.toString().startsWith("HTTP/1.1 101 "));
        byteBufferPool = new MappedByteBufferPool();
        generator = new Generator(byteBufferPool);
        final AtomicReference<HeadersFrame> headersRef = new AtomicReference<>();
        final AtomicReference<DataFrame> dataRef = new AtomicReference<>();
        final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>(new CountDownLatch(2));
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                headersRef.set(frame);
                latchRef.get().countDown();
            }

            @Override
            public void onData(DataFrame frame) {
                dataRef.set(frame);
                latchRef.get().countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
        HeadersFrame response = headersRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        DataFrame responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        String content = BufferUtil.toString(responseData.getData());
        // The upgrade request is seen as HTTP/1.1.
        assertThat(content, containsString("Hello from Jetty using HTTP/1.1"));
        assertThat(content, containsString("uri=/one"));
        // Send a HTTP/2 request.
        headersRef.set(null);
        dataRef.set(null);
        latchRef.set(new CountDownLatch(2));
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:" + connector.getLocalPort()), "/two", HttpVersion.HTTP_2, new HttpFields());
        generator.control(lease, new HeadersFrame(3, metaData, null, true));
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        output.flush();
        parseResponse(client, parser);
        Assert.assertTrue(latchRef.get().await(5, TimeUnit.SECONDS));
        response = headersRef.get();
        Assert.assertNotNull(response);
        responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
        responseData = dataRef.get();
        Assert.assertNotNull(responseData);
        content = BufferUtil.toString(responseData.getData());
        assertThat(content, containsString("Hello from Jetty using HTTP/2.0"));
        assertThat(content, containsString("uri=/two"));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) 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) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) InputStream(java.io.InputStream) Utf8StringBuilder(org.eclipse.jetty.util.Utf8StringBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Parser(org.eclipse.jetty.http2.parser.Parser) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

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