Search in sources :

Example 21 with RawFrame

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

the class FrameOutputBuffer method write.

public void write(final RawFrame frame, final OutputStream outStream) throws IOException {
    if (frame == null) {
        return;
    }
    final int type = frame.getType();
    final long streamId = frame.getStreamId();
    final int flags = frame.getFlags();
    final ByteBuffer payload = frame.getPayload();
    final int payloadLen = payload != null ? payload.remaining() : 0;
    if (payload != null && payload.remaining() > maxFramePayloadSize) {
        throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, "Frame size exceeds maximum");
    }
    buffer[0] = (byte) (payloadLen >> 16 & 0xff);
    buffer[1] = (byte) (payloadLen >> 8 & 0xff);
    buffer[2] = (byte) (payloadLen & 0xff);
    buffer[3] = (byte) (type & 0xff);
    buffer[4] = (byte) (flags & 0xff);
    buffer[5] = (byte) (streamId >> 24 & 0xff);
    buffer[6] = (byte) (streamId >> 16 & 0xff);
    buffer[7] = (byte) (streamId >> 8 & 0xff);
    buffer[8] = (byte) (streamId & 0xff);
    int frameLen = FrameConsts.HEAD_LEN;
    int padding = 0;
    if ((flags & FrameFlag.PADDED.getValue()) > 0) {
        padding = 16;
        buffer[9] = (byte) (padding & 0xff);
        frameLen++;
    }
    if (payload != null) {
        payload.get(buffer, frameLen, payload.remaining());
        frameLen += payloadLen;
    }
    for (int i = 0; i < padding; i++) {
        buffer[frameLen++] = 0;
    }
    outStream.write(buffer, 0, frameLen);
    metrics.incrementFramesTransferred();
    metrics.incrementBytesTransferred(frameLen);
}
Also used : H2ConnectionException(org.apache.hc.core5.http2.H2ConnectionException) ByteBuffer(java.nio.ByteBuffer)

Example 22 with RawFrame

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

the class AbstractH2StreamMultiplexer method incrementInputCapacity.

private void incrementInputCapacity(final int streamId, final AtomicInteger inputWindow, final int inputCapacity) throws IOException {
    if (inputCapacity > 0) {
        final int streamWinSize = inputWindow.get();
        final int remainingCapacity = Integer.MAX_VALUE - streamWinSize;
        final int chunk = Math.min(inputCapacity, remainingCapacity);
        if (chunk != 0) {
            final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(streamId, chunk);
            commitFrame(windowUpdateFrame);
            updateInputWindow(streamId, inputWindow, chunk);
        }
    }
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame)

Example 23 with RawFrame

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

the class AbstractH2StreamMultiplexer method consumeContinuationFrame.

private void consumeContinuationFrame(final RawFrame frame, final H2Stream stream) throws HttpException, IOException {
    final int streamId = frame.getStreamId();
    final ByteBuffer payload = frame.getPayload();
    continuation.copyPayload(payload);
    if (frame.isFlagSet(FrameFlag.END_HEADERS)) {
        final List<Header> headers = decodeHeaders(continuation.getContent());
        if (stream.isRemoteInitiated() && streamId > processedRemoteStreamId) {
            processedRemoteStreamId = streamId;
        }
        if (streamListener != null) {
            streamListener.onHeaderInput(this, streamId, headers);
        }
        if (stream.isRemoteClosed()) {
            throw new H2StreamResetException(H2Error.STREAM_CLOSED, "Stream already closed");
        }
        if (stream.isLocalReset()) {
            return;
        }
        if (continuation.endStream) {
            stream.setRemoteEndStream();
        }
        if (continuation.type == FrameType.PUSH_PROMISE.getValue()) {
            stream.consumePromise(headers);
        } else {
            stream.consumeHeader(headers);
        }
        continuation = null;
    }
}
Also used : Header(org.apache.hc.core5.http.Header) H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) ByteBuffer(java.nio.ByteBuffer)

Example 24 with RawFrame

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

the class AbstractH2StreamMultiplexer method onInput.

public final void onInput(final ByteBuffer src) throws HttpException, IOException {
    if (connState == ConnectionHandshake.SHUTDOWN) {
        ioSession.clearEvent(SelectionKey.OP_READ);
    } else {
        for (; ; ) {
            final RawFrame frame = inputBuffer.read(src, ioSession);
            if (frame == null) {
                break;
            }
            if (streamListener != null) {
                streamListener.onFrameInput(this, frame.getStreamId(), frame);
            }
            consumeFrame(frame);
        }
    }
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame)

Example 25 with RawFrame

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

the class AbstractH2StreamMultiplexer method consumeHeaderFrame.

private void consumeHeaderFrame(final RawFrame frame, final H2Stream stream) throws HttpException, IOException {
    final int streamId = stream.getId();
    if (!frame.isFlagSet(FrameFlag.END_HEADERS)) {
        continuation = new Continuation(streamId, frame.getType(), frame.isFlagSet(FrameFlag.END_STREAM));
    }
    final ByteBuffer payload = frame.getPayloadContent();
    if (frame.isFlagSet(FrameFlag.PRIORITY)) {
        // Priority not supported
        payload.getInt();
        payload.get();
    }
    if (continuation == null) {
        final List<Header> headers = decodeHeaders(payload);
        if (stream.isRemoteInitiated() && streamId > processedRemoteStreamId) {
            processedRemoteStreamId = streamId;
        }
        if (streamListener != null) {
            streamListener.onHeaderInput(this, streamId, headers);
        }
        if (stream.isRemoteClosed()) {
            throw new H2StreamResetException(H2Error.STREAM_CLOSED, "Stream already closed");
        }
        if (stream.isLocalReset()) {
            return;
        }
        if (frame.isFlagSet(FrameFlag.END_STREAM)) {
            stream.setRemoteEndStream();
        }
        stream.consumeHeader(headers);
    } else {
        continuation.copyPayload(payload);
    }
}
Also used : Header(org.apache.hc.core5.http.Header) H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) ByteBuffer(java.nio.ByteBuffer)

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