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.

103 lines
3.2 KiB

  1. <?php
  2. namespace p3k;
  3. class HTTP {
  4. public $_timeout = 4;
  5. public $_max_redirects = 8;
  6. private $_transport;
  7. private $_user_agent;
  8. public function __construct($user_agent=null, HTTP\Transport $transport=null) {
  9. if($user_agent) {
  10. $this->_user_agent = $user_agent;
  11. }
  12. if(!$transport) {
  13. $this->_transport = new HTTP\Curl();
  14. } else {
  15. $this->set_transport($transport);
  16. }
  17. }
  18. public function set_user_agent($ua) {
  19. $this->_user_agent = $ua;
  20. }
  21. public function set_max_redirects($max) {
  22. $this->_max_redirects = $max;
  23. }
  24. public function set_timeout($timeout) {
  25. $this->_timeout = $timeout;
  26. }
  27. public function set_transport(HTTP\Transport $transport) {
  28. $this->_transport = $transport;
  29. }
  30. public function get($url, $headers=[]) {
  31. $this->_transport->set_timeout($this->_timeout);
  32. $this->_transport->set_max_redirects($this->_max_redirects);
  33. if($this->_user_agent) {
  34. $headers[] = 'User-Agent: ' . $this->_user_agent;
  35. }
  36. $response = $this->_transport->get($url, $headers);
  37. $response = $this->_build_response($response);
  38. return $response;
  39. }
  40. public function post($url, $body, $headers=[]) {
  41. $this->_transport->set_timeout($this->_timeout);
  42. $this->_transport->set_max_redirects($this->_max_redirects);
  43. if($this->_user_agent) {
  44. $headers[] = 'User-Agent: ' . $this->_user_agent;
  45. }
  46. $response = $this->_transport->post($url, $body, $headers);
  47. $response = $this->_build_response($response);
  48. return $response;
  49. }
  50. public function head($url, $headers=[]) {
  51. $this->_transport->set_timeout($this->_timeout);
  52. $this->_transport->set_max_redirects($this->_max_redirects);
  53. if($this->_user_agent) {
  54. $headers[] = 'User-Agent: ' . $this->_user_agent;
  55. }
  56. $response = $this->_transport->head($url, $headers);
  57. $response = $this->_build_response($response);
  58. return $response;
  59. }
  60. private function _build_response($response) {
  61. // Parses the HTTP headers and adds the "headers" and "rels" response keys
  62. $response['headers'] = self::_parse_headers($response['header']);
  63. $response['rels'] = \IndieWeb\http_rels($response['header']);
  64. unset($response['header']);
  65. return $response;
  66. }
  67. private static function _parse_headers($headers) {
  68. $retVal = array();
  69. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  70. foreach($fields as $field) {
  71. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  72. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  73. return strtoupper($m[0]);
  74. }, strtolower(trim($match[1])));
  75. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  76. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  77. return strtoupper($m[0]);
  78. }, strtolower(trim($match[1])));
  79. if(isset($retVal[$match[1]])) {
  80. if(!is_array($retVal[$match[1]]))
  81. $retVal[$match[1]] = array($retVal[$match[1]]);
  82. $retVal[$match[1]][] = $match[2];
  83. } else {
  84. $retVal[$match[1]] = trim($match[2]);
  85. }
  86. }
  87. }
  88. return $retVal;
  89. }
  90. }