use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.
the class StaticFileReader method runAsyncFileRead.
private CompletableFuture<Void> runAsyncFileRead(RequestInfo info, RenderStaticResponse renderStatic) throws IOException {
boolean isFile = true;
String fullFilePath = renderStatic.getFilePath();
if (fullFilePath == null) {
isFile = false;
fullFilePath = renderStatic.getDirectory() + renderStatic.getRelativePath();
}
String extension = null;
int lastDirIndex = fullFilePath.lastIndexOf("/");
int lastDot = fullFilePath.lastIndexOf(".");
if (lastDot > lastDirIndex) {
extension = fullFilePath.substring(lastDot + 1);
}
ResponseEncodingTuple tuple = responseCreator.createResponse(info.getRequest(), StatusCode.HTTP_200_OK, extension, "application/octet-stream", false);
Http2Response response = tuple.response;
//On startup, we protect developers from breaking clients. In http, all files that change
//must also change the hash automatically and the %%{ }%% tag generates those hashes so the
//files loaded are always the latest
Long timeMs = config.getStaticFileCacheTimeSeconds();
if (timeMs != null)
response.addHeader(new Http2Header(Http2HeaderName.CACHE_CONTROL, "max-age=" + timeMs));
Path file;
Compression compr = compressionLookup.createCompressionStream(info.getRouterRequest().encodings, extension, tuple.mimeType);
//during startup as I don't feel like paying a cpu penalty for compressing while live
if (compr != null && compr.getCompressionType().equals(routerConfig.getStartupCompression())) {
response.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, compr.getCompressionType()));
File routesCache = renderStatic.getTargetCache();
File fileReference;
if (isFile) {
String fileName = fullFilePath.substring(lastDirIndex + 1);
fileReference = new File(routesCache, fileName);
} else {
fileReference = new File(routesCache, renderStatic.getRelativePath());
}
fullFilePath = fileReference.getAbsolutePath();
file = fetchFile("Compressed File from cache=", fullFilePath + ".gz");
} else {
file = fetchFile("File=", fullFilePath);
}
AsynchronousFileChannel asyncFile = AsynchronousFileChannel.open(file, options, fileExecutor);
CompletableFuture<StreamWriter> future;
try {
log.info(() -> "sending chunked file via async read=" + file);
long length = file.toFile().length();
AtomicLong remaining = new AtomicLong(length);
future = info.getResponseSender().sendResponse(response).thenCompose(s -> readLoop(s, info.getPool(), file, asyncFile, 0, remaining));
} catch (Throwable e) {
future = new CompletableFuture<StreamWriter>();
future.completeExceptionally(e);
}
return //our finally block for failures
future.handle((s, exc) -> handleClose(info, s, exc)).thenAccept(s -> empty());
}
use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.
the class TestSBasicRequestResponse method testWithDataAndTrailingHeaders.
@Test
public void testWithDataAndTrailingHeaders() throws InterruptedException, ExecutionException, TimeoutException {
MockStreamWriter mockSw = new MockStreamWriter();
mockSw.setDefaultRetValToThis();
mockListener.addMockStreamToReturn(mockSw);
Http2Request request1 = Http2Requests.createRequest(1, false);
DataFrame data1 = Http2Requests.createData1(request1.getStreamId(), false);
Http2Trailers trailing = Http2Requests.createTrailers(request1.getStreamId());
mockChannel.send(request1);
PassedIn incoming1 = mockListener.getSingleRequest();
Assert.assertEquals(request1, incoming1.request);
mockChannel.send(data1);
DataFrame incoming2 = (DataFrame) mockSw.getSingleFrame();
Assert.assertEquals(3, incoming2.getData().getReadableSize());
//clear window update frames
Assert.assertEquals(2, mockChannel.getFramesAndClear().size());
mockChannel.send(trailing);
Http2Headers incoming = (Http2Headers) mockSw.getSingleFrame();
Assert.assertEquals(trailing, incoming);
Http2Response resp = Http2Requests.createResponse(request1.getStreamId(), false);
CompletableFuture<StreamWriter> future = incoming1.stream.sendResponse(resp);
Http2Msg response = mockChannel.getFrameAndClear();
Assert.assertEquals(resp, response);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
DataFrame data2 = Http2Requests.createData2(request1.getStreamId(), false);
writer.processPiece(data2);
DataFrame dataResp = (DataFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(1, dataResp.getData().getReadableSize());
Http2Trailers trailingResp = Http2Requests.createTrailers(request1.getStreamId());
writer.processPiece(trailingResp);
Http2Headers trailers = (Http2Headers) mockChannel.getFrameAndClear();
Assert.assertEquals(trailingResp, trailers);
}
use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.
the class TestSBasicRequestResponse method testWithData.
@Test
public void testWithData() throws InterruptedException, ExecutionException, TimeoutException {
MockStreamWriter mockSw = new MockStreamWriter();
mockSw.setDefaultRetValToThis();
mockListener.addMockStreamToReturn(mockSw);
Http2Request request1 = Http2Requests.createRequest(1, false);
DataFrame data = Http2Requests.createData1(request1.getStreamId(), true);
mockChannel.send(request1);
PassedIn incoming1 = mockListener.getSingleRequest();
Assert.assertEquals(request1, incoming1.request);
mockChannel.send(data);
DataFrame incoming = (DataFrame) mockSw.getSingleFrame();
Assert.assertEquals(3, incoming.getData().getReadableSize());
//clear window update frames
Assert.assertEquals(2, mockChannel.getFramesAndClear().size());
Http2Response resp = Http2Requests.createResponse(request1.getStreamId(), false);
CompletableFuture<StreamWriter> future = incoming1.stream.sendResponse(resp);
Http2Msg response = mockChannel.getFrameAndClear();
Assert.assertEquals(resp, response);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
DataFrame data2 = Http2Requests.createData2(request1.getStreamId(), true);
writer.processPiece(data2);
DataFrame dataResp = (DataFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(1, dataResp.getData().getReadableSize());
}
use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.
the class TestSMaxConcurrentSetting method sendTwoRequests.
private WriterHolder sendTwoRequests() throws InterruptedException, ExecutionException, TimeoutException {
PassedIn in1 = sendRequestToServer(1, true);
PassedIn in2 = sendRequestToServer(3, true);
Assert.assertTrue(in1.stream != in2.stream);
Http2Push push1 = Http2Requests.createPush(in1.request.getStreamId());
CompletableFuture<PushPromiseListener> future1 = in1.stream.openPushStream().process(push1);
Http2Msg push1Recv = mockChannel.getFrameAndClear();
Assert.assertEquals(push1, push1Recv);
PushPromiseListener pushWriter1 = future1.get(2, TimeUnit.SECONDS);
Http2Push push2 = Http2Requests.createPush(in2.request.getStreamId());
CompletableFuture<PushPromiseListener> future2 = in2.stream.openPushStream().process(push2);
Http2Msg push2Recv = mockChannel.getFrameAndClear();
Assert.assertEquals(push2, push2Recv);
Assert.assertEquals(push1, push1Recv);
PushPromiseListener writer2 = future2.get(2, TimeUnit.SECONDS);
//send the two responses following the pushes
Http2Response resp1 = Http2Requests.createResponse(push1.getPromisedStreamId(), false);
CompletableFuture<StreamWriter> fut1 = pushWriter1.processPushResponse(resp1);
StreamWriter writer1 = fut1.get(2, TimeUnit.SECONDS);
Http2Response resp2 = Http2Requests.createResponse(push2.getPromisedStreamId(), false);
CompletableFuture<StreamWriter> fut2 = writer2.processPushResponse(resp2);
Assert.assertTrue(!fut2.isDone());
Http2Response clientRecvResp = (Http2Response) mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, clientRecvResp);
return new WriterHolder(writer1, fut2, resp1, resp2);
}
use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.
the class Http1_1StreamImpl method sendResponse.
@Override
public CompletableFuture<StreamWriter> sendResponse(Http2Response headers) {
closeCheck();
HttpResponse response = Http2Translations.translateResponse(headers);
if (headers.isEndOfStream()) {
validateHeader(response);
remove(headers);
return write(response).thenApply(w -> {
permitQueue.releasePermit();
return new NoWritesWriter();
});
} else if (contentLengthGreaterThanZero(headers)) {
return write(response).thenApply(w -> new ContentLengthResponseWriter(headers));
}
response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
return write(response).thenApply(c -> new Http11ChunkedWriter());
}
Aggregations