4 * This module contains the HTTP fetcher interface
8 * LICENSE: See the COPYING file included in this distribution.
11 * @author JanRain, Inc. <openid@janrain.com>
12 * @copyright 2005-2008 Janrain, Inc.
13 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
17 * Require logging functionality
19 require_once "Auth/OpenID.php";
21 define('Auth_OpenID_FETCHER_MAX_RESPONSE_KB', 1024);
22 define('Auth_OpenID_USER_AGENT',
23 'php-openid/'.Auth_OpenID_VERSION.' (php/'.phpversion().')');
25 class Auth_Yadis_HTTPResponse {
26 function Auth_Yadis_HTTPResponse($final_url = null, $status = null,
27 $headers = null, $body = null)
29 $this->final_url = $final_url;
30 $this->status = $status;
31 $this->headers = $headers;
37 * This class is the interface for HTTP fetchers the Yadis library
38 * uses. This interface is only important if you need to write a new
39 * fetcher for some reason.
44 class Auth_Yadis_HTTPFetcher {
46 var $timeout = 20; // timeout in seconds.
49 * Return whether a URL can be fetched. Returns false if the URL
50 * scheme is not allowed or is not supported by this fetcher
51 * implementation; returns true otherwise.
55 function canFetchURL($url)
57 if ($this->isHTTPS($url) && !$this->supportsSSL()) {
58 Auth_OpenID::log("HTTPS URL unsupported fetching %s",
63 if (!$this->allowedURL($url)) {
64 Auth_OpenID::log("URL fetching not allowed for '%s'",
73 * Return whether a URL should be allowed. Override this method to
74 * conform to your local policy.
76 * By default, will attempt to fetch any http or https URL.
78 function allowedURL($url)
80 return $this->URLHasAllowedScheme($url);
84 * Does this fetcher implementation (and runtime) support fetching
85 * HTTPS URLs? May inspect the runtime environment.
87 * @return bool $support True if this fetcher supports HTTPS
88 * fetching; false if not.
90 function supportsSSL()
92 trigger_error("not implemented", E_USER_ERROR);
96 * Is this an https URL?
100 function isHTTPS($url)
102 return (bool)preg_match('/^https:\/\//i', $url);
106 * Is this an http or https URL?
110 function URLHasAllowedScheme($url)
112 return (bool)preg_match('/^https?:\/\//i', $url);
118 function _findRedirect($headers, $url)
120 foreach ($headers as $line) {
121 if (strpos(strtolower($line), "location: ") === 0) {
122 $parts = explode(" ", $line, 2);
124 $ppos = strpos($loc, "://");
125 if ($ppos === false || $ppos > strpos($loc, "/")) {
126 /* no host; add it */
127 $hpos = strpos($url, "://");
128 $prt = substr($url, 0, $hpos+3);
129 $url = substr($url, $hpos+3);
130 if (substr($loc, 0, 1) == "/") {
132 $fspos = strpos($url, "/");
133 if ($fspos) $loc = $prt.substr($url, 0, $fspos).$loc;
134 else $loc = $prt.$url.$loc;
139 $xpos = strpos($url, "/");
140 if ($xpos === false) break;
141 $apos = strpos($url, "?");
142 if ($apos !== false && $apos < $xpos) break;
143 $apos = strpos($url, "&");
144 if ($apos !== false && $apos < $xpos) break;
145 $pp .= substr($url, 0, $xpos+1);
146 $url = substr($url, $xpos+1);
158 * Fetches the specified URL using optional extra headers and
159 * returns the server's response.
161 * @param string $url The URL to be fetched.
162 * @param array $extra_headers An array of header strings
163 * (e.g. "Accept: text/html").
164 * @return mixed $result An array of ($code, $url, $headers,
165 * $body) if the URL could be fetched; null if the URL does not
166 * pass the URLHasAllowedScheme check or if the server's response
169 function get($url, $headers = null)
171 trigger_error("not implemented", E_USER_ERROR);