Search in sources :

Example 1 with HeadersFrame

use of com.webpieces.http2parser.api.dto.HeadersFrame in project webpieces by deanhiller.

the class HeadersMarshaller method unmarshal.

@Override
public AbstractHttp2Frame unmarshal(Http2MementoImpl state, DataWrapper framePayloadData) {
    HeadersFrame frame = new HeadersFrame();
    super.unmarshalFrame(state, frame);
    byte flagsByte = state.getFrameHeaderData().getFlagsByte();
    frame.setEndOfStream((flagsByte & 0x1) == 0x1);
    frame.setEndHeaders((flagsByte & 0x4) == 0x4);
    boolean isPadded = (flagsByte & 0x8) == 0x8;
    PriorityDetails priorityDetails = null;
    if ((flagsByte & 0x20) == 0x20) {
        priorityDetails = new PriorityDetails();
        frame.setPriorityDetails(priorityDetails);
    }
    DataSplit padSplit = PaddingUtil.extractPayloadAndPadding(isPadded, framePayloadData, frame.getStreamId());
    frame.setPadding(padSplit.getPadding());
    DataWrapper paddingStripped = padSplit.getPayload();
    if (priorityDetails != null) {
        //1 bit Exclusive flag, 31 bits stream dependency, and 8 bits weight = 5 bytes....
        List<? extends DataWrapper> split = dataGen.split(paddingStripped, 5);
        ByteBuffer preludeBytes = bufferPool.createWithDataWrapper(split.get(0));
        int firstInt = preludeBytes.getInt();
        priorityDetails.setStreamDependencyIsExclusive((firstInt >>> 31) == 0x1);
        int streamDependency = firstInt & 0x7FFFFFFF;
        if (streamDependency == frame.getStreamId()) {
            // Can't depend on self
            throw new ConnectionException(CancelReasonCode.BAD_STREAM_DEPENDENCY, streamDependency, "stream id=" + streamDependency + " depends on itself");
        }
        priorityDetails.setStreamDependency(streamDependency);
        priorityDetails.setWeight((short) (preludeBytes.get() & 0xFF));
        frame.setHeaderFragment(split.get(1));
        bufferPool.releaseBuffer(preludeBytes);
    } else {
        frame.setHeaderFragment(paddingStripped);
    }
    if (frame.getStreamId() == 0)
        throw new ConnectionException(CancelReasonCode.INVALID_STREAM_ID, frame.getStreamId(), "headers frame had invalid stream id=" + frame.getStreamId());
    return frame;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) PriorityDetails(com.webpieces.http2parser.api.dto.lib.PriorityDetails) HeadersFrame(com.webpieces.http2parser.api.dto.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) DataSplit(com.webpieces.http2parser.impl.DataSplit) ConnectionException(com.webpieces.http2parser.api.dto.error.ConnectionException)

Example 2 with HeadersFrame

use of com.webpieces.http2parser.api.dto.HeadersFrame in project webpieces by deanhiller.

the class HeadersMarshaller method marshal.

@Override
public DataWrapper marshal(Http2Frame frame) {
    HeadersFrame castFrame = (HeadersFrame) frame;
    if (frame.getStreamId() == 0)
        throw new ConnectionException(CancelReasonCode.INVALID_STREAM_ID, frame.getStreamId(), "Headers frame cannot be streamId 0");
    int paddingSize = castFrame.getPadding().getReadableSize();
    byte value = 0x0;
    if (castFrame.isEndOfStream())
        value |= 0x1;
    if (castFrame.isEndHeaders())
        value |= 0x4;
    if (paddingSize > 0)
        value |= 0x8;
    if (castFrame.isPriority())
        value |= 0x20;
    DataWrapper preludeDW;
    PriorityDetails priorityDetails = castFrame.getPriorityDetails();
    if (priorityDetails != null) {
        preludeDW = PriorityMarshaller.marshalPriorityDetails(bufferPool, priorityDetails, frame);
    } else {
        preludeDW = dataGen.emptyWrapper();
    }
    DataWrapper unpadded = dataGen.chainDataWrappers(preludeDW, castFrame.getHeaderFragment());
    DataWrapper payload = PaddingUtil.padDataIfNeeded(unpadded, castFrame.getPadding());
    return super.marshalFrame(frame, value, payload);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) PriorityDetails(com.webpieces.http2parser.api.dto.lib.PriorityDetails) HeadersFrame(com.webpieces.http2parser.api.dto.HeadersFrame) ConnectionException(com.webpieces.http2parser.api.dto.error.ConnectionException)

Example 3 with HeadersFrame

use of com.webpieces.http2parser.api.dto.HeadersFrame in project webpieces by deanhiller.

the class HpackParserImpl method combineAndSendHeadersToClient.

