From e4320db87ffcbb285d16ccd7254651ce0556e407 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Mon, 21 Mar 2016 14:49:43 +0100 Subject: [PATCH] Finish some send file missing work --- .../core/http/impl/FileStreamChannel.java | 4 +- .../http/impl/Http2ServerResponseImpl.java | 2 +- .../core/http/impl/VertxHttp2NetSocket.java | 2 +- .../io/vertx/test/core/Http2ServerTest.java | 41 +++++++++++++++---- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/vertx/core/http/impl/FileStreamChannel.java b/src/main/java/io/vertx/core/http/impl/FileStreamChannel.java index 1a2e7efd16c..c4e026241f8 100644 --- a/src/main/java/io/vertx/core/http/impl/FileStreamChannel.java +++ b/src/main/java/io/vertx/core/http/impl/FileStreamChannel.java @@ -34,6 +34,7 @@ import io.netty.channel.EventLoop; import io.netty.channel.nio.NioEventLoop; import io.netty.handler.stream.ChunkedFile; +import io.netty.handler.stream.ChunkedStream; import io.netty.handler.stream.ChunkedWriteHandler; import io.vertx.core.AsyncResult; import io.vertx.core.Future; @@ -63,6 +64,7 @@ class FileStreamChannel extends AbstractChannel { FileStreamChannel( Handler> resultHandler, VertxHttp2Stream stream, + long offset, long length) { super(null, Id.INSTANCE); @@ -75,7 +77,7 @@ protected void initChannel(Channel ch) throws Exception { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof RandomAccessFile) { - ChannelFuture fut = ctx.writeAndFlush(new ChunkedFile((RandomAccessFile) evt)); + ChannelFuture fut = ctx.writeAndFlush(new ChunkedFile((RandomAccessFile) evt, offset, length, 8192 /* default chunk size */ )); fut.addListener(f -> { if (resultHandler != null) { if (f.isSuccess()) { diff --git a/src/main/java/io/vertx/core/http/impl/Http2ServerResponseImpl.java b/src/main/java/io/vertx/core/http/impl/Http2ServerResponseImpl.java index 8f94b61961a..abfd5604424 100644 --- a/src/main/java/io/vertx/core/http/impl/Http2ServerResponseImpl.java +++ b/src/main/java/io/vertx/core/http/impl/Http2ServerResponseImpl.java @@ -497,7 +497,7 @@ public HttpServerResponse sendFile(String filename, long offset, long length, Ha resultHandler.handle(Future.succeededFuture()); }); } - }, stream_, contentLength); + }, stream_, offset, contentLength); drainHandler(fileChannel.drainHandler); ctx.channel().eventLoop().register(fileChannel); fileChannel.pipeline().fireUserEventTriggered(raf); diff --git a/src/main/java/io/vertx/core/http/impl/VertxHttp2NetSocket.java b/src/main/java/io/vertx/core/http/impl/VertxHttp2NetSocket.java index 021c936fc47..c997bef8d60 100644 --- a/src/main/java/io/vertx/core/http/impl/VertxHttp2NetSocket.java +++ b/src/main/java/io/vertx/core/http/impl/VertxHttp2NetSocket.java @@ -215,7 +215,7 @@ public NetSocket sendFile(String filename, long offset, long length, Handler { HttpServerResponse resp = req.response(); resp.bodyEndHandler(v -> { - assertEquals(resp.bytesWritten(), len); + assertEquals(resp.bytesWritten(), length); complete(); }); - resp.sendFile(f.getAbsolutePath()); + resp.sendFile(path, offset, length); }); startServer(); TestClient client = new TestClient(); @@ -1315,7 +1327,7 @@ public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int buffer.appendBuffer(Buffer.buffer(data.duplicate())); if (endOfStream) { vertx.runOnContext(v -> { - assertEquals("" + len, responseHeaders.get("content-length").toString()); + assertEquals("" + length, responseHeaders.get("content-length").toString()); assertEquals(expected, buffer); complete(); }); @@ -2087,11 +2099,24 @@ public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int @Test public void testNetSocketSendFile() throws Exception { - Buffer expected = TestUtils.randomBuffer(1000 * 1000); + Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(1000 * 1000)); File tmp = createTempFile(expected); + testNetSocketSendFile(expected, tmp.getAbsolutePath(), 0, expected.length()); + } + + @Test + public void testNetSocketSendFileRange() throws Exception { + Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(1000 * 1000)); + File tmp = createTempFile(expected); + int from = 200 * 1000; + int to = 700 * 1000; + testNetSocketSendFile(expected.slice(from, to), tmp.getAbsolutePath(), from, to - from); + } + + private void testNetSocketSendFile(Buffer expected, String path, long offset, long length) throws Exception { server.requestHandler(req -> { NetSocket socket = req.netSocket(); - socket.sendFile(tmp.getAbsolutePath(), ar -> { + socket.sendFile(path, offset, length, ar -> { assertTrue(ar.succeeded()); socket.end(); });