use of org.webpieces.httpparser.api.dto.HttpLastChunk 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.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
Assert.assertEquals(req, req1);
HttpChunk chunk = new HttpChunk();
String bodyStr = "hi here and there";
DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
chunk.setBody(data);
mockChannel.sendToSvr(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.sendToSvr(last);
DataFrame lastEmpty = (DataFrame) mockStreamWriter.getSingleFrame();
Assert.assertTrue(lastEmpty.isEndOfStream());
Assert.assertEquals(0, lastEmpty.getData().getReadableSize());
}
use of org.webpieces.httpparser.api.dto.HttpLastChunk in project webpieces by deanhiller.
the class TestHttp11Backpressure method create4SplitPayloads.
private List<ByteBuffer> create4SplitPayloads() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
HttpChunk chunk = new HttpChunk();
String bodyStr = "hi here and there";
DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
chunk.setBody(data);
HttpLastChunk lastChunk = new HttpLastChunk();
HttpStatefulParser parser = HttpParserFactory.createStatefulParser("a", new SimpleMeterRegistry(), new TwoPools("pl", new SimpleMeterRegistry()));
ByteBuffer buf1 = parser.marshalToByteBuffer(req);
ByteBuffer buf2 = parser.marshalToByteBuffer(chunk);
ByteBuffer buf3 = parser.marshalToByteBuffer(lastChunk);
byte[] part1 = new byte[10];
byte[] part2 = new byte[buf1.remaining()];
buf1.get(part1);
int toWrite = buf1.remaining();
buf1.get(part2, 0, toWrite);
buf2.get(part2, toWrite, part2.length - toWrite);
byte[] part3 = new byte[buf2.remaining() + 2];
int toWrite2 = buf2.remaining();
buf2.get(part3, 0, toWrite2);
buf3.get(part3, toWrite2, 2);
byte[] part4 = new byte[buf3.remaining()];
buf3.get(part4);
List<ByteBuffer> buffers = new ArrayList<>();
buffers.add(ByteBuffer.wrap(part1));
buffers.add(ByteBuffer.wrap(part2));
buffers.add(ByteBuffer.wrap(part3));
buffers.add(ByteBuffer.wrap(part4));
return buffers;
}
use of org.webpieces.httpparser.api.dto.HttpLastChunk 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<>();
int chunkSize = parseExtensions(chunkMetaData, extensions);
HttpChunk chunk = new HttpChunk();
if (chunkSize == 0)
chunk = new HttpLastChunk();
chunk.setExtensions(extensions);
// 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.HttpLastChunk 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 (HttpData.class == payload.getClass()) {
// in case server just sends it's received HttpData to a client, we have to translate it back to HttpChunk
HttpChunk chunk = parserUtil.translateData((HttpData) payload);
return parserUtil.chunkedBytes(chunk);
} else if (HttpLastData.class == payload.getClass()) {
HttpLastChunk lastChunk = parserUtil.translate((HttpLastData) payload);
return parserUtil.chunkedBytes(lastChunk);
} else if (payload.getMessageType() == HttpMessageType.CHUNK || payload.getMessageType() == HttpMessageType.LAST_CHUNK) {
return parserUtil.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(ParserUtil.ISO8859_1);
return stringPiece;
}
use of org.webpieces.httpparser.api.dto.HttpLastChunk 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);
}
Aggregations