-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathdata_parse.phpt
120 lines (109 loc) · 3.27 KB
/
data_parse.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
--TEST--
swoole_http_server: http server data parse test
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
function getRandomData(int $num): array
{
$data = [];
foreach (range(1, $num) as $_) {
$key = substr(get_safe_random(32), 0, mt_rand(1, 32));
$value = substr(get_safe_random(64), 0, mt_rand(0, 64));
$data[$key] = $value;
}
return $data;
}
function arrayToMultipartString(array $var, string $boundary): string
{
$ret = '';
foreach ($var as $name => $value) {
$value = (string)($value);
$ret .= "--{$boundary}\r\nContent-Disposition: form-data; name=\"{$name}\"\r\n\r\n{$value}\r\n";
}
$ret .= "--{$boundary}--\r\n";
return $ret;
}
function sendData(string $host, int $port, array $get, array $post): string
{
$client = new Co\Client(SWOOLE_SOCK_TCP);
if (!$client->connect($host, $port, 1)) {
exit("connect failed. Error: {$client->errCode}\n");
}
$get = http_build_query($get);
$content_type = '';
switch (mt_rand(0, 1)) {
case 0:
{
$content_type = 'application/x-www-form-urlencoded';
$post = http_build_query($post);
break;
}
case 1:
{
$boundary = '++++' . md5(get_safe_random(16));
$content_type = "multipart/form-data; boundary={$boundary}";
$post = arrayToMultipartString($post, $boundary);
break;
}
case 2:
{
$content_type = 'application/json';
$post = json_encode($post);
break;
}
}
$content_length = strlen($post);
$CR = "\r";
$data = <<<HTTP
POST /?{$get} HTTP/1.1{$CR}
Host: {$host}{$CR}
Connection: closed{$CR}
Accept: */*{$CR}
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36{$CR}
Content-Type: {$content_type}{$CR}
Content-Length: {$content_length}{$CR}
{$CR}
{$post}
HTTP;
Assert::assert($client->send($data));
$data = '';
while ($tmp = $client->recv()) {
$data .= $tmp;
}
return $data;
}
$pm = new ProcessManager;
$pm->parentFunc = function () use ($pm) {
for ($c = MAX_CONCURRENCY; $c--;) {
go(function () use ($pm) {
$get = getRandomData(50);
$post = getRandomData(100);
$ret = sendData('127.0.0.1', $pm->getFreePort(), $get, $post);
list($_, $body) = explode("\r\n\r\n", $ret);
Assert::same($body, var_dump_return($get, $post));
});
}
Swoole\Event::wait();
$pm->kill();
echo "DONE\n";
};
$pm->childFunc = function () use ($pm) {
$http = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
$http->set(['log_file' => '/dev/null']);
$http->on("WorkerStart", function ($serv, $wid) {
global $pm;
$pm->wakeup();
});
$http->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($http) {
$response->end(var_dump_return($request->get, $request->post));
$http->close($request->fd);
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE