Skip to content

Commit

Permalink
Support send empty file (#3318)
Browse files Browse the repository at this point in the history
  • Loading branch information
twose authored May 14, 2020
1 parent e2fda65 commit bb43cde
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
16 changes: 7 additions & 9 deletions swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -934,12 +934,7 @@ static PHP_METHOD(swoole_http_response, sendfile)
php_swoole_sys_error(E_WARNING, "stat(%s) failed", file);
RETURN_FALSE;
}
if (file_stat.st_size == 0)
{
php_swoole_error(E_WARNING, "can't send empty file[%s]", file);
RETURN_FALSE;
}
if (file_stat.st_size <= offset)
if (file_stat.st_size < offset)
{
php_swoole_error(E_WARNING, "parameter $offset[" ZEND_LONG_FMT "] exceeds the file size", offset);
RETURN_FALSE;
Expand Down Expand Up @@ -981,10 +976,13 @@ static PHP_METHOD(swoole_http_response, sendfile)
}
}

if (!ctx->sendfile(ctx, file, l_file, offset, length))
if (length != 0)
{
ctx->close(ctx);
RETURN_FALSE;
if (!ctx->sendfile(ctx, file, l_file, offset, length))
{
ctx->close(ctx);
RETURN_FALSE;
}
}

ctx->end = 1;
Expand Down
37 changes: 37 additions & 0 deletions tests/swoole_http_server/send_empty_file.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
swoole_http_server: send empty file
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

const TMP_FILE = '/tmp/sendfile.txt';

$pm = new ProcessManager;
$pm->parentFunc = function ($pid) use ($pm) {
Co\run(function () use ($pm) {
file_put_contents(TMP_FILE, '');
$recv_file = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}");
unlink(TMP_FILE);
Assert::same($recv_file, '');
});
echo "DONE\n";
$pm->kill();
};
$pm->childFunc = function () use ($pm) {
$http = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
$http->set(['worker_num' => 1]);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});
$http->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$response->sendfile(TMP_FILE);
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE

0 comments on commit bb43cde

Please sign in to comment.