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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations