Search in sources :

Example 1 with DataFrame

use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.

the class TestBasicHttp2Client method testMaxConcurrentOne.

@Test
public void testMaxConcurrentOne() throws InterruptedException, ExecutionException {
    Http2Request request1 = Requests.createRequest();
    Http2Request request2 = Requests.createRequest();
    MockStreamWriter writer1 = new MockStreamWriter();
    MockResponseListener respListener1 = new MockResponseListener();
    respListener1.setIncomingRespDefault(XFuture.completedFuture(writer1));
    MockResponseListener respListener2 = new MockResponseListener();
    StreamRef streamRef1 = httpSocket.openStream().process(request1, respListener1);
    XFuture<StreamWriter> future = streamRef1.getWriter();
    StreamRef streamRef2 = httpSocket.openStream().process(request2, respListener2);
    XFuture<StreamWriter> future2 = streamRef2.getWriter();
    // max concurrent only 1 so only get 1
    Http2Request req = (Http2Request) mockChannel.getFrameAndClear();
    Assert.assertEquals(1, req.getStreamId());
    Assert.assertEquals(request1, req);
    Assert.assertTrue(future.isDone());
    // do not ack upstream until out the door(backpressure)
    Assert.assertFalse(future2.isDone());
    Http2Response resp1 = Requests.createResponse(request1.getStreamId());
    // endOfStream=false
    mockChannel.write(resp1);
    Http2Response response1 = respListener1.getSingleReturnValueIncomingResponse();
    Assert.assertEquals(resp1, response1);
    Assert.assertFalse(future2.isDone());
    // endOfStream=false
    mockChannel.write(new DataFrame(request1.getStreamId(), false));
    writer1.getSingleFrame();
    // at this point, should not have a call outstanding
    mockChannel.assertNoIncomingMessages();
    // WRITE OUT END STREAM data so the first request starts going again!!
    Assert.assertFalse(future2.isDone());
    DataFrame dataLast = new DataFrame(request1.getStreamId(), true);
    // endOfStream = true
    mockChannel.write(dataLast);
    Assert.assertTrue(future2.isDone());
    DataFrame data = (DataFrame) writer1.getSingleFrame();
    Assert.assertEquals(dataLast.getStreamId(), data.getStreamId());
    Http2Request req2 = (Http2Request) mockChannel.getFrameAndClear();
    Assert.assertEquals(request2, req2);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Test(org.junit.Test)

Example 2 with DataFrame

use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1ReceiveBadFrameAfterReceiveEndStream.

/**
 * An endpoint MUST NOT send frames other than PRIORITY on a closed stream. An endpoint
 * that receives any frame other than PRIORITY after receiving a RST_STREAM MUST
 * treat that as a stream error (Section 5.4.2) of type STREAM_CLOSED. Similarly, an
 * endpoint that receives any frames after receiving a frame with the
 * -----END_STREAM flag---- set MUST treat that as a connection error (Section 5.4.1) of
 * type STREAM_CLOSED, unless the frame is permitted as described below.
 */
@Test
public void testSection5_1ReceiveBadFrameAfterReceiveEndStream() {
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
    Http2Request request = sendRequestToServer(listener1);
    sendEosResponseFromServer(listener1, request);
    DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
    mockChannel.write(dataFrame);
    // remote receives goAway
    GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(Http2ErrorCode.STREAM_CLOSED, goAway.getKnownErrorCode());
    DataWrapper debugData = goAway.getDebugData();
    String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    Assert.assertEquals("ConnectionException: MockHttp2Channel1:stream1:" + "(CLOSED_STREAM) Stream must have been closed as it no longer exists.  " + "high mark=1  your frame=DataFrame{streamId=1, endStream=false, data.len=0, padding=0}", msg);
    Assert.assertTrue(mockChannel.isClosed());
    Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
    // send new request on closed connection
    Http2Request request1 = Requests.createRequest();
    StreamRef streamRef = httpSocket.openStream().process(request1, listener1);
    XFuture<StreamWriter> future = streamRef.getWriter();
    ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
    Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ConnectionClosedException(com.webpieces.http2engine.api.error.ConnectionClosedException) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) Test(org.junit.Test)

Example 3 with DataFrame

use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1BadFrameReceivedInReservedRemoteState.

/**
 * Receiving any type of frame other than HEADERS, RST_STREAM, or PRIORITY on a
 * stream in this state MUST be treated as a connection
 * error (Section 5.4.1) of type PROTOCOL_ERROR.
 */
@Test
public void testSection5_1BadFrameReceivedInReservedRemoteState() {
    MockPushListener pushListener = new MockPushListener();
    pushListener.setIncomingRespDefault(XFuture.<Void>completedFuture(null));
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
    listener1.addReturnValuePush(pushListener);
    Http2Request request = sendRequestToServer(listener1);
    Http2Push push = sendPushFromServer(listener1, request);
    DataFrame dataFrame = new DataFrame(push.getPromisedStreamId(), false);
    mockChannel.write(dataFrame);
    // remote receives goAway
    GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(Http2ErrorCode.PROTOCOL_ERROR, goAway.getKnownErrorCode());
    DataWrapper debugData = goAway.getDebugData();
    String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    Assert.assertEquals("ConnectionException: MockHttp2Channel1:stream2:(BAD_FRAME_RECEIVED_FOR_THIS_STATE) " + "No transition defined on statemachine for event=RECV_DATA when in state=Reserved(remote)", msg);
    Assert.assertTrue(mockChannel.isClosed());
    ShutdownStream failResp1 = (ShutdownStream) listener1.getSingleRstStream();
    Assert.assertEquals(CancelReasonCode.BAD_FRAME_RECEIVED_FOR_THIS_STATE, failResp1.getCause().getReasonCode());
    ShutdownStream failResp2 = (ShutdownStream) listener1.getSingleCancelPush();
    Assert.assertEquals(CancelReasonCode.BAD_FRAME_RECEIVED_FOR_THIS_STATE, failResp2.getCause().getReasonCode());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) MockPushListener(org.webpieces.http2client.mock.MockPushListener) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) Test(org.junit.Test)

