Search in sources :

Example 1 with HasHeaderFragment

use of com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment 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(), f.isEndOfStream());
        fullHeaders.setStreamId(f.getStreamId());
        fullHeaders.setPriorityDetails(f.getPriorityDetails());
        fullHeaders.setEndOfStream(f.isEndOfStream());
        state.addParsedMessage(fullHeaders);
    } else if (firstFrame instanceof PushPromiseFrame) {
        PushPromiseFrame f = (PushPromiseFrame) firstFrame;
        Http2Push fullHeaders = new Http2Push(headers);
        fullHeaders.setStreamId(f.getStreamId());
        fullHeaders.setPromisedStreamId(f.getPromisedStreamId());
        state.addParsedMessage(fullHeaders);
    }
    hasHeaderFragmentList.clear();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HasHeaderFragment(com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment) HashMap(java.util.HashMap) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push) PushPromiseFrame(com.webpieces.http2.api.dto.lowlevel.PushPromiseFrame) HeadersFrame(com.webpieces.http2.api.dto.lowlevel.HeadersFrame)

Example 2 with HasHeaderFragment

use of com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment in project webpieces by deanhiller.

the class HpackParserImpl method processFrameImpl.

private void processFrameImpl(UnmarshalStateImpl state, Http2Frame frame) {
    List<HasHeaderFragment> headerFragList = state.getHeadersToCombine();
    if (frame instanceof HasHeaderFragment) {
        HasHeaderFragment headerFrame = (HasHeaderFragment) frame;
        headerFragList.add(headerFrame);
        validateHeader(state, headerFrame);
        if (headerFrame.isEndHeaders())
            combineAndSendHeadersToClient(state);
        return;
    } else if (headerFragList.size() > 0) {
        throw new ConnectionException(CancelReasonCode.HEADERS_MIXED_WITH_FRAMES, state.getLogId(), frame.getStreamId(), "Parser in the middle of accepting headers(spec " + "doesn't allow frames between header fragments).  frame=" + frame + " list=" + headerFragList);
    }
    if (frame instanceof UnknownFrame && ignoreUnkownFrames) {
    // do nothing
    } else if (frame instanceof Http2Msg) {
        state.addParsedMessage((Http2Msg) frame);
    } else {
        throw new IllegalStateException("bug forgot support for frame=" + frame);
    }
}
Also used : HasHeaderFragment(com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment) UnknownFrame(com.webpieces.http2.api.dto.lowlevel.UnknownFrame) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException)

Example 3 with HasHeaderFragment

use of com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment 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.http2.api.dto.lowlevel.lib.HasHeaderFragment) ContinuationFrame(com.webpieces.http2.api.dto.lowlevel.ContinuationFrame) PushPromiseFrame(com.webpieces.http2.api.dto.lowlevel.PushPromiseFrame) HeadersFrame(com.webpieces.http2.api.dto.lowlevel.HeadersFrame) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException)

Example 4 with HasHeaderFragment

use of com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment in project webpieces by deanhiller.

the class HeaderEncoding method createHeaderFrames.

private List<Http2Frame> createHeaderFrames(HasHeaderFragment initialFrame, List<Http2Header> headers, Encoder encoder, long maxFrameSize) {
    int maxSize = (int) maxFrameSize;
    if (maxFrameSize > Integer.MAX_VALUE)
        throw new IllegalStateException("max frame size too large for this hpack library");
    List<Http2Frame> headerFrames = new LinkedList<>();
    DataWrapper serializedHeaders = serializeHeaders(encoder, headers);
    HasHeaderFragment currentFrame = initialFrame;
    HasHeaderFragment lastFrame = currentFrame;
    DataWrapper dataLeftOver = serializedHeaders;
    while (dataLeftOver.getReadableSize() > 0) {
        lastFrame = currentFrame;
        int splitSize = Math.min(dataLeftOver.getReadableSize(), maxSize);
        List<? extends DataWrapper> split = dataGen.split(dataLeftOver, splitSize);
        DataWrapper fragment = split.get(0);
        currentFrame.setHeaderFragment(fragment);
        headerFrames.add(currentFrame);
        currentFrame = new ContinuationFrame();
        currentFrame.setStreamId(initialFrame.getStreamId());
        dataLeftOver = split.get(1);
    }
    // last frame is currentFrame so set end header
    lastFrame.setEndHeaders(true);
    return headerFrames;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HasHeaderFragment(com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment) ContinuationFrame(com.webpieces.http2.api.dto.lowlevel.ContinuationFrame) Http2Frame(com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame) LinkedList(java.util.LinkedList)

Aggregations

HasHeaderFragment (com.webpieces.http2.api.dto.lowlevel.lib.HasHeaderFragment)4 ConnectionException (com.webpieces.http2.api.dto.error.ConnectionException)2 ContinuationFrame (com.webpieces.http2.api.dto.lowlevel.ContinuationFrame)2 HeadersFrame (com.webpieces.http2.api.dto.lowlevel.HeadersFrame)2 PushPromiseFrame (com.webpieces.http2.api.dto.lowlevel.PushPromiseFrame)2 DataWrapper (org.webpieces.data.api.DataWrapper)2 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)1 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)1 UnknownFrame (com.webpieces.http2.api.dto.lowlevel.UnknownFrame)1 Http2Frame (com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)1 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1