Skip to content

Commit 6a04cc1

Browse files
committed
Use ephemeral ports during curl tests with dev server
1 parent f95a04e commit 6a04cc1

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

ext/curl/tests/CONFLICTS

Lines changed: 0 additions & 1 deletion
This file was deleted.

ext/curl/tests/bug79033.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var_dump(curl_getinfo($ch)["request_header"]);
2121
string(%d) "array(0) {
2222
}
2323
"
24-
string(90) "POST /get.inc?test=post HTTP/1.1
24+
string(%d) "POST /get.inc?test=post HTTP/1.1
2525
Host: localhost:%d
2626
Accept: */*
2727
Content-Length: 0

ext/curl/tests/server.inc

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,61 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

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')) {
95
return getenv('PHP_CURL_HTTP_REMOTE_SERVER');
106
}
117

8+
if ($listen === null) {
9+
$listen = 'localhost';
10+
}
11+
1212
$php_executable = getenv('TEST_PHP_EXECUTABLE');
1313
$doc_root = __DIR__;
1414
$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];
1616
$descriptorspec = array(
1717
0 => STDIN,
1818
1 => STDOUT,
19-
2 => array("null"),
19+
2 => ['pipe', 'w'],
2020
);
2121
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
2222

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.
2352
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
2453
// it might not be listening yet...need to wait until fsockopen() call returns
2554
$error = "Unable to connect to server\n";
2655
for ($i=0; $i < 60; $i++) {
2756
usleep(50000); // 50ms per try
2857
$status = proc_get_status($handle);
29-
$fp = @fsockopen(PHP_CURL_SERVER_HOSTNAME, PHP_CURL_SERVER_PORT);
58+
$fp = @fsockopen("tcp://$bound");
3059
// Failure, the server is no longer running
3160
if (!($status && $status['running'])) {
3261
$error = "Server is not running\n";
@@ -64,5 +93,5 @@ function curl_cli_server_start() {
6493
$handle
6594
);
6695

67-
return PHP_CURL_SERVER_ADDRESS;
96+
return $bound;
6897
}

0 commit comments

Comments
 (0)