Merge branch 'v2.6.x'
[m6w6/ext-http] / tests / helper / server.inc
1 <?php
2
3 ini_set("log_errors", true);
4 ini_set("error_log", __DIR__."/server.log");
5
6 function logger() {
7 if (!ini_get("date.timezone")) {
8 date_default_timezone_set(@date_default_timezone_get());
9 }
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())
14 ));
15 }
16
17 $php = getenv('TEST_PHP_EXECUTABLE');
18 if ($php) {
19 define('PHP_BIN', $php);
20 } else if (defined('PHP_BINARY')) {
21 define('PHP_BIN', PHP_BINARY);
22 } else {
23 // PHP-5.3
24 define("PHP_BIN", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
25 }
26
27 foreach (array("raphf", "propro", "http") as $ext) {
28 if (!extension_loaded($ext)) {
29 switch (PHP_SHLIB_SUFFIX) {
30 case "dll":
31 dl("php_$ext.dll");
32 break;
33 default:
34 dl($ext .".". PHP_SHLIB_SUFFIX);
35 }
36 }
37 }
38
39 function serve($cb) {
40 /* stream_socket_server() automatically sets SO_REUSEADDR,
41 * which is, well, bad if the tests are run in parallel
42 */
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);
49 do {
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);
56 }
57 try {
58 $R = array($client);
59 while (!feof($client) && stream_select($R, $W, $E, 1, 0)) {
60 logger("serve: Handle client %d", (int) $client);
61 $cb($client);
62 }
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);
69 }
70 break;
71 }
72 }
73 } while ($select);
74 return;
75 }
76 }
77 }
78
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]);
84 }
85 proc(PHP_BIN, $args, $cb);
86 }
87
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__))) {
94 $stdin = $pipes[0];
95 $stdout = $pipes[1];
96 $stderr = $pipes[2];
97
98 usleep(50000);
99 $status = proc_get_status($proc);
100
101 if (!$status["running"]) {
102 continue;
103 }
104
105 try {
106 $cb($port, $stdin, $stdout, $stderr);
107 } catch (Exception $e) {
108 echo $e,"\n";
109 }
110
111 proc_terminate($proc);
112
113 fpassthru($stderr);
114 fpassthru($stdout);
115 return;
116 }
117 }
118
119 }
120
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__))) {
125 $stdin = $pipes[0];
126 $stdout = $pipes[1];
127 $stderr = $pipes[2];
128
129 do {
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));
133
134 if (is_numeric($port)) {
135 try {
136 $cb($port, $stdin, $stdout, $stderr);
137 } catch (Exception $e) {
138 echo $e,"\n";
139 }
140 }
141
142 proc_terminate($proc);
143
144 fpassthru($stderr);
145 fpassthru($stdout);
146 }
147 }