use of org.webpieces.httpparser.api.dto.HttpChunk in project webpieces by deanhiller.
the class HttpParserImpl method readInChunkBody.
//Returns true if body read in and false otherwise
private void readInChunkBody(MementoImpl memento, boolean stripAndCompareLastTwo) {
HttpChunk message = memento.getHalfParsedChunk();
DataWrapper dataToRead = memento.getLeftOverData();
int readableSize = dataToRead.getReadableSize();
int numBytesNeeded = memento.getNumBytesLeftToReadOnChunk();
if (numBytesNeeded <= readableSize) {
List<? extends DataWrapper> split = dataGen.split(dataToRead, numBytesNeeded);
DataWrapper data = split.get(0);
if (stripAndCompareLastTwo) {
List<? extends DataWrapper> splitPieces = dataGen.split(data, data.getReadableSize() - 2);
data = splitPieces.get(0);
DataWrapper trailer = splitPieces.get(1);
String trailerStr = trailer.createStringFrom(0, trailer.getReadableSize(), iso8859_1);
if (!TRAILER_STR.equals(trailerStr))
throw new IllegalStateException("The chunk did not end with \\r\\n . The format is invalid");
}
message.setBody(data);
memento.setLeftOverData(split.get(1));
memento.setNumBytesLeftToReadOnChunk(0);
memento.addMessage(message);
//clear any cached message we were waiting for more data for
memento.setHalfParsedChunk(null);
return;
}
}
use of org.webpieces.httpparser.api.dto.HttpChunk in project webpieces by deanhiller.
the class HttpParserImpl method marshalToBytes.
public byte[] marshalToBytes(MarshalState s, HttpPayload payload) {
MarshalStateImpl state = (MarshalStateImpl) s;
if (state.getParsingDataSize() != null) {
return parseData(state, payload);
} else if (payload.getMessageType() == HttpMessageType.CHUNK || payload.getMessageType() == HttpMessageType.LAST_CHUNK) {
return chunkedBytes((HttpChunk) payload);
}
HttpMessage msg = (HttpMessage) payload;
String result = marshalHeaders(payload);
Header header = msg.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
if (header != null && !header.getValue().equals("0")) {
String value = header.getValue();
int lengthOfBodyFromHeader = toInteger(value, "" + header);
state.setParsingDataSize(lengthOfBodyFromHeader);
}
byte[] stringPiece = result.getBytes(iso8859_1);
return stringPiece;
}
use of org.webpieces.httpparser.api.dto.HttpChunk in project webpieces by deanhiller.
the class HttpParserImpl method createHttpChunk.
private HttpChunk createHttpChunk(MementoImpl memento, int i) {
DataWrapper dataToRead = memento.getLeftOverData();
//split off the header AND /r/n (ie. the +2)
List<? extends DataWrapper> split = dataGen.split(dataToRead, i + 2);
DataWrapper chunkMetaData = split.get(0);
memento.setLeftOverData(split.get(1));
List<HttpChunkExtension> extensions = new ArrayList<>();
String chunkMetaStr = chunkMetaData.createStringFrom(0, chunkMetaData.getReadableSize(), iso8859_1);
String hexSize = chunkMetaStr.trim();
if (chunkMetaStr.contains(";")) {
String[] extensionsArray = chunkMetaStr.split(";");
hexSize = extensionsArray[0];
for (int n = 1; n < extensionsArray.length; n++) {
HttpChunkExtension ext = createExtension(extensionsArray[n]);
extensions.add(ext);
}
}
int chunkSize = Integer.parseInt(hexSize, 16);
HttpChunk chunk = new HttpChunk();
if (chunkSize == 0)
chunk = new HttpLastChunk();
//must read in all the data of the chunk AND /r/n
int size = 2 + chunkSize;
memento.setNumBytesLeftToReadOnChunk(size);
return chunk;
}
use of org.webpieces.httpparser.api.dto.HttpChunk 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.write(req);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2Translations.translateRequest(in1.request);
Assert.assertEquals(req, req1);
HttpResponse resp = Requests.createResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
Http2Response headers = Http2Translations.responseToHeaders(resp);
CompletableFuture<StreamWriter> future = in1.stream.sendResponse(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 = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
dataFrame.setData(data);
writer.processPiece(dataFrame);
List<HttpPayload> frames = mockChannel.getFramesAndClear();
Assert.assertEquals(2, frames.size());
HttpChunk chunk = (HttpChunk) frames.get(0);
DataWrapper body = chunk.getBodyNonNull();
String result = body.createStringFromUtf8(0, body.getReadableSize());
Assert.assertEquals(bodyStr, result);
HttpLastChunk last = (HttpLastChunk) frames.get(1);
Assert.assertEquals(0, last.getBodyNonNull().getReadableSize());
}
use of org.webpieces.httpparser.api.dto.HttpChunk in project webpieces by deanhiller.
the class TestHttp11Basic method testPostWithChunking.
@Test
public void testPostWithChunking() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2Translations.translateRequest(in1.request);
Assert.assertEquals(req, req1);
HttpChunk chunk = new HttpChunk();
String bodyStr = "hi here and there";
DataWrapper data = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
chunk.setBody(data);
mockChannel.write(chunk);
DataFrame singleFrame = (DataFrame) mockStreamWriter.getSingleFrame();
Assert.assertTrue(!singleFrame.isEndOfStream());
DataWrapper data2 = singleFrame.getData();
String result = data2.createStringFromUtf8(0, data2.getReadableSize());
Assert.assertEquals(bodyStr, result);
HttpLastChunk last = new HttpLastChunk();
mockChannel.write(last);
DataFrame lastEmpty = (DataFrame) mockStreamWriter.getSingleFrame();
Assert.assertTrue(lastEmpty.isEndOfStream());
Assert.assertEquals(0, lastEmpty.getData().getReadableSize());
}
Aggregations