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'] : ""),
87 (1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB),
88 "Port: ".$parts['port']);
94 foreach ($extra_headers as $h) {
99 @$sock = fsockopen($host, $parts['port'], $errno, $errstr,
101 if ($sock === false) {
105 stream_set_timeout($sock, $this->timeout);
107 fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
111 while (!feof($sock) &&
112 $kilobytes < Auth_OpenID_FETCHER_MAX_RESPONSE_KB ) {
113 $data .= fgets($sock, 1024);
119 // Split response into header and body sections
120 list($headers, $body) = explode("\r\n\r\n", $data, 2);
121 $headers = explode("\r\n", $headers);
123 $http_code = explode(" ", $headers[0]);
124 $code = $http_code[1];
126 if (in_array($code, array('301', '302'))) {
127 $url = $this->_findRedirect($headers);
133 $off = $stop - time();
136 $new_headers = array();
138 foreach ($headers as $header) {
139 if (preg_match("/:/", $header)) {
140 $parts = explode(": ", $header, 2);
142 if (count($parts) == 2) {
143 list($name, $value) = $parts;
144 $new_headers[$name] = $value;
150 return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body);
153 function post($url, $body, $extra_headers = null)
155 if (!$this->canFetchURL($url)) {
159 $parts = parse_url($url);
163 $post_path = $parts['path'];
164 if (isset($parts['query'])) {
165 $post_path .= '?' . $parts['query'];
168 $headers[] = "POST ".$post_path." HTTP/1.0";
169 $headers[] = "Host: " . $parts['host'];
170 $headers[] = "Content-type: application/x-www-form-urlencoded";
171 $headers[] = "Content-length: " . strval(strlen($body));
173 if ($extra_headers &&
174 is_array($extra_headers)) {
175 $headers = array_merge($headers, $extra_headers);
178 // Join all headers together.
179 $all_headers = implode("\r\n", $headers);
181 // Add headers, two newlines, and request body.
182 $request = $all_headers . "\r\n\r\n" . $body;
184 // Set a default port.
185 if (!array_key_exists('port', $parts)) {
186 if ($parts['scheme'] == 'http') {
188 } elseif ($parts['scheme'] == 'https') {
189 $parts['port'] = 443;
195 if ($parts['scheme'] == 'https') {
196 $parts['host'] = sprintf("ssl://%s", $parts['host']);
199 // Connect to the remote server.
203 $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr,
206 if ($sock === false) {
210 stream_set_timeout($sock, $this->timeout);
212 // Write the POST request.
213 fputs($sock, $request);
215 // Get the response from the server.
217 while (!feof($sock)) {
218 if ($data = fgets($sock, 128)) {
225 // Split the request into headers and body.
226 list($headers, $response_body) = explode("\r\n\r\n", $response, 2);
228 $headers = explode("\r\n", $headers);
230 // Expect the first line of the headers data to be something
231 // like HTTP/1.1 200 OK. Split the line on spaces and take
232 // the second token, which should be the return code.
233 $http_code = explode(" ", $headers[0]);
234 $code = $http_code[1];
236 $new_headers = array();
238 foreach ($headers as $header) {
239 if (preg_match("/:/", $header)) {
240 list($name, $value) = explode(": ", $header, 2);
241 $new_headers[$name] = $value;
246 return new Auth_Yadis_HTTPResponse($url, $code,
247 $new_headers, $response_body);