xref: /PHP-5.5/ext/curl/tests/server.inc (revision c507c9f0)
1<?php
2
3define ("PHP_CURL_SERVER_HOSTNAME", "localhost");
4define ("PHP_CURL_SERVER_PORT", 8964);
5define ("PHP_CURL_SERVER_ADDRESS", PHP_CURL_SERVER_HOSTNAME.":".PHP_CURL_SERVER_PORT);
6
7function curl_cli_server_start() {
8    if(getenv('PHP_CURL_HTTP_REMOTE_SERVER')) {
9        return getenv('PHP_CURL_HTTP_REMOTE_SERVER');
10    }
11
12	$php_executable = getenv('TEST_PHP_EXECUTABLE');
13	$doc_root = __DIR__;
14	$router = "responder/get.php";
15
16	$descriptorspec = array(
17		0 => STDIN,
18		1 => STDOUT,
19		2 => STDERR,
20	);
21
22	if (substr(PHP_OS, 0, 3) == 'WIN') {
23		$cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
24        $cmd .= " {$router}";
25		$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true,  "suppress_errors" => true));
26	} else {
27		$cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
28		$cmd .= " {$router}";
29		$cmd .= " 2>/dev/null";
30
31		$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
32	}
33
34	// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
35	//       it might not be listening yet...need to wait until fsockopen() call returns
36    $i = 0;
37    while (($i++ < 30) && !($fp = @fsockopen(PHP_CURL_SERVER_HOSTNAME, PHP_CURL_SERVER_PORT))) {
38        usleep(10000);
39    }
40
41    if ($fp) {
42        fclose($fp);
43    }
44
45	register_shutdown_function(
46		function($handle) use($router) {
47			proc_terminate($handle);
48		},
49			$handle
50		);
51	// don't bother sleeping, server is already up
52	// server can take a variable amount of time to be up, so just sleeping a guessed amount of time
53	// does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
54	// sleeping doesn't work.
55    return PHP_CURL_SERVER_ADDRESS;
56}
57