Search in sources :

Example 6 with AbstractHttp2Frame

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

the class Http2ParserImpl method parseBody.

private boolean parseBody(Http2MementoImpl state) {
    DataWrapper allData = state.getLeftOverData();
    FrameHeaderData headerData = state.getFrameHeaderData();
    if (headerData == null)
        throw new IllegalArgumentException("Bug, this should never be null at this point");
    int payloadLen = headerData.getPayloadLength();
    if (allData.getReadableSize() < payloadLen)
        return false;
    List<? extends DataWrapper> split = dataGen.split(allData, payloadLen);
    DataWrapper framePayloadData = split.get(0);
    AbstractHttp2Frame frame;
    Optional<Http2FrameType> optFrameType = Http2FrameType.fromId(headerData.getFrameTypeId());
    if (optFrameType.isPresent()) {
        Http2FrameType frameType = optFrameType.get();
        FrameMarshaller marshaller = dtoToMarshaller.get(frameType);
        if (marshaller == null)
            throw new IllegalArgumentException("bug, our developer forgot to add marshaller and only added the enum=" + frameType);
        frame = marshaller.unmarshal(state, framePayloadData);
    } else {
        frame = new UnknownFrame(headerData.getFlagsByte(), headerData.getFrameTypeId(), headerData.getStreamId(), framePayloadData);
    }
    int headerSize = 9;
    int size = framePayloadData.getReadableSize() + headerSize;
    // reset header data
    state.setFrameHeaderData(null);
    // subtract out read message
    state.setLeftOverData(split.get(1), -size);
    state.addParsedFrame(frame);
    return true;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) UnknownFrame(com.webpieces.http2.api.dto.lowlevel.UnknownFrame) FrameMarshaller(com.webpieces.http2parser.impl.marshallers.FrameMarshaller) Http2FrameType(com.webpieces.http2.api.dto.lowlevel.lib.Http2FrameType) AbstractHttp2Frame(com.webpieces.http2.api.dto.lowlevel.lib.AbstractHttp2Frame)

Example 7 with AbstractHttp2Frame

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

the class GoAwayMarshaller method unmarshal.

@Override
public AbstractHttp2Frame unmarshal(Http2MementoImpl state, DataWrapper framePayloadData) {
    GoAwayFrame frame = new GoAwayFrame();
    super.unmarshalFrame(state, frame);
    int streamId = state.getFrameHeaderData().getStreamId();
    if (streamId != 0)
        throw new ConnectionException(CancelReasonCode.INVALID_STREAM_ID, streamId, "goaway frame had stream id=" + streamId);
    List<? extends DataWrapper> split = DATA_GEN.split(framePayloadData, 8);
    ByteBuffer preludeBytes = bufferPool.createWithDataWrapper(split.get(0));
    long lastStreamId = UnsignedData.getUnsignedInt(preludeBytes);
    long errorCode = UnsignedData.getUnsignedInt(preludeBytes);
    frame.setLastStreamId(lastStreamId);
    frame.setErrorCode(errorCode);
    frame.setDebugData(split.get(1));
    bufferPool.releaseBuffer(preludeBytes);
    if (frame.getStreamId() != 0)
        throw new IllegalArgumentException("GoAwayFrame can never be any other stream id except 0 which is already set");
    return frame;
}
Also used : ByteBuffer(java.nio.ByteBuffer) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)

Example 8 with AbstractHttp2Frame

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

the class PriorityMarshaller method unmarshal.

@Override
public AbstractHttp2Frame unmarshal(Http2MementoImpl state, DataWrapper framePayloadData) {
    FrameHeaderData frameHeaderData = state.getFrameHeaderData();
    int streamId = frameHeaderData.getStreamId();
    if (state.getFrameHeaderData().getPayloadLength() != 5)
        throw new ConnectionException(CancelReasonCode.FRAME_SIZE_INCORRECT, streamId, "priority size not 5 and instead is=" + state.getFrameHeaderData().getPayloadLength());
    PriorityFrame frame = new PriorityFrame();
    PriorityDetails priorityDetails = frame.getPriorityDetails();
    super.unmarshalFrame(state, frame);
    ByteBuffer payloadByteBuffer = bufferPool.createWithDataWrapper(framePayloadData);
    int firstInt = payloadByteBuffer.getInt();
    priorityDetails.setStreamDependencyIsExclusive((firstInt >>> 31) == 0x1);
    int streamDependency = firstInt & 0x7FFFFFFF;
    if (frame.getStreamId() == 0) {
        throw new ConnectionException(CancelReasonCode.INVALID_STREAM_ID, frame.getStreamId(), "priority cannot be streamid 0 and was=" + frame.getStreamId());
    } else 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) (payloadByteBuffer.get() & 0xFF));
    bufferPool.releaseBuffer(payloadByteBuffer);
    return frame;
}
Also used : FrameHeaderData(com.webpieces.http2parser.impl.FrameHeaderData) PriorityDetails(com.webpieces.http2.api.dto.lowlevel.lib.PriorityDetails) PriorityFrame(com.webpieces.http2.api.dto.lowlevel.PriorityFrame) ByteBuffer(java.nio.ByteBuffer) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException)

