029cd46e55359262f4288f6c2336e877db13706f
[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 function serve($cb) {
28 /* stream_socket_server() automatically sets SO_REUSEADDR,
29 * which is, well, bad if the tests are run in parallel
30 */
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);
37 do {
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);
44 }
45 try {
46 while (!feof($client)) {
47 logger("serve: Handle client %d", (int) $client);
48 $cb($client);
49 }
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);
56 }
57 break;
58 }
59 }
60 } while ($select);
61 return;
62 }
63 }
64 }
65
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]);
71 }
72 proc(PHP_BIN, $args, $cb);
73 }
74
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__))) {
81 $stdin = $pipes[0];
82 $stdout = $pipes[1];
83 $stderr = $pipes[2];
84
85 usleep(50000);
86 $status = proc_get_status($proc);
87
88 if (!$status["running"]) {
89 continue;
90 }
91
92 try {
93 $cb($port, $stdin, $stdout, $stderr);
94 } catch (Exception $e) {
95 echo $e,"\n";
96 }
97
98 proc_terminate($proc);
99
100 fpassthru($stderr);
101 fpassthru($stdout);
102 return;
103 }
104 }
105
106 }
107
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__))) {
112 $stdin = $pipes[0];
113 $stdout = $pipes[1];
114 $stderr = $pipes[2];
115
116 do {
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));
120
121 if (is_numeric($port)) {
122 try {
123 $cb($port, $stdin, $stdout, $stderr);
124 } catch (Exception $e) {
125 echo $e,"\n";
126 }
127 }
128
129 proc_terminate($proc);
130
131 fpassthru($stderr);
132 fpassthru($stdout);
133 }
134 }