index.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 '/404-gzipped':
  56. header('Content-Type: text/plain', true, 404);
  57. ob_start('ob_gzhandler');
  58. @ob_flush();
  59. flush();
  60. usleep(300000);
  61. echo 'some text';
  62. exit;
  63. case '/301':
  64. if ('Basic Zm9vOmJhcg==' === $vars['HTTP_AUTHORIZATION']) {
  65. header('Location: http://127.0.0.1:8057/302', true, 301);
  66. }
  67. break;
  68. case '/301/bad-tld':
  69. header('Location: http://foo.example.', true, 301);
  70. break;
  71. case '/301/invalid':
  72. header('Location: //?foo=bar', true, 301);
  73. break;
  74. case '/302':
  75. if (!isset($vars['HTTP_AUTHORIZATION'])) {
  76. header('Location: http://localhost:8057/', true, 302);
  77. }
  78. break;
  79. case '/302/relative':
  80. header('Location: ..', true, 302);
  81. break;
  82. case '/304':
  83. header('Content-Length: 10', true, 304);
  84. echo '12345';
  85. return;
  86. case '/307':
  87. header('Location: http://localhost:8057/post', true, 307);
  88. break;
  89. case '/length-broken':
  90. header('Content-Length: 1000');
  91. break;
  92. case '/post':
  93. $output = json_encode($_POST + ['REQUEST_METHOD' => $vars['REQUEST_METHOD']], \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
  94. header('Content-Type: application/json', true);
  95. header('Content-Length: '.strlen($output));
  96. echo $output;
  97. exit;
  98. case '/timeout-header':
  99. usleep(300000);
  100. break;
  101. case '/timeout-body':
  102. echo '<1>';
  103. @ob_flush();
  104. flush();
  105. usleep(500000);
  106. echo '<2>';
  107. exit;
  108. case '/timeout-long':
  109. ignore_user_abort(false);
  110. sleep(1);
  111. while (true) {
  112. echo '<1>';
  113. @ob_flush();
  114. flush();
  115. usleep(500);
  116. }
  117. exit;
  118. case '/chunked':
  119. header('Transfer-Encoding: chunked');
  120. echo "8\r\nSymfony \r\n5\r\nis aw\r\n6\r\nesome!\r\n0\r\n\r\n";
  121. exit;
  122. case '/chunked-broken':
  123. header('Transfer-Encoding: chunked');
  124. echo "8\r\nSymfony \r\n5\r\nis aw\r\n6\r\ne";
  125. exit;
  126. case '/gzip-broken':
  127. header('Content-Encoding: gzip');
  128. echo str_repeat('-', 1000);
  129. exit;
  130. case '/max-duration':
  131. ignore_user_abort(false);
  132. while (true) {
  133. echo '<1>';
  134. @ob_flush();
  135. flush();
  136. usleep(500);
  137. }
  138. exit;
  139. case '/json':
  140. header('Content-Type: application/json');
  141. echo json_encode([
  142. 'documents' => [
  143. ['id' => '/json/1'],
  144. ['id' => '/json/2'],
  145. ['id' => '/json/3'],
  146. ],
  147. ]);
  148. exit;
  149. case '/json/1':
  150. case '/json/2':
  151. case '/json/3':
  152. header('Content-Type: application/json');
  153. echo json_encode([
  154. 'title' => $vars['REQUEST_URI'],
  155. ]);
  156. exit;
  157. }
  158. header('Content-Type: application/json', true);
  159. echo $json;