Search in sources :

Example 1 with HttpPayload

use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.

the class MockTcpChannel method write.

@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
    DataWrapper data = dataGen.wrapByteBuffer(b);
    memento = parser.parse(memento, data);
    List<HttpPayload> parsedMessages = memento.getParsedMessages();
    for (HttpPayload payload : parsedMessages) {
        if (payload instanceof HttpResponse) {
            sendResponse((HttpResponse) payload);
        } else {
            sendData((HttpData) payload);
        }
    }
    return CompletableFuture.completedFuture(this);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse)

Example 2 with HttpPayload

use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.

the class TestResponseParsing method testPartialHttpMessage.

@Test
public void testPartialHttpMessage() {
    HttpResponse response = createOkResponse();
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, response));
    byte[] firstPart = new byte[10];
    byte[] secondPart = new byte[payload.length - firstPart.length];
    // let's split the payload up into two pieces
    System.arraycopy(payload, 0, firstPart, 0, firstPart.length);
    System.arraycopy(payload, firstPart.length, secondPart, 0, secondPart.length);
    DataWrapper data1 = dataGen.wrapByteArray(firstPart);
    DataWrapper data2 = dataGen.wrapByteArray(secondPart);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, data1);
    Assert.assertEquals(0, memento.getParsedMessages().size());
    memento = parser.parse(memento, data2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
    HttpPayload httpMessage = memento.getParsedMessages().get(0);
    Assert.assertEquals(response, httpMessage);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) Test(org.junit.Test)

Example 3 with HttpPayload

use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.

the class TestNewChunkedParsing method testBasic.

@Test
public void testBasic() {
    String chunkedData = "4\r\nWiki\r\n5\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n";
    HttpResponse resp = TestResponseParsing.createOkResponse();
    resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
    byte[] chunked = chunkedData.getBytes();
    byte[] all = new byte[bytes.length + chunked.length];
    System.arraycopy(bytes, 0, all, 0, bytes.length);
    System.arraycopy(chunked, 0, all, bytes.length, chunked.length);
    DataWrapper wrapper = dataGen.wrapByteArray(all);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrapper);
    List<HttpPayload> msgs = memento.getParsedMessages();
    Assert.assertEquals(5, msgs.size());
    HttpPayload msg = msgs.get(0).getHttpResponse();
    Assert.assertEquals(resp, msg);
    HttpData chunk1 = msgs.get(1).getHttpData();
    String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
    Assert.assertEquals("Wiki", first);
    HttpData chunk3 = msgs.get(3).getHttpData();
    String third = chunk3.getBody().createStringFrom(0, chunk3.getBody().getReadableSize(), Charset.defaultCharset());
    Assert.assertEquals(" in\r\n\r\nchunks.", third);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) 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) Test(org.junit.Test)

Example 4 with HttpPayload

use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.

the class TestNewChunkedParsing method testSplitChunkBodyAndResponseAfter.

@Test
public void testSplitChunkBodyAndResponseAfter() {
    String chunkedData = "1E\r\n012345678901234567890123456789\r\n7\r\nchunks.\r\n0\r\n\r\n";
    HttpResponse resp = TestResponseParsing.createOkResponse();
    resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
    byte[] chunked = chunkedData.getBytes();
    HttpResponse resp400 = create400Response();
    byte[] tail = unwrap(parser.marshalToByteBuffer(state, resp400));
    int lengthOfChunked1stHalf = 15;
    int lengthOfChunked2ndHalf = chunked.length - 15;
    byte[] firstPiece = new byte[bytes.length + lengthOfChunked1stHalf];
    byte[] secondPiece = new byte[chunked.length - lengthOfChunked1stHalf + tail.length];
    System.arraycopy(bytes, 0, firstPiece, 0, bytes.length);
    System.arraycopy(chunked, 0, firstPiece, bytes.length, lengthOfChunked1stHalf);
    System.arraycopy(chunked, lengthOfChunked1stHalf, secondPiece, 0, lengthOfChunked2ndHalf);
    System.arraycopy(tail, 0, secondPiece, lengthOfChunked2ndHalf, tail.length);
    DataWrapper first = dataGen.wrapByteArray(firstPiece);
    DataWrapper second = dataGen.wrapByteArray(secondPiece);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, first);
    Assert.assertEquals(ParsingState.CHUNK, memento.getUnParsedState().getCurrentlyParsing());
    // The new implementation consumes AS MUCH AS POSSIBLE
    Assert.assertEquals(0, memento.getUnParsedState().getCurrentUnparsedSize());
    List<HttpPayload> msgs = memento.getParsedMessages();
    Assert.assertEquals(2, msgs.size());
    Assert.assertEquals(resp, msgs.get(0));
    HttpData data = (HttpData) msgs.get(1);
    Assert.assertTrue(data.isStartOfChunk());
    Assert.assertFalse(data.isEndOfChunk());
    Assert.assertFalse(data.isEndOfData());
    memento = parser.parse(memento, second);
    List<HttpPayload> parsedMessages = memento.getParsedMessages();
    Assert.assertEquals(4, parsedMessages.size());
    Assert.assertEquals(resp400, parsedMessages.get(3));
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) 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) Test(org.junit.Test)

Example 5 with HttpPayload

use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.

the class TestRequestBody method testBody.

@Test
public void testBody() {
    HttpDummyRequest request = createPostRequestWithBody();
    byte[] expected = request.getHttpData().getBody().createByteArray();
    byte[] data = unwrap(request);
    DataWrapper wrap1 = dataGen.wrapByteArray(data);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrap1);
    Assert.assertEquals(2, memento.getParsedMessages().size());
    HttpPayload msg = memento.getParsedMessages().get(0);
    Assert.assertEquals(request.getRequest(), msg);
    HttpData data1 = (HttpData) memento.getParsedMessages().get(1);
    DataWrapper body = data1.getBody();
    byte[] bodyBytesActual = body.createByteArray();
    Assert.assertArrayEquals(expected, bodyBytesActual);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Aggregations

HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)35 DataWrapper (org.webpieces.data.api.DataWrapper)30 Test (org.junit.Test)24 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)23 Header (org.webpieces.httpparser.api.common.Header)18 HttpData (org.webpieces.httpparser.api.dto.HttpData)14 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)13 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)6 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)4 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)4 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)3 ByteBuffer (java.nio.ByteBuffer)3 Memento (org.webpieces.httpparser.api.Memento)3 HttpLastData (org.webpieces.httpparser.api.dto.HttpLastData)3 XFuture (org.webpieces.util.futures.XFuture)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2