-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize static handler, add more tests (#5492)
* Optimize StaticHandler::hit() * fix
- Loading branch information
Showing
6 changed files
with
226 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--TEST-- | ||
swoole_http_server/static_handler: link to a file outside the document root | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../../include/skipif.inc'; ?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../../include/bootstrap.php'; | ||
|
||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
$doc_root = __DIR__ . '/docroot'; | ||
$image_dir = 'image/'; | ||
mkdir($doc_root); | ||
mkdir($doc_root . '/' . $image_dir); | ||
$image_link = $doc_root . '/' . $image_dir . '/image.jpg'; | ||
symlink(TEST_IMAGE, $image_link); | ||
|
||
$cleanup_fn = function () use ($doc_root, $image_dir, $image_link) { | ||
if (is_file($image_link)) { | ||
unlink($image_link); | ||
} | ||
rmdir($doc_root . '/' . $image_dir); | ||
rmdir($doc_root); | ||
}; | ||
|
||
$pm = new ProcessManager; | ||
$pm->parentFunc = function () use ($pm, $doc_root, $image_dir, $image_link) { | ||
Swoole\Coroutine\run(function () use ($pm, $doc_root, $image_dir, $image_link) { | ||
$data = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}/{$image_dir}/image.jpg"); | ||
Assert::assert(md5($data) === md5_file(TEST_IMAGE)); | ||
}); | ||
$pm->kill(); | ||
echo "DONE\n"; | ||
}; | ||
$pm->childFunc = function () use ($pm, $doc_root, $image_dir, $image_link) { | ||
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE); | ||
$http->set([ | ||
'log_file' => '/dev/null', | ||
'open_http2_protocol' => true, | ||
'enable_static_handler' => true, | ||
'document_root' => $doc_root, | ||
'static_handler_locations' => ['/image'] | ||
]); | ||
$http->on('workerStart', function () use ($pm) { | ||
$pm->wakeup(); | ||
}); | ||
$http->on('request', function (Request $request, Response $response) { | ||
}); | ||
$http->start(); | ||
}; | ||
$pm->childFirst(); | ||
$pm->run(); | ||
$cleanup_fn(); | ||
?> | ||
--EXPECT-- | ||
DONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/swoole_http_server/static_handler/relative_path_2.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--TEST-- | ||
swoole_http_server/static_handler: static handler with relative path [2] | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../../include/skipif.inc'; ?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../../include/bootstrap.php'; | ||
|
||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
$pm = new ProcessManager; | ||
$pm->parentFunc = function () use ($pm) { | ||
foreach ([false, true] as $http2) { | ||
Swoole\Coroutine\run(function () use ($pm, $http2) { | ||
$data = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}/../examples/test.jpg", ['http2' => $http2]); | ||
Assert::notEmpty($data); | ||
Assert::same(md5($data), md5_file(TEST_IMAGE)); | ||
|
||
$data = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}/../docs/swoole-logo.svg", ['http2' => $http2]); | ||
Assert::eq("hello world", $data); | ||
}); | ||
} | ||
echo "DONE\n"; | ||
$pm->kill(); | ||
}; | ||
$pm->childFunc = function () use ($pm) { | ||
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE); | ||
$http->set([ | ||
'log_file' => '/dev/null', | ||
'open_http2_protocol' => true, | ||
'enable_static_handler' => true, | ||
'document_root' => SOURCE_ROOT_PATH . '/examples', | ||
]); | ||
$http->on('workerStart', function () use ($pm) { | ||
$pm->wakeup(); | ||
}); | ||
$http->on('request', function (Request $request, Response $response) use ($http) { | ||
$response->end("hello world"); | ||
}); | ||
$http->start(); | ||
}; | ||
$pm->childFirst(); | ||
$pm->run(); | ||
?> | ||
--EXPECT-- | ||
DONE |
60 changes: 60 additions & 0 deletions
60
tests/swoole_http_server/static_handler/relative_path_3.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--TEST-- | ||
swoole_http_server/static_handler: doc root with same prefix | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../../include/skipif.inc'; ?> | ||
--FILE-- | ||
<?php | ||
require __DIR__ . '/../../include/bootstrap.php'; | ||
|
||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
$doc1_root = __DIR__ . '/docroot'; | ||
$doc2_root = __DIR__ . '/docroot2'; | ||
mkdir($doc1_root); | ||
mkdir($doc2_root); | ||
file_put_contents($doc1_root . '/image.jpg', file_get_contents(TEST_IMAGE)); | ||
file_put_contents($doc2_root . '/uuid.txt', uniqid()); | ||
|
||
$cleanup_fn = function () use ($doc1_root, $doc2_root) { | ||
unlink($doc1_root . '/image.jpg'); | ||
unlink($doc2_root . '/uuid.txt'); | ||
rmdir($doc1_root); | ||
rmdir($doc2_root); | ||
}; | ||
|
||
$pm = new ProcessManager; | ||
$pm->parentFunc = function () use ($pm, $doc1_root, $doc2_root) { | ||
Swoole\Coroutine\run(function () use ($pm, $doc1_root, $doc2_root) { | ||
$data = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}/../docroot/image.jpg"); | ||
Assert::assert(md5($data) === md5_file(TEST_IMAGE)); | ||
|
||
$data = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}/../docroot2/uuid.txt"); | ||
Assert::isEmpty($data); | ||
}); | ||
$pm->kill(); | ||
echo "DONE\n"; | ||
}; | ||
$pm->childFunc = function () use ($pm, $doc1_root, $doc2_root) { | ||
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE); | ||
$http->set([ | ||
'log_file' => '/dev/null', | ||
'open_http2_protocol' => true, | ||
'enable_static_handler' => true, | ||
'document_root' => $doc1_root, | ||
]); | ||
$http->on('workerStart', function () use ($pm) { | ||
$pm->wakeup(); | ||
}); | ||
$http->on('request', function (Request $request, Response $response) { | ||
}); | ||
$http->start(); | ||
}; | ||
$pm->childFirst(); | ||
$pm->run(); | ||
$cleanup_fn(); | ||
?> | ||
--EXPECT-- | ||
DONE |