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);
58 while (!feof($client)) {
59 logger("serve: Handle client %d", (int) $client);
62 logger("serve: EOF on client %d", (int) $client);
63 } catch (Exception $ex) {
64 logger("serve: Exception on client %d: %s", (int) $client, $ex->getMessage());
65 /* ignore disconnect */
66 if ($ex->getMessage() !== "Empty message received from stream") {
67 fprintf(STDERR, "%s\n", $ex);
78 function server($handler, $cb) {
79 $args = explode(' ', getenv('TEST_PHP_ARGS'));
80 $args[] = __DIR__."/$handler";
81 foreach ($args as $k => $v) {
82 if (!$v) unset($args[$k]);
84 proc(PHP_BIN, $args, $cb);
87 function nghttpd($cb) {
88 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
89 $offset = rand(0,2000);
90 foreach (range(8000+$offset, 9000+$offset) as $port) {
91 $comm = "exec nghttpd -d html $port http2.key http2.crt";
92 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
98 $status = proc_get_status($proc);
100 if (!$status["running"]) {
105 $cb($port, $stdin, $stdout, $stderr);
106 } catch (Exception $e) {
110 proc_terminate($proc);
120 function proc($bin, $args, $cb) {
121 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
122 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
123 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
129 $port = trim(fgets($stderr));
130 $R = array($stderr); $W = array(); $E = array();
131 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
133 if (is_numeric($port)) {
135 $cb($port, $stdin, $stdout, $stderr);
136 } catch (Exception $e) {
141 proc_terminate($proc);