Merge branch 'master' of git.php.net:/pecl/http/pecl_http
[m6w6/ext-http] / tests / helper / server.inc
1 <?php
2
3 // PHP-5.3
4 if (!defined("PHP_BINARY")) {
5 define("PHP_BINARY", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
6 }
7
8 function serve($cb) {
9 foreach (range(8000, 9000) as $port) {
10 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
11 fprintf(STDERR, "%s\n", $port);
12 do {
13 $R = array($server); $W = array(); $E = array();
14 $select = stream_select($R, $E, $E, 0, 10000);
15 if ($select && ($client = stream_socket_accept($server, 1))) {
16 if (getenv("PHP_HTTP_TEST_SSL")) {
17 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
18 }
19 try {
20 while (!feof($client)) {
21 $cb($client);
22 }
23 } catch (Exception $ex) {
24 /* ignore disconnect */
25 if ($ex->getMessage() !== "Empty message received from stream") {
26 fprintf(STDERR, "%s\n", $ex);
27 }
28 break;
29 }
30 }
31 } while ($select !== false);
32 return;
33 }
34 }
35 }
36
37 function server($handler, $cb) {
38 proc(PHP_BINARY, array(__DIR__."/$handler"), $cb);
39 }
40
41 function nghttpd($cb) {
42 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
43 foreach (range(8000, 9000) as $port) {
44 $comm = "exec nghttpd -d html $port http2.key http2.crt";
45 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
46 $stdin = $pipes[0];
47 $stdout = $pipes[1];
48 $stderr = $pipes[2];
49
50 usleep(50000);
51 $status = proc_get_status($proc);
52
53 if (!$status["running"]) {
54 continue;
55 }
56
57 try {
58 $cb($port, $stdin, $stdout, $stderr);
59 } catch (Exception $e) {
60 echo $e,"\n";
61 }
62
63 proc_terminate($proc);
64
65 fpassthru($stderr);
66 fpassthru($stdout);
67 return;
68 }
69 }
70
71 }
72
73 function proc($bin, $args, $cb) {
74 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
75 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
76 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
77 $stdin = $pipes[0];
78 $stdout = $pipes[1];
79 $stderr = $pipes[2];
80
81 do {
82 $port = trim(fgets($stderr));
83 $R = array($stderr); $W = array(); $E = array();
84 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
85
86 if (is_numeric($port)) {
87 try {
88 $cb($port, $stdin, $stdout, $stderr);
89 } catch (Exception $e) {
90 echo $e,"\n";
91 }
92 }
93
94 proc_terminate($proc);
95
96 fpassthru($stderr);
97 fpassthru($stdout);
98 }
99 }