-
Notifications
You must be signed in to change notification settings - Fork 11
/
hyper-run
executable file
·181 lines (150 loc) · 5.51 KB
/
hyper-run
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env php
<?php
if (PHP_SAPI !== 'cli') {
header('Content-Type: text/plain; charset=UTF-8', true, 400);
exit("This script only works on php-cli.\n");
}
if (is_file(__DIR__ . '/autoload.php')) {
require __DIR__ . '/autoload.php';
} elseif (is_file(__DIR__ . '/../../autoload.php')) {
require __DIR__ . '/../../autoload.php';
} elseif (is_file(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
} else {
fwrite(STDERR,
"You need to set up the project dependencies using the following commands:\n" .
"curl -s http://getcomposer.org/installer | php\n" .
"php composer.phar install\n"
);
exit(1);
}
function usage()
{
$cert = __DIR__ . '/certificate.pem';
fwrite(STDERR,
"
Usage:
$_SERVER[SCRIPT_NAME] <options>
Example:
$_SERVER[SCRIPT_NAME] -S localhost:8000 -s localhost:44300
[Required]
-S \"<Host>:<Port>\" of an HTTP server. Multiple arguments can be accepted.
-s \"<Host>:<Port>\" of an HTTPS server. Multiple arguments can be accepted.
[Optional]
-n The number of PHP built-in server clusters, from 1 to 20. Default is 10.
-t Path for the document root. Default is the current directory.
-r Path for the router script. Default is empty.
-c Path for the PEM-encoded certificate.
Default is \"$cert\".
Restriction:
- The option -s is only supported on PHP 5.6.0 or later.
- Access logs will not be displayed on Windows.
"
);
}
$options = getopt('S:s:n:t:r:c:h');
$help = isset($options['h']);
$servers = isset($options['S']) ? (array)$options['S'] : [];
$secures = isset($options['s']) ? (array)$options['s'] : [];
$docroot = isset($options['t']) ? current((array)$options['t']) : null;
$number = isset($options['n']) ? current((array)$options['n']) : '10';
$router = isset($options['r']) ? current((array)$options['r']) : null;
$cert = isset($options['c']) ? current((array)$options['c']) : (__DIR__ . '/certificate.pem');
if ($help) {
usage();
exit(0);
}
if (!$servers && !$secures) {
fwrite(STDERR, "Error: At least 1 server must be specified.\n");
usage();
exit(1);
}
function startListener($master, $listener)
{
$host = sprintf(
'%s://%s:%s',
$listener[2] ? 'https' : 'http', $listener[0], $listener[1]
);
fwrite(STDOUT, "Development server started: <{$host}>\n");
try {
call_user_func_array([$master, 'addListener'], $listener);
} catch (React\Socket\ConnectionException $exception) {
$reason = 'Address already in use';
$isPortSpecified = $listener[4];
if ($isPortSpecified || strpos($exception->getMessage(), $reason) === false) {
throw $exception;
}
fwrite(STDERR, sprintf(
"Failed to listen on %s:%s (reason: %s)\n",
$listener[0], $listener[1], $reason
));
$listener[1] = (int) $listener[1] + 1;
startListener($master, $listener);
}
}
try {
if (!ctype_digit($number) || $number < 1 || $number > 20) {
throw new \RuntimeException('The number of clusters must be between 1 and 20.');
}
if ($docroot !== null && !is_dir($docroot)) {
throw new \RuntimeException("No such document root directory: $docroot");
}
if ($router !== null && !is_file($router)) {
throw new \RuntimeException("No such router script file: $router");
}
if (!is_file($cert)) {
throw new \RuntimeException("No such certificate file: $cert");
}
if (!openssl_pkey_get_public("file://$cert")) {
throw new \RuntimeException("Invalid certificate file: $cert");
}
$listeners = [];
foreach ([$servers, $secures] as $type => $group) {
$isSecure = $type === 1;
foreach ($group as $i => $server) {
list($host, $port) = explode(':', $server, 2) + [1 => ''];
if ($port === '') {
$port = $isSecure ? '44300' : '8000';
$isPortSpecified = false;
} else {
$isPortSpecified = true;
}
$ip = filter_var(gethostbyname($host), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
$regex = '/\A(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\z/';
if ($ip === false || !preg_match($regex, $port)) {
throw new \RuntimeException("Invalid host or port: $server");
}
if (isset($listeners[$server])) {
throw new \RuntimeException("Duplicated entry: $server");
}
$listeners[$server] = [$host, $port, $isSecure, $cert, $isPortSpecified];
}
}
$loop = React\EventLoop\Factory::create();
$usedProcesses = [];
$factory = new mpyw\HyperBuiltinServer\BuiltinServerFactory($loop);
$factory
->createMultipleAsync($number, '127.0.0.1', $docroot, $router)
->then(function (array $processes) use ($loop, $listeners, &$usedProcesses) {
$usedProcesses = $processes;
$master = new mpyw\HyperBuiltinServer\Master($loop, $processes);
foreach ($listeners as $listener) {
startListener($master, $listener);
}
})
->then(null, function ($e) use (&$usedProcesses) {
foreach ($usedProcesses as $process) {
$process->terminate();
}
throw $e;
})
->done();
set_time_limit(0);
$loop->run();
} catch (\Throwable $e) {
fwrite(STDERR, "Error: {$e->getMessage()}\n");
exit(1);
} catch (\Exception $e) {
fwrite(STDERR, "Error: {$e->getMessage()}\n");
exit(1);
}