|
1 |
| -<?php |
| 1 | +<?php declare(strict_types=1); |
2 | 2 |
|
3 |
| -define ("PHP_CURL_SERVER_HOSTNAME", "localhost"); |
4 |
| -define ("PHP_CURL_SERVER_PORT", 8964); |
5 |
| -define ("PHP_CURL_SERVER_ADDRESS", PHP_CURL_SERVER_HOSTNAME.":".PHP_CURL_SERVER_PORT); |
6 |
| - |
7 |
| -function curl_cli_server_start() { |
8 |
| - if(getenv('PHP_CURL_HTTP_REMOTE_SERVER')) { |
| 3 | +function curl_cli_server_start(?string $listen = NULL) { |
| 4 | + if(($listen === null) && getenv('PHP_CURL_HTTP_REMOTE_SERVER')) { |
9 | 5 | return getenv('PHP_CURL_HTTP_REMOTE_SERVER');
|
10 | 6 | }
|
11 | 7 |
|
| 8 | + if ($listen === null) { |
| 9 | + $listen = 'localhost'; |
| 10 | + } |
| 11 | + |
12 | 12 | $php_executable = getenv('TEST_PHP_EXECUTABLE');
|
13 | 13 | $doc_root = __DIR__;
|
14 | 14 | $router = "responder/get.inc";
|
15 |
| - $cmd = [$php_executable, '-t', $doc_root, '-n', '-S', PHP_CURL_SERVER_ADDRESS, $router]; |
| 15 | + $cmd = [$php_executable, '-t', $doc_root, '-n', '-S', $listen, $router]; |
16 | 16 | $descriptorspec = array(
|
17 | 17 | 0 => STDIN,
|
18 | 18 | 1 => STDOUT,
|
19 |
| - 2 => array("null"), |
| 19 | + 2 => ['pipe', 'w'], |
20 | 20 | );
|
21 | 21 | $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
|
22 | 22 |
|
| 23 | + // First, wait for the dev server to declare itself ready. |
| 24 | + $bound = null; |
| 25 | + stream_set_blocking($pipes[2], false); |
| 26 | + for ($i = 0; $i < 60; $i++) { |
| 27 | + usleep(50000); // 50ms per try |
| 28 | + $status = proc_get_status($handle); |
| 29 | + if (empty($status['running'])) { |
| 30 | + echo "Server is not running\n"; |
| 31 | + proc_terminate($handle); |
| 32 | + exit(1); |
| 33 | + } |
| 34 | + |
| 35 | + while (($line = fgets($pipes[2])) !== false) { |
| 36 | + if (preg_match('@PHP \S* Development Server \(https?://(.*?:\d+)\) started@', $line, $matches)) { |
| 37 | + $bound = $matches[1]; |
| 38 | + // Now that we've identified the listen address, close STDERR. |
| 39 | + // Otherwise the pipe may clog up with unread log messages. |
| 40 | + fclose($pipes[2]); |
| 41 | + break 2; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + if ($bound === null) { |
| 46 | + echo "Server did not output startup message"; |
| 47 | + proc_terminate($handle); |
| 48 | + exit(1); |
| 49 | + } |
| 50 | + |
| 51 | + // Now wait for a connection to succeed. |
23 | 52 | // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
|
24 | 53 | // it might not be listening yet...need to wait until fsockopen() call returns
|
25 | 54 | $error = "Unable to connect to server\n";
|
26 | 55 | for ($i=0; $i < 60; $i++) {
|
27 | 56 | usleep(50000); // 50ms per try
|
28 | 57 | $status = proc_get_status($handle);
|
29 |
| - $fp = @fsockopen(PHP_CURL_SERVER_HOSTNAME, PHP_CURL_SERVER_PORT); |
| 58 | + $fp = @fsockopen("tcp://$bound"); |
30 | 59 | // Failure, the server is no longer running
|
31 | 60 | if (!($status && $status['running'])) {
|
32 | 61 | $error = "Server is not running\n";
|
@@ -64,5 +93,5 @@ function curl_cli_server_start() {
|
64 | 93 | $handle
|
65 | 94 | );
|
66 | 95 |
|
67 |
| - return PHP_CURL_SERVER_ADDRESS; |
| 96 | + return $bound; |
68 | 97 | }
|
0 commit comments