Search in sources :

Example 66 with DataWrapper

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;
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpRequestLine(org.webpieces.httpparser.api.dto.HttpRequestLine) ArrayList(java.util.ArrayList) HttpUri(org.webpieces.httpparser.api.dto.HttpUri) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData)

Example 67 with DataWrapper

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;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper)

Example 68 with DataWrapper

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpData(org.webpieces.httpparser.api.dto.HttpData)

Example 69 with DataWrapper

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) CompletableFuture(java.util.concurrent.CompletableFuture) Http2Msg(com.webpieces.http2parser.api.dto.lib.Http2Msg)

Example 70 with DataWrapper

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);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Aggregations

DataWrapper (org.webpieces.data.api.DataWrapper)115 Test (org.junit.Test)47 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)22 ByteBuffer (java.nio.ByteBuffer)20 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)18 Header (org.webpieces.httpparser.api.common.Header)17 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)17 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)16 HttpData (org.webpieces.httpparser.api.dto.HttpData)15 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)12 Http2Request (com.webpieces.hpack.api.dto.Http2Request)10 StreamWriter (com.webpieces.http2engine.api.StreamWriter)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 Http2Response (com.webpieces.hpack.api.dto.Http2Response)6 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)6 ArrayList (java.util.ArrayList)6 ShutdownStream (com.webpieces.http2engine.api.error.ShutdownStream)5 ConnectionException (com.webpieces.http2parser.api.dto.error.ConnectionException)5