PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/my-plugins/openid-plus/Services/Yadis/PlainHTTPFetcher.php

https://github.com/clioweb/DH-Answers
PHP | 160 lines | 96 code | 27 blank | 37 comment | 14 complexity | d3f1e1b1e5cc8520a663a61957f17e4a MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * This module contains the plain non-curl HTTP fetcher
  4. * implementation.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: See the COPYING file included in this distribution.
  9. *
  10. * @package Yadis
  11. * @author JanRain, Inc. <openid@janrain.com>
  12. * @copyright 2005 Janrain, Inc.
  13. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  14. */
  15. /**
  16. * Interface import; need to inherit from the base HTTP fetcher class.
  17. */
  18. require_once("HTTPFetcher.php");
  19. /**
  20. * This class implements a plain, hand-built socket-based fetcher
  21. * which will be used in the event that CURL is unavailable.
  22. *
  23. * @package Yadis
  24. */
  25. class Services_Yadis_PlainHTTPFetcher extends Services_Yadis_HTTPFetcher {
  26. function Services_Yadis_PlainHTTPFetcher($timeout)
  27. {
  28. $this->timeout = $timeout;
  29. }
  30. /**
  31. * Fetches the specified URL using optional extra headers and
  32. * returns the server's response. Uses plain PHP library calls
  33. * and doesn't rely on any extensions.
  34. *
  35. * @param string $url The URL to be fetched.
  36. * @param array $extra_headers An array of header strings
  37. * (e.g. "Accept: text/html").
  38. * @return mixed $result An array of ($code, $url, $headers,
  39. * $body) if the URL could be fetched; null if the URL does not
  40. * pass the URLHasAllowedScheme check or if the server's response
  41. * is malformed.
  42. */
  43. function get($url, $extra_headers = null)
  44. {
  45. if (!$this->allowedURL($url)) {
  46. trigger_error("Bad URL scheme in url: " . $url,
  47. E_USER_WARNING);
  48. return null;
  49. }
  50. $redir = true;
  51. $stop = time() + $this->timeout;
  52. $off = $this->timeout;
  53. while ($redir && ($off > 0)) {
  54. $parts = parse_url($url);
  55. $default_port = false;
  56. // Set a default port.
  57. if (!array_key_exists('port', $parts)) {
  58. $default_port = true;
  59. if ($parts['scheme'] == 'http') {
  60. $parts['port'] = 80;
  61. } elseif ($parts['scheme'] == 'https') {
  62. $parts['port'] = 443;
  63. } else {
  64. trigger_error("fetcher post method doesn't support " .
  65. " scheme '" . $parts['scheme'] .
  66. "', no default port available",
  67. E_USER_WARNING);
  68. return null;
  69. }
  70. }
  71. $host = $parts['host'];
  72. if ($parts['scheme'] == 'https') {
  73. $host = 'ssl://' . $host;
  74. }
  75. $user_agent = "PHP Yadis Library Fetcher";
  76. $headers = array(
  77. "GET ".$parts['path'].
  78. (array_key_exists('query', $parts) ?
  79. "?".$parts['query'] : "").
  80. " HTTP/1.1",
  81. "User-Agent: $user_agent",
  82. "Host: ".$parts['host'].(!$default_port ?
  83. ":".$parts['port'] : ""),
  84. "Port: ".$parts['port'],
  85. "Cache-Control: no-cache",
  86. "Connection: close");
  87. if ($extra_headers) {
  88. foreach ($extra_headers as $h) {
  89. $headers[] = $h;
  90. }
  91. }
  92. $errno = 0;
  93. $errstr = '';
  94. $sock = fsockopen($host, $parts['port'], $errno, $errstr,
  95. $this->timeout);
  96. if ($sock === false) {
  97. return false;
  98. }
  99. stream_set_timeout($sock, $this->timeout);
  100. fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
  101. $data = fgets($sock);
  102. while (!feof($sock)) {
  103. $chunk = fgets($sock, 1024);
  104. $data .= $chunk;
  105. }
  106. fclose($sock);
  107. // Split response into header and body sections
  108. list($headers, $body) = explode("\r\n\r\n", $data, 2);
  109. $headers = explode("\r\n", $headers);
  110. $http_code = explode(" ", $headers[0]);
  111. $code = $http_code[1];
  112. if (in_array($code, array('301', '302'))) {
  113. $url = $this->_findRedirect($headers);
  114. $redir = true;
  115. } else {
  116. $redir = false;
  117. }
  118. $off = $stop - time();
  119. }
  120. $new_headers = array();
  121. foreach ($headers as $header) {
  122. if (preg_match("/:/", $header)) {
  123. list($name, $value) = explode(": ", $header, 2);
  124. $new_headers[$name] = $value;
  125. }
  126. }
  127. return new Services_Yadis_HTTPResponse($url, $code,
  128. $new_headers, $body);
  129. }
  130. }
  131. ?>