private void combineAndSendHeadersToClient(UnmarshalStateImpl state) {
    List<HasHeaderFragment> hasHeaderFragmentList = state.getHeadersToCombine();
    // Now we set the full header list on the first frame and just return that
    HasHeaderFragment firstFrame = hasHeaderFragmentList.get(0);
    DataWrapper allSerializedHeaders = dataGen.emptyWrapper();
    for (HasHeaderFragment iterFrame : hasHeaderFragmentList) {
        allSerializedHeaders = dataGen.chainDataWrappers(allSerializedHeaders, iterFrame.getHeaderFragment());
    }
    Map<Http2HeaderName, Http2Header> knownHeaders = new HashMap<>();
    List<Http2Header> headers = decoding.decode(state, allSerializedHeaders, firstFrame.getStreamId(), header -> knownHeaders.put(header.getKnownName(), header));
    if (firstFrame instanceof HeadersFrame) {
        HeadersFrame f = (HeadersFrame) firstFrame;
        Http2Headers fullHeaders = createCorrectType(knownHeaders, headers, state.getLogId(), f.getStreamId());
        fullHeaders.setStreamId(f.getStreamId());
        fullHeaders.setPriorityDetails(f.getPriorityDetails());
        fullHeaders.setEndOfStream(f.isEndOfStream());
        state.getParsedFrames().add(fullHeaders);
    } else if (firstFrame instanceof PushPromiseFrame) {
        PushPromiseFrame f = (PushPromiseFrame) firstFrame;
        Http2Push fullHeaders = new Http2Push(headers);
        fullHeaders.setStreamId(f.getStreamId());
        fullHeaders.setPromisedStreamId(f.getPromisedStreamId());
        state.getParsedFrames().add(fullHeaders);
    }
    hasHeaderFragmentList.clear();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HasHeaderFragment(com.webpieces.http2parser.api.dto.lib.HasHeaderFragment) HashMap(java.util.HashMap) Http2Headers(com.webpieces.hpack.api.dto.Http2Headers) Http2HeaderName(com.webpieces.http2parser.api.dto.lib.Http2HeaderName) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) Http2Push(com.webpieces.hpack.api.dto.Http2Push) PushPromiseFrame(com.webpieces.http2parser.api.dto.PushPromiseFrame) HeadersFrame(com.webpieces.http2parser.api.dto.HeadersFrame)

Example 4 with HeadersFrame

use of com.webpieces.http2parser.api.dto.HeadersFrame in project webpieces by deanhiller.

the class HeaderEncoding method translateToFrames.

public List<Http2Frame> translateToFrames(long maxFrameSize, Encoder encoder, Http2Headers headers) {
    HeadersFrame frame = new HeadersFrame();
    frame.setStreamId(headers.getStreamId());
    frame.setEndOfStream(headers.isEndOfStream());
    frame.setPriorityDetails(headers.getPriorityDetails());
    List<Http2Header> headerList = headers.getHeaders();
    List<Http2Frame> headerFrames = toHeaderFrames(maxFrameSize, encoder, frame, headerList);
    return headerFrames;
}
Also used : Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HeadersFrame(com.webpieces.http2parser.api.dto.HeadersFrame) Http2Frame(com.webpieces.http2parser.api.dto.lib.Http2Frame)

Example 5 with HeadersFrame

use of com.webpieces.http2parser.api.dto.HeadersFrame in project webpieces by deanhiller.

the class HpackParserImpl method validateHeader.

private void validateHeader(UnmarshalStateImpl state, HasHeaderFragment lowLevelFrame) {
    List<HasHeaderFragment> list = state.getHeadersToCombine();
    HasHeaderFragment first = list.get(0);
    int streamId = first.getStreamId();
    if (first instanceof PushPromiseFrame) {
        PushPromiseFrame f = (PushPromiseFrame) first;
        streamId = f.getPromisedStreamId();
    }
    String logId = state.getLogId();
    if (list.size() == 1) {
        if (!(first instanceof HeadersFrame) && !(first instanceof PushPromiseFrame))
            throw new ConnectionException(CancelReasonCode.HEADERS_MIXED_WITH_FRAMES, logId, lowLevelFrame.getStreamId(), "First has header frame must be HeadersFrame or PushPromiseFrame first frame=" + first);
    } else if (streamId != lowLevelFrame.getStreamId()) {
        throw new ConnectionException(CancelReasonCode.HEADERS_MIXED_WITH_FRAMES, logId, lowLevelFrame.getStreamId(), "Headers/continuations from two different streams per spec cannot be" + " interleaved.  frames=" + list);
    } else if (!(lowLevelFrame instanceof ContinuationFrame)) {
        throw new ConnectionException(CancelReasonCode.HEADERS_MIXED_WITH_FRAMES, logId, lowLevelFrame.getStreamId(), "Must be continuation frame and wasn't.  frames=" + list);
    }
}
Also used : HasHeaderFragment(com.webpieces.http2parser.api.dto.lib.HasHeaderFragment) ContinuationFrame(com.webpieces.http2parser.api.dto.ContinuationFrame) PushPromiseFrame(com.webpieces.http2parser.api.dto.PushPromiseFrame) HeadersFrame(com.webpieces.http2parser.api.dto.HeadersFrame) ConnectionException(com.webpieces.http2parser.api.dto.error.ConnectionException)

Aggregations

HeadersFrame (com.webpieces.http2parser.api.dto.HeadersFrame)5 ConnectionException (com.webpieces.http2parser.api.dto.error.ConnectionException)3 DataWrapper (org.webpieces.data.api.DataWrapper)3 PushPromiseFrame (com.webpieces.http2parser.api.dto.PushPromiseFrame)2 HasHeaderFragment (com.webpieces.http2parser.api.dto.lib.HasHeaderFragment)2 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)2 PriorityDetails (com.webpieces.http2parser.api.dto.lib.PriorityDetails)2 Http2Headers (com.webpieces.hpack.api.dto.Http2Headers)1 Http2Push (com.webpieces.hpack.api.dto.Http2Push)1 ContinuationFrame (com.webpieces.http2parser.api.dto.ContinuationFrame)1 Http2Frame (com.webpieces.http2parser.api.dto.lib.Http2Frame)1 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)1 DataSplit (com.webpieces.http2parser.impl.DataSplit)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1