Search in sources :

Example 1 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.

the class TestRequestBody method testPartialBodyThenHalfBodyWithNextHttpMsg.

@Test
public void testPartialBodyThenHalfBodyWithNextHttpMsg() {
    HttpDummyRequest request = createPostRequestWithBody();
    byte[] expected = request.getHttpData().getBody().createByteArray();
    byte[] data = unwrap(request);
    HttpRequest request2 = TestRequestParsing.createPostRequest();
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, request2));
    byte[] first = new byte[data.length - 20];
    byte[] second = new byte[data.length - first.length + payload.length];
    System.arraycopy(data, 0, first, 0, first.length);
    int lenOfLastPiece = data.length - first.length;
    System.arraycopy(data, first.length, second, 0, lenOfLastPiece);
    System.arraycopy(payload, 0, second, lenOfLastPiece, payload.length);
    DataWrapper wrap1 = dataGen.wrapByteArray(first);
    DataWrapper wrap2 = dataGen.wrapByteArray(second);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrap1);
    Assert.assertEquals(2, memento.getParsedMessages().size());
    HttpPayload msg = memento.getParsedMessages().get(0);
    Assert.assertEquals(request.getRequest(), msg);
    HttpData data1 = (HttpData) memento.getParsedMessages().get(1);
    memento = parser.parse(memento, wrap2);
    Assert.assertEquals(2, memento.getParsedMessages().size());
    HttpData data2 = (HttpData) memento.getParsedMessages().get(0);
    DataWrapper body = dataGen.chainDataWrappers(data1.getBody(), data2.getBody());
    byte[] bodyBytesActual = body.createByteArray();
    Assert.assertArrayEquals(expected, bodyBytesActual);
    HttpPayload msg2 = memento.getParsedMessages().get(1);
    Assert.assertEquals(request2, msg2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 2 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.

the class TestRequestBody method testBody.

@Test
public void testBody() {
    HttpDummyRequest request = createPostRequestWithBody();
    byte[] expected = request.getHttpData().getBody().createByteArray();
    byte[] data = unwrap(request);
    DataWrapper wrap1 = dataGen.wrapByteArray(data);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrap1);
    Assert.assertEquals(2, memento.getParsedMessages().size());
    HttpPayload msg = memento.getParsedMessages().get(0);
    Assert.assertEquals(request.getRequest(), msg);
    HttpData data1 = (HttpData) memento.getParsedMessages().get(1);
    DataWrapper body = data1.getBody();
    byte[] bodyBytesActual = body.createByteArray();
    Assert.assertArrayEquals(expected, bodyBytesActual);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 3 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.

the class HttpParserImpl method parseData.

private byte[] parseData(MarshalStateImpl state, HttpPayload payload) {
    if (!(payload instanceof HttpData))
        throw new IllegalStateException("You passed in a request or response earlier with Content-Length so must pass in all the HttpData next not this=" + payload);
    HttpData data = payload.getHttpData();
    DataWrapper body = data.getBodyNonNull();
    state.addMoreBytes(body.getReadableSize());
    if (state.getTotalRead() > state.getParsingDataSize())
        throw new IllegalStateException("Content-Length was " + state.getParsingDataSize() + " but you have so far passed in " + state.getTotalRead() + " bytes");
    else if (state.getTotalRead() == state.getParsingDataSize()) {
        state.resetDataReading();
    }
    return body.createByteArray();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpData(org.webpieces.httpparser.api.dto.HttpData)

Example 4 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.

the class TestRequestBody method testPartialBody.

@Test
public void testPartialBody() {
    HttpDummyRequest request = createPostRequestWithBody();
    byte[] expected = request.getHttpData().getBody().createByteArray();
    byte[] data = unwrap(request);
    byte[] first = new byte[data.length - 20];
    byte[] second = new byte[data.length - first.length];
    System.arraycopy(data, 0, first, 0, first.length);
    System.arraycopy(data, first.length, second, 0, second.length);
    DataWrapper wrap1 = dataGen.wrapByteArray(first);
    DataWrapper wrap2 = dataGen.wrapByteArray(second);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, wrap1);
    Assert.assertEquals(ParsingState.BODY, memento.getUnParsedState().getCurrentlyParsing());
    Assert.assertEquals(0, memento.getUnParsedState().getCurrentUnparsedSize());
    HttpRequest httpRequest = memento.getParsedMessages().get(0).getHttpRequest();
    Assert.assertEquals(request.getRequest(), httpRequest);
    HttpData data1 = memento.getParsedMessages().get(1).getHttpData();
    Assert.assertEquals(2, memento.getParsedMessages().size());
    memento = parser.parse(memento, wrap2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
    HttpData data2 = memento.getParsedMessages().get(0).getHttpData();
    DataWrapper body = dataGen.chainDataWrappers(data1.getBody(), data2.getBody());
    byte[] bodyBytesActual = body.createByteArray();
    Assert.assertArrayEquals(expected, bodyBytesActual);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 5 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.

the class TestHttp11Basic method testSendTwoRequestsStreamFirstResponse.

@Test
public void testSendTwoRequestsStreamFirstResponse() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    mockChannel.write(req2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
    Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
    CompletableFuture<StreamWriter> future = in1.stream.sendResponse(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, true);
    DataFrame data = (DataFrame) Http2Translations.translateData(data1);
    writer.processPiece(data);
    HttpData d = (HttpData) mockChannel.getFrameAndClear();
    Assert.assertEquals(10, d.getBody().getReadableSize());
    PassedIn in2 = mockListener.getSingleRequest();
    HttpResponse resp2 = Requests.createResponse(2);
    resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
    in2.stream.sendResponse(headers2);
    HttpPayload payload2 = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, payload2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) StreamWriter(com.webpieces.http2engine.api.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Aggregations

DataWrapper (org.webpieces.data.api.DataWrapper)15 HttpData (org.webpieces.httpparser.api.dto.HttpData)15 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)11 Header (org.webpieces.httpparser.api.common.Header)9 Test (org.junit.Test)8 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)7 Http2Response (com.webpieces.hpack.api.dto.Http2Response)4 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)4 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)4 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)3 StreamWriter (com.webpieces.http2engine.api.StreamWriter)2 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)2 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)2 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)2 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)1 ArrayList (java.util.ArrayList)1