Search in sources :

Example 31 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class TestHttp11Basic method testSendTwoRequestsStreamSecond.

@Test
public void testSendTwoRequestsStreamSecond() throws InterruptedException, ExecutionException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    req2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
    mockChannel.write(req2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, false);
    mockChannel.write(data1);
    DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
    HttpData data2 = new HttpData(dataWrapper2, true);
    mockChannel.write(data2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    //send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
    in1.stream.sendResponse(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    PassedIn in2 = mockListener.getSingleRequest();
    HttpResponse resp2 = Requests.createResponse(2);
    resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
    in2.stream.sendResponse(headers2);
    HttpPayload payload2 = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, payload2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 32 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class PushPromiseMarshaller method marshal.

@Override
public DataWrapper marshal(Http2Frame frame) {
    PushPromiseFrame castFrame = (PushPromiseFrame) frame;
    int originalStreamId = castFrame.getPromisedStreamId();
    int streamId = originalStreamId & 0x7FFFFFFF;
    if (streamId != originalStreamId)
        throw new RuntimeException("your promisedStreamId is too large per spec. frame=" + frame);
    int paddingSize = castFrame.getPadding().getReadableSize();
    byte value = 0x0;
    if (castFrame.isEndHeaders())
        value |= 0x4;
    if (paddingSize > 0)
        value |= 0x8;
    ByteBuffer prelude = bufferPool.nextBuffer(4);
    prelude.putInt(castFrame.getPromisedStreamId());
    prelude.flip();
    DataWrapper headersDW = castFrame.getHeaderFragment();
    DataWrapper finalDW = dataGen.chainDataWrappers(dataGen.wrapByteBuffer(prelude), headersDW);
    DataWrapper payload = PaddingUtil.padDataIfNeeded(finalDW, castFrame.getPadding());
    return super.marshalFrame(frame, value, payload);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) PushPromiseFrame(com.webpieces.http2parser.api.dto.PushPromiseFrame) ByteBuffer(java.nio.ByteBuffer)

Example 33 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class RstStreamMarshaller method marshal.

@Override
public DataWrapper marshal(Http2Frame frame) {
    RstStreamFrame castFrame = (RstStreamFrame) frame;
    ByteBuffer payload = bufferPool.nextBuffer(4);
    UnsignedData.putUnsignedInt(payload, castFrame.getErrorCode());
    payload.flip();
    DataWrapper dataPayload = dataGen.wrapByteBuffer(payload);
    return super.marshalFrame(frame, (byte) 0, dataPayload);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) RstStreamFrame(com.webpieces.http2parser.api.dto.RstStreamFrame) ByteBuffer(java.nio.ByteBuffer)

Example 34 with DataWrapper

use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.

the class SettingsMarshaller method marshal.

@Override
public DataWrapper marshal(Http2Frame frame) {
    if (frame.getStreamId() != 0)
        throw new IllegalArgumentException("SettingsFrame can never be any other stream id except 0 which is already set");
    SettingsFrame castFrame = (SettingsFrame) frame;
    for (Http2Setting setting : castFrame.getSettings()) {
        validate(setting);
    }
    byte flags = 0x0;
    if (castFrame.isAck())
        flags |= 0x1;
    DataWrapper dataPayload;
    if (castFrame.isAck()) {
        if (castFrame.getSettings() != null && castFrame.getSettings().size() > 0)
            throw new IllegalArgumentException("Ack SettingsFrame can not have setting in it");
        dataPayload = dataGen.emptyWrapper();
    } else if (castFrame.getSettings().size() == 0) {
        dataPayload = dataGen.emptyWrapper();
    } else {
        List<Http2Setting> settings = castFrame.getSettings();
        dataPayload = marshalOut(settings);
    }
    return super.marshalFrame(frame, flags, dataPayload);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) SettingsFrame(com.webpieces.http2parser.api.dto.SettingsFrame) Http2Setting(com.webpieces.http2parser.api.dto.lib.Http2Setting) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with DataWrapper

use of org.webpieces.data.api.DataWrapper 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);
    }
    //reset header data
    state.setFrameHeaderData(null);
    //set leftover data
    state.setLeftOverData(split.get(1));
    state.addParsedFrame(frame);
    return true;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) UnknownFrame(com.webpieces.http2parser.api.dto.UnknownFrame) FrameMarshaller(com.webpieces.http2parser.impl.marshallers.FrameMarshaller) Http2FrameType(com.webpieces.http2parser.api.dto.lib.Http2FrameType) AbstractHttp2Frame(com.webpieces.http2parser.api.dto.lib.AbstractHttp2Frame)

Aggregations

DataWrapper (org.webpieces.data.api.DataWrapper)115 Test (org.junit.Test)47 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)22 ByteBuffer (java.nio.ByteBuffer)20 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)18 Header (org.webpieces.httpparser.api.common.Header)17 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)17 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)16 HttpData (org.webpieces.httpparser.api.dto.HttpData)15 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)12 Http2Request (com.webpieces.hpack.api.dto.Http2Request)10 StreamWriter (com.webpieces.http2engine.api.StreamWriter)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 Http2Response (com.webpieces.hpack.api.dto.Http2Response)6 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)6 ArrayList (java.util.ArrayList)6 ShutdownStream (com.webpieces.http2engine.api.error.ShutdownStream)5 ConnectionException (com.webpieces.http2parser.api.dto.error.ConnectionException)5