Merge branch 'R_2_5'
[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 while (!feof($client)) {
59 logger("serve: Handle client %d", (int) $client);
60 $cb($client);
61 }
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);
68 }
69 break;
70 }
71 }
72 } while ($select);
73 return;
74 }
75 }
76 }
77
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]);
83 }
84 proc(PHP_BIN, $args, $cb);
85 }
86
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__))) {
93 $stdin = $pipes[0];
94 $stdout = $pipes[1];
95 $stderr = $pipes[2];
96
97 usleep(50000);
98 $status = proc_get_status($proc);
99
100 if (!$status["running"]) {
101 continue;
102 }
103
104 try {
105 $cb($port, $stdin, $stdout, $stderr);
106 } catch (Exception $e) {
107 echo $e,"\n";
108 }
109
110 proc_terminate($proc);
111
112 fpassthru($stderr);
113 fpassthru($stdout);
114 return;
115 }
116 }
117
118 }
119
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__))) {
124 $stdin = $pipes[0];
125 $stdout = $pipes[1];
126 $stderr = $pipes[2];
127
128 do {
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));
132
133 if (is_numeric($port)) {
134 try {
135 $cb($port, $stdin, $stdout, $stderr);
136 } catch (Exception $e) {
137 echo $e,"\n";
138 }
139 }
140
141 proc_terminate($proc);
142
143 fpassthru($stderr);
144 fpassthru($stdout);
145 }
146 }