You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
16
vendor/swoole/tests/include/api/swoole_http_client/connect_host_not_found.php
vendored
Executable file
16
vendor/swoole/tests/include/api/swoole_http_client/connect_host_not_found.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
function main() {
|
||||
$cli = new \swoole_http_client("11.11.11.11", 9000);
|
||||
|
||||
$cli->on('close', function($cli) {
|
||||
assert(false);
|
||||
});
|
||||
|
||||
$cli->on('error', function($cli) {
|
||||
echo "error";
|
||||
});
|
||||
|
||||
$cli->get('/', function(swoole_http_client $cli) {});
|
||||
}
|
||||
|
||||
main();
|
12
vendor/swoole/tests/include/api/swoole_http_client/connect_port_not_listen.php
vendored
Executable file
12
vendor/swoole/tests/include/api/swoole_http_client/connect_port_not_listen.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
$cli = new swoole_http_client("127.0.0.1", 65535);
|
||||
|
||||
$cli->on('close', function($cli) {
|
||||
echo "close\n";
|
||||
});
|
||||
|
||||
$cli->on('error', function($cli) {
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
$cli->get('/', function(swoole_http_client $cli) {});
|
19
vendor/swoole/tests/include/api/swoole_http_client/connect_timeout.php
vendored
Executable file
19
vendor/swoole/tests/include/api/swoole_http_client/connect_timeout.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
$cli = new \swoole_http_client("127.0.0.1", 65535);
|
||||
|
||||
$cli->on('close', function($cli) {
|
||||
echo 'close\n';
|
||||
});
|
||||
|
||||
$cli->on('error', function($cli) {
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
swoole_timer_after(500, function() {
|
||||
swoole_event_exit();
|
||||
echo "time out\n";
|
||||
});
|
||||
$cli->get('/', function(swoole_http_client $cli) {});
|
10
vendor/swoole/tests/include/api/swoole_http_client/http_request_connect_timeout.php
vendored
Executable file
10
vendor/swoole/tests/include/api/swoole_http_client/http_request_connect_timeout.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
$httpClient = new swoole_http_client("11.11.11.11", 9000);
|
||||
$httpClient->set(['timeout' => 1]);
|
||||
|
||||
$httpClient->get("/", function ($client)
|
||||
{
|
||||
assert($client->errCode == 110);
|
||||
assert($client->statusCode == -1);
|
||||
assert(!$client->body);
|
||||
});
|
38
vendor/swoole/tests/include/api/swoole_http_client/meomry_leak.php
vendored
Executable file
38
vendor/swoole/tests/include/api/swoole_http_client/meomry_leak.php
vendored
Executable file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
// swoole_server
|
||||
$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
||||
socket_setopt($s, SOL_SOCKET, SO_REUSEADDR, 1);
|
||||
socket_bind($s, "127.0.0.1", 9090);
|
||||
socket_listen($s);
|
||||
|
||||
$func = "hello";
|
||||
|
||||
while($conn = socket_accept($s)) {
|
||||
socket_write($conn, "HTTP/1.1 200 OK\r\n\r\n");
|
||||
socket_write($conn, "HTTP/1.1 200 OK\r\nX-Func: {$func}\r\n\r\n");
|
||||
socket_close($conn);
|
||||
}
|
||||
|
||||
|
||||
// client
|
||||
function hello() {
|
||||
echo "\n\nhello world!\n\n";
|
||||
swoole_event_exit();
|
||||
exit();
|
||||
}
|
||||
|
||||
function req() {
|
||||
$cli = new swoole_http_client("127.0.0.1", 9090);
|
||||
$cli->on("close", function() {
|
||||
req();
|
||||
});
|
||||
$cli->get("/", function(swoole_http_client $cli) {
|
||||
echo "receive:", $cli->body, "\n";
|
||||
});
|
||||
}
|
||||
|
||||
req();
|
||||
|
||||
|
||||
|
27
vendor/swoole/tests/include/api/swoole_http_client/on_error_close.php
vendored
Executable file
27
vendor/swoole/tests/include/api/swoole_http_client/on_error_close.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
// 旧版本因为没有做判断, 构造失败之后, 之后调用close会core
|
||||
|
||||
function error_close_test($desc, $ip, $port)
|
||||
{
|
||||
$cli = new swoole_http_client($ip, $port);
|
||||
$cli->on("error", function() use($desc) { echo "$desc error\n"; });
|
||||
$cli->on("close", function() use($desc) { echo "$desc close\n"; });
|
||||
$cli->get("/", function($cli){ });
|
||||
swoole_timer_after(1000, function() use($cli) { $cli->close(); });
|
||||
}
|
||||
|
||||
// 触发close 回调
|
||||
error_close_test("baidu", "115.239.211.112", 80);
|
||||
|
||||
// 触发error回调
|
||||
error_close_test("localhost", "127.0.0.1", 9090);
|
||||
|
||||
// TODO 此处行为不正确
|
||||
// 应该校验参数是否合法ip, 抛出异常(构造函数无法返回错误)
|
||||
error_close_test("\\0", "\0", 9090);
|
||||
|
||||
// TODO 同上
|
||||
error_close_test("", "null string", 9090);
|
27
vendor/swoole/tests/include/api/swoole_http_client/on_receive_core.php
vendored
Executable file
27
vendor/swoole/tests/include/api/swoole_http_client/on_receive_core.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
function create()
|
||||
{
|
||||
$cli = new swoole_http_client("127.0.0.1", 80);
|
||||
$cli->setHeaders([
|
||||
"Host" => "xxx.xxx.xxx",
|
||||
]);
|
||||
$cli->on("error", function() { echo "error"; });
|
||||
$cli->on("close", function() { echo "close\n\n"; post(create()); });
|
||||
return $cli;
|
||||
}
|
||||
post(create());
|
||||
function post($cli) {
|
||||
$cli->post("/xxx/xxx/xxx", [
|
||||
"ua" => "younipf",
|
||||
"debug" => "json",
|
||||
], function($cli) {
|
||||
echo $cli->statusCode, "\n";
|
||||
post($cli);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
$payload = <<<HTML
|
||||
HTTP/1.1 400 Bad Request\r\nDate: Fri, 10 Mar 2017 10:47:07 GMT\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 242\r\nConnection: close\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n<html>\r\n<head><title>400 Bad Request</title></head>\r\n<body bgcolor=\"white\">\r\n<h1>400 Bad Request</h1>\r\n<p>Your browser sent a request that this swoole_server could not understand.</body>\r\n</html>\r\n
|
||||
HTML;
|
352
vendor/swoole/tests/include/api/swoole_http_client/simple_http_client.php
vendored
Executable file
352
vendor/swoole/tests/include/api/swoole_http_client/simple_http_client.php
vendored
Executable file
@ -0,0 +1,352 @@
|
||||
<?php
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
function makeHttpClient($host = HTTP_SERVER_HOST, $port = HTTP_SERVER_PORT, $ssl = false, $output = false, callable $done = null)
|
||||
{
|
||||
$httpClient = new \swoole_http_client($host, $port, $ssl);
|
||||
|
||||
$httpClient->set([
|
||||
"socket_buffer_size" => 1024 * 1024 * 2,
|
||||
'timeout' => 1.0,
|
||||
]);
|
||||
if ($ssl) {
|
||||
$httpClient->set([
|
||||
'ssl_cert_file' => __DIR__ . '../swoole_http_server/localhost-ssl/swoole_server.crt',
|
||||
'ssl_key_file' => __DIR__ . '../swoole_http_server/localhost-ssl/swoole_server.key',
|
||||
]);
|
||||
}
|
||||
|
||||
$httpClient->on("connect", function(\swoole_http_client $httpClient) {
|
||||
assert($httpClient->isConnected() === true);
|
||||
// debug_log("connect");
|
||||
});
|
||||
|
||||
$httpClient->on("error", function(\swoole_http_client $httpClient) use($output, $done) {
|
||||
if ($output) {
|
||||
echo "error";
|
||||
}
|
||||
if ($done) {
|
||||
$done();
|
||||
}
|
||||
// debug_log("error");
|
||||
});
|
||||
|
||||
$httpClient->on("close", function(\swoole_http_client $httpClient) use($output, $done) {
|
||||
if ($output) {
|
||||
echo "close";
|
||||
}
|
||||
if ($done) {
|
||||
$done();
|
||||
}
|
||||
// debug_log("close");
|
||||
});
|
||||
|
||||
return $httpClient;
|
||||
}
|
||||
|
||||
function testUri($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
|
||||
$ok = $httpClient->get("/uri", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === "/uri");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testHttpGet($host, $port, array $query, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
|
||||
$queryStr = http_build_query($query);
|
||||
$ok = $httpClient->get("/get?$queryStr", function (\swoole_http_client $httpClient) use ($query, $fin, $queryStr)
|
||||
{
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
if ($queryStr === "")
|
||||
{
|
||||
assert($httpClient->body === "null");
|
||||
}
|
||||
else
|
||||
{
|
||||
$ret = json_decode($httpClient->body, true);
|
||||
assert(arrayEqual($ret, $query, false));
|
||||
}
|
||||
if ($fin)
|
||||
{
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
|
||||
function testPost($host, $port, array $query, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
|
||||
$ok = $httpClient->post("/post", $query, function(\swoole_http_client $httpClient) use($query, $fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
// $httpClient->headers;
|
||||
$ret = json_decode($httpClient->body, true);
|
||||
assert(arrayEqual($ret, $query, false));
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
|
||||
function testMethod($host, $port, $method, $data = null, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
|
||||
$ok = $httpClient->setMethod($method);
|
||||
assert($ok);
|
||||
if ($data) {
|
||||
$httpClient->setData($data);
|
||||
}
|
||||
$ok = $httpClient->execute("/method", function(\swoole_http_client $httpClient) use($method, $fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === $method);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testCookie($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$ok = $httpClient->setCookies(["hello" => "world"]);
|
||||
assert($ok);
|
||||
|
||||
$ok = $httpClient->get("/cookie", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === "{\"hello\":\"world\"}");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
// setCookies 已经加入类型限制
|
||||
function testCookieCore($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$ok = $httpClient->setCookies("hello=world; path=/;");
|
||||
assert($ok);
|
||||
|
||||
$ok = $httpClient->get("/cookie", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === "null");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testHeader($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$httpClient->setting += ["keep_alive" => true];
|
||||
// TODO 只要调用setHeaders 则会变为 connection close
|
||||
$ok = $httpClient->setHeaders(["hello" => "world"]);
|
||||
assert($ok);
|
||||
|
||||
$ok = $httpClient->get("/header", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
$headers = json_decode($httpClient->body, true);
|
||||
assert(isset($headers["hello"]) && $headers["hello"] === "world");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
// 已经修复
|
||||
function testHeaderCore($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
|
||||
// $httpClient->setting += ["keep_alive" => true];
|
||||
// COREDUMP
|
||||
// 旧版传递字符串会发生coredump
|
||||
// $httpClient->setHeaders("Hello: World\r\nHello: World\r\n");
|
||||
$r = $httpClient->setHeaders(["\0" => "\0"]);
|
||||
|
||||
$ok = $httpClient->get("/header", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
$headers = json_decode($httpClient->body, true);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testSleep($host, $port)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$ok = $httpClient->get("/sleep", function(\swoole_http_client $httpClient) {
|
||||
assert(false);
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
// http message 分多次接受有问题 (10k message)
|
||||
function testBigBodyMethodNotSupport($host, $port, callable $fin = null)
|
||||
{
|
||||
if ($fin) {
|
||||
$httpClient = makeHttpClient($host, $port, false, true, $fin);
|
||||
} else {
|
||||
$httpClient = makeHttpClient($host, $port, false, true);
|
||||
}
|
||||
$body = str_repeat("\0", 10240);
|
||||
$ok = $httpClient->post("/", $body, function(\swoole_http_client $httpClient) use($fin) {
|
||||
echo "SUCCESS\n";
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
// http message 分多次接受有问题 (间隔1s发送)
|
||||
function testBigBodyMethodNotSupport2($host, $port, callable $fin = null)
|
||||
{
|
||||
$cli = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
|
||||
|
||||
$cli->on("connect", function(swoole_client $cli) {
|
||||
$cli->send("POST / HTTP/1.0\r\nHost: 127.0.0.1\r\nConnection: close\r\nContent-Length: 1\r\n\r\n");
|
||||
swoole_timer_after(1, function() use($cli) {
|
||||
$cli->send("\0");
|
||||
});
|
||||
});
|
||||
|
||||
$cli->on("receive", function(swoole_client $cli, $data){
|
||||
echo "SUCCESS";
|
||||
});
|
||||
|
||||
$cli->on("error", function(swoole_client $cli) use($fin) {
|
||||
echo "error";
|
||||
if ($fin) {
|
||||
$fin();
|
||||
}
|
||||
});
|
||||
|
||||
$cli->on("close", function(swoole_client $cli) use($fin) {
|
||||
echo "close";
|
||||
if ($fin) {
|
||||
$fin();
|
||||
}
|
||||
});
|
||||
|
||||
$cli->connect($host, $port);
|
||||
}
|
||||
|
||||
function testSendfile($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$httpClient->setMethod("GET");
|
||||
$ok = $httpClient->execute("/file", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testRawCookie($host, $port, $cookie, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$httpClient->setMethod("POST");
|
||||
$httpClient->setData($cookie);
|
||||
$ok = $httpClient->execute("/rawcookie", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
|
||||
function testRawcontent($host, $port, $data, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
if ($data !== false) {
|
||||
$httpClient->setData($data);
|
||||
}
|
||||
|
||||
$httpClient->setMethod("POST");
|
||||
|
||||
$ok = $httpClient->execute("/rawcontent", function(\swoole_http_client $httpClient) use($fin, $data) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testExecute($host, $port, $method, $data, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
if ($data !== false) {
|
||||
$httpClient->setData($data);
|
||||
}
|
||||
|
||||
if ($method) {
|
||||
$httpClient->setMethod("POST");
|
||||
}
|
||||
|
||||
$ok = $httpClient->execute("/content_length", function(\swoole_http_client $httpClient) use($fin, $data) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function request($host, $port, $method, $url, $body, array $header, array $cookie, callable $finish)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port);
|
||||
$httpClient->setMethod($method);
|
||||
|
||||
if ($cookie) {
|
||||
$httpClient->setCookies($cookie);
|
||||
}
|
||||
if ($header) {
|
||||
$httpClient->setCookies($header);
|
||||
}
|
||||
|
||||
if ($body) {
|
||||
$httpClient->setData($body);
|
||||
}
|
||||
|
||||
$httpClient->setting += ["keep_alive" => false];
|
||||
$httpClient->execute($url, function(\swoole_http_client $httpClient) use($finish) {
|
||||
$finish($httpClient);
|
||||
$httpClient->close();
|
||||
});
|
||||
}
|
75
vendor/swoole/tests/include/api/swoole_http_client/simple_http_client_test.php
vendored
Executable file
75
vendor/swoole/tests/include/api/swoole_http_client/simple_http_client_test.php
vendored
Executable file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
`pkill php-fpm`;
|
||||
require __DIR__ . "/../../../include/bootstrap.php";
|
||||
require_once __DIR__ . "/simple_http_client.php";
|
||||
|
||||
$host = HTTP_SERVER_HOST;
|
||||
$port = HTTP_SERVER_PORT;
|
||||
|
||||
|
||||
$data = null;
|
||||
testExecute($host, $port, null, $data, function($httpClient) use($data) {
|
||||
assert(0 === intval($httpClient->body));
|
||||
echo "SUCCESS";
|
||||
});
|
||||
|
||||
|
||||
$data = null;
|
||||
testExecute($host, $port, "POST", $data, function($httpClient) use($data) {
|
||||
assert(0 === intval($httpClient->body));
|
||||
echo "SUCCESS";
|
||||
});
|
||||
|
||||
|
||||
$data = RandStr::gen(rand(0, 1024));
|
||||
testExecute($host, $port, "POST", $data, function($httpClient) use($data) {
|
||||
assert(strlen($data) === intval($httpClient->body));
|
||||
echo "SUCCESS";
|
||||
});
|
||||
|
||||
exit;
|
||||
|
||||
|
||||
|
||||
testHttpsHeaderCore($host, $port);
|
||||
testUri($host, $port);
|
||||
|
||||
testUri($host, $port);
|
||||
testGet($host, $port, []);
|
||||
testGet($host, $port, $_SERVER);
|
||||
testPost($host, $port, $_SERVER);
|
||||
|
||||
testMethod($host, $port, "GET");
|
||||
testMethod($host, $port, "DELETE");
|
||||
|
||||
testMethod($host, $port, "POST", "payload");
|
||||
testMethod($host, $port, "PUT", "payload");
|
||||
testMethod($host, $port, "PATCH", "payload");
|
||||
|
||||
|
||||
// TODO bug, 没有校验
|
||||
// testMethod($host, $port, "GET", "http_body");
|
||||
// testMethod($host, $port, "DELETE", "http_body");
|
||||
//testMethod($host, $port, "POST", null);
|
||||
//testMethod($host, $port, "PUT", null);
|
||||
//testMethod($host, $port, "PATCH", null);
|
||||
|
||||
|
||||
testCookie($host, $port);
|
||||
// TODO coredump
|
||||
// testCookieCore($host, $port);
|
||||
|
||||
testHttpsHeaderCore($host, $port);
|
||||
testHeader($host, $port);
|
||||
|
||||
testSleep($host, $port);
|
||||
|
||||
|
||||
|
||||
//request($host, $port, "GET", "/", null,
|
||||
// ["cookie_key" => "cookie_value"],
|
||||
// ["header_key" => "header_value"],
|
||||
// swoole_function(swoole_http_client $cli) {
|
||||
// assert($cli->body === "Hello World!");
|
||||
// });
|
280
vendor/swoole/tests/include/api/swoole_http_client/simple_https_client.php
vendored
Executable file
280
vendor/swoole/tests/include/api/swoole_http_client/simple_https_client.php
vendored
Executable file
@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
|
||||
/*
|
||||
class swoole_http_client
|
||||
{
|
||||
public swoole_function __construct() {}
|
||||
public swoole_function __destruct() {}
|
||||
public swoole_function set() {}
|
||||
public swoole_function setMethod() {}
|
||||
public swoole_function setHeaders() {}
|
||||
public swoole_function setCookies() {}
|
||||
public swoole_function setData() {}
|
||||
public swoole_function execute() {}
|
||||
public swoole_function push() {}
|
||||
public swoole_function get() {}
|
||||
public swoole_function post() {}
|
||||
public swoole_function isConnected() {}
|
||||
public swoole_function close() {}
|
||||
public swoole_function on() {}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
function addTimer(\swoole_http_client $httpClient)
|
||||
{
|
||||
if (property_exists($httpClient, "timeo_id")) {
|
||||
return false;
|
||||
}
|
||||
return $httpClient->timeo_id = swoole_timer_after(1000, function() use($httpClient) {
|
||||
debug_log("http request timeout");
|
||||
|
||||
// TODO 超时强制关闭连接 server端: ERROR swFactoryProcess_finish (ERROR 1005): session#%d does not exist.
|
||||
$httpClient->close();
|
||||
assert($httpClient->isConnected() === false);
|
||||
});
|
||||
}
|
||||
|
||||
function cancelTimer($httpClient)
|
||||
{
|
||||
if (property_exists($httpClient, "timeo_id")) {
|
||||
$ret = swoole_timer_clear($httpClient->timeo_id);
|
||||
unset($httpClient->timeo_id);
|
||||
return $ret;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
function makeHttpClient($host = HTTP_SERVER_HOST, $port = HTTP_SERVER_PORT, $ssl = true)
|
||||
{
|
||||
$httpClient = new \swoole_http_client($host, $port, $ssl);
|
||||
|
||||
$httpClient->set([
|
||||
'timeout' => 1,
|
||||
"socket_buffer_size" => 1024 * 1024 * 2,
|
||||
]);
|
||||
if ($ssl) {
|
||||
$httpClient->set([
|
||||
'ssl_cert_file' => __DIR__ . '/../swoole_http_server/localhost-ssl/server.crt',
|
||||
'ssl_key_file' => __DIR__ . '/../swoole_http_server/localhost-ssl/server.key',
|
||||
]);
|
||||
}
|
||||
|
||||
$httpClient->on("connect", function(\swoole_http_client $httpClient) {
|
||||
assert($httpClient->isConnected() === true);
|
||||
// debug_log("connect");
|
||||
});
|
||||
|
||||
$httpClient->on("error", function(\swoole_http_client $httpClient) {
|
||||
// debug_log("error");
|
||||
});
|
||||
|
||||
$httpClient->on("close", function(\swoole_http_client $httpClient) {
|
||||
// debug_log("close");
|
||||
});
|
||||
|
||||
return $httpClient;
|
||||
}
|
||||
|
||||
function testUri($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
|
||||
addTimer($httpClient);
|
||||
$ok = $httpClient->get("/uri", function(\swoole_http_client $httpClient) use($fin) {
|
||||
cancelTimer($httpClient);
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === "/uri");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testHttpsGet($host, $port, array $query, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
|
||||
$queryStr = http_build_query($query);
|
||||
$ok = $httpClient->get("/get?$queryStr", function(\swoole_http_client $httpClient) use($query, $fin, $queryStr) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
// $httpClient->headers;
|
||||
if ($queryStr === "") {
|
||||
assert($httpClient->body === "null");
|
||||
} else {
|
||||
$ret = json_decode($httpClient->body, true);
|
||||
assert(arrayEqual($ret, $query, false));
|
||||
}
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
|
||||
function testPost($host, $port, array $query, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
|
||||
$ok = $httpClient->post("/post", $query, function(\swoole_http_client $httpClient) use($query, $fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
// $httpClient->headers;
|
||||
$ret = json_decode($httpClient->body, true);
|
||||
assert(arrayEqual($ret, $query, false));
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
|
||||
function testMethod($host, $port, $method, $data = null, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
|
||||
addTimer($httpClient);
|
||||
$ok = $httpClient->setMethod($method);
|
||||
assert($ok);
|
||||
if ($data) {
|
||||
$httpClient->setData($data);
|
||||
}
|
||||
$ok = $httpClient->execute("/method", function(\swoole_http_client $httpClient) use($method, $fin) {
|
||||
cancelTimer($httpClient);
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === $method);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testCookie($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
addTimer($httpClient);
|
||||
$ok = $httpClient->setCookies(["hello" => "world"]);
|
||||
assert($ok);
|
||||
|
||||
$ok = $httpClient->get("/cookie", function(\swoole_http_client $httpClient) use($fin) {
|
||||
cancelTimer($httpClient);
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
assert($httpClient->body === "{\"hello\":\"world\"}");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testCookieCore($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
addTimer($httpClient);
|
||||
$ok = $httpClient->setCookies("hello=world; path=/;");
|
||||
assert($ok);
|
||||
|
||||
$ok = $httpClient->get("/cookie", function(\swoole_http_client $httpClient) use($fin) {
|
||||
cancelTimer($httpClient);
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
var_dump($httpClient->body);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testHeader($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
addTimer($httpClient);
|
||||
|
||||
$httpClient->setting += ["keep_alive" => true];
|
||||
// TODO 只要调用setHeaders 则会变为 connection close
|
||||
$ok = $httpClient->setHeaders(["hello" => "world"]);
|
||||
assert($ok);
|
||||
|
||||
$ok = $httpClient->get("/header", function(\swoole_http_client $httpClient) use($fin) {
|
||||
cancelTimer($httpClient);
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
$headers = json_decode($httpClient->body, true);
|
||||
assert(isset($headers["hello"]) && $headers["hello"] === "world");
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
// 已经修复
|
||||
function testHttpsHeaderCore($host, $port, callable $fin = null)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
// $httpClient->setting += ["keep_alive" => true];
|
||||
// COREDUMP
|
||||
// 旧版传递字符串会发生coredump
|
||||
// $httpClient->setHeaders("Hello: World\r\nHello: World\r\n");
|
||||
$r = $httpClient->setHeaders(["\0" => "\0"]);
|
||||
|
||||
$ok = $httpClient->get("/header", function(\swoole_http_client $httpClient) use($fin) {
|
||||
assert($httpClient->statusCode === 200);
|
||||
assert($httpClient->errCode === 0);
|
||||
$headers = json_decode($httpClient->body, true);
|
||||
if ($fin) {
|
||||
$fin($httpClient);
|
||||
}
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
function testSleep($host, $port)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
addTimer($httpClient);
|
||||
|
||||
$ok = $httpClient->get("/sleep", function(\swoole_http_client $httpClient) {
|
||||
assert(false);
|
||||
});
|
||||
assert($ok);
|
||||
}
|
||||
|
||||
|
||||
function request($host, $port, $method, $url, $body, array $header, array $cookie, callable $finish)
|
||||
{
|
||||
$httpClient = makeHttpClient($host, $port, true);
|
||||
addTimer($httpClient);
|
||||
$httpClient->setMethod($method);
|
||||
|
||||
if ($cookie) {
|
||||
$httpClient->setCookies($cookie);
|
||||
}
|
||||
if ($header) {
|
||||
$httpClient->setCookies($header);
|
||||
}
|
||||
|
||||
if ($body) {
|
||||
$httpClient->setData($body);
|
||||
}
|
||||
|
||||
$httpClient->setting += ["keep_alive" => false];
|
||||
$httpClient->execute($url, function(\swoole_http_client $httpClient) use($finish) {
|
||||
cancelTimer($httpClient);
|
||||
$finish($httpClient);
|
||||
$httpClient->close();
|
||||
});
|
||||
}
|
44
vendor/swoole/tests/include/api/swoole_http_client/simple_https_client_test.php
vendored
Executable file
44
vendor/swoole/tests/include/api/swoole_http_client/simple_https_client_test.php
vendored
Executable file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
require_once __DIR__ . "/simple_https_client.php";
|
||||
|
||||
|
||||
//request($host, $port, "GET", "/", null,
|
||||
// ["cookie_key" => "cookie_value"],
|
||||
// ["header_key" => "header_value"],
|
||||
// swoole_function(swoole_http_client $cli) {
|
||||
// assert($cli->body === "Hello World!");
|
||||
// });
|
||||
|
||||
|
||||
|
||||
testUri($host, $port);
|
||||
testGet($host, $port, []);
|
||||
testGet($host, $port, $_SERVER);
|
||||
testPost($host, $port, $_SERVER);
|
||||
|
||||
testMethod($host, $port, "GET");
|
||||
testMethod($host, $port, "DELETE");
|
||||
|
||||
testMethod($host, $port, "POST", "payload");
|
||||
testMethod($host, $port, "PUT", "payload");
|
||||
testMethod($host, $port, "PATCH", "payload");
|
||||
|
||||
|
||||
// TODO bug, 没有校验
|
||||
// testMethod($host, $port, "GET", "http_body");
|
||||
// testMethod($host, $port, "DELETE", "http_body");
|
||||
//testMethod($host, $port, "POST", null);
|
||||
//testMethod($host, $port, "PUT", null);
|
||||
//testMethod($host, $port, "PATCH", null);
|
||||
|
||||
|
||||
testCookie($host, $port);
|
||||
// TODO coredump
|
||||
// testCookieCore();
|
||||
|
||||
testHttpsHeaderCore($host, $port);
|
||||
testHeader($host, $port);
|
||||
|
||||
testSleep($host, $port);
|
33
vendor/swoole/tests/include/api/swoole_http_client/swoole_http_client_RST.php
vendored
Executable file
33
vendor/swoole/tests/include/api/swoole_http_client/swoole_http_client_RST.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
|
||||
// 旧版本一个bug, 连接收到RST会coredump
|
||||
|
||||
// 1. 对端发送RST
|
||||
// 2. 对端不回任何segment
|
||||
function test_connect_refused()
|
||||
{
|
||||
static $clients = [];
|
||||
$hosts = ["115.239.211.112", "127.0.0.1", "11.11.11.11"];
|
||||
|
||||
for ($i = 0; $i < 2000; $i++) {
|
||||
$host = $hosts[$i % 3];
|
||||
$port = 8000 + $i;
|
||||
echo "get $host:$port\n";
|
||||
$cli = new swoole_http_client($host, $port);
|
||||
$cli->setHeaders(["Connection" => "close"]);
|
||||
$cli->get("/", function(swoole_http_client $cli) {
|
||||
echo "receive:", $cli->body, "\n";
|
||||
});
|
||||
swoole_timer_after(3000, function() use($cli, &$clients) {
|
||||
$cli->close();
|
||||
unset($clients[spl_object_hash($cli)]);
|
||||
});
|
||||
|
||||
$clients[spl_object_hash($cli)] = $cli; // 防止swoole 引用计数处理错误
|
||||
}
|
||||
}
|
||||
|
||||
test_connect_refused();
|
10
vendor/swoole/tests/include/api/swoole_http_client/swoole_http_client_simple.php
vendored
Executable file
10
vendor/swoole/tests/include/api/swoole_http_client/swoole_http_client_simple.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
|
||||
$cli = new swoole_http_client("115.239.211.112", 80);
|
||||
$cli->setHeaders(["Connection" => "close"]);
|
||||
$cli->get("/", function(swoole_http_client $cli) {
|
||||
echo "receive:", $cli->body, "\n";
|
||||
});
|
41
vendor/swoole/tests/include/api/swoole_http_client/uaf_client.php
vendored
Executable file
41
vendor/swoole/tests/include/api/swoole_http_client/uaf_client.php
vendored
Executable file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
/*
|
||||
require('net').createServer(swoole_function(socket) {
|
||||
socket.on('data', swoole_function(data) {
|
||||
socket.write('HTTP/1.1 200 OK\r\n');
|
||||
socket.write('Transfer-Encoding: chunked\r\n');
|
||||
socket.write('\r\n');
|
||||
|
||||
var php_func = "hello"
|
||||
// var php_func = "ReflectionClass::export"
|
||||
|
||||
socket.write('4\r\n');
|
||||
socket.write('func\r\n');
|
||||
socket.write('0\r\n');
|
||||
socket.write('\r\n');
|
||||
socket.write('HTTP/1.1 200 OK\r\n');
|
||||
socket.write('Transfer-Encoding: ' + php_func + '\r\n');
|
||||
socket.write('\r\n');
|
||||
});
|
||||
}).listen(9090, '127.0.0.1');
|
||||
*/
|
||||
|
||||
|
||||
// 旧版本会因为因为 use after free
|
||||
// 回调的zval 指向 parser header的zval
|
||||
// 最后 call hello
|
||||
|
||||
function hello() {
|
||||
echo "=======================================\n";
|
||||
echo "call hello\n";
|
||||
var_dump(func_get_args());
|
||||
}
|
||||
|
||||
|
||||
$cli = new swoole_http_client("127.0.0.1", 9090);
|
||||
$cli->get("/", function(swoole_http_client $cli) {
|
||||
echo "receive:", $cli->body, "\n";
|
||||
});
|
21
vendor/swoole/tests/include/api/swoole_http_client/uaf_server.js
vendored
Executable file
21
vendor/swoole/tests/include/api/swoole_http_client/uaf_server.js
vendored
Executable file
@ -0,0 +1,21 @@
|
||||
require('net').createServer(function(socket) {
|
||||
socket.on('data', function(data) {
|
||||
socket.write('HTTP/1.1 200 OK\r\n');
|
||||
socket.write('Transfer-Encoding: chunked\r\n');
|
||||
socket.write('\r\n');
|
||||
|
||||
var php_func = "hello"
|
||||
// var php_func = "ReflectionClass::export"
|
||||
|
||||
socket.write('4\r\n');
|
||||
socket.write('func\r\n');
|
||||
socket.write('0\r\n');
|
||||
socket.write('\r\n');
|
||||
|
||||
// 故意构造两条响应
|
||||
|
||||
socket.write('HTTP/1.1 200 OK\r\n');
|
||||
socket.write('Transfer-Encoding: ' + php_func + '\r\n');
|
||||
socket.write('\r\n');
|
||||
});
|
||||
}).listen(9090, '127.0.0.1');
|
Reference in New Issue
Block a user