Search in sources :

Example 16 with StreamRef

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

the class TestC4FrameSizeAndHeaders method testSection4_2FrameTooLarge.

/**
 * An endpoint MUST send an error code of FRAME_SIZE_ERROR if a frame
 * exceeds the size defined in SETTINGS_MAX_FRAME_SIZE, exceeds any
 * limit defined for the frame type, or is too small to contain
 * mandatory frame data. A frame size error in a frame that could alter
 * the state of the entire connection MUST be treated as a connection
 * error (Section 5.4.1); this includes any frame carrying a header
 * block (Section 4.3) (that is, HEADERS, PUSH_PROMISE, and
 * CONTINUATION), SETTINGS, and any frame with a stream identifier of 0.
 */
@Test
public void testSection4_2FrameTooLarge() {
    MockStreamWriter mockStreamWriter = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(mockStreamWriter));
    Http2Request request = sendRequestToServer(listener1);
    sendResponseFromServer(listener1, request);
    DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
    byte[] buf = new byte[localSettings.getMaxFrameSize() + 4];
    dataFrame.setData(DATA_GEN.wrapByteArray(buf));
    // endOfStream=false
    mockChannel.write(dataFrame);
    // remote receives goAway
    GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(Http2ErrorCode.FRAME_SIZE_ERROR, goAway.getKnownErrorCode());
    DataWrapper debugData = goAway.getDebugData();
    String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    Assert.assertEquals("ConnectionException: stream1:(EXCEEDED_MAX_FRAME_SIZE) Frame size=16389 was greater than max=16385", msg);
    Assert.assertTrue(mockChannel.isClosed());
    ShutdownStream failResp = (ShutdownStream) listener1.getSingleRstStream();
    Assert.assertEquals(CancelReasonCode.EXCEEDED_MAX_FRAME_SIZE, failResp.getCause().getReasonCode());
    // 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 : 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) DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Test(org.junit.Test)

Example 17 with StreamRef

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

the class EScopedRouter method invokeRouter.

private RouterStreamRef invokeRouter(AbstractRouter router, RequestContext ctx, ProxyStreamHandle handler, boolean isCorsRequest) {
    RouterStreamRef streamRef = invokeWithProtection(router, ctx, handler, isCorsRequest);
    XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
        if (t == null)
            return XFuture.completedFuture(r);
        XFuture<StreamWriter> fut = new XFuture<>();
        Throwable exc = convert(router.getMatchInfo(), t);
        fut.completeExceptionally(exc);
        return fut;
    }).thenCompose(Function.identity());
    return new RouterStreamRef("eScoped2", writer, streamRef);
}
Also used : ProcessCors(org.webpieces.router.api.routebldr.ProcessCors) SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) LoggerFactory(org.slf4j.LoggerFactory) RouterHeader(org.webpieces.ctx.api.RouterHeader) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NotFoundException(org.webpieces.http.exception.NotFoundException) Strings(com.google.common.base.Strings) RouterRequest(org.webpieces.ctx.api.RouterRequest) RequestContext(org.webpieces.ctx.api.RequestContext) Map(java.util.Map) RouterInfo(org.webpieces.router.impl.model.RouterInfo) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) Logger(org.slf4j.Logger) RouterFutureUtil(org.webpieces.router.impl.RouterFutureUtil) AccessResult(org.webpieces.router.api.routebldr.AccessResult) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException) TimeUnit(java.util.concurrent.TimeUnit) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) MatchResult2(org.webpieces.router.impl.model.MatchResult2) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 18 with StreamRef

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

the class TestHttp1Backpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createChunkedResponse(1);
    HttpChunk response2 = Requests.createHttpChunk(250);
    HttpLastChunk lastChunk = new HttpLastChunk();
    List<ByteBuffer> buffers = create4BuffersWith3Messags(response1, response2, lastChunk);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since it never made it to the client.
    Assert.assertTrue(fut1.isDone());
    // this keeps bytes in-memory that is less than bufPool.getSuggestedSize. ie. < 5k
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    MockStreamWriter mockWriter = new MockStreamWriter();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases the response BUT 'some' data was with response so can't ack yet
    requestFuture.complete(mockWriter);
    // not resolved yet since requestFuture AND streamWriterFutuer BOTH need to be resolved
    Assert.assertFalse(fut2.isDone());
    // This is BECAUSE 68 bytes resulted in TWO messages and both futures can ack the 68 bytes.
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
    Assert.assertFalse(fut4.isDone());
    lastChunkAck.complete(null);
    fut4.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 19 with StreamRef

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

the class Layer2Http11Handler method processInitialPieceOfRequest.

private XFuture<Void> processInitialPieceOfRequest(FrontendSocketImpl socket, HttpRequest http1Req, Http2Request headers) {
    int id = counter.getAndAdd(2);
    PermitQueue permitQueue = socket.getPermitQueue();
    return permitQueue.runRequest(() -> {
        Http11StreamImpl currentStream = new Http11StreamImpl(id, socket, httpParser, permitQueue, http1Req, headers);
        HttpStream streamHandle = httpListener.openStream(socket);
        currentStream.setStreamHandle(streamHandle);
        socket.setCurrentStream(currentStream);
        if (!headers.isEndOfStream()) {
            // in this case, we are NOT at the end of the request so we must let the next piece of
            // data run right after the request
            // TODO(dhiller): Replace this section with futureUtil.trySuccessFinally
            StreamRef streamRef = streamHandle.incomingRequest(headers, currentStream);
            currentStream.setStreamRef(streamRef);
            return streamRef.getWriter().thenApply(w -> {
                // must release the permit so the next data piece(which may be cached) can come in
                permitQueue.releasePermit();
                return null;
            });
        } else {
            // in this case, since this is the END of the request, we cannot release the permit in the
            // permit queue as we do not want to let the next request to start until the full response is
            // sent back to the client
            currentStream.setSentFullRequest(true);
            StreamRef streamRef = streamHandle.incomingRequest(headers, currentStream);
            currentStream.setStreamRef(streamRef);
            return streamRef.getWriter().thenApply(w -> null);
        }
    });
}
Also used : PermitQueue(org.webpieces.util.locking.PermitQueue) StreamRef(com.webpieces.http2.api.streaming.StreamRef) HttpStream(org.webpieces.frontend2.api.HttpStream)

Example 20 with StreamRef

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

the class FrontendSocketImpl method cancelAllStreams.

private void cancelAllStreams(StreamListener httpListener, FarEndClosedConnection f) {
    Http11StreamImpl stream = getCurrentStream();
    if (stream == null)
        return;
    ShutdownStream shutdown = new ShutdownStream(stream.getStreamId(), f);
    StreamRef streamRef = stream.getStreamRef();
    streamRef.cancel(shutdown);
}
Also used : ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) StreamRef(com.webpieces.http2.api.streaming.StreamRef)

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