server.inc 1.7 KB

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