Example 4 with DataFrame

use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.

the class ResponseCacher method run.

public XFuture<FullResponse> run(FullRequest request) {
    SingleResponseListener responseListener = new SingleResponseListener();
    RequestStreamHandle streamHandle = openStreamFunc.get();
    Http2Request req = request.getHeaders();
    if (request.getPayload() == null) {
        request.getHeaders().setEndOfStream(true);
        streamHandle.process(req, responseListener);
        return responseListener.fetchResponseFuture();
    } else if (request.getTrailingHeaders() == null) {
        request.getHeaders().setEndOfStream(false);
        DataFrame data = createData(request, true);
        StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
        return streamRef.getWriter().thenCompose(writer -> {
            data.setStreamId(req.getStreamId());
            return writer.processPiece(data);
        }).thenCompose(writer -> responseListener.fetchResponseFuture());
    }
    request.getHeaders().setEndOfStream(false);
    DataFrame data = createData(request, false);
    Http2Trailers trailers = request.getTrailingHeaders();
    trailers.setEndOfStream(true);
    StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
    return streamRef.getWriter().thenCompose(writer -> writeStuff(writer, req, data, trailers, responseListener));
}
Also used : FullRequest(org.webpieces.http2client.api.dto.FullRequest) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) StreamRef(com.webpieces.http2.api.streaming.StreamRef) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapper(org.webpieces.data.api.DataWrapper) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Supplier(java.util.function.Supplier) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 5 with DataFrame

use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.

the class ResponseCacher method createData.

private DataFrame createData(FullRequest request, boolean isEndOfStream) {
    DataWrapper payload = request.getPayload();
    DataFrame data = new DataFrame();
    data.setEndOfStream(isEndOfStream);
    data.setData(payload);
    return data;
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Aggregations

DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)52 Test (org.junit.Test)31 DataWrapper (org.webpieces.data.api.DataWrapper)28 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)19 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)13 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)11 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)9 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)9 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)8 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)8 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)7 StreamRef (com.webpieces.http2.api.streaming.StreamRef)6 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)6 ConnectionClosedException (com.webpieces.http2engine.api.error.ConnectionClosedException)5 Header (org.webpieces.httpparser.api.common.Header)5 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)4 HttpData (org.webpieces.httpparser.api.dto.HttpData)4 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)4 ConnectionException (com.webpieces.http2.api.dto.error.ConnectionException)3 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)3