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());
}
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;
}
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());
}
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());
}
Aggregations