3 ini_set("log_errors", true);
4 ini_set("error_log", __DIR__."/server.log");
7 error_log(sprintf("%s(%s): %s",
8 basename(getenv("SCRIPT_FILENAME"), ".php"),
9 basename(current(get_included_files()), ".inc"),
10 call_user_func_array("sprintf", func_get_args())
14 $php = getenv('TEST_PHP_EXECUTABLE');
16 define('PHP_BIN', $php);
17 } else if (defined('PHP_BINARY')) {
18 define('PHP_BIN', PHP_BINARY);
21 define("PHP_BIN", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
25 /* stream_socket_server() automatically sets SO_REUSEADDR,
26 * which is, well, bad if the tests are run in parallel
28 $offset = rand(0,2000);
29 foreach (range(8000+$offset, 9000+$offset) as $port) {
30 logger("serve: Trying port %d", $port);
31 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
32 fprintf(STDERR, "%s\n", $port);
33 logger("serve: Using port %d", $port);
35 $R = array($server); $W = array(); $E = array();
36 $select = stream_select($R, $E, $E, 0, 10000);
37 if ($select && ($client = stream_socket_accept($server, 1))) {
38 logger("serve: Accept client %d", (int) $client);
39 if (getenv("PHP_HTTP_TEST_SSL")) {
40 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
43 while (!feof($client)) {
44 logger("serve: Handle client %d", (int) $client);
47 logger("serve: EOF on client %d", (int) $client);
48 } catch (Exception $ex) {
49 logger("serve: Exception on client %d", (int) $client);
50 /* ignore disconnect */
51 if ($ex->getMessage() !== "Empty message received from stream") {
52 fprintf(STDERR, "%s\n", $ex);
57 } while ($select !== false);
63 function server($handler, $cb) {
64 $args = explode(' ', getenv('TEST_PHP_ARGS'));
65 $args[] = __DIR__."/$handler";
66 foreach ($args as $k => $v) {
67 if (!$v) unset($args[$k]);
69 proc(PHP_BIN, $args, $cb);
72 function nghttpd($cb) {
73 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
74 $offset = rand(0,2000);
75 foreach (range(8000+$offset, 9000+$offset) as $port) {
76 $comm = "exec nghttpd -d html $port http2.key http2.crt";
77 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
83 $status = proc_get_status($proc);
85 if (!$status["running"]) {
90 $cb($port, $stdin, $stdout, $stderr);
91 } catch (Exception $e) {
95 proc_terminate($proc);
105 function proc($bin, $args, $cb) {
106 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
107 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
108 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
114 $port = trim(fgets($stderr));
115 $R = array($stderr); $W = array(); $E = array();
116 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
118 if (is_numeric($port)) {
120 $cb($port, $stdin, $stdout, $stderr);
121 } catch (Exception $e) {
126 proc_terminate($proc);