Skip to content

Commit 1327491

Browse files
committed
Add php_cli_server_connect() helper
To encapsulate the repeated fsockopen() code. This gives us a chance to control the timeout in one place: Raise it to one second.
1 parent 5317ea6 commit 1327491

33 files changed

+83
-251
lines changed

sapi/cli/tests/bug43177.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,10 @@ php_cli_server_start(<<<'SCRIPT'
3535
SCRIPT
3636
);
3737

38-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
39-
$port = intval($port)?:80;
38+
$host = PHP_CLI_SERVER_HOSTNAME;
4039

4140
foreach(array("parse", "fatal", "fatal2", "compile") as $url) {
42-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
43-
if (!$fp) {
44-
die("connect failed");
45-
}
46-
41+
$fp = php_cli_server_connect();
4742
if(fwrite($fp, <<<HEADER
4843
GET /$url HTTP/1.1
4944
Host: {$host}

sapi/cli/tests/bug61679.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ echo "This should never echo";
1212
PHP
1313
);
1414

15-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
16-
$port = intval($port)?:80;
17-
18-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
19-
if (!$fp) {
20-
die("connect failed");
21-
}
15+
$host = PHP_CLI_SERVER_HOSTNAME;
16+
$fp = php_cli_server_connect();
2217

2318
// Send a request with a fictitious request method,
2419
// I like smurfs, the smurf everything.

sapi/cli/tests/bug61977.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ $mimetypes = ['html', 'htm', 'svg', 'css', 'js', 'png', 'webm', 'ogv', 'ogg'];
1717

1818
function test_mimetypes($mimetypes) {
1919
foreach ($mimetypes as $mimetype) {
20-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
21-
$port = intval($port) ? : 80;
22-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
20+
$host = PHP_CLI_SERVER_HOSTNAME;
21+
$fp = php_cli_server_connect();
2322
if (!$fp) die('Connect failed');
2423
file_put_contents(__DIR__ . "/foo.{$mimetype}", '');
2524
$header = <<<HEADER

sapi/cli/tests/bug65066_100.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ include "skipif.inc";
1111
include "php_cli_server.inc";
1212
php_cli_server_start('http_response_code(100);');
1313

14-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15-
$port = intval($port)?:80;
16-
17-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18-
if (!$fp) {
19-
die("connect failed");
20-
}
14+
$host = PHP_CLI_SERVER_HOSTNAME;
15+
$fp = php_cli_server_connect();
2116

2217
if(fwrite($fp, <<<HEADER
2318
GET / HTTP/1.1

sapi/cli/tests/bug65066_422.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ include "skipif.inc";
1111
include "php_cli_server.inc";
1212
php_cli_server_start('http_response_code(422);');
1313

14-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15-
$port = intval($port)?:80;
16-
17-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18-
if (!$fp) {
19-
die("connect failed");
20-
}
14+
$host = PHP_CLI_SERVER_HOSTNAME;
15+
$fp = php_cli_server_connect();
2116

2217
if(fwrite($fp, <<<HEADER
2318
GET / HTTP/1.1

sapi/cli/tests/bug65066_511.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ include "skipif.inc";
1111
include "php_cli_server.inc";
1212
php_cli_server_start('http_response_code(511);');
1313

14-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15-
$port = intval($port)?:80;
16-
17-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18-
if (!$fp) {
19-
die("connect failed");
20-
}
14+
$host = PHP_CLI_SERVER_HOSTNAME;
15+
$fp = php_cli_server_connect();
2116

2217
if(fwrite($fp, <<<HEADER
2318
GET / HTTP/1.1

sapi/cli/tests/bug65633.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ var_dump($_COOKIE, $_SERVER['HTTP_FOO']);
1212
PHP
1313
);
1414

15-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
16-
$port = intval($port)?:80;
17-
18-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
19-
if (!$fp) {
20-
die("connect failed");
21-
}
15+
$host = PHP_CLI_SERVER_HOSTNAME;
16+
$fp = php_cli_server_connect();
2217

2318
if(fwrite($fp, <<<HEADER
2419
GET / HTTP/1.1

sapi/cli/tests/bug66606_2.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ include "skipif.inc";
1111
include "php_cli_server.inc";
1212
php_cli_server_start('var_dump($_SERVER["CONTENT_TYPE"], $_SERVER["CONTENT_LENGTH"])');
1313

14-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15-
$port = intval($port)?:80;
16-
17-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18-
if (!$fp) {
19-
die("connect failed");
20-
}
14+
$host = PHP_CLI_SERVER_HOSTNAME;
15+
$fp = php_cli_server_connect();
2116

2217
if (fwrite($fp, <<<HEADER
2318
POST /index.php HTTP/1.1

sapi/cli/tests/bug66830.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ header(' ');
1212
PHP
1313
);
1414

15-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
16-
$port = intval($port)?:80;
17-
18-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
19-
if (!$fp) {
20-
die("connect failed");
21-
}
15+
$host = PHP_CLI_SERVER_HOSTNAME;
16+
$fp = php_cli_server_connect();
2217

2318
if(fwrite($fp, <<<HEADER
2419
GET / HTTP/1.1

sapi/cli/tests/bug67429_1.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ http_response_code(308);
1313
PHP
1414
);
1515

16-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
17-
$port = intval($port)?:80;
18-
19-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
20-
if (!$fp) {
21-
die("connect failed");
22-
}
16+
$host = PHP_CLI_SERVER_HOSTNAME;
17+
$fp = php_cli_server_connect();
2318

2419
if(fwrite($fp, <<<HEADER
2520
GET / HTTP/1.1

sapi/cli/tests/bug67429_2.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ http_response_code(426);
1313
PHP
1414
);
1515

16-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
17-
$port = intval($port)?:80;
18-
19-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
20-
if (!$fp) {
21-
die("connect failed");
22-
}
16+
$host = PHP_CLI_SERVER_HOSTNAME;
17+
$fp = php_cli_server_connect();
2318

2419
if(fwrite($fp, <<<HEADER
2520
GET / HTTP/1.1

sapi/cli/tests/bug68745.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start('var_dump(count($_SERVER));', 'not-index.php');
1111

12-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
13-
$port = intval($port)?:80;
14-
15-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
16-
if (!$fp) {
17-
die("connect failed");
18-
}
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
1914

2015
if(fwrite($fp, "GET www.example.com:80 HTTP/1.1\r\n\r\n")) {
2116
while (!feof($fp)) {

sapi/cli/tests/bug70470.phpt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start("var_dump(getAllheaders());");
1111

12-
$fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errmsg, 0.5);
13-
14-
if (!$fp) {
15-
die("connect failed: " . $errmsg);
16-
}
12+
$fp = php_cli_server_connect();
1713

1814
fwrite($fp, "GET / HTTP/1.1\r\n");
1915
fwrite($fp, "Host: " . PHP_CLI_SERVER_HOSTNAME . "\r\n");

sapi/cli/tests/bug71005.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ EOF;
1515
include "php_cli_server.inc";
1616
php_cli_server_start($code);
1717

18-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
19-
$port = intval($port) ?: 80;
20-
21-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
22-
if (!$fp) {
23-
die("connect failed");
24-
}
18+
$host = PHP_CLI_SERVER_HOSTNAME;
19+
$fp = php_cli_server_connect();
2520

2621
if(fwrite($fp, <<<HEADER
2722
GET / HTTP/1.1

sapi/cli/tests/emptyheader.phpt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start("var_dump(getAllheaders());");
1111

12-
$fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errmsg, 0.5);
13-
14-
if (!$fp) {
15-
die("connect failed: " . $errmsg);
16-
}
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
1714

1815
fwrite($fp, "GET / HTTP/1.1\r\nUser-Agent\r\nAccept: */*\r\nReferer:\r\nHi\r\n\r\n");
1916
fflush($fp);

sapi/cli/tests/php_cli_server.inc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
3-
define ("PHP_CLI_SERVER_PORT", 8964);
4-
define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
2+
define("PHP_CLI_SERVER_HOSTNAME", "localhost");
3+
define("PHP_CLI_SERVER_PORT", 8964);
4+
define("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
55

66
function php_cli_server_start(
77
?string $code = 'echo "Hello world";',
@@ -96,4 +96,14 @@ php_cli_server_start_error:
9696

9797
return $handle;
9898
}
99+
100+
function php_cli_server_connect() {
101+
$timeout = 1.0;
102+
$fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errstr, $timeout);
103+
if (!$fp) {
104+
die("connect failed");
105+
}
106+
return $fp;
107+
}
108+
99109
?>

sapi/cli/tests/php_cli_server_004.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ include "skipif.inc";
1111
include "php_cli_server.inc";
1212
php_cli_server_start('foreach($_SERVER as $k=>$v) { if (!strncmp($k, "HTTP", 4)) var_dump( $k . ":" . $v); }');
1313

14-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15-
$port = intval($port)?:80;
16-
17-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18-
if (!$fp) {
19-
die("connect failed");
20-
}
14+
$host = PHP_CLI_SERVER_HOSTNAME;
15+
$fp = php_cli_server_connect();
2116

2217
if(fwrite($fp, <<<HEADER
2318
GET / HTTP/1.1

sapi/cli/tests/php_cli_server_005.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start('var_dump($_FILES);');
1111

12-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
13-
$port = intval($port)?:80;
14-
15-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
16-
if (!$fp) {
17-
die("connect failed");
18-
}
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
1914

2015
$post_data = <<<POST
2116
-----------------------------114782935826962

sapi/cli/tests/php_cli_server_006.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start('var_dump($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);');
1111

12-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
13-
$port = intval($port)?:80;
14-
15-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
16-
if (!$fp) {
17-
die("connect failed");
18-
}
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
1914

2015
if(fwrite($fp, <<<HEADER
2116
GET / HTTP/1.1

sapi/cli/tests/php_cli_server_007.phpt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start('header(\'WWW-Authenticate: Digest realm="foo",qop="auth",nonce="XXXXX",opaque="'.md5("foo").'"\');');
1111

12-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
13-
$port = intval($port)?:80;
14-
15-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
16-
if (!$fp) {
17-
die("connect failed");
18-
}
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
1914

2015
if(fwrite($fp, <<<HEADER
2116
GET / HTTP/1.1

sapi/cli/tests/php_cli_server_008.phpt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ include "skipif.inc";
99
include "php_cli_server.inc";
1010
php_cli_server_start('var_dump($_SERVER["SERVER_PROTOCOL"]);');
1111

12-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
13-
$port = intval($port)?:80;
14-
15-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
16-
if (!$fp) {
17-
die("connect failed");
18-
}
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
1914

2015
if(fwrite($fp, <<<HEADER
2116
GET / HTTP/1.1
@@ -31,11 +26,7 @@ HEADER
3126

3227
fclose($fp);
3328

34-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
35-
if (!$fp) {
36-
die("connect failed");
37-
}
38-
29+
$fp = php_cli_server_connect();
3930

4031
if(fwrite($fp, <<<HEADER
4132
GET / HTTP/1.0

sapi/cli/tests/php_cli_server_009.phpt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ include "skipif.inc";
1212
include "php_cli_server.inc";
1313
php_cli_server_start('var_dump($_SERVER["PATH_INFO"]);', null);
1414

15-
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
16-
$port = intval($port)?:80;
17-
18-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
19-
if (!$fp) {
20-
die("connect failed");
21-
}
15+
$host = PHP_CLI_SERVER_HOSTNAME;
16+
$fp = php_cli_server_connect();
2217

2318
if(fwrite($fp, <<<HEADER
2419
GET /foo/bar HTTP/1.1
@@ -34,11 +29,7 @@ HEADER
3429

3530
fclose($fp);
3631

37-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
38-
if (!$fp) {
39-
die("connect failed");
40-
}
41-
32+
$fp = php_cli_server_connect();
4233

4334
if(fwrite($fp, <<<HEADER
4435
GET /foo/bar/ HTTP/1.0
@@ -54,11 +45,7 @@ HEADER
5445

5546
fclose($fp);
5647

57-
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
58-
if (!$fp) {
59-
die("connect failed");
60-
}
61-
48+
$fp = php_cli_server_connect();
6249

6350
if(fwrite($fp, <<<HEADER
6451
GET /foo/bar.js HTTP/1.0

0 commit comments

Comments
 (0)