Skip to content

Commit

Permalink
Finish some send file missing work
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Mar 21, 2016
1 parent cdd9fc3 commit e4320db
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/vertx/core/http/impl/FileStreamChannel.java
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,7 @@ class FileStreamChannel extends AbstractChannel {
FileStreamChannel(
Handler<AsyncResult<Long>> resultHandler,
VertxHttp2Stream stream,
long offset,
long length) {
super(null, Id.INSTANCE);

Expand All @@ -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()) {
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -215,7 +215,7 @@ public NetSocket sendFile(String filename, long offset, long length, Handler<Asy
resultHandler.handle(Future.succeededFuture());
});
}
}, this, contentLength);
}, this, offset, contentLength);
drainHandler(fileChannel.drainHandler);
channel.eventLoop().register(fileChannel);
fileChannel.pipeline().fireUserEventTriggered(raf);
Expand Down
41 changes: 33 additions & 8 deletions src/test/java/io/vertx/test/core/Http2ServerTest.java
Expand Up @@ -1288,17 +1288,29 @@ private static File createTempFile(Buffer buffer) throws Exception {

@Test
public void testSendFile() throws Exception {
Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(1000 * 1000));
File tmp = createTempFile(expected);
testSendFile(expected, tmp.getAbsolutePath(), 0, expected.length());
}

@Test
public void testSendFileRange() throws Exception {
Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(1000 * 1000));
File tmp = createTempFile(expected);
int from = 200 * 1000;
int to = 700 * 1000;
testSendFile(expected.slice(from, to), tmp.getAbsolutePath(), from, to - from);
}

private void testSendFile(Buffer expected, String path, long offset, long length) throws Exception {
waitFor(2);
int len = 1000 * 1000;
Buffer expected = TestUtils.randomBuffer(len);
File f = createTempFile(expected);
server.requestHandler(req -> {
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();
Expand All @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down

0 comments on commit e4320db

Please sign in to comment.