use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Errors method testCloseBeforeFirstRequestCompletes.
@Test
public void testCloseBeforeFirstRequestCompletes() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
XFuture<StreamWriter> futA = XFuture.completedFuture(null);
MockStreamRef mockStreamRefA = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStreamRefA);
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
XFuture<StreamWriter> futB = XFuture.completedFuture(null);
MockStreamRef mockStreamRefB = new MockStreamRef(futB);
mockListener.addMockStreamToReturn(mockStreamRefB);
XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
XFuture<StreamWriter> future = in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
Assert.assertFalse(mockStreamRefA.isCancelled());
Assert.assertFalse(mockStreamRefB.isCancelled());
mockChannel.simulateClose();
// this request is done, nothing to cancel
Assert.assertTrue(mockStreamRefA.isCancelled());
Assert.assertFalse(mockStreamRefB.isCancelled());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp1Backpressure method testBasicBackpressure.
@Test
public void testBasicBackpressure() 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.createResponse(1, 250);
HttpData response2 = Requests.createHttpData(250);
List<ByteBuffer> buffers = create3BuffersWithTwoMessags(response1, response2);
DataListener dataListener = mockChannel.getConnectedListener();
XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
listener.addProcessResponse(requestFuture);
XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
// resolved since data is cached in parser (client doesn't receive this data)
Assert.assertTrue(fut1.isDone());
// This next one is confusing BUT in http1.1 parsing terms, data is data for content length so this results
// in a full HttpData packet actually...
XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
// not resolved yet since client only has part of the data
Assert.assertFalse(fut2.isDone());
MockStreamWriter mockWriter = new MockStreamWriter();
XFuture<Void> streamWriterFuture = new XFuture<Void>();
mockWriter.addProcessResponse(streamWriterFuture);
// This releases BOTH packets above to be processed!!!(not just the one)
requestFuture.complete(mockWriter);
Assert.assertFalse(fut2.isDone());
streamWriterFuture.complete(null);
// NOW it's resolved
fut2.get(2, TimeUnit.SECONDS);
// feed the last buffer in
XFuture<Void> future3 = new XFuture<Void>();
mockWriter.addProcessResponse(future3);
XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
Assert.assertFalse(fut3.isDone());
future3.complete(null);
fut3.get(2, TimeUnit.SECONDS);
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestWriteReads method testBasicReadWrite.
@Test
public void testBasicReadWrite() 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());
MockResponseListener listener2 = new MockResponseListener();
request.getHeaderLookupStruct().getHeader("serverid").setValue("2");
mockChannel.addWriteResponse(XFuture.completedFuture(null));
StreamRef streamRef1 = handle.process(request, listener2);
XFuture<StreamWriter> writer2 = streamRef1.getWriter();
Assert.assertTrue(writer2.isDone());
Assert.assertEquals(request, mockChannel.getLastWriteParam());
Http2Response response1 = Requests.createResponse(1, 0);
listener.addProcessResponse(XFuture.completedFuture(null));
XFuture<Void> fut1 = mockChannel.writeResponse(response1);
// throws if exception occurred and ensures future resolved
fut1.get(2, TimeUnit.SECONDS);
Http2Response msg = listener.getIncomingMsg();
Assert.assertEquals(response1, msg);
Http2Response response2 = Requests.createResponse(2, 0);
listener2.addProcessResponse(XFuture.completedFuture(null));
XFuture<Void> fut2 = mockChannel.writeResponse(response2);
// throws if exception occurred and ensures future resolved
fut2.get(2, TimeUnit.SECONDS);
Http2Response msg2 = listener2.getIncomingMsg();
Assert.assertEquals(response2, msg2);
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class CompressionChunkingHandle method process.
@Override
public XFuture<StreamWriter> process(Http2Response response) {
if (lastResponseSent != null)
throw new IllegalStateException("You already sent a response. " + "do not call Actions.redirect or Actions.render more than once. previous response=" + lastResponseSent + " 2nd response=" + response);
lastResponseSent = response;
Compression compression = checkForCompression(response);
ChunkedStream chunkedStream = new ChunkedStream(webSettings.getMaxBodySizeToSend());
Http2Response finalResp = response;
if (Current.isContextSet()) {
// in some exceptional cases like incoming cookies failing to parse, there will be no context
List<OverwritePlatformResponse> callbacks = Current.getContext().getCallbacks();
for (OverwritePlatformResponse callback : callbacks) {
finalResp = (Http2Response) callback.modifyOrReplace(finalResp);
}
}
boolean closeAfterResponding = false;
if (closeAfterResponding(originalRequest))
closeAfterResponding = true;
boolean shouldClose = closeAfterResponding;
return handler.process(response).thenApply(w -> possiblyClose(shouldClose, response, w)).thenApply(w -> new ProxyStreamWriter(shouldClose, compression, chunkedStream, w));
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class ProxyStreamHandle method sendRedirectAndClearCookie.
public XFuture<StreamWriter> sendRedirectAndClearCookie(RouterRequest req, String badCookieName) {
RedirectResponse httpResponse = new RedirectResponse(false, req.isHttps, req.domain, req.port, req.relativePath);
Http2Response response = responseCreator.createRedirect(originalHttp2Request, httpResponse);
responseCreator.addDeleteCookie(response, badCookieName);
log.info("sending REDIRECT(due to bad cookie) response responseSender=" + this);
return process(response);
}
Aggregations