use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestChunkedParsing method testMarshalOut.
@Test
public void testMarshalOut() {
DataWrapper payload1 = dataGen.wrapByteArray("0123456789".getBytes());
HttpChunk chunk = new HttpChunk();
chunk.addExtension(new HttpChunkExtension("asdf", "value"));
chunk.addExtension(new HttpChunkExtension("something"));
chunk.setBody(payload1);
byte[] payload = unwrap(parser.marshalToByteBuffer(state, chunk));
String str = new String(payload);
Assert.assertEquals("a;asdf=value;something\r\n0123456789\r\n", str);
HttpLastChunk lastChunk = new HttpLastChunk();
lastChunk.addExtension(new HttpChunkExtension("this", "that"));
lastChunk.addHeader(new Header("customer", "value"));
String lastPayload = parser.marshalToString(lastChunk);
Assert.assertEquals("0;this=that\r\ncustomer: value\r\n\r\n", lastPayload);
byte[] lastBytes = unwrap(parser.marshalToByteBuffer(state, lastChunk));
String lastPayloadFromBytes = new String(lastBytes, HttpParserFactory.iso8859_1);
Assert.assertEquals("0;this=that\r\ncustomer: value\r\n\r\n", lastPayloadFromBytes);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class HttpParserImpl method processChunks.
private void processChunks(MementoImpl memento) {
if (memento.getHalfParsedChunk() != null) {
readInChunkBody(memento, true);
if (memento.getHalfParsedChunk() != null)
//we are still reading in the body
return;
}
int i = memento.getReadingHttpMessagePointer();
for (; i < memento.getLeftOverData().getReadableSize() - 1; i++) {
DataWrapper dataToRead = memento.getLeftOverData();
byte firstByte = dataToRead.readByteAt(i);
byte secondByte = dataToRead.readByteAt(i + 1);
boolean isFirstCr = conversion.isCarriageReturn(firstByte);
boolean isSecondLineFeed = conversion.isLineFeed(secondByte);
if (isFirstCr && isSecondLineFeed) {
readChunk(memento, i);
//since we swapped out memento.getLeftOverData to be
//what's left, we can read from 0 again
i = 0;
if (//we are done processing chunks
!memento.isInChunkParsingMode() || //we are in the middle of processing chunk body
memento.getHalfParsedChunk() != null)
break;
}
}
memento.setReadingHttpMessagePointer(i);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class MockHttp2Channel method sendFrame.
public void sendFrame(Http2Frame frame) {
DataWrapper data = frameParser.marshal(frame);
byte[] bytes = data.createByteArray();
if (bytes.length == 0)
throw new IllegalArgumentException("how do you marshal to 0 bytes...WTF");
ByteBuffer buf = ByteBuffer.wrap(bytes);
listener.incomingData(mockHttp2Channel, buf);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestC4FrameSizeAndHeaders method testSection4_3BadDecompression.
/**
* A decoding error in a header block MUST be treated as a connection error (Section 5.4.1) of type COMPRESSION_ERROR.
*
*/
@Test
public void testSection4_3BadDecompression() {
MockResponseListener listener1 = new MockResponseListener();
listener1.setIncomingRespDefault(CompletableFuture.<StreamWriter>completedFuture(null));
Http2Request request = sendRequestToServer(listener1);
//has to be 1 since we use 1 in the response
Assert.assertEquals(1, request.getStreamId());
String badHeaderFrame = // length
"00 00 10" + // type
"01" + // flags (ack)
"05" + // R + streamid
"00 00 00 01" + //payload
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
//endOfStream=false
mockChannel.writeHexBack(badHeaderFrame);
//remote receives goAway
GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.COMPRESSION_ERROR, goAway.getKnownErrorCode());
DataWrapper debugData = goAway.getDebugData();
String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
Assert.assertEquals("ConnectionException: MockHttp2Channel1:stream1:(HEADER_DECODE) Error from hpack library", msg);
Assert.assertTrue(mockChannel.isClosed());
List<CancelReason> results = listener1.getRstStreams();
Assert.assertEquals(1, results.size());
ShutdownStream failResp = (ShutdownStream) results.get(0);
Assert.assertEquals(CancelReasonCode.HEADER_DECODE, failResp.getCause().getReasonCode());
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestC5_1StreamStates method testSection5_1ReceiveBadFrameAfterReceiveEndStream.
/**
* 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_1ReceiveBadFrameAfterReceiveEndStream() {
MockResponseListener listener1 = new MockResponseListener();
listener1.setIncomingRespDefault(CompletableFuture.<StreamWriter>completedFuture(null));
Http2Request request = sendRequestToServer(listener1);
sendEosResponseFromServer(listener1, request);
DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
mockChannel.write(dataFrame);
//remote receives goAway
GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.STREAM_CLOSED, goAway.getKnownErrorCode());
DataWrapper debugData = goAway.getDebugData();
String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
Assert.assertEquals("ConnectionException: MockHttp2Channel1:stream1:" + "(CLOSED_STREAM) Stream must have been closed as it no longer exists. " + "high mark=1 your frame=DataFrame{streamId=1, endStream=false, data.len=0, padding=0}", msg);
Assert.assertTrue(mockChannel.isClosed());
Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
//send new request on closed connection
Http2Request request1 = Requests.createRequest();
CompletableFuture<StreamWriter> future = httpSocket.openStream().process(request1, listener1);
ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Aggregations