Search in sources :

Example 36 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1ReceiveValidFramesAfterSendRstStreamFrame.

/**
 * If this state is reached as a result of sending a RST_STREAM frame, the
 * peer that receives the RST_STREAM might have already sent — or enqueued for
 * sending — frames on the stream that cannot be withdrawn. An endpoint MUST ignore
 * frames that it receives on closed streams after it has sent a RST_STREAM frame. An
 * endpoint MAY choose to limit the period over which it ignores frames and
 * treat frames that arrive after this time as being in error.
 * @throws TimeoutException
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test
public void testSection5_1ReceiveValidFramesAfterSendRstStreamFrame() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
    Http2Request request1 = Requests.createRequest();
    RequestStreamHandle stream = httpSocket.openStream();
    StreamRef streamRef = httpSocket.openStream().process(request1, listener1);
    XFuture<StreamWriter> future = streamRef.getWriter();
    @SuppressWarnings("unused") StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Http2Msg req = mockChannel.getFrameAndClear();
    Assert.assertEquals(request1, req);
    RstStreamFrame rst = new RstStreamFrame(request1.getStreamId(), Http2ErrorCode.CANCEL);
    XFuture<Void> cancel = streamRef.cancel(rst);
    cancel.get(2, TimeUnit.SECONDS);
    Http2Msg svrRst = mockChannel.getFrameAndClear();
    Assert.assertEquals(rst, svrRst);
    // simulate server responding before receiving the cancel
    Http2Response resp1 = Requests.createEosResponse(request1.getStreamId());
    // endOfStream=true
    mockChannel.write(resp1);
// Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
// Assert.assertFalse(mockChannel.isClosed());
// 
// Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) Test(org.junit.Test)

Example 37 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1ReceiveBadFrameAfterReceiveRstStreamFrame.

/**
 * 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_1ReceiveBadFrameAfterReceiveRstStreamFrame() {
    MockStreamWriter mockWriter = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(mockWriter));
    Http2Request request = sendRequestToServer(listener1);
    sendResetFromServer(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) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) Test(org.junit.Test)

Example 38 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1BadFrameReceivedInIdleState.

/**
 * Receiving any frame other than HEADERS 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_1BadFrameReceivedInIdleState() {
    DataFrame dataFrame = new DataFrame(1, false);
    // endOfStream=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:stream1:(BAD_FRAME_RECEIVED_FOR_THIS_STATE) " + "Stream in idle state and received this frame which should not happen in " + "idle state.  frame=DataFrame{streamId=1, endStream=false, data.len=0, padding=0}", msg);
    Assert.assertTrue(mockChannel.isClosed());
    // send new request on closed connection
    MockResponseListener listener1 = new MockResponseListener();
    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 39 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class RouterServiceImpl method incomingRequest.

@Override
public StreamRef incomingRequest(Http2Request req, RouterResponseHandler handler) {
    // ******************************************************************************************
    // DO NOT ADD CODE HERE OR ABOVE THIS METHOD in RouterService.  This is our CATCH-ALL point so
    // ANY code above that is not protected from our catch and respond to clients
    // ******************************************************************************************
    String txId = generate();
    // I do NOT like doing Guice creation on the request path(Dagger creation would probably be ok) BUT this
    // is very worth it AND customers can swap out these critical classes if they need to quickly temporarily fix a bug while
    // we work on the bug.  We can easily give customers bug fixes like add binder.bind(ClassWithBug.class).to(BugFixCode.class)
    ProxyStreamHandle proxyHandler = proxyProvider.get();
    proxyHandler.init(handler, req);
    MDC.put("txId", txId);
    // top level handler...
    try {
        RouterStreamRef streamRef = incomingRequestProtected(req, proxyHandler);
        XFuture<StreamWriter> writer = streamRef.getWriter().thenApply(w -> new TxStreamWriter(txId, w));
        XFuture<StreamWriter> finalWriter = writer.handle((r, t) -> {
            if (t == null)
                return XFuture.completedFuture(r);
            XFuture<StreamWriter> fut = proxyHandler.topLevelFailure(req, t);
            return fut;
        }).thenCompose(Function.identity());
        return new RouterStreamRef("routerSevcTop", finalWriter, streamRef);
    } finally {
        MDC.remove("txId");
    }
}
Also used : RouterCookie(org.webpieces.ctx.api.RouterCookie) Provider(javax.inject.Provider) UriInfo(org.webpieces.ctx.api.UriInfo) LoggerFactory(org.slf4j.LoggerFactory) RouterConfig(org.webpieces.router.api.RouterConfig) HashMap(java.util.HashMap) Random(java.util.Random) Singleton(javax.inject.Singleton) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Function(java.util.function.Function) ContentType(org.webpieces.ctx.api.ContentType) ArrayList(java.util.ArrayList) ParsedContentType(com.webpieces.http2.api.subparsers.ParsedContentType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) RouterRequest(org.webpieces.ctx.api.RouterRequest) Locale(java.util.Locale) AcceptType(com.webpieces.http2.api.subparsers.AcceptType) Map(java.util.Map) Logger(org.slf4j.Logger) RouterStreamHandle(org.webpieces.router.api.RouterStreamHandle) UrlEncodedParser(org.webpieces.util.urlparse.UrlEncodedParser) Set(java.util.Set) RouterService(org.webpieces.router.api.RouterService) Injector(com.google.inject.Injector) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) AcceptMediaType(org.webpieces.ctx.api.AcceptMediaType) StreamRef(com.webpieces.http2.api.streaming.StreamRef) MDC(org.slf4j.MDC) FileMeta(org.webpieces.router.impl.compression.FileMeta) RouterResponseHandler(org.webpieces.router.api.RouterResponseHandler) Entry(java.util.Map.Entry) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HeaderPriorityParser(com.webpieces.http2.api.subparsers.HeaderPriorityParser) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) HeaderPriorityParserImpl(com.webpieces.http2.impl.subparsers.HeaderPriorityParserImpl) Arguments(org.webpieces.util.cmdline2.Arguments) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) XFuture(org.webpieces.util.futures.XFuture) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Aggregations

StreamRef (com.webpieces.http2.api.streaming.StreamRef)34 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)27 Test (org.junit.Test)22 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)17 XFuture (org.webpieces.util.futures.XFuture)12 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)8 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)8 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)6 DataWrapper (org.webpieces.data.api.DataWrapper)6 RouterService (org.webpieces.router.api.RouterService)6 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)6 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)5 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)5 Function (java.util.function.Function)5 RequestContext (org.webpieces.ctx.api.RequestContext)5 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)5 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)4 ConnectionClosedException (com.webpieces.http2engine.api.error.ConnectionClosedException)4