Search in sources :

Example 1 with PriorityDetails

use of com.webpieces.http2parser.api.dto.lib.PriorityDetails 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 PriorityDetails

use of com.webpieces.http2parser.api.dto.lib.PriorityDetails 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 PriorityDetails

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

the class TestHttp2Priority method testMarshalPriorityFrameMSB.

@Test
public void testMarshalPriorityFrameMSB() {
    PriorityFrame frame = new PriorityFrame();
    frame.setStreamId(1);
    PriorityDetails details = frame.getPriorityDetails();
    details.setStreamDependency(4);
    details.setStreamDependencyIsExclusive(true);
    details.setWeight((short) 0xFF);
    Assert.assertEquals(details.getWeight(), 255);
    byte[] data = parser.marshal(frame).createByteArray();
    String hexFrame = Util.toHexString(data);
    Assert.assertEquals(priorityFrameMSB(), hexFrame);
}
Also used : PriorityDetails(com.webpieces.http2parser.api.dto.lib.PriorityDetails) PriorityFrame(com.webpieces.http2parser.api.dto.PriorityFrame) Test(org.junit.Test)

Example 4 with PriorityDetails

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

the class TestHttp2Priority method testParsePriorityFrame.

@Test
public void testParsePriorityFrame() {
    DataWrapper data = Util.hexToBytes(priorityFrame());
    parser.parse(memento, data);
    PriorityFrame frame = (PriorityFrame) assertGood();
    Assert.assertEquals(1, frame.getStreamId());
    PriorityDetails details = frame.getPriorityDetails();
    Assert.assertTrue(details.isStreamDependencyIsExclusive());
    Assert.assertEquals(5, details.getWeight());
    Assert.assertEquals(4, details.getStreamDependency());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) PriorityDetails(com.webpieces.http2parser.api.dto.lib.PriorityDetails) PriorityFrame(com.webpieces.http2parser.api.dto.PriorityFrame) Test(org.junit.Test)

Example 5 with PriorityDetails

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

the class TestC5_1StreamStates method testSection5_1ReceivePriorityAfterReceiveRstStreamFrame.

/**
	 * An endpoint MUST NOT send frames other than ----PRIORITY---- on a closed stream. An endpoint 
	 * that receives any frame other than ----PRIORITY---- after receiving a ----RST_STREAM---- MUST 
	 * treat that as a stream error (Section 5.4.2) of type STREAM_CLOSED. Similarly, an 
	 * endpoint that receives any frames after receiving a frame with the 
	 * END_STREAM flag set MUST treat that as a connection error (Section 5.4.1) of 
	 * type STREAM_CLOSED, unless the frame is permitted as described below.
	 * 
	 */
@Test
public void testSection5_1ReceivePriorityAfterReceiveRstStreamFrame() {
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(CompletableFuture.<StreamWriter>completedFuture(null));
    Http2Request request = sendRequestToServer(listener1);
    sendResetFromServer(listener1, request);
    PriorityDetails details = new PriorityDetails();
    details.setStreamDependency(3);
    PriorityFrame dataFrame = new PriorityFrame(request.getStreamId(), details);
    mockChannel.write(dataFrame);
    //priority is ignored
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
    Assert.assertFalse(mockChannel.isClosed());
    Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
Also used : Http2Request(com.webpieces.hpack.api.dto.Http2Request) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) PriorityDetails(com.webpieces.http2parser.api.dto.lib.PriorityDetails) PriorityFrame(com.webpieces.http2parser.api.dto.PriorityFrame) Test(org.junit.Test)

Aggregations

PriorityDetails (com.webpieces.http2parser.api.dto.lib.PriorityDetails)9 PriorityFrame (com.webpieces.http2parser.api.dto.PriorityFrame)7 Test (org.junit.Test)5 DataWrapper (org.webpieces.data.api.DataWrapper)5 ConnectionException (com.webpieces.http2parser.api.dto.error.ConnectionException)3 HeadersFrame (com.webpieces.http2parser.api.dto.HeadersFrame)2 ByteBuffer (java.nio.ByteBuffer)2 Http2Request (com.webpieces.hpack.api.dto.Http2Request)1 StreamException (com.webpieces.http2parser.api.dto.error.StreamException)1 DataSplit (com.webpieces.http2parser.impl.DataSplit)1 FrameHeaderData (com.webpieces.http2parser.impl.FrameHeaderData)1 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)1