Search in sources :

Example 6 with HttpChunk

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

the class TestChunkedParsing 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);
    HttpChunk chunk1 = msgs.get(1).getHttpChunk();
    String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
    Assert.assertEquals("Wiki", first);
    HttpChunk chunk3 = msgs.get(3).getHttpChunk();
    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) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 7 with HttpChunk

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

the class TestChunkedParsing method testResponseAfterChunked.

@Test
public void testResponseAfterChunked() {
    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();
    HttpResponse resp400 = create400Response();
    byte[] tail = unwrap(parser.marshalToByteBuffer(state, resp400));
    byte[] all = new byte[bytes.length + chunked.length + tail.length];
    System.arraycopy(bytes, 0, all, 0, bytes.length);
    System.arraycopy(chunked, 0, all, bytes.length, chunked.length);
    System.arraycopy(tail, 0, all, bytes.length + chunked.length, tail.length);
    DataWrapper wrapper = dataGen.wrapByteArray(all);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrapper);
    List<HttpPayload> msgs = memento.getParsedMessages();
    Assert.assertEquals(6, msgs.size());
    HttpPayload msg = msgs.get(0).getHttpResponse();
    Assert.assertEquals(resp, msg);
    HttpChunk chunk1 = msgs.get(1).getHttpChunk();
    String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
    Assert.assertEquals("Wiki", first);
    HttpChunk chunk3 = msgs.get(3).getHttpChunk();
    String third = chunk3.getBody().createStringFrom(0, chunk3.getBody().getReadableSize(), Charset.defaultCharset());
    Assert.assertEquals(" in\r\n\r\nchunks.", third);
    HttpPayload tailMsg = msgs.get(5);
    Assert.assertEquals(resp400, tailMsg);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 8 with HttpChunk

use of org.webpieces.httpparser.api.dto.HttpChunk 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 9 with HttpChunk

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

the class HttpParserImpl method readChunk.

private void readChunk(MementoImpl memento, int i) {
    HttpChunk chunk = createHttpChunk(memento, i);
    memento.setHalfParsedChunk(chunk);
    readInChunkBody(memento, true);
    if (chunk.getBody() != null && chunk.getBody().getReadableSize() == 0) {
        //this is the last chunk as it is 0 size
        memento.setInChunkParsingMode(false);
    }
}
Also used : HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk)

Example 10 with HttpChunk

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

the class TestGooglePlayback method testHttpsGooglePlayback.

@Test
public void testHttpsGooglePlayback() {
    List<HttpPayload> results = runPlayback("https.google.com.recording");
    Assert.assertEquals(3, results.size());
    HttpResponse resp = (HttpResponse) results.get(0);
    HttpChunk chunk = (HttpChunk) results.get(1);
    HttpLastChunk lastChunk = (HttpLastChunk) results.get(2);
    Assert.assertEquals("chunked", resp.getHeaderLookupStruct().getHeader(KnownHeaderName.TRANSFER_ENCODING).getValue());
    Assert.assertEquals(10396, chunk.getBody().getReadableSize());
    Assert.assertTrue(lastChunk.isEndOfData());
}
Also used : HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Aggregations

HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)11 Test (org.junit.Test)7 DataWrapper (org.webpieces.data.api.DataWrapper)7 Header (org.webpieces.httpparser.api.common.Header)6 HttpLastChunk (org.webpieces.httpparser.api.dto.HttpLastChunk)6 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)5 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)5 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)2 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)2 HttpChunkExtension (org.webpieces.httpparser.api.dto.HttpChunkExtension)2 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)2 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 StreamWriter (com.webpieces.http2engine.api.StreamWriter)1 ArrayList (java.util.ArrayList)1 HttpMessage (org.webpieces.httpparser.api.dto.HttpMessage)1