Search in sources :

Example 41 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method consumeDataFrame.

private void consumeDataFrame(final RawFrame frame, final H2Stream stream) throws HttpException, IOException {
    final int streamId = stream.getId();
    final ByteBuffer payload = frame.getPayloadContent();
    if (payload != null) {
        final int frameLength = frame.getLength();
        final int streamWinSize = updateInputWindow(streamId, stream.getInputWindow(), -frameLength);
        if (streamWinSize < lowMark && !stream.isRemoteClosed()) {
            stream.produceInputCapacityUpdate();
        }
        final int connWinSize = updateInputWindow(0, connInputWindow, -frameLength);
        if (connWinSize < CONNECTION_WINDOW_LOW_MARK) {
            maximizeConnWindow(connWinSize);
        }
    }
    if (stream.isRemoteClosed()) {
        throw new H2StreamResetException(H2Error.STREAM_CLOSED, "Stream already closed");
    }
    if (frame.isFlagSet(FrameFlag.END_STREAM)) {
        stream.setRemoteEndStream();
    }
    if (stream.isLocalReset()) {
        return;
    }
    stream.consumeData(payload);
}
Also used : H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) ByteBuffer(java.nio.ByteBuffer)

Example 42 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method streamDataFrame.

private void streamDataFrame(final int streamId, final AtomicInteger streamOutputWindow, final ByteBuffer payload, final int chunk) throws IOException {
    final RawFrame dataFrame = frameFactory.createData(streamId, payload, false);
    if (streamListener != null) {
        streamListener.onFrameOutput(this, streamId, dataFrame);
    }
    updateOutputWindow(0, connOutputWindow, -chunk);
    updateOutputWindow(streamId, streamOutputWindow, -chunk);
    outputBuffer.write(dataFrame, ioSession);
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame)

Example 43 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method onConnect.

public final void onConnect() throws HttpException, IOException {
    connState = ConnectionHandshake.ACTIVE;
    final RawFrame settingsFrame = frameFactory.createSettings(new H2Setting(H2Param.HEADER_TABLE_SIZE, localConfig.getHeaderTableSize()), new H2Setting(H2Param.ENABLE_PUSH, localConfig.isPushEnabled() ? 1 : 0), new H2Setting(H2Param.MAX_CONCURRENT_STREAMS, localConfig.getMaxConcurrentStreams()), new H2Setting(H2Param.INITIAL_WINDOW_SIZE, localConfig.getInitialWindowSize()), new H2Setting(H2Param.MAX_FRAME_SIZE, localConfig.getMaxFrameSize()), new H2Setting(H2Param.MAX_HEADER_LIST_SIZE, localConfig.getMaxHeaderListSize()));
    commitFrame(settingsFrame);
    localSettingState = SettingsHandshake.TRANSMITTED;
    maximizeConnWindow(connInputWindow.get());
    if (streamListener != null) {
        final int initInputWindow = connInputWindow.get();
        streamListener.onInputFlowControl(this, 0, initInputWindow, initInputWindow);
        final int initOutputWindow = connOutputWindow.get();
        streamListener.onOutputFlowControl(this, 0, initOutputWindow, initOutputWindow);
    }
}
Also used : H2Setting(org.apache.hc.core5.http2.config.H2Setting) RawFrame(org.apache.hc.core5.http2.frame.RawFrame)

Example 44 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame in project httpcomponents-core by apache.

the class H2TlsAlpnRequestExecutionExample method main.

