4 * This module contains the plain non-curl HTTP fetcher
9 * LICENSE: See the COPYING file included in this distribution.
12 * @author JanRain, Inc. <openid@janrain.com>
13 * @copyright 2005-2008 Janrain, Inc.
14 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
20 require_once "Auth/Yadis/HTTPFetcher.php";
23 * This class implements a plain, hand-built socket-based fetcher
24 * which will be used in the event that CURL is unavailable.
28 class Auth_Yadis_PlainHTTPFetcher extends Auth_Yadis_HTTPFetcher {
30 * Does this fetcher support SSL URLs?
32 function supportsSSL()
34 return function_exists('openssl_open');
37 function get($url, $extra_headers = null)
39 if (!$this->canFetchURL($url)) {
45 $stop = time() + $this->timeout;
46 $off = $this->timeout;
48 while ($redir && ($off > 0)) {
50 $parts = parse_url($url);
54 // Set a default port.
55 if (!array_key_exists('port', $parts)) {
56 $specify_port = false;
57 if ($parts['scheme'] == 'http') {
59 } elseif ($parts['scheme'] == 'https') {
66 if (!array_key_exists('path', $parts)) {
70 $host = $parts['host'];
72 if ($parts['scheme'] == 'https') {
73 $host = 'ssl://' . $host;
76 $user_agent = Auth_OpenID_USER_AGENT;
79 "GET ".$parts['path'].
80 (array_key_exists('query', $parts) ?
81 "?".$parts['query'] : "").
83 "User-Agent: $user_agent",
84 "Host: ".$parts['host'].
85 ($specify_port ? ":".$parts['port'] : ""),
86 "Port: ".$parts['port']);
92 foreach ($extra_headers as $h) {
97 @$sock = fsockopen($host, $parts['port'], $errno, $errstr,
99 if ($sock === false) {
103 stream_set_timeout($sock, $this->timeout);
105 fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
109 while (!feof($sock) &&
110 $kilobytes < Auth_OpenID_FETCHER_MAX_RESPONSE_KB ) {
111 $data .= fgets($sock, 1024);
117 // Split response into header and body sections
118 list($headers, $body) = explode("\r\n\r\n", $data, 2);
119 $headers = explode("\r\n", $headers);
121 $http_code = explode(" ", $headers[0]);
122 $code = $http_code[1];
124 if (in_array($code, array('301', '302'))) {
125 $url = $this->_findRedirect($headers);
131 $off = $stop - time();
134 $new_headers = array();
136 foreach ($headers as $header) {
137 if (preg_match("/:/", $header)) {
138 $parts = explode(": ", $header, 2);
140 if (count($parts) == 2) {
141 list($name, $value) = $parts;
142 $new_headers[$name] = $value;
148 return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body);
151 function post($url, $body, $extra_headers = null)
153 if (!$this->canFetchURL($url)) {
157 $parts = parse_url($url);
161 $post_path = $parts['path'];
162 if (isset($parts['query'])) {
163 $post_path .= '?' . $parts['query'];
166 $headers[] = "POST ".$post_path." HTTP/1.0";
167 $headers[] = "Host: " . $parts['host'];
168 $headers[] = "Content-type: application/x-www-form-urlencoded";
169 $headers[] = "Content-length: " . strval(strlen($body));
171 if ($extra_headers &&
172 is_array($extra_headers)) {
173 $headers = array_merge($headers, $extra_headers);
176 // Join all headers together.
177 $all_headers = implode("\r\n", $headers);
179 // Add headers, two newlines, and request body.
180 $request = $all_headers . "\r\n\r\n" . $body;
182 // Set a default port.
183 if (!array_key_exists('port', $parts)) {
184 if ($parts['scheme'] == 'http') {
186 } elseif ($parts['scheme'] == 'https') {
187 $parts['port'] = 443;
193 if ($parts['scheme'] == 'https') {
194 $parts['host'] = sprintf("ssl://%s", $parts['host']);
197 // Connect to the remote server.
201 $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr,
204 if ($sock === false) {
208 stream_set_timeout($sock, $this->timeout);
210 // Write the POST request.
211 fputs($sock, $request);
213 // Get the response from the server.
215 while (!feof($sock)) {
216 if ($data = fgets($sock, 128)) {
223 // Split the request into headers and body.
224 list($headers, $response_body) = explode("\r\n\r\n", $response, 2);
226 $headers = explode("\r\n", $headers);
228 // Expect the first line of the headers data to be something
229 // like HTTP/1.1 200 OK. Split the line on spaces and take
230 // the second token, which should be the return code.
231 $http_code = explode(" ", $headers[0]);
232 $code = $http_code[1];
234 $new_headers = array();
236 foreach ($headers as $header) {
237 if (preg_match("/:/", $header)) {
238 list($name, $value) = explode(": ", $header, 2);
239 $new_headers[$name] = $value;
244 return new Auth_Yadis_HTTPResponse($url, $code,
245 $new_headers, $response_body);