Search in sources :

Example 1 with HttpLastData

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

the class TestHttp11Basic method testFileDownloadWithChunking.

@Test
public void testFileDownloadWithChunking() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    HttpResponse resp = Requests.createResponse();
    resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    Http2Response headers = Http11ToHttp2.responseToHeaders(resp);
    XFuture<StreamWriter> future = in1.stream.process(headers);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    DataFrame dataFrame = new DataFrame();
    dataFrame.setEndOfStream(true);
    String bodyStr = "hi here and there";
    DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    dataFrame.setData(data);
    writer.processPiece(dataFrame);
    List<HttpPayload> frames = mockChannel.getFramesAndClear();
    Assert.assertEquals(2, frames.size());
    HttpData chunk = (HttpData) frames.get(0);
    DataWrapper body = chunk.getBodyNonNull();
    String result = body.createStringFromUtf8(0, body.getReadableSize());
    Assert.assertEquals(bodyStr, result);
    HttpLastData last = (HttpLastData) frames.get(1);
    Assert.assertEquals(0, last.getBodyNonNull().getReadableSize());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) 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) Test(org.junit.Test)

Example 2 with HttpLastData

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

the class HttpParserImpl method readChunkToItsEnd.

private HttpData readChunkToItsEnd(MementoImpl memento, DataWrapper leftOver, int numBytesToReadForFullChunk, boolean isStartOfChunk) {
    // read all but the 2 bytes
    List<? extends DataWrapper> splitPieces = dataGen.split(leftOver, numBytesToReadForFullChunk);
    // read the 2 bytes
    List<? extends DataWrapper> split2 = dataGen.split(splitPieces.get(1), 2);
    DataWrapper trailer = split2.get(0);
    String trailerStr = trailer.createStringFrom(0, trailer.getReadableSize(), ParserUtil.ISO8859_1);
    if (!TRAILER_STR.equals(trailerStr))
        throw new IllegalStateException("The chunk did not end with \\r\\n .  The format is invalid");
    DataWrapper body = splitPieces.get(0);
    inPayloadSize.record(body.getReadableSize());
    HttpData data = new HttpData();
    if (memento.isLastChunk())
        data = new HttpLastData();
    data.setBody(body);
    data.setStartOfChunk(isStartOfChunk);
    // since we are reading the full chunk to the \r\n, this is the end of A chunk(more to come if isFinalChunk = false)
    data.setEndOfChunk(true);
    data.setEndOfData(memento.isLastChunk());
    if (memento.isLastChunk())
        // reset for next chain of chunking
        memento.setLastChunk(false);
    memento.setLeftOverData(split2.get(1));
    memento.setNumBytesLeftToReadOnChunk(0);
    memento.addMessage(data);
    memento.setReadingChunkHeader(true);
    if (body.getReadableSize() == 0) {
        // this is the last chunk as it is 0 size
        memento.setInChunkParsingMode(false);
    }
    return data;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData)

Example 3 with HttpLastData

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

the class TestGooglePlayback method testHttpsGooglePlayback.

@Test
public void testHttpsGooglePlayback() {
    List<HttpPayload> results = runPlayback("https.google.com.recording");
    int size = 0;
    for (HttpPayload result : results) {
        if (result.getMessageType() != HttpMessageType.DATA)
            continue;
        size += result.getHttpData().getBodyNonNull().getReadableSize();
    }
    Assert.assertEquals(10396, size);
    HttpResponse resp = (HttpResponse) results.get(0);
    HttpLastData lastChunk = (HttpLastData) results.get(results.size() - 1);
    Assert.assertEquals("chunked", resp.getHeaderLookupStruct().getHeader(KnownHeaderName.TRANSFER_ENCODING).getValue());
    Assert.assertTrue(lastChunk.isEndOfData());
}
Also used : HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData) Test(org.junit.Test)

Example 4 with HttpLastData

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

the class TestGooglePlayback method testGooglePlayback.

@Test
public void testGooglePlayback() {
    List<HttpPayload> results = runPlayback("google.com.11015.recording");
    int size = 0;
    for (HttpPayload result : results) {
        if (result.getMessageType() != HttpMessageType.DATA)
            continue;
        size += result.getHttpData().getBodyNonNull().getReadableSize();
    }
    Assert.assertEquals(10349, size);
    HttpResponse resp = (HttpResponse) results.get(0);
    HttpLastData lastChunk = (HttpLastData) results.get(results.size() - 1);
    Assert.assertEquals("chunked", resp.getHeaderLookupStruct().getHeader(KnownHeaderName.TRANSFER_ENCODING).getValue());
    Assert.assertTrue(lastChunk.isEndOfData());
}
Also used : HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData) Test(org.junit.Test)

Aggregations

HttpLastData (org.webpieces.httpparser.api.dto.HttpLastData)4 Test (org.junit.Test)3 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)3 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)3 DataWrapper (org.webpieces.data.api.DataWrapper)2 HttpData (org.webpieces.httpparser.api.dto.HttpData)2 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)1 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)1 Header (org.webpieces.httpparser.api.common.Header)1 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)1