public static void main(final String[] args) throws Exception {
    // Create and start requester
    final H2Config h2Config = H2Config.custom().setPushEnabled(false).build();
    final HttpAsyncRequester requester = H2RequesterBootstrap.bootstrap().setH2Config(h2Config).setTlsStrategy(new H2ClientTlsStrategy(SSLContexts.createSystemDefault(), (endpoint, sslEngine) -> {
        // ====
        return null;
    })).setStreamListener(new H2StreamListener() {

        @Override
        public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
            for (int i = 0; i < headers.size(); i++) {
                System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
            }
        }

        @Override
        public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
            for (int i = 0; i < headers.size(); i++) {
                System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
            }
        }

        @Override
        public void onFrameInput(final HttpConnection connection, final int streamId, final RawFrame frame) {
        }

        @Override
        public void onFrameOutput(final HttpConnection connection, final int streamId, final RawFrame frame) {
        }

        @Override
        public void onInputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
        }

        @Override
        public void onOutputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
        }
    }).create();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.out.println("HTTP requester shutting down");
        requester.close(CloseMode.GRACEFUL);
    }));
    requester.start();
    final HttpHost target = new HttpHost("https", "nghttp2.org", 443);
    final String[] requestUris = new String[] { "/httpbin/ip", "/httpbin/user-agent", "/httpbin/headers" };
    final CountDownLatch latch = new CountDownLatch(requestUris.length);
    for (final String requestUri : requestUris) {
        final Future<AsyncClientEndpoint> future = requester.connect(target, Timeout.ofSeconds(5));
        final AsyncClientEndpoint clientEndpoint = future.get();
        clientEndpoint.execute(AsyncRequestBuilder.get().setHttpHost(target).setPath(requestUri).build(), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, String>>() {

            @Override
            public void completed(final Message<HttpResponse, String> message) {
                clientEndpoint.releaseAndReuse();
                final HttpResponse response = message.getHead();
                final String body = message.getBody();
                System.out.println(requestUri + "->" + response.getCode() + " " + response.getVersion());
                System.out.println(body);
                latch.countDown();
            }

            @Override
            public void failed(final Exception ex) {
                clientEndpoint.releaseAndDiscard();
                System.out.println(requestUri + "->" + ex);
                latch.countDown();
            }

            @Override
            public void cancelled() {
                clientEndpoint.releaseAndDiscard();
                System.out.println(requestUri + " cancelled");
                latch.countDown();
            }
        });
    }
    latch.await();
    System.out.println("Shutting down I/O reactor");
    requester.initiateShutdown();
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Message(org.apache.hc.core5.http.Message) SSLContexts(org.apache.hc.core5.ssl.SSLContexts) H2Config(org.apache.hc.core5.http2.config.H2Config) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) H2RequesterBootstrap(org.apache.hc.core5.http2.impl.nio.bootstrap.H2RequesterBootstrap) Header(org.apache.hc.core5.http.Header) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Timeout(org.apache.hc.core5.util.Timeout) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Future(java.util.concurrent.Future) HttpHost(org.apache.hc.core5.http.HttpHost) HttpConnection(org.apache.hc.core5.http.HttpConnection) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) H2StreamListener(org.apache.hc.core5.http2.impl.nio.H2StreamListener) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester) CloseMode(org.apache.hc.core5.io.CloseMode) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) HttpResponse(org.apache.hc.core5.http.HttpResponse) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) Message(org.apache.hc.core5.http.Message) HttpConnection(org.apache.hc.core5.http.HttpConnection) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) H2StreamListener(org.apache.hc.core5.http2.impl.nio.H2StreamListener) HttpHost(org.apache.hc.core5.http.HttpHost) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) List(java.util.List) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) HttpResponse(org.apache.hc.core5.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) Header(org.apache.hc.core5.http.Header) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) H2Config(org.apache.hc.core5.http2.config.H2Config)

Aggregations

RawFrame (org.apache.hc.core5.http2.frame.RawFrame)38 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.jupiter.api.Test)15 Header (org.apache.hc.core5.http.Header)10 HttpConnection (org.apache.hc.core5.http.HttpConnection)9 H2StreamListener (org.apache.hc.core5.http2.impl.nio.H2StreamListener)9 HttpResponse (org.apache.hc.core5.http.HttpResponse)8 List (java.util.List)7 HttpAsyncRequester (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester)7 H2Config (org.apache.hc.core5.http2.config.H2Config)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Message (org.apache.hc.core5.http.Message)6 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)6 H2ConnectionException (org.apache.hc.core5.http2.H2ConnectionException)6 Map (java.util.Map)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 AsyncClientEndpoint (org.apache.hc.core5.http.nio.AsyncClientEndpoint)5 H2StreamResetException (org.apache.hc.core5.http2.H2StreamResetException)5