You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.0 KiB

  1. <?php
  2. namespace p3k\HTTP;
  3. // This implements the same interface as the main class p3k\HTTP
  4. // despite the fact that it looks like just a transport plugin.
  5. // This is so that the main p3k\HTTP object can be replaced with this
  6. // object in the test suite.
  7. class Test implements Transport {
  8. private $_testDataPath;
  9. private $_redirects_remaining;
  10. public function __construct($testDataPath) {
  11. $this->_testDataPath = $testDataPath;
  12. }
  13. protected $_timeout = 4;
  14. protected $_max_redirects = 8;
  15. public function set_max_redirects($max) {
  16. $this->_max_redirects = $max;
  17. }
  18. public function set_timeout($timeout) {
  19. $this->_timeout = $timeout;
  20. }
  21. public function set_transport(Transport $transport) {
  22. }
  23. public function get($url, $headers=[]) {
  24. $this->_redirects_remaining = $this->_max_redirects;
  25. $parts = parse_url($url);
  26. unset($parts['fragment']);
  27. $url = self::_build_url($parts);
  28. return $this->_read_file($url);
  29. }
  30. public function post($url, $body, $headers=[]) {
  31. return $this->_read_file($url);
  32. }
  33. public function head($url, $headers=[]) {
  34. $response = $this->_read_file($url);
  35. return [
  36. 'code' => (int)$response['code'],
  37. 'header' => $response['header'],
  38. 'headers' => $response['headers'],
  39. 'rels' => $response['rels'],
  40. 'error' => '',
  41. 'error_description' => '',
  42. 'url' => $response['url']
  43. ];
  44. }
  45. private function _read_file($url) {
  46. $parts = parse_url($url);
  47. if($parts['path']) {
  48. $parts['path'] = '/'.str_replace('/','_',substr($parts['path'],1));
  49. $filepathurl = self::_build_url($parts);
  50. }
  51. $filename = $this->_testDataPath.preg_replace('/https?:\/\//', '', $filepathurl);
  52. if(!file_exists($filename)) {
  53. $filename = $this->_testDataPath.'404.response.txt';
  54. }
  55. $response = file_get_contents($filename);
  56. $split = explode("\r\n\r\n", $response);
  57. if(count($split) < 2) {
  58. throw new \Exception("Invalid file contents in test data, check that newlines are CRLF: $url");
  59. }
  60. $headers = array_shift($split);
  61. $body = implode("\r\n", $split);
  62. if(preg_match('/HTTP\/1\.1 (\d+)/', $headers, $match)) {
  63. $code = $match[1];
  64. }
  65. $headers = preg_replace('/HTTP\/1\.1 \d+ .+/', '', $headers);
  66. $parsedHeaders = self::_parse_headers($headers);
  67. if(array_key_exists('Location', $parsedHeaders)) {
  68. $effectiveUrl = \mf2\resolveUrl($url, $parsedHeaders['Location']);
  69. if($this->_redirects_remaining > 0) {
  70. $this->_redirects_remaining--;
  71. return $this->_read_file($effectiveUrl);
  72. } else {
  73. return [
  74. 'code' => 0,
  75. 'header' => $headers,
  76. 'headers' => $parsedHeaders,
  77. 'rels' => \IndieWeb\http_rels($headers),
  78. 'body' => $body,
  79. 'error' => 'too_many_redirects',
  80. 'error_description' => '',
  81. 'url' => $effectiveUrl
  82. ];
  83. }
  84. } else {
  85. $effectiveUrl = $url;
  86. }
  87. return [
  88. 'code' => (int)$code,
  89. 'header' => $headers,
  90. 'headers' => $parsedHeaders,
  91. 'rels' => \IndieWeb\http_rels($headers),
  92. 'body' => $body,
  93. 'error' => (isset($parsedHeaders['X-Test-Error']) ? $parsedHeaders['X-Test-Error'] : ''),
  94. 'error_description' => '',
  95. 'url' => $effectiveUrl
  96. ];
  97. }
  98. private static function _parse_headers($headers) {
  99. $retVal = [];
  100. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  101. foreach($fields as $field) {
  102. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  103. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  104. return strtoupper($m[0]);
  105. }, strtolower(trim($match[1])));
  106. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  107. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  108. return strtoupper($m[0]);
  109. }, strtolower(trim($match[1])));
  110. if(isset($retVal[$match[1]])) {
  111. if(!is_array($retVal[$match[1]]))
  112. $retVal[$match[1]] = [$retVal[$match[1]]];
  113. $retVal[$match[1]][] = $match[2];
  114. } else {
  115. $retVal[$match[1]] = trim($match[2]);
  116. }
  117. }
  118. }
  119. return $retVal;
  120. }
  121. private static function _build_url($parsed_url) {
  122. $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  123. $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  124. $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  125. $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  126. $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
  127. $pass = ($user || $pass) ? "$pass@" : '';
  128. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  129. $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
  130. $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  131. return "$scheme$user$pass$host$port$path$query$fragment";
  132. }
  133. }