Search in sources :

Example 21 with HttpPayload

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

the class TestHttp11Backpressure method initialize.

private void initialize() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    XFuture<Void> future2 = mockChannel.sendToSvrAsync(req);
    // The default return was a completed future so no backpressure here
    Assert.assertTrue(future2.isDone());
    PassedIn in1 = mockListener.getSingleRequest();
    HttpResponse resp1 = Requests.createResponse(1);
    Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
    XFuture<StreamWriter> future = in1.stream.process(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    // server should add content-length 0 for firefox
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Assert.assertEquals(resp1, payload);
    future.get(2, TimeUnit.SECONDS);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)

Example 22 with HttpPayload

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

the class TestHttp11Errors method testCloseAfter2ndRequest.

@Test
public void testCloseAfter2ndRequest() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    XFuture<StreamWriter> futA = XFuture.completedFuture(null);
    MockStreamRef mockStreamRefA = new MockStreamRef(futA);
    mockListener.addMockStreamToReturn(mockStreamRefA);
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    XFuture<StreamWriter> futB = XFuture.completedFuture(null);
    MockStreamRef mockStreamRefB = new MockStreamRef(futB);
    mockListener.addMockStreamToReturn(mockStreamRefB);
    XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
    Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
    XFuture<StreamWriter> future = in1.stream.process(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, true);
    DataFrame data = (DataFrame) Http11ToHttp2.translateData(data1);
    writer.processPiece(data);
    fut1.get(2, TimeUnit.SECONDS);
    Assert.assertFalse(mockStreamRefA.isCancelled());
    Assert.assertFalse(mockStreamRefB.isCancelled());
    mockChannel.simulateClose();
    // this request is done, nothing to cancel
    Assert.assertFalse(mockStreamRefA.isCancelled());
    Assert.assertTrue(mockStreamRefB.isCancelled());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Example 23 with HttpPayload

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

Example 24 with HttpPayload

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

the class TestHttp11Basic method testSendTwoRequestsStreamFirst.

@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, false);
    mockChannel.sendToSvr(data1);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    DataWrapper dataWrapper2 = DATA_GEN.wrapByteArray(buf);
    HttpData data2 = new HttpData(dataWrapper2, true);
    mockChannel.sendToSvr(data2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    XFuture<Void> fut = mockChannel.sendToSvrAsync(req2);
    Assert.assertFalse(fut.isDone());
    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 = Http11ToHttp2.responseToHeaders(resp1);
    in1.stream.process(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    fut.get(2, TimeUnit.SECONDS);
    PassedIn in2 = mockListener.getSingleRequest();
    HttpResponse resp2 = Requests.createResponse(2);
    resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers2 = Http11ToHttp2.responseToHeaders(resp2);
    in2.stream.process(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.http2.api.dto.highlevel.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 25 with HttpPayload

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

the class Layer2Http11Handler method processWithBackpressure.

public XFuture<Void> processWithBackpressure(FrontendSocketImpl socket, int newDataSize, int numBytesRead) {
    Memento state = socket.getHttp11ParseState();
    List<HttpPayload> parsed = state.getParsedMessages();
    WebSession session = (WebSession) socket.getSession().get(WEB_SESSION_KEY);
    if (session == null) {
        session = new WebSession();
        socket.getSession().put(WEB_SESSION_KEY, session);
    }
    // ALL of the below MUST happen AFTER the previous processing happened
    // which may not have finished so chain the below with the previous future
    XFuture<Void> future = session.getProcessFuture();
    for (HttpPayload payload : parsed) {
        // VERY IMPORTANT: Writing the code like this would slam through calling process N times
        // BUT it doesn't give the clients a chance to seet a flag between packets
        // Mainly done for exceptions and streaming so you can log exc, set a boolean so you
        // don't get 100 exceptions while something is happening like socket disconnect
        // In these 2 lines of code, processCorrectly is CALLED N times RIGHT NOW
        // The code below this only calls them right now IF AND ONLY IF the client returns
        // a completed future each time!!!
        // XFuture<Void> fut = processCorrectly(socket, payload);
        // future = future.thenCompose(s -> fut);
        future = future.thenCompose(s -> processCorrectly(socket, payload));
    }
    // replace with new future so it blocks any future pieces if these are not processed
    session.setProcessFuture(future);
    return future;
}
Also used : Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) Http11ToHttp2(org.webpieces.http2translations.api.Http11ToHttp2) LoggerFactory(org.slf4j.LoggerFactory) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) ByteBuffer(java.nio.ByteBuffer) HttpStream(org.webpieces.frontend2.api.HttpStream) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataWrapper(org.webpieces.data.api.DataWrapper) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpMessageType(org.webpieces.httpparser.api.dto.HttpMessageType) DataWrapperGeneratorFactory(org.webpieces.data.api.DataWrapperGeneratorFactory) PermitQueue(org.webpieces.util.locking.PermitQueue) Logger(org.slf4j.Logger) MarshalState(org.webpieces.httpparser.api.MarshalState) HttpParser(org.webpieces.httpparser.api.HttpParser) StreamListener(org.webpieces.frontend2.api.StreamListener) FutureHelper(org.webpieces.util.futures.FutureHelper) Memento(org.webpieces.httpparser.api.Memento) Consumer(java.util.function.Consumer) List(java.util.List) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) StreamRef(com.webpieces.http2.api.streaming.StreamRef) MDC(org.slf4j.MDC) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapperGenerator(org.webpieces.data.api.DataWrapperGenerator) MDCUtil(org.webpieces.nio.impl.cm.basic.MDCUtil) Memento(org.webpieces.httpparser.api.Memento) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload)

Aggregations

HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)35 DataWrapper (org.webpieces.data.api.DataWrapper)30 Test (org.junit.Test)24 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)23 Header (org.webpieces.httpparser.api.common.Header)18 HttpData (org.webpieces.httpparser.api.dto.HttpData)14 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)13 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)6 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)4 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)4 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)3 ByteBuffer (java.nio.ByteBuffer)3 Memento (org.webpieces.httpparser.api.Memento)3 HttpLastData (org.webpieces.httpparser.api.dto.HttpLastData)3 XFuture (org.webpieces.util.futures.XFuture)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2