index.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. if ('cli-server' !== \PHP_SAPI) {
  3. // safe guard against unwanted execution
  4. throw new \Exception("You cannot run this script directly, it's a fixture for TestHttpServer.");
  5. }
  6. $vars = [];
  7. if (!$_POST) {
  8. $_POST = json_decode(file_get_contents('php://input'), true);
  9. $_POST['content-type'] = $_SERVER['HTTP_CONTENT_TYPE'] ?? '?';
  10. }
  11. foreach ($_SERVER as $k => $v) {
  12. switch ($k) {
  13. default:
  14. if (0 !== strpos($k, 'HTTP_')) {
  15. continue 2;
  16. }
  17. // no break
  18. case 'SERVER_NAME':
  19. case 'SERVER_PROTOCOL':
  20. case 'REQUEST_URI':
  21. case 'REQUEST_METHOD':
  22. case 'PHP_AUTH_USER':
  23. case 'PHP_AUTH_PW':
  24. $vars[$k] = $v;
  25. }
  26. }
  27. switch ($vars['REQUEST_URI']) {
  28. default:
  29. exit;
  30. case '/':
  31. case '/?a=a&b=b':
  32. case 'http://127.0.0.1:8057/':
  33. case 'http://localhost:8057/':
  34. header('Content-Type: application/json');
  35. ob_start('ob_gzhandler');
  36. break;
  37. case '/404':
  38. header('Content-Type: application/json', true, 404);
  39. break;
  40. case '/301':
  41. if ('Basic Zm9vOmJhcg==' === $vars['HTTP_AUTHORIZATION']) {
  42. header('Location: http://127.0.0.1:8057/302', true, 301);
  43. }
  44. break;
  45. case '/301/bad-tld':
  46. header('Location: http://foo.example.', true, 301);
  47. break;
  48. case '/302':
  49. if (!isset($vars['HTTP_AUTHORIZATION'])) {
  50. header('Location: http://localhost:8057/', true, 302);
  51. }
  52. break;
  53. case '/302/relative':
  54. header('Location: ..', true, 302);
  55. break;
  56. case '/307':
  57. header('Location: http://localhost:8057/post', true, 307);
  58. break;
  59. case '/length-broken':
  60. header('Content-Length: 1000');
  61. break;
  62. case '/post':
  63. $output = json_encode($_POST + ['REQUEST_METHOD' => $vars['REQUEST_METHOD']], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  64. header('Content-Type: application/json', true);
  65. header('Content-Length: '.\strlen($output));
  66. echo $output;
  67. exit;
  68. case '/timeout-header':
  69. usleep(300000);
  70. break;
  71. case '/timeout-body':
  72. echo '<1>';
  73. @ob_flush();
  74. flush();
  75. usleep(500000);
  76. echo '<2>';
  77. exit;
  78. case '/timeout-long':
  79. ignore_user_abort(false);
  80. sleep(1);
  81. while (true) {
  82. echo '<1>';
  83. @ob_flush();
  84. flush();
  85. usleep(500);
  86. }
  87. exit;
  88. case '/chunked':
  89. header('Transfer-Encoding: chunked');
  90. echo "8\r\nSymfony \r\n5\r\nis aw\r\n6\r\nesome!\r\n0\r\n\r\n";
  91. exit;
  92. case '/chunked-broken':
  93. header('Transfer-Encoding: chunked');
  94. echo "8\r\nSymfony \r\n5\r\nis aw\r\n6\r\ne";
  95. exit;
  96. case '/gzip-broken':
  97. header('Content-Encoding: gzip');
  98. echo str_repeat('-', 1000);
  99. exit;
  100. }
  101. header('Content-Type: application/json', true);
  102. echo json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);