Search in sources :

Example 26 with StreamWriter

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

the class Level8NotifyClntListeners method sendPieceToApp.

@Override
public XFuture<Void> sendPieceToApp(Stream stream, StreamMsg payload) {
    ClientStream str = (ClientStream) stream;
    StreamWriter writer = str.getResponseWriter();
    return writer.processPiece(payload).thenApply(s -> null);
}
Also used : StreamWriter(com.webpieces.http2.api.streaming.StreamWriter)

Example 27 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter 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 28 with StreamWriter

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

the class ResponseStaticProcessor method renderStaticResponse.

public RouterStreamRef renderStaticResponse(RenderStaticResponse renderStatic) {
    if (log.isDebugEnabled())
        log.debug("Sending render static html response. req=" + request);
    RequestInfo requestInfo = new RequestInfo(routerRequest, request, pool, handler);
    XFuture<StreamWriter> writer = futureUtil.catchBlockWrap(() -> reader.sendRenderStatic(requestInfo, renderStatic, handler), (t) -> convert(t));
    // the file an pushing it back...
    return new RouterStreamRef("staticRef", writer, null);
}
Also used : StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RequestInfo(org.webpieces.router.impl.proxyout.filereaders.RequestInfo)

Example 29 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter 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 30 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter 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)

Aggregations

StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)60 Test (org.junit.Test)43 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)37 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)32 StreamRef (com.webpieces.http2.api.streaming.StreamRef)25 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)24 DataWrapper (org.webpieces.data.api.DataWrapper)19 XFuture (org.webpieces.util.futures.XFuture)17 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)15 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)13 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 MockStreamRef (org.webpieces.httpfrontend2.api.mock2.MockStreamRef)8 Header (org.webpieces.httpparser.api.common.Header)8 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)8 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)7 ByteBuffer (java.nio.ByteBuffer)7 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)7 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)7