To encapsulate the repeated fsockopen() code.
This gives us a chance to control the timeout in one place:
Raise it to one second.
SCRIPT
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
+$host = PHP_CLI_SERVER_HOSTNAME;
foreach(array("parse", "fatal", "fatal2", "compile") as $url) {
- $fp = fsockopen($host, $port, $errno, $errstr, 0.5);
- if (!$fp) {
- die("connect failed");
- }
-
+ $fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /$url HTTP/1.1
Host: {$host}
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
// Send a request with a fictitious request method,
// I like smurfs, the smurf everything.
function test_mimetypes($mimetypes) {
foreach ($mimetypes as $mimetype) {
- list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
- $port = intval($port) ? : 80;
- $fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+ $host = PHP_CLI_SERVER_HOSTNAME;
+ $fp = php_cli_server_connect();
if (!$fp) die('Connect failed');
file_put_contents(__DIR__ . "/foo.{$mimetype}", '');
$header = <<<HEADER
include "php_cli_server.inc";
php_cli_server_start('http_response_code(100);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('http_response_code(422);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('http_response_code(511);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["CONTENT_TYPE"], $_SERVER["CONTENT_LENGTH"])');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if (fwrite($fp, <<<HEADER
POST /index.php HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('var_dump(count($_SERVER));', 'not-index.php');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, "GET www.example.com:80 HTTP/1.1\r\n\r\n")) {
while (!feof($fp)) {
include "php_cli_server.inc";
php_cli_server_start("var_dump(getAllheaders());");
-$fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errmsg, 0.5);
-
-if (!$fp) {
- die("connect failed: " . $errmsg);
-}
+$fp = php_cli_server_connect();
fwrite($fp, "GET / HTTP/1.1\r\n");
fwrite($fp, "Host: " . PHP_CLI_SERVER_HOSTNAME . "\r\n");
include "php_cli_server.inc";
php_cli_server_start($code);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port) ?: 80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start("var_dump(getAllheaders());");
-$fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errmsg, 0.5);
-
-if (!$fp) {
- die("connect failed: " . $errmsg);
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
fwrite($fp, "GET / HTTP/1.1\r\nUser-Agent\r\nAccept: */*\r\nReferer:\r\nHi\r\n\r\n");
fflush($fp);
<?php
-define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
-define ("PHP_CLI_SERVER_PORT", 8964);
-define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
+define("PHP_CLI_SERVER_HOSTNAME", "localhost");
+define("PHP_CLI_SERVER_PORT", 8964);
+define("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
function php_cli_server_start(
?string $code = 'echo "Hello world";',
return $handle;
}
+
+function php_cli_server_connect() {
+ $timeout = 1.0;
+ $fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errstr, $timeout);
+ if (!$fp) {
+ die("connect failed");
+ }
+ return $fp;
+}
+
?>
include "php_cli_server.inc";
php_cli_server_start('foreach($_SERVER as $k=>$v) { if (!strncmp($k, "HTTP", 4)) var_dump( $k . ":" . $v); }');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('var_dump($_FILES);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
$post_data = <<<POST
-----------------------------114782935826962
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('header(\'WWW-Authenticate: Digest realm="foo",qop="auth",nonce="XXXXX",opaque="'.md5("foo").'"\');');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["SERVER_PROTOCOL"]);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
fclose($fp);
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
-
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.0
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["PATH_INFO"]);', null);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /foo/bar HTTP/1.1
fclose($fp);
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
-
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /foo/bar/ HTTP/1.0
fclose($fp);
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
-
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /foo/bar.js HTTP/1.0
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["PHP_SELF"], $_SERVER["SCRIPT_NAME"], $_SERVER["PATH_INFO"], $_SERVER["QUERY_STRING"]);', null);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /foo/bar?foo=bar HTTP/1.1
fclose($fp);
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
-
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /index.php/foo/bar/?foo=bar HTTP/1.0
$doc_root = __DIR__;
file_put_contents($doc_root . '/request.php', '<?php print_r($_REQUEST); ?>');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
POST /request.php HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start(NULL, NULL);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
$output = '';
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
-
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
POST / HTTP/1.1
$output = '';
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /main/style.css HTTP/1.1
fclose($fp);
$output = '';
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
HEAD /main/foo/bar HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('echo "done\n";', null);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
$output = '';
-
-// note: select() on Windows (& some other platforms) has historical issues with
-// timeouts less than 1000 millis(0.5). it may be better to increase these
-// timeouts to 1000 millis(1.0) (fsockopen eventually calls select()).
-// see articles like: http://support.microsoft.com/kb/257821
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
POST /index.php HTTP/1.1
fclose($fp);
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
POST /main/no-exists.php HTTP/1.1
Host: {$host}
file_put_contents($dir . "/syntax_error.php", "<?php non_exists_function(); ?>");
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
$output = '';
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET /index.php HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
POST /no-exists.jpg HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
POST / HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
PATCH / HTTP/1.1
PHP
);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
GET / HTTP/1.1
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["REQUEST_METHOD"]);');
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port) ?: 80;
-
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
if(fwrite($fp, <<<HEADER
SEARCH / HTTP/1.1
php_cli_server_start("var_dump(\$_FILES);", null,
["-d", "post_max_size=3G", "-d", "upload_max_filesize=3G"]);
-list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
-$port = intval($port)?:80;
$length = 2150000000;
$output = "";
-$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
-if (!$fp) {
- die("connect failed");
-}
+$host = PHP_CLI_SERVER_HOSTNAME;
+$fp = php_cli_server_connect();
$prev = "----123
Content-Type: text/plain; charset=UTF-8