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");
28 /* stream_socket_server() automatically sets SO_REUSEADDR,
29 * which is, well, bad if the tests are run in parallel
31 $offset = rand(0,2000);
32 foreach (range(8000+$offset, 9000+$offset) as $port) {
33 logger("serve: Trying port %d", $port);
34 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
35 fprintf(STDERR, "%s\n", $port);
36 logger("serve: Using port %d", $port);
38 $R = array($server); $W = array(); $E = array();
39 $select = stream_select($R, $E, $E, 10, 0);
40 if ($select && ($client = stream_socket_accept($server, 1))) {
41 logger("serve: Accept client %d", (int) $client);
42 if (getenv("PHP_HTTP_TEST_SSL")) {
43 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
47 while (!feof($client) && stream_select($R, $W, $E, 1, 0)) {
48 logger("serve: Handle client %d", (int) $client);
51 logger("serve: EOF/timeout on client %d", (int) $client);
52 } catch (Exception $ex) {
53 logger("serve: Exception on client %d: %s", (int) $client, $ex->getMessage());
54 /* ignore disconnect */
55 if ($ex->getMessage() !== "Empty message received from stream") {
56 fprintf(STDERR, "%s\n", $ex);
67 function server($handler, $cb) {
68 $args = explode(' ', getenv('TEST_PHP_ARGS'));
69 $args[] = __DIR__."/$handler";
70 foreach ($args as $k => $v) {
71 if (!$v) unset($args[$k]);
73 proc(PHP_BIN, $args, $cb);
76 function nghttpd($cb) {
77 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
78 $offset = rand(0,2000);
79 foreach (range(8000+$offset, 9000+$offset) as $port) {
80 $comm = "exec nghttpd -d html $port http2.key http2.crt";
81 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
87 $status = proc_get_status($proc);
89 if (!$status["running"]) {
94 $cb($port, $stdin, $stdout, $stderr);
95 } catch (Exception $e) {
99 proc_terminate($proc);
109 function proc($bin, $args, $cb) {
110 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
111 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
112 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
118 $port = trim(fgets($stderr));
119 $R = array($stderr); $W = array(); $E = array();
120 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
122 if (is_numeric($port)) {
124 $cb($port, $stdin, $stdout, $stderr);
125 } catch (Exception $e) {
130 proc_terminate($proc);