3 ini_set("log_errors", true);
4 ini_set("error_log", __DIR__."/server.log");
6 $php = getenv('TEST_PHP_EXECUTABLE');
8 define('PHP_BIN', $php);
9 } else if (defined('PHP_BINARY')) {
10 define('PHP_BIN', PHP_BINARY);
13 define("PHP_BIN", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
17 /* stream_socket_server() automatically sets SO_REUSEADDR,
18 * which is, well, bad if the tests are run in parallel
20 $offset = rand(0,2000);
21 foreach (range(8000+$offset, 9000+$offset) as $port) {
22 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
23 fprintf(STDERR, "%s\n", $port);
25 $R = array($server); $W = array(); $E = array();
26 $select = stream_select($R, $E, $E, 0, 10000);
27 if ($select && ($client = stream_socket_accept($server, 1))) {
28 if (getenv("PHP_HTTP_TEST_SSL")) {
29 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
32 while (!feof($client)) {
35 } catch (Exception $ex) {
36 /* ignore disconnect */
37 if ($ex->getMessage() !== "Empty message received from stream") {
38 fprintf(STDERR, "%s\n", $ex);
43 } while ($select !== false);
49 function server($handler, $cb) {
50 $args = explode(' ', getenv('TEST_PHP_ARGS'));
51 $args[] = __DIR__."/$handler";
52 foreach ($args as $k => $v) {
53 if (!$v) unset($args[$k]);
55 proc(PHP_BIN, $args, $cb);
58 function nghttpd($cb) {
59 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
60 $offset = rand(0,2000);
61 foreach (range(8000+$offset, 9000+$offset) as $port) {
62 $comm = "exec nghttpd -d html $port http2.key http2.crt";
63 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
69 $status = proc_get_status($proc);
71 if (!$status["running"]) {
76 $cb($port, $stdin, $stdout, $stderr);
77 } catch (Exception $e) {
81 proc_terminate($proc);
91 function proc($bin, $args, $cb) {
92 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
93 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
94 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
100 $port = trim(fgets($stderr));
101 $R = array($stderr); $W = array(); $E = array();
102 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
104 if (is_numeric($port)) {
106 $cb($port, $stdin, $stdout, $stderr);
107 } catch (Exception $e) {
112 proc_terminate($proc);