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.

138 lines
4.1 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 = \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) {
  34. $response = $this->_read_file($url);
  35. return array(
  36. 'code' => $response['code'],
  37. 'headers' => $response['headers'],
  38. 'rels' => $response['rels'],
  39. 'error' => '',
  40. 'error_description' => '',
  41. 'url' => $response['url']
  42. );
  43. }
  44. private function _read_file($url) {
  45. $parts = parse_url($url);
  46. if($parts['path']) {
  47. $parts['path'] = '/'.str_replace('/','_',substr($parts['path'],1));
  48. $url = \build_url($parts);
  49. }
  50. $filename = $this->_testDataPath.preg_replace('/https?:\/\//', '', $url);
  51. if(!file_exists($filename)) {
  52. $filename = $this->_testDataPath.'404.response.txt';
  53. }
  54. $response = file_get_contents($filename);
  55. $split = explode("\r\n\r\n", $response);
  56. if(count($split) < 2) {
  57. throw new \Exception("Invalid file contents in test data, check that newlines are CRLF: $url");
  58. }
  59. $headers = array_shift($split);
  60. $body = implode("\r\n", $split);
  61. if(preg_match('/HTTP\/1\.1 (\d+)/', $headers, $match)) {
  62. $code = $match[1];
  63. }
  64. $headers = preg_replace('/HTTP\/1\.1 \d+ .+/', '', $headers);
  65. $parsedHeaders = self::_parse_headers($headers);
  66. if(array_key_exists('Location', $parsedHeaders)) {
  67. $effectiveUrl = \mf2\resolveUrl($url, $parsedHeaders['Location']);
  68. if($this->_redirects_remaining > 0) {
  69. $this->_redirects_remaining--;
  70. return $this->_read_file($effectiveUrl);
  71. } else {
  72. return [
  73. 'code' => 0,
  74. 'headers' => $parsedHeaders,
  75. 'rels' => \IndieWeb\http_rels($headers),
  76. 'body' => $body,
  77. 'error' => 'too_many_redirects',
  78. 'error_description' => '',
  79. 'url' => $effectiveUrl
  80. ];
  81. }
  82. } else {
  83. $effectiveUrl = $url;
  84. }
  85. return array(
  86. 'code' => $code,
  87. 'headers' => $parsedHeaders,
  88. 'rels' => \IndieWeb\http_rels($headers),
  89. 'body' => $body,
  90. 'error' => (isset($parsedHeaders['X-Test-Error']) ? $parsedHeaders['X-Test-Error'] : ''),
  91. 'error_description' => '',
  92. 'url' => $effectiveUrl
  93. );
  94. }
  95. private static function _parse_headers($headers) {
  96. $retVal = array();
  97. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  98. foreach($fields as $field) {
  99. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  100. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  101. return strtoupper($m[0]);
  102. }, strtolower(trim($match[1])));
  103. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  104. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  105. return strtoupper($m[0]);
  106. }, strtolower(trim($match[1])));
  107. if(isset($retVal[$match[1]])) {
  108. if(!is_array($retVal[$match[1]]))
  109. $retVal[$match[1]] = array($retVal[$match[1]]);
  110. $retVal[$match[1]][] = $match[2];
  111. } else {
  112. $retVal[$match[1]] = trim($match[2]);
  113. }
  114. }
  115. }
  116. return $retVal;
  117. }
  118. }