use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class Requests method createPostRequest.
public static List<HttpPayload> createPostRequest(String url, String... argTuples) {
if (argTuples.length % 2 != 0)
throw new IllegalArgumentException("argTuples.length must be of even size (key/value)");
HttpUri httpUri = new HttpUri(url);
HttpRequestLine requestLine = new HttpRequestLine();
requestLine.setMethod(KnownHttpMethod.POST);
requestLine.setUri(httpUri);
HttpRequest req = new HttpRequest();
req.setRequestLine(requestLine);
req.addHeader(new Header(KnownHeaderName.HOST, "myhost.com"));
String encodedParams = "";
for (int i = 0; i < argTuples.length; i += 2) {
String key = URLEncoder.encode(argTuples[i], StandardCharsets.UTF_8);
String value = URLEncoder.encode(argTuples[i + 1], StandardCharsets.UTF_8);
if (!"".equals(encodedParams))
encodedParams += "&";
encodedParams += key + "=" + value;
}
HttpData data = new HttpData();
data.setEndOfData(true);
byte[] bytes = encodedParams.getBytes(StandardCharsets.UTF_8);
DataWrapper body = gen.wrapByteArray(bytes);
data.setBody(body);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + body.getReadableSize()));
req.addHeader(new Header(KnownHeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded"));
List<HttpPayload> payloads = new ArrayList<>();
payloads.add(req);
payloads.add(data);
return payloads;
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class HttpParserImpl method chunkedBytes.
private byte[] chunkedBytes(HttpChunk request) {
DataWrapper dataWrapper = request.getBody();
int size = dataWrapper.getReadableSize();
String metaLine = request.createMetaLine();
String lastPart = request.createTrailer();
byte[] hex = metaLine.getBytes(iso8859_1);
byte[] endData = lastPart.getBytes(iso8859_1);
byte[] data = new byte[hex.length + size + endData.length];
//copy chunk header of <size>/r/n
System.arraycopy(hex, 0, data, 0, hex.length);
copyData(dataWrapper, data, hex.length);
//copy closing /r/n (and headers if isLastChunk)
System.arraycopy(endData, 0, data, data.length - endData.length, endData.length);
return data;
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class HttpParserImpl method readInContentLengthBody.
private void readInContentLengthBody(MementoImpl memento) {
Integer toRead = memento.getContentLengthLeftToRead();
DataWrapper data = memento.getLeftOverData();
int readSize = Math.min(toRead, data.getReadableSize());
if (readSize == 0) {
//wait for more data
return;
}
int newSizeLeft = toRead - readSize;
boolean isEos;
if (newSizeLeft == 0) {
isEos = true;
memento.setContentLengthLeftToRead(null);
} else {
isEos = false;
memento.setContentLengthLeftToRead(newSizeLeft);
}
List<? extends DataWrapper> split = dataGen.split(data, readSize);
HttpData httpData = new HttpData(split.get(0), isEos);
memento.setLeftOverData(split.get(1));
memento.addMessage(httpData);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class Http2ChannelCache method write.
@SuppressWarnings("unchecked")
@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
DataWrapper data = dataGen.wrapByteBuffer(b);
parser.unmarshal(unmarshalState, data);
List<Http2Msg> parsedFrames = unmarshalState.getParsedFrames();
return (CompletableFuture<Channel>) super.calledMethod(Method.INCOMING_FRAME, parsedFrames);
}
use of org.webpieces.data.api.DataWrapper 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);
}
Aggregations