index.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $json = json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  28. switch ($vars['REQUEST_URI']) {
  29. default:
  30. exit;
  31. case '/head':
  32. header('Content-Length: '.strlen($json), true);
  33. break;
  34. case '/':
  35. case '/?a=a&b=b':
  36. case 'http://127.0.0.1:8057/':
  37. case 'http://localhost:8057/':
  38. ob_start('ob_gzhandler');
  39. break;
  40. case '/103':
  41. header('HTTP/1.1 103 Early Hints');
  42. header('Link: </style.css>; rel=preload; as=style', false);
  43. header('Link: </script.js>; rel=preload; as=script', false);
  44. flush();
  45. usleep(1000);
  46. echo "HTTP/1.1 200 OK\r\n";
  47. echo "Date: Fri, 26 May 2017 10:02:11 GMT\r\n";
  48. echo "Content-Length: 13\r\n";
  49. echo "\r\n";
  50. echo 'Here the body';
  51. exit;
  52. case '/404':
  53. header('Content-Type: application/json', true, 404);
  54. break;
  55. case '/301':
  56. if ('Basic Zm9vOmJhcg==' === $vars['HTTP_AUTHORIZATION']) {
  57. header('Location: http://127.0.0.1:8057/302', true, 301);
  58. }
  59. break;
  60. case '/301/bad-tld':
  61. header('Location: http://foo.example.', true, 301);
  62. break;
  63. case '/301/invalid':
  64. header('Location: //?foo=bar', true, 301);
  65. break;
  66. case '/302':
  67. if (!isset($vars['HTTP_AUTHORIZATION'])) {
  68. header('Location: http://localhost:8057/', true, 302);
  69. }
  70. break;
  71. case '/302/relative':
  72. header('Location: ..', true, 302);
  73. break;
  74. case '/304':
  75. header('Content-Length: 10', true, 304);
  76. echo '12345';
  77. return;
  78. case '/307':
  79. header('Location: http://localhost:8057/post', true, 307);
  80. break;
  81. case '/length-broken':
  82. header('Content-Length: 1000');
  83. break;
  84. case '/post':
  85. $output = json_encode($_POST + ['REQUEST_METHOD' => $vars['REQUEST_METHOD']], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  86. header('Content-Type: application/json', true);
  87. header('Content-Length: '.strlen($output));
  88. echo $output;
  89. exit;
  90. case '/timeout-header':
  91. usleep(300000);
  92. break;
  93. case '/timeout-body':
  94. echo '<1>';
  95. @ob_flush();
  96. flush();
  97. usleep(500000);
  98. echo '<2>';
  99. exit;
  100. case '/timeout-long':
  101. ignore_user_abort(false);
  102. sleep(1);
  103. while (true) {
  104. echo '<1>';
  105. @ob_flush();
  106. flush();
  107. usleep(500);
  108. }
  109. exit;
  110. case '/chunked':
  111. header('Transfer-Encoding: chunked');
  112. echo "8\r\nSymfony \r\n5\r\nis aw\r\n6\r\nesome!\r\n0\r\n\r\n";
  113. exit;
  114. case '/chunked-broken':
  115. header('Transfer-Encoding: chunked');
  116. echo "8\r\nSymfony \r\n5\r\nis aw\r\n6\r\ne";
  117. exit;
  118. case '/gzip-broken':
  119. header('Content-Encoding: gzip');
  120. echo str_repeat('-', 1000);
  121. exit;
  122. case '/max-duration':
  123. ignore_user_abort(false);
  124. while (true) {
  125. echo '<1>';
  126. @ob_flush();
  127. flush();
  128. usleep(500);
  129. }
  130. exit;
  131. case '/json':
  132. header("Content-Type: application/json");
  133. echo json_encode([
  134. 'documents' => [
  135. ['id' => '/json/1'],
  136. ['id' => '/json/2'],
  137. ['id' => '/json/3'],
  138. ],
  139. ]);
  140. exit;
  141. case '/json/1':
  142. case '/json/2':
  143. case '/json/3':
  144. header("Content-Type: application/json");
  145. echo json_encode([
  146. 'title' => $vars['REQUEST_URI'],
  147. ]);
  148. exit;
  149. }
  150. header('Content-Type: application/json', true);
  151. echo $json;