Example 9 with AbstractHttp2Frame

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

the class PushPromiseMarshaller method unmarshal.

@Override
public AbstractHttp2Frame unmarshal(Http2MementoImpl state, DataWrapper framePayloadData) {
    PushPromiseFrame frame = new PushPromiseFrame();
    super.unmarshalFrame(state, frame);
    if (frame.getStreamId() == 0)
        throw new ConnectionException(CancelReasonCode.INVALID_STREAM_ID, frame.getStreamId(), "pushpromise frame had invalid stream id=" + frame.getStreamId());
    byte flags = state.getFrameHeaderData().getFlagsByte();
    frame.setEndHeaders((flags & 0x4) == 0x4);
    boolean isPadded = (flags & 0x8) == 0x8;
    List<? extends DataWrapper> split = DATA_GEN.split(framePayloadData, 4);
    ByteBuffer prelude = bufferPool.createWithDataWrapper(split.get(0));
    DataSplit padSplit = PaddingUtil.extractPayloadAndPadding(isPadded, split.get(1), frame.getStreamId());
    frame.setHeaderFragment(padSplit.getPayload());
    frame.setPadding(padSplit.getPadding());
    frame.setPromisedStreamId(prelude.getInt());
    bufferPool.releaseBuffer(prelude);
    return frame;
}
Also used : PushPromiseFrame(com.webpieces.http2.api.dto.lowlevel.PushPromiseFrame) ByteBuffer(java.nio.ByteBuffer) ConnectionException(com.webpieces.http2.api.dto.error.ConnectionException) DataSplit(com.webpieces.http2parser.impl.DataSplit)

Example 10 with AbstractHttp2Frame

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

the class ContinuationMarshaller method unmarshal.

@Override
public AbstractHttp2Frame unmarshal(Http2MementoImpl state, DataWrapper framePayloadData) {
    ContinuationFrame frame = new ContinuationFrame();
    super.unmarshalFrame(state, frame);
    byte flags = state.getFrameHeaderData().getFlagsByte();
    frame.setEndHeaders((flags & 0x4) == 0x4);
    frame.setHeaderFragment(framePayloadData);
    return frame;
}
Also used : ContinuationFrame(com.webpieces.http2.api.dto.lowlevel.ContinuationFrame)

Aggregations

ConnectionException (com.webpieces.http2.api.dto.error.ConnectionException)9 ByteBuffer (java.nio.ByteBuffer)8 FrameHeaderData (com.webpieces.http2parser.impl.FrameHeaderData)5 DataSplit (com.webpieces.http2parser.impl.DataSplit)3 PriorityDetails (com.webpieces.http2.api.dto.lowlevel.lib.PriorityDetails)2 DataWrapper (org.webpieces.data.api.DataWrapper)2 ContinuationFrame (com.webpieces.http2.api.dto.lowlevel.ContinuationFrame)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)1 HeadersFrame (com.webpieces.http2.api.dto.lowlevel.HeadersFrame)1 PingFrame (com.webpieces.http2.api.dto.lowlevel.PingFrame)1 PriorityFrame (com.webpieces.http2.api.dto.lowlevel.PriorityFrame)1 PushPromiseFrame (com.webpieces.http2.api.dto.lowlevel.PushPromiseFrame)1 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)1 SettingsFrame (com.webpieces.http2.api.dto.lowlevel.SettingsFrame)1 UnknownFrame (com.webpieces.http2.api.dto.lowlevel.UnknownFrame)1 WindowUpdateFrame (com.webpieces.http2.api.dto.lowlevel.WindowUpdateFrame)1 AbstractHttp2Frame (com.webpieces.http2.api.dto.lowlevel.lib.AbstractHttp2Frame)1 Http2FrameType (com.webpieces.http2.api.dto.lowlevel.lib.Http2FrameType)1 Http2Setting (com.webpieces.http2.api.dto.lowlevel.lib.Http2Setting)1