Search in sources :

Example 6 with HttpData

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

the class TestHttp11Basic method testSendTwoRequestsStreamSecond.

@Test
public void testSendTwoRequestsStreamSecond() throws InterruptedException, ExecutionException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    req2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
    mockChannel.write(req2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, false);
    mockChannel.write(data1);
    DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
    HttpData data2 = new HttpData(dataWrapper2, true);
    mockChannel.write(data2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    //send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
    in1.stream.sendResponse(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    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) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 7 with HttpData

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

the class TestHttp11Basic method testSendTwoRequestsStreamFirst.

@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, false);
    mockChannel.write(data1);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
    HttpData data2 = new HttpData(dataWrapper2, true);
    mockChannel.write(data2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    mockChannel.write(req2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    //send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
    in1.stream.sendResponse(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    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) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 8 with HttpData

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

the class TestHttp11Basic method testUploadWithBody.

@Test
public void testUploadWithBody() throws InterruptedException, ExecutionException, TimeoutException {
    String bodyStr = "hi there, how are you";
    DataWrapper dataWrapper = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpData body = new HttpData(dataWrapper, true);
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + dataWrapper.getReadableSize()));
    mockChannel.write(req);
    mockChannel.write(body);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2Translations.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    DataFrame frame = (DataFrame) mockStreamWriter.getSingleFrame();
    DataWrapper data = frame.getData();
    Assert.assertEquals(bodyStr, data.createStringFromUtf8(0, data.getReadableSize()));
    Assert.assertTrue(frame.isEndOfStream());
    HttpResponse resp = Requests.createNobodyResponse();
    Http2Response http2Resp = Http2Translations.responseToHeaders(resp);
    CompletableFuture<StreamWriter> fut = in1.stream.sendResponse(http2Resp);
    fut.get(2, TimeUnit.SECONDS);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) 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)

Example 9 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData 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 10 with HttpData

use of org.webpieces.httpparser.api.dto.HttpData 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)

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