WPHTTP-testcase-redirection-script.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // Thanks WordPress...
  3. function is_ssl() {
  4. if ( isset($_SERVER['HTTPS']) ) {
  5. if ( 'on' == strtolower($_SERVER['HTTPS']) )
  6. return true;
  7. if ( '1' == $_SERVER['HTTPS'] )
  8. return true;
  9. } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
  10. return true;
  11. }
  12. return false;
  13. }
  14. $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . (!empty($_SERVER['HTTP_POST']) && 80 != $_SERVER['HTTP_POST'] ? ':' . $_SERVER['HTTP_POST'] : '');
  15. if ( strpos($_SERVER['REQUEST_URI'], '?') )
  16. $url .= substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?'));
  17. else
  18. $url .= $_SERVER['REQUEST_URI'];
  19. if ( isset($_GET['source']) ) {
  20. highlight_file(__FILE__ );
  21. exit;
  22. }
  23. if ( isset($_GET['201-location']) ) {
  24. header("HTTP/1.1 201 OK");
  25. if ( isset($_GET['fail']) ) {
  26. echo "FAIL";
  27. } else {
  28. header("Location: $url?201-location&fail=true", true, 201);
  29. echo "PASS";
  30. }
  31. exit;
  32. }
  33. if ( isset($_GET['header-check']) ) {
  34. $out = array();
  35. header("Content-type: text/plain");
  36. foreach ( $_SERVER as $key => $value ) {
  37. if ( stripos($key, 'http') === 0 ) {
  38. $key = strtolower(substr($key, 5));
  39. echo "$key:$value\n";
  40. }
  41. }
  42. exit;
  43. }
  44. if ( isset($_GET['multiple-headers']) ) {
  45. header("HeaderName: One", false);
  46. header("HeaderName: Two", false);
  47. header("HeaderName: Three", false);
  48. exit;
  49. }
  50. if ( isset( $_GET['post-redirect-to-method'] ) ) {
  51. $method = $_SERVER['REQUEST_METHOD'];
  52. $response_code = isset( $_GET['response_code'] ) ? $_GET['response_code'] : 301;
  53. if ( 'POST' == $method && ! isset( $_GET['redirection-performed'] ) ) {
  54. header( "Location: $url?post-redirect-to-method=1&redirection-performed=1", true, $response_code );
  55. exit;
  56. }
  57. echo $method;
  58. exit;
  59. }
  60. if ( isset( $_GET['location-with-200'] ) ) {
  61. if ( ! isset( $_GET['redirection-performed'] ) ) {
  62. header( "HTTP/1.1 200 OK" );
  63. header( "Location: $url?location-with-200=1&redirection-performed", true, 200 );
  64. echo 'PASS';
  65. exit;
  66. }
  67. // Redirection was followed.
  68. echo 'FAIL';
  69. exit;
  70. }
  71. if ( isset( $_GET['print-pass'] ) ) {
  72. echo 'PASS';
  73. exit;
  74. }
  75. if ( isset( $_GET['multiple-location-headers'] ) ) {
  76. if ( ! isset( $_GET['redirected'] ) ) {
  77. header( "Location: $url?multiple-location-headers=1&redirected=one", false );
  78. header( "Location: $url?multiple-location-headers=1&redirected=two", false );
  79. exit;
  80. }
  81. if ( 'two' != $_GET['redirected'] )
  82. echo 'FAIL';
  83. else
  84. echo 'PASS';
  85. exit;
  86. }
  87. if ( isset( $_GET['cookie-test'] ) ) {
  88. if ( 'test-cookie' != $_GET['cookie-test'] ) {
  89. setcookie( 'api_test_cookie', 'value', time() + 365*24*60*60, '/core/tests/1.0/', 'api.wordpress.org' );
  90. setcookie( 'api_test_cookie_minimal', 'value' );
  91. setcookie( 'api_test_cookie_wrong_host', 'value', time() + 365*24*60*60, '/', 'example.com' );
  92. setcookie( 'api_test_wildcard_domain', 'value', time() + 365*24*60*60, '/', '.wordpress.org' );
  93. setcookie( 'api_test_cookie_expired', 'value', time() - 365*24*60*60, '/', '.wordpress.org' );
  94. header( "Location: $url?cookie-test=test-cookie" );
  95. exit;
  96. }
  97. if ( empty( $_COOKIE['api_test_cookie'] ) || 'value' != $_COOKIE['api_test_cookie'] )
  98. die( 'FAIL_NO_COOKIE' );
  99. if ( empty( $_COOKIE['api_test_cookie_minimal'] ) )
  100. die( 'FAIL_NO_MINIMAL' );
  101. if ( isset( $_COOKIE['api_test_cookie_wrong_host'] ) )
  102. die( 'FAIL_WRONG_HOST' );
  103. if ( empty( $_COOKIE['api_test_wildcard_domain'] ) )
  104. die( 'FAIL_NO_WILDCARD' );
  105. if ( isset( $_COOKIE['api_test_cookie_expired'] ) )
  106. die( 'FAIL_EXPIRED_COOKIE' );
  107. echo 'PASS';
  108. exit;
  109. }
  110. $rt = isset($_GET['rt']) ? $_GET['rt'] : 5;
  111. $r = isset($_GET['r']) ? $_GET['r'] : 0;
  112. if ( $r < $rt ) {
  113. $code = isset($_GET['code']) ? (int)$_GET['code'] : 302;
  114. header("Location: $url?rt=" . $rt . "&r=" . ($r+1), true, $code);
  115. echo "Redirect $r of $rt";
  116. exit;
  117. }
  118. echo "Redirect $r of $rt is FINAL.<br/>";
  119. echo "GET['rt'] = Total times to redirect. Defaults to 5.<br />";
  120. echo "GET['r'] = Current redirection. Defaults to 0.<br />";
  121. echo "<a href='$url?source=true'>View Source</a>";