3 ini_set("log_errors", true);
4 ini_set("error_log", __DIR__."/server.log");
7 if (!ini_get("date.timezone")) {
8 date_default_timezone_set(@date_default_timezone_get());
10 error_log(sprintf("%s(%s): %s",
11 basename(getenv("SCRIPT_FILENAME"), ".php"),
12 basename(current(get_included_files()), ".inc"),
13 call_user_func_array("sprintf", func_get_args())
17 $php = getenv('TEST_PHP_EXECUTABLE');
19 define('PHP_BIN', $php);
20 } else if (defined('PHP_BINARY')) {
21 define('PHP_BIN', PHP_BINARY);
24 define("PHP_BIN", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
27 foreach (array("raphf", "propro", "http") as $ext) {
28 if (!extension_loaded($ext)) {
29 switch (PHP_SHLIB_SUFFIX) {
34 dl($ext .".". PHP_SHLIB_SUFFIX);
40 /* stream_socket_server() automatically sets SO_REUSEADDR,
41 * which is, well, bad if the tests are run in parallel
43 $offset = rand(0,2000);
44 foreach (range(8000+$offset, 9000+$offset) as $port) {
45 logger("serve: Trying port %d", $port);
46 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
47 fprintf(STDERR, "%s\n", $port);
48 logger("serve: Using port %d", $port);
50 $R = array($server); $W = array(); $E = array();
51 $select = stream_select($R, $E, $E, 10, 0);
52 if ($select && ($client = stream_socket_accept($server, 1))) {
53 logger("serve: Accept client %d", (int) $client);
54 if (getenv("PHP_HTTP_TEST_SSL")) {
55 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
59 while (!feof($client) && stream_select($R, $W, $E, 1, 0)) {
60 logger("serve: Handle client %d", (int) $client);
63 logger("serve: EOF/timeout on client %d", (int) $client);
64 } catch (Exception $ex) {
65 logger("serve: Exception on client %d: %s", (int) $client, $ex->getMessage());
66 /* ignore disconnect */
67 if ($ex->getMessage() !== "Empty message received from stream") {
68 fprintf(STDERR, "%s\n", $ex);
79 function server($handler, $cb) {
80 $args = explode(' ', getenv('TEST_PHP_ARGS'));
81 $args[] = __DIR__."/$handler";
82 foreach ($args as $k => $v) {
83 if (!$v) unset($args[$k]);
85 proc(PHP_BIN, $args, $cb);
88 function nghttpd($cb) {
89 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
90 $offset = rand(0,2000);
91 foreach (range(8000+$offset, 9000+$offset) as $port) {
92 $comm = "exec nghttpd -d html $port http2.key http2.crt";
93 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
99 $status = proc_get_status($proc);
101 if (!$status["running"]) {
106 $cb($port, $stdin, $stdout, $stderr);
107 } catch (Exception $e) {
111 proc_terminate($proc);
121 function proc($bin, $args, $cb) {
122 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
123 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
124 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
130 $port = trim(fgets($stderr));
131 $R = array($stderr); $W = array(); $E = array();
132 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
134 if (is_numeric($port)) {
136 $cb($port, $stdin, $stdout, $stderr);
137 } catch (Exception $e) {
142 proc_terminate($proc);