4 * This module contains the CURL-based HTTP fetcher implementation.
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
19 require_once "Auth/Yadis/HTTPFetcher.php";
21 require_once "Auth/OpenID.php";
24 * A paranoid {@link Auth_Yadis_HTTPFetcher} class which uses CURL
29 class Auth_Yadis_ParanoidHTTPFetcher extends Auth_Yadis_HTTPFetcher {
30 function Auth_Yadis_ParanoidHTTPFetcher()
37 $this->headers = array();
44 function _writeHeader($ch, $header)
46 array_push($this->headers, rtrim($header));
47 return strlen($header);
53 function _writeData($ch, $data)
55 if (strlen($this->data) > 1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB) {
64 * Does this fetcher support SSL URLs?
66 function supportsSSL()
70 return in_array('https', $v['protocols']);
71 } elseif (is_string($v)) {
72 return preg_match('/OpenSSL/i', $v);
78 function get($url, $extra_headers = null)
80 if (!$this->canFetchURL($url)) {
84 $stop = time() + $this->timeout;
85 $off = $this->timeout;
89 while ($redir && ($off > 0)) {
96 "curl_init returned false; could not " .
97 "initialize for URL '%s'", $url);
101 if (defined('CURLOPT_NOSIGNAL')) {
102 curl_setopt($c, CURLOPT_NOSIGNAL, true);
105 if (!$this->allowedURL($url)) {
106 Auth_OpenID::log("Fetching URL not allowed: %s",
111 curl_setopt($c, CURLOPT_WRITEFUNCTION,
112 array($this, "_writeData"));
113 curl_setopt($c, CURLOPT_HEADERFUNCTION,
114 array($this, "_writeHeader"));
116 if ($extra_headers) {
117 curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
120 $cv = curl_version();
122 $curl_user_agent = 'curl/'.$cv['version'];
124 $curl_user_agent = $cv;
126 curl_setopt($c, CURLOPT_USERAGENT,
127 Auth_OpenID_USER_AGENT.' '.$curl_user_agent);
128 curl_setopt($c, CURLOPT_TIMEOUT, $off);
129 curl_setopt($c, CURLOPT_URL, $url);
131 if (defined('Auth_OpenID_VERIFY_HOST')) {
132 // set SSL verification options only if Auth_OpenID_VERIFY_HOST
133 // is explicitly set, otherwise use system default.
134 if (Auth_OpenID_VERIFY_HOST) {
135 curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
136 curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
137 if (defined('Auth_OpenID_CAINFO')) {
138 curl_setopt($c, CURLOPT_CAINFO, Auth_OpenID_CAINFO);
141 curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
144 if (defined('Auth_OpenID_HTTP_PROXY')) {
145 curl_setopt($c, CURLOPT_PROXY, Auth_OpenID_HTTP_PROXY);
149 $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
151 $headers = $this->headers;
154 Auth_OpenID::log("Got no response code when fetching %s", $url);
155 Auth_OpenID::log("CURL error (%s): %s",
156 curl_errno($c), curl_error($c));
160 if (in_array($code, array(301, 302, 303, 307))) {
161 $url = $this->_findRedirect($headers, $url);
167 if (defined('Auth_OpenID_VERIFY_HOST') &&
168 Auth_OpenID_VERIFY_HOST == true &&
169 $this->isHTTPS($url)) {
170 Auth_OpenID::log('OpenID: Verified SSL host %s using '.
173 $new_headers = array();
175 foreach ($headers as $header) {
176 if (strpos($header, ': ')) {
177 list($name, $value) = explode(': ', $header, 2);
178 $new_headers[$name] = $value;
182 return new Auth_Yadis_HTTPResponse($url, $code,
183 $new_headers, $body);
186 $off = $stop - time();
192 function post($url, $body, $extra_headers = null)
194 if (!$this->canFetchURL($url)) {
202 if (defined('CURLOPT_NOSIGNAL')) {
203 curl_setopt($c, CURLOPT_NOSIGNAL, true);
206 if (defined('Auth_OpenID_HTTP_PROXY')) {
207 curl_setopt($c, CURLOPT_PROXY, Auth_OpenID_HTTP_PROXY);
210 curl_setopt($c, CURLOPT_POST, true);
211 curl_setopt($c, CURLOPT_POSTFIELDS, $body);
212 curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
213 curl_setopt($c, CURLOPT_URL, $url);
214 curl_setopt($c, CURLOPT_WRITEFUNCTION,
215 array($this, "_writeData"));
217 if (defined('Auth_OpenID_VERIFY_HOST')) {
218 // set SSL verification options only if Auth_OpenID_VERIFY_HOST
219 // is explicitly set, otherwise use system default.
220 if (Auth_OpenID_VERIFY_HOST) {
221 curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
222 curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
223 if (defined('Auth_OpenID_CAINFO')) {
224 curl_setopt($c, CURLOPT_CAINFO, Auth_OpenID_CAINFO);
227 curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
233 $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
236 Auth_OpenID::log("Got no response code when fetching %s", $url);
237 Auth_OpenID::log("CURL error (%s): %s",
238 curl_errno($c), curl_error($c));
242 if (defined('Auth_OpenID_VERIFY_HOST') &&
243 Auth_OpenID_VERIFY_HOST == true &&
244 $this->isHTTPS($url)) {
245 Auth_OpenID::log('OpenID: Verified SSL host %s using '.
252 $new_headers = $extra_headers;
254 foreach ($this->headers as $header) {
255 if (strpos($header, ': ')) {
256 list($name, $value) = explode(': ', $header, 2);
257 $new_headers[$name] = $value;
262 return new Auth_Yadis_HTTPResponse($url, $code,
263 $new_headers, $body);