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, 0, 10000);
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);
46 while (!feof($client)) {
47 logger("serve: Handle client %d", (int) $client);
50 logger("serve: EOF on client %d", (int) $client);
51 } catch (Exception $ex) {
52 logger("serve: Exception on client %d: %s", (int) $client, $ex->getMessage());
53 /* ignore disconnect */
54 if ($ex->getMessage() !== "Empty message received from stream") {
55 fprintf(STDERR, "%s\n", $ex);
66 function server($handler, $cb) {
67 $args = explode(' ', getenv('TEST_PHP_ARGS'));
68 $args[] = __DIR__."/$handler";
69 foreach ($args as $k => $v) {
70 if (!$v) unset($args[$k]);
72 proc(PHP_BIN, $args, $cb);
75 function nghttpd($cb) {
76 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
77 $offset = rand(0,2000);
78 foreach (range(8000+$offset, 9000+$offset) as $port) {
79 $comm = "exec nghttpd -d html $port http2.key http2.crt";
80 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
86 $status = proc_get_status($proc);
88 if (!$status["running"]) {
93 $cb($port, $stdin, $stdout, $stderr);
94 } catch (Exception $e) {
98 proc_terminate($proc);
108 function proc($bin, $args, $cb) {
109 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
110 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
111 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
117 $port = trim(fgets($stderr));
118 $R = array($stderr); $W = array(); $E = array();
119 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
121 if (is_numeric($port)) {
123 $cb($port, $stdin, $stdout, $stderr);
124 } catch (Exception $e) {
129 proc_terminate($proc);