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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations