Search in sources :

Example 76 with DataWrapper

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) Header(org.webpieces.httpparser.api.common.Header) HttpChunkExtension(org.webpieces.httpparser.api.dto.HttpChunkExtension) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 77 with DataWrapper

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper)

Example 78 with DataWrapper

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 79 with DataWrapper

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());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) CancelReason(com.webpieces.http2parser.api.dto.CancelReason) Http2Request(com.webpieces.hpack.api.dto.Http2Request) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) Test(org.junit.Test)

Example 80 with DataWrapper

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());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.hpack.api.dto.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2engine.api.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ConnectionClosedException(com.webpieces.http2engine.api.ConnectionClosedException) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) Test(org.junit.Test)

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