• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/openid/Services/Yadis/PlainHTTPFetcher.php

Go to the documentation of this file.
00001 <?php
00002 
00020 require_once "Services/Yadis/HTTPFetcher.php";
00021 
00028 class Services_Yadis_PlainHTTPFetcher extends Services_Yadis_HTTPFetcher {
00029     function Services_Yadis_PlainHTTPFetcher($timeout)
00030     {
00031         $this->timeout = $timeout;
00032     }
00033 
00047     function get($url, $extra_headers = null)
00048     {
00049         if (!$this->allowedURL($url)) {
00050             trigger_error("Bad URL scheme in url: " . $url,
00051                           E_USER_WARNING);
00052             return null;
00053         }
00054 
00055         $redir = true;
00056 
00057         $stop = time() + $this->timeout;
00058         $off = $this->timeout;
00059 
00060         while ($redir && ($off > 0)) {
00061 
00062             $parts = parse_url($url);
00063 
00064             $default_port = false;
00065 
00066             // Set a default port.
00067             if (!array_key_exists('port', $parts)) {
00068                 $default_port = true;
00069                 if ($parts['scheme'] == 'http') {
00070                     $parts['port'] = 80;
00071                 } elseif ($parts['scheme'] == 'https') {
00072                     $parts['port'] = 443;
00073                 } else {
00074                     trigger_error("fetcher post method doesn't support " .
00075                                   " scheme '" . $parts['scheme'] .
00076                                   "', no default port available",
00077                                   E_USER_WARNING);
00078                     return null;
00079                 }
00080             }
00081 
00082             $host = $parts['host'];
00083 
00084             if ($parts['scheme'] == 'https') {
00085                 $host = 'ssl://' . $host;
00086             }
00087 
00088             $user_agent = "PHP Yadis Library Fetcher";
00089 
00090             $headers = array(
00091                              "GET ".$parts['path'].
00092                              (array_key_exists('query', $parts) ?
00093                               "?".$parts['query'] : "").
00094                                   " HTTP/1.1",
00095                              "User-Agent: $user_agent",
00096                              "Host: ".$parts['host'].(!$default_port ?
00097                                                       ":".$parts['port'] : ""),
00098                              "Port: ".$parts['port'],
00099                              "Cache-Control: no-cache",
00100                              "Connection: close");
00101 
00102             if ($extra_headers) {
00103                 foreach ($extra_headers as $h) {
00104                     $headers[] = $h;
00105                 }
00106             }
00107 
00108             $errno = 0;
00109             $errstr = '';
00110 
00111             $sock = fsockopen($host, $parts['port'], $errno, $errstr,
00112                               $this->timeout);
00113             if ($sock === false) {
00114                 return false;
00115             }
00116 
00117             stream_set_timeout($sock, $this->timeout);
00118 
00119             fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
00120 
00121             $data = fgets($sock);
00122             while (!feof($sock)) {
00123                 $chunk = fgets($sock, 1024);
00124                 $data .= $chunk;
00125             }
00126 
00127             fclose($sock);
00128 
00129             // Split response into header and body sections
00130             list($headers, $body) = explode("\r\n\r\n", $data, 2);
00131             $headers = explode("\r\n", $headers);
00132 
00133             $http_code = explode(" ", $headers[0]);
00134             $code = $http_code[1];
00135 
00136             if (in_array($code, array('301', '302'))) {
00137                 $url = $this->_findRedirect($headers);
00138                 $redir = true;
00139             } else {
00140                 $redir = false;
00141             }
00142 
00143             $off = $stop - time();
00144         }
00145 
00146         $new_headers = array();
00147 
00148         foreach ($headers as $header) {
00149             if (preg_match("/:/", $header)) {
00150                 list($name, $value) = explode(": ", $header, 2);
00151                 $new_headers[$name] = $value;
00152             }
00153         }
00154 
00155         return new Services_Yadis_HTTPResponse($url, $code,
00156                                                $new_headers, $body);
00157     }
00158 }
00159 
00160 ?>

Generated on Sun Jan 2 2011 04:55:32 for Pragyan CMS by  doxygen 1.7.1