[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/HTTP/ -> HTTPClient.php (source)

   1  <?php
   2  
   3  namespace dokuwiki\HTTP;
   4  
   5  define('HTTP_NL',"\r\n");
   6  
   7  
   8  /**
   9   * This class implements a basic HTTP client
  10   *
  11   * It supports POST and GET, Proxy usage, basic authentication,
  12   * handles cookies and referers. It is based upon the httpclient
  13   * function from the VideoDB project.
  14   *
  15   * @link   http://www.splitbrain.org/go/videodb
  16   * @author Andreas Goetz <cpuidle@gmx.de>
  17   * @author Andreas Gohr <andi@splitbrain.org>
  18   * @author Tobias Sarnowski <sarnowski@new-thoughts.org>
  19   */
  20  class HTTPClient {
  21      //set these if you like
  22      public $agent;         // User agent
  23      public $http;          // HTTP version defaults to 1.0
  24      public $timeout;       // read timeout (seconds)
  25      public $cookies;
  26      public $referer;
  27      public $max_redirect;
  28      public $max_bodysize;
  29      public $max_bodysize_abort = true;  // if set, abort if the response body is bigger than max_bodysize
  30      public $header_regexp; // if set this RE must match against the headers, else abort
  31      public $headers;
  32      public $debug;
  33      public $start = 0.0; // for timings
  34      public $keep_alive = true; // keep alive rocks
  35  
  36      // don't set these, read on error
  37      public $error;
  38      public $redirect_count;
  39  
  40      // read these after a successful request
  41      public $status;
  42      public $resp_body;
  43      public $resp_headers;
  44  
  45      // set these to do basic authentication
  46      public $user;
  47      public $pass;
  48  
  49      // set these if you need to use a proxy
  50      public $proxy_host;
  51      public $proxy_port;
  52      public $proxy_user;
  53      public $proxy_pass;
  54      public $proxy_ssl; //boolean set to true if your proxy needs SSL
  55      public $proxy_except; // regexp of URLs to exclude from proxy
  56  
  57      // list of kept alive connections
  58      protected static $connections = array();
  59  
  60      // what we use as boundary on multipart/form-data posts
  61      protected $boundary = '---DokuWikiHTTPClient--4523452351';
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @author Andreas Gohr <andi@splitbrain.org>
  67       */
  68      public function __construct(){
  69          $this->agent        = 'Mozilla/4.0 (compatible; DokuWiki HTTP Client; '.PHP_OS.')';
  70          $this->timeout      = 15;
  71          $this->cookies      = array();
  72          $this->referer      = '';
  73          $this->max_redirect = 3;
  74          $this->redirect_count = 0;
  75          $this->status       = 0;
  76          $this->headers      = array();
  77          $this->http         = '1.0';
  78          $this->debug        = false;
  79          $this->max_bodysize = 0;
  80          $this->header_regexp= '';
  81          if(extension_loaded('zlib')) $this->headers['Accept-encoding'] = 'gzip';
  82          $this->headers['Accept'] = 'text/xml,application/xml,application/xhtml+xml,'.
  83              'text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
  84          $this->headers['Accept-Language'] = 'en-us';
  85      }
  86  
  87  
  88      /**
  89       * Simple function to do a GET request
  90       *
  91       * Returns the wanted page or false on an error;
  92       *
  93       * @param  string $url       The URL to fetch
  94       * @param  bool   $sloppy304 Return body on 304 not modified
  95       * @return false|string  response body, false on error
  96       *
  97       * @author Andreas Gohr <andi@splitbrain.org>
  98       */
  99      public function get($url,$sloppy304=false){
 100          if(!$this->sendRequest($url)) return false;
 101          if($this->status == 304 && $sloppy304) return $this->resp_body;
 102          if($this->status < 200 || $this->status > 206) return false;
 103          return $this->resp_body;
 104      }
 105  
 106      /**
 107       * Simple function to do a GET request with given parameters
 108       *
 109       * Returns the wanted page or false on an error.
 110       *
 111       * This is a convenience wrapper around get(). The given parameters
 112       * will be correctly encoded and added to the given base URL.
 113       *
 114       * @param  string $url       The URL to fetch
 115       * @param  array  $data      Associative array of parameters
 116       * @param  bool   $sloppy304 Return body on 304 not modified
 117       * @return false|string  response body, false on error
 118       *
 119       * @author Andreas Gohr <andi@splitbrain.org>
 120       */
 121      public function dget($url,$data,$sloppy304=false){
 122          if(strpos($url,'?')){
 123              $url .= '&';
 124          }else{
 125              $url .= '?';
 126          }
 127          $url .= $this->postEncode($data);
 128          return $this->get($url,$sloppy304);
 129      }
 130  
 131      /**
 132       * Simple function to do a POST request
 133       *
 134       * Returns the resulting page or false on an error;
 135       *
 136       * @param  string $url       The URL to fetch
 137       * @param  array  $data      Associative array of parameters
 138       * @return false|string  response body, false on error
 139       * @author Andreas Gohr <andi@splitbrain.org>
 140       */
 141      public function post($url,$data){
 142          if(!$this->sendRequest($url,$data,'POST')) return false;
 143          if($this->status < 200 || $this->status > 206) return false;
 144          return $this->resp_body;
 145      }
 146  
 147      /**
 148       * Send an HTTP request
 149       *
 150       * This method handles the whole HTTP communication. It respects set proxy settings,
 151       * builds the request headers, follows redirects and parses the response.
 152       *
 153       * Post data should be passed as associative array. When passed as string it will be
 154       * sent as is. You will need to setup your own Content-Type header then.
 155       *
 156       * @param  string $url    - the complete URL
 157       * @param  mixed  $data   - the post data either as array or raw data
 158       * @param  string $method - HTTP Method usually GET or POST.
 159       * @return bool - true on success
 160       *
 161       * @author Andreas Goetz <cpuidle@gmx.de>
 162       * @author Andreas Gohr <andi@splitbrain.org>
 163       */
 164      public function sendRequest($url,$data='',$method='GET'){
 165          $this->start  = $this->time();
 166          $this->error  = '';
 167          $this->status = 0;
 168          $this->resp_body = '';
 169          $this->resp_headers = array();
 170  
 171          // save unencoded data for recursive call
 172          $unencodedData = $data;
 173  
 174          // don't accept gzip if truncated bodies might occur
 175          if($this->max_bodysize &&
 176              !$this->max_bodysize_abort &&
 177              $this->headers['Accept-encoding'] == 'gzip'){
 178              unset($this->headers['Accept-encoding']);
 179          }
 180  
 181          // parse URL into bits
 182          $uri = parse_url($url);
 183          $server = $uri['host'];
 184          $path   = !empty($uri['path']) ? $uri['path'] : '/';
 185          $uriPort = !empty($uri['port']) ? $uri['port'] : null;
 186          if(!empty($uri['query'])) $path .= '?'.$uri['query'];
 187          if(isset($uri['user'])) $this->user = $uri['user'];
 188          if(isset($uri['pass'])) $this->pass = $uri['pass'];
 189  
 190          // proxy setup
 191          if($this->useProxyForUrl($url)){
 192              $request_url = $url;
 193              $server      = $this->proxy_host;
 194              $port        = $this->proxy_port;
 195              if (empty($port)) $port = 8080;
 196              $use_tls     = $this->proxy_ssl;
 197          }else{
 198              $request_url = $path;
 199              $port = $uriPort ?: ($uri['scheme'] == 'https' ? 443 : 80);
 200              $use_tls     = ($uri['scheme'] == 'https');
 201          }
 202  
 203          // add SSL stream prefix if needed - needs SSL support in PHP
 204          if($use_tls) {
 205              if(!in_array('ssl', stream_get_transports())) {
 206                  $this->status = -200;
 207                  $this->error = 'This PHP version does not support SSL - cannot connect to server';
 208              }
 209              $server = 'ssl://'.$server;
 210          }
 211  
 212          // prepare headers
 213          $headers               = $this->headers;
 214          $headers['Host']       = $uri['host']
 215              . ($uriPort ? ':' . $uriPort : '');
 216          $headers['User-Agent'] = $this->agent;
 217          $headers['Referer']    = $this->referer;
 218  
 219          if($method == 'POST'){
 220              if(is_array($data)){
 221                  if (empty($headers['Content-Type'])) {
 222                      $headers['Content-Type'] = null;
 223                  }
 224                  switch ($headers['Content-Type']) {
 225                      case 'multipart/form-data':
 226                          $headers['Content-Type']   = 'multipart/form-data; boundary=' . $this->boundary;
 227                          $data = $this->postMultipartEncode($data);
 228                          break;
 229                      default:
 230                          $headers['Content-Type']   = 'application/x-www-form-urlencoded';
 231                          $data = $this->postEncode($data);
 232                  }
 233              }
 234          }elseif($method == 'GET'){
 235              $data = ''; //no data allowed on GET requests
 236          }
 237  
 238          $contentlength = strlen($data);
 239          if($contentlength)  {
 240              $headers['Content-Length'] = $contentlength;
 241          }
 242  
 243          if($this->user) {
 244              $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->pass);
 245          }
 246          if($this->proxy_user) {
 247              $headers['Proxy-Authorization'] = 'Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass);
 248          }
 249  
 250          // already connected?
 251          $connectionId = $this->uniqueConnectionId($server,$port);
 252          $this->debug('connection pool', self::$connections);
 253          $socket = null;
 254          if (isset(self::$connections[$connectionId])) {
 255              $this->debug('reusing connection', $connectionId);
 256              $socket = self::$connections[$connectionId];
 257          }
 258          if (is_null($socket) || feof($socket)) {
 259              $this->debug('opening connection', $connectionId);
 260              // open socket
 261              $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);
 262              if (!$socket){
 263                  $this->status = -100;
 264                  $this->error = "Could not connect to $server:$port\n$errstr ($errno)";
 265                  return false;
 266              }
 267  
 268              // try establish a CONNECT tunnel for SSL
 269              try {
 270                  if($this->ssltunnel($socket, $request_url)){
 271                      // no keep alive for tunnels
 272                      $this->keep_alive = false;
 273                      // tunnel is authed already
 274                      if(isset($headers['Proxy-Authentication'])) unset($headers['Proxy-Authentication']);
 275                  }
 276              } catch (HTTPClientException $e) {
 277                  $this->status = $e->getCode();
 278                  $this->error = $e->getMessage();
 279                  fclose($socket);
 280                  return false;
 281              }
 282  
 283              // keep alive?
 284              if ($this->keep_alive) {
 285                  self::$connections[$connectionId] = $socket;
 286              } else {
 287                  unset(self::$connections[$connectionId]);
 288              }
 289          }
 290  
 291          if ($this->keep_alive && !$this->useProxyForUrl($request_url)) {
 292              // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive
 293              // connection token to a proxy server. We still do keep the connection the
 294              // proxy alive (well except for CONNECT tunnels)
 295              $headers['Connection'] = 'Keep-Alive';
 296          } else {
 297              $headers['Connection'] = 'Close';
 298          }
 299  
 300          try {
 301              //set non-blocking
 302              stream_set_blocking($socket, 0);
 303  
 304              // build request
 305              $request  = "$method $request_url HTTP/".$this->http.HTTP_NL;
 306              $request .= $this->buildHeaders($headers);
 307              $request .= $this->getCookies();
 308              $request .= HTTP_NL;
 309              $request .= $data;
 310  
 311              $this->debug('request',$request);
 312              $this->sendData($socket, $request, 'request');
 313  
 314              // read headers from socket
 315              $r_headers = '';
 316              do{
 317                  $r_line = $this->readLine($socket, 'headers');
 318                  $r_headers .= $r_line;
 319              }while($r_line != "\r\n" && $r_line != "\n");
 320  
 321              $this->debug('response headers',$r_headers);
 322  
 323              // check if expected body size exceeds allowance
 324              if($this->max_bodysize && preg_match('/\r?\nContent-Length:\s*(\d+)\r?\n/i',$r_headers,$match)){
 325                  if($match[1] > $this->max_bodysize){
 326                      if ($this->max_bodysize_abort)
 327                          throw new HTTPClientException('Reported content length exceeds allowed response size');
 328                      else
 329                          $this->error = 'Reported content length exceeds allowed response size';
 330                  }
 331              }
 332  
 333              // get Status
 334              if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/s', $r_headers, $m))
 335                  throw new HTTPClientException('Server returned bad answer '.$r_headers);
 336  
 337              $this->status = $m[2];
 338  
 339              // handle headers and cookies
 340              $this->resp_headers = $this->parseHeaders($r_headers);
 341              if(isset($this->resp_headers['set-cookie'])){
 342                  foreach ((array) $this->resp_headers['set-cookie'] as $cookie){
 343                      list($cookie)   = explode(';',$cookie,2);
 344                      list($key,$val) = explode('=',$cookie,2);
 345                      $key = trim($key);
 346                      if($val == 'deleted'){
 347                          if(isset($this->cookies[$key])){
 348                              unset($this->cookies[$key]);
 349                          }
 350                      }elseif($key){
 351                          $this->cookies[$key] = $val;
 352                      }
 353                  }
 354              }
 355  
 356              $this->debug('Object headers',$this->resp_headers);
 357  
 358              // check server status code to follow redirect
 359              if(in_array($this->status, [301, 302, 303, 307, 308])){
 360                  if (empty($this->resp_headers['location'])){
 361                      throw new HTTPClientException('Redirect but no Location Header found');
 362                  }elseif($this->redirect_count == $this->max_redirect){
 363                      throw new HTTPClientException('Maximum number of redirects exceeded');
 364                  }else{
 365                      // close the connection because we don't handle content retrieval here
 366                      // that's the easiest way to clean up the connection
 367                      fclose($socket);
 368                      unset(self::$connections[$connectionId]);
 369  
 370                      $this->redirect_count++;
 371                      $this->referer = $url;
 372                      // handle non-RFC-compliant relative redirects
 373                      if (!preg_match('/^http/i', $this->resp_headers['location'])){
 374                          if($this->resp_headers['location'][0] != '/'){
 375                              $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uriPort.
 376                                  dirname($path).'/'.$this->resp_headers['location'];
 377                          }else{
 378                              $this->resp_headers['location'] = $uri['scheme'].'://'.$uri['host'].':'.$uriPort.
 379                                  $this->resp_headers['location'];
 380                          }
 381                      }
 382                      if($this->status == 307 || $this->status == 308) {
 383                          // perform redirected request, same method as before (required by RFC)
 384                          return $this->sendRequest($this->resp_headers['location'],$unencodedData,$method);
 385                      }else{
 386                          // perform redirected request, always via GET (required by RFC)
 387                          return $this->sendRequest($this->resp_headers['location'],array(),'GET');
 388                      }
 389                  }
 390              }
 391  
 392              // check if headers are as expected
 393              if($this->header_regexp && !preg_match($this->header_regexp,$r_headers))
 394                  throw new HTTPClientException('The received headers did not match the given regexp');
 395  
 396              //read body (with chunked encoding if needed)
 397              $r_body    = '';
 398              if(
 399                  (
 400                      isset($this->resp_headers['transfer-encoding']) &&
 401                      $this->resp_headers['transfer-encoding'] == 'chunked'
 402                  ) || (
 403                      isset($this->resp_headers['transfer-coding']) &&
 404                      $this->resp_headers['transfer-coding'] == 'chunked'
 405                  )
 406              ) {
 407                  $abort = false;
 408                  do {
 409                      $chunk_size = '';
 410                      while (preg_match('/^[a-zA-Z0-9]?$/',$byte=$this->readData($socket,1,'chunk'))){
 411                          // read chunksize until \r
 412                          $chunk_size .= $byte;
 413                          if (strlen($chunk_size) > 128) // set an abritrary limit on the size of chunks
 414                              throw new HTTPClientException('Allowed response size exceeded');
 415                      }
 416                      $this->readLine($socket, 'chunk');     // readtrailing \n
 417                      $chunk_size = hexdec($chunk_size);
 418  
 419                      if($this->max_bodysize && $chunk_size+strlen($r_body) > $this->max_bodysize){
 420                          if ($this->max_bodysize_abort)
 421                              throw new HTTPClientException('Allowed response size exceeded');
 422                          $this->error = 'Allowed response size exceeded';
 423                          $chunk_size = $this->max_bodysize - strlen($r_body);
 424                          $abort = true;
 425                      }
 426  
 427                      if ($chunk_size > 0) {
 428                          $r_body .= $this->readData($socket, $chunk_size, 'chunk');
 429                          $this->readData($socket, 2, 'chunk'); // read trailing \r\n
 430                      }
 431                  } while ($chunk_size && !$abort);
 432              }elseif(isset($this->resp_headers['content-length']) && !isset($this->resp_headers['transfer-encoding'])){
 433                  /* RFC 2616
 434                   * If a message is received with both a Transfer-Encoding header field and a Content-Length
 435                   * header field, the latter MUST be ignored.
 436                   */
 437  
 438                  // read up to the content-length or max_bodysize
 439                  // for keep alive we need to read the whole message to clean up the socket for the next read
 440                  if(
 441                      !$this->keep_alive &&
 442                      $this->max_bodysize &&
 443                      $this->max_bodysize < $this->resp_headers['content-length']
 444                  ) {
 445                      $length = $this->max_bodysize + 1;
 446                  }else{
 447                      $length = $this->resp_headers['content-length'];
 448                  }
 449  
 450                  $r_body = $this->readData($socket, $length, 'response (content-length limited)', true);
 451              }elseif( !isset($this->resp_headers['transfer-encoding']) && $this->max_bodysize && !$this->keep_alive){
 452                  $r_body = $this->readData($socket, $this->max_bodysize+1, 'response (content-length limited)', true);
 453              } elseif ((int)$this->status === 204) {
 454                  // request has no content
 455              } else{
 456                  // read entire socket
 457                  while (!feof($socket)) {
 458                      $r_body .= $this->readData($socket, 4096, 'response (unlimited)', true);
 459                  }
 460              }
 461  
 462              // recheck body size, we might have read max_bodysize+1 or even the whole body, so we abort late here
 463              if($this->max_bodysize){
 464                  if(strlen($r_body) > $this->max_bodysize){
 465                      if ($this->max_bodysize_abort) {
 466                          throw new HTTPClientException('Allowed response size exceeded');
 467                      } else {
 468                          $this->error = 'Allowed response size exceeded';
 469                      }
 470                  }
 471              }
 472  
 473          } catch (HTTPClientException $err) {
 474              $this->error = $err->getMessage();
 475              if ($err->getCode())
 476                  $this->status = $err->getCode();
 477              unset(self::$connections[$connectionId]);
 478              fclose($socket);
 479              return false;
 480          }
 481  
 482          if (!$this->keep_alive ||
 483              (isset($this->resp_headers['connection']) && $this->resp_headers['connection'] == 'Close')) {
 484              // close socket
 485              fclose($socket);
 486              unset(self::$connections[$connectionId]);
 487          }
 488  
 489          // decode gzip if needed
 490          if(isset($this->resp_headers['content-encoding']) &&
 491              $this->resp_headers['content-encoding'] == 'gzip' &&
 492              strlen($r_body) > 10 && substr($r_body,0,3)=="\x1f\x8b\x08"){
 493              $this->resp_body = @gzinflate(substr($r_body, 10));
 494              if($this->resp_body === false){
 495                  $this->error = 'Failed to decompress gzip encoded content';
 496                  $this->resp_body = $r_body;
 497              }
 498          }else{
 499              $this->resp_body = $r_body;
 500          }
 501  
 502          $this->debug('response body',$this->resp_body);
 503          $this->redirect_count = 0;
 504          return true;
 505      }
 506  
 507      /**
 508       * Tries to establish a CONNECT tunnel via Proxy
 509       *
 510       * Protocol, Servername and Port will be stripped from the request URL when a successful CONNECT happened
 511       *
 512       * @param resource &$socket
 513       * @param string   &$requesturl
 514       * @throws HTTPClientException when a tunnel is needed but could not be established
 515       * @return bool true if a tunnel was established
 516       */
 517      protected function ssltunnel(&$socket, &$requesturl){
 518          if(!$this->useProxyForUrl($requesturl)) return false;
 519          $requestinfo = parse_url($requesturl);
 520          if($requestinfo['scheme'] != 'https') return false;
 521          if(empty($requestinfo['port'])) $requestinfo['port'] = 443;
 522  
 523          // build request
 524          $request  = "CONNECT {$requestinfo['host']}:{$requestinfo['port']} HTTP/1.0".HTTP_NL;
 525          $request .= "Host: {$requestinfo['host']}".HTTP_NL;
 526          if($this->proxy_user) {
 527              $request .= 'Proxy-Authorization: Basic '.base64_encode($this->proxy_user.':'.$this->proxy_pass).HTTP_NL;
 528          }
 529          $request .= HTTP_NL;
 530  
 531          $this->debug('SSL Tunnel CONNECT',$request);
 532          $this->sendData($socket, $request, 'SSL Tunnel CONNECT');
 533  
 534          // read headers from socket
 535          $r_headers = '';
 536          do{
 537              $r_line = $this->readLine($socket, 'headers');
 538              $r_headers .= $r_line;
 539          }while($r_line != "\r\n" && $r_line != "\n");
 540  
 541          $this->debug('SSL Tunnel Response',$r_headers);
 542          if(preg_match('/^HTTP\/1\.[01] 200/i',$r_headers)){
 543              // set correct peer name for verification (enabled since PHP 5.6)
 544              stream_context_set_option($socket, 'ssl', 'peer_name', $requestinfo['host']);
 545  
 546              // SSLv3 is broken, use only TLS connections.
 547              // @link https://bugs.php.net/69195
 548              if (PHP_VERSION_ID >= 50600 && PHP_VERSION_ID <= 50606) {
 549                  $cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT;
 550              } else {
 551                  // actually means neither SSLv2 nor SSLv3
 552                  $cryptoMethod = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
 553              }
 554  
 555              if (@stream_socket_enable_crypto($socket, true, $cryptoMethod)) {
 556                  $requesturl = $requestinfo['path'].
 557                      (!empty($requestinfo['query'])?'?'.$requestinfo['query']:'');
 558                  return true;
 559              }
 560  
 561              throw new HTTPClientException(
 562                  'Failed to set up crypto for secure connection to '.$requestinfo['host'], -151
 563              );
 564          }
 565  
 566          throw new HTTPClientException('Failed to establish secure proxy connection', -150);
 567      }
 568  
 569      /**
 570       * Safely write data to a socket
 571       *
 572       * @param  resource $socket     An open socket handle
 573       * @param  string   $data       The data to write
 574       * @param  string   $message    Description of what is being read
 575       * @throws HTTPClientException
 576       *
 577       * @author Tom N Harris <tnharris@whoopdedo.org>
 578       */
 579      protected function sendData($socket, $data, $message) {
 580          // send request
 581          $towrite = strlen($data);
 582          $written = 0;
 583          while($written < $towrite){
 584              // check timeout
 585              $time_used = $this->time() - $this->start;
 586              if($time_used > $this->timeout)
 587                  throw new HTTPClientException(sprintf('Timeout while sending %s (%.3fs)',$message, $time_used), -100);
 588              if(feof($socket))
 589                  throw new HTTPClientException("Socket disconnected while writing $message");
 590  
 591              // select parameters
 592              $sel_r = null;
 593              $sel_w = array($socket);
 594              $sel_e = null;
 595              // wait for stream ready or timeout (1sec)
 596              if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
 597                  usleep(1000);
 598                  continue;
 599              }
 600  
 601              // write to stream
 602              $nbytes = fwrite($socket, substr($data,$written,4096));
 603              if($nbytes === false)
 604                  throw new HTTPClientException("Failed writing to socket while sending $message", -100);
 605              $written += $nbytes;
 606          }
 607      }
 608  
 609      /**
 610       * Safely read data from a socket
 611       *
 612       * Reads up to a given number of bytes or throws an exception if the
 613       * response times out or ends prematurely.
 614       *
 615       * @param  resource $socket     An open socket handle in non-blocking mode
 616       * @param  int      $nbytes     Number of bytes to read
 617       * @param  string   $message    Description of what is being read
 618       * @param  bool     $ignore_eof End-of-file is not an error if this is set
 619       * @throws HTTPClientException
 620       * @return string
 621       *
 622       * @author Tom N Harris <tnharris@whoopdedo.org>
 623       */
 624      protected function readData($socket, $nbytes, $message, $ignore_eof = false) {
 625          $r_data = '';
 626          // Does not return immediately so timeout and eof can be checked
 627          if ($nbytes < 0) $nbytes = 0;
 628          $to_read = $nbytes;
 629          do {
 630              $time_used = $this->time() - $this->start;
 631              if ($time_used > $this->timeout)
 632                  throw new HTTPClientException(
 633                      sprintf('Timeout while reading %s after %d bytes (%.3fs)', $message,
 634                          strlen($r_data), $time_used), -100);
 635              if(feof($socket)) {
 636                  if(!$ignore_eof)
 637                      throw new HTTPClientException("Premature End of File (socket) while reading $message");
 638                  break;
 639              }
 640  
 641              if ($to_read > 0) {
 642                  // select parameters
 643                  $sel_r = array($socket);
 644                  $sel_w = null;
 645                  $sel_e = null;
 646                  // wait for stream ready or timeout (1sec)
 647                  if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
 648                      usleep(1000);
 649                      continue;
 650                  }
 651  
 652                  $bytes = fread($socket, $to_read);
 653                  if($bytes === false)
 654                      throw new HTTPClientException("Failed reading from socket while reading $message", -100);
 655                  $r_data .= $bytes;
 656                  $to_read -= strlen($bytes);
 657              }
 658          } while ($to_read > 0 && strlen($r_data) < $nbytes);
 659          return $r_data;
 660      }
 661  
 662      /**
 663       * Safely read a \n-terminated line from a socket
 664       *
 665       * Always returns a complete line, including the terminating \n.
 666       *
 667       * @param  resource $socket     An open socket handle in non-blocking mode
 668       * @param  string   $message    Description of what is being read
 669       * @throws HTTPClientException
 670       * @return string
 671       *
 672       * @author Tom N Harris <tnharris@whoopdedo.org>
 673       */
 674      protected function readLine($socket, $message) {
 675          $r_data = '';
 676          do {
 677              $time_used = $this->time() - $this->start;
 678              if ($time_used > $this->timeout)
 679                  throw new HTTPClientException(
 680                      sprintf('Timeout while reading %s (%.3fs) >%s<', $message, $time_used, $r_data),
 681                      -100);
 682              if(feof($socket))
 683                  throw new HTTPClientException("Premature End of File (socket) while reading $message");
 684  
 685              // select parameters
 686              $sel_r = array($socket);
 687              $sel_w = null;
 688              $sel_e = null;
 689              // wait for stream ready or timeout (1sec)
 690              if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
 691                  usleep(1000);
 692                  continue;
 693              }
 694  
 695              $r_data = fgets($socket, 1024);
 696          } while (!preg_match('/\n$/',$r_data));
 697          return $r_data;
 698      }
 699  
 700      /**
 701       * print debug info
 702       *
 703       * Uses _debug_text or _debug_html depending on the SAPI name
 704       *
 705       * @author Andreas Gohr <andi@splitbrain.org>
 706       *
 707       * @param string $info
 708       * @param mixed  $var
 709       */
 710      protected function debug($info,$var=null){
 711          if(!$this->debug) return;
 712          if(php_sapi_name() == 'cli'){
 713              $this->debugText($info, $var);
 714          }else{
 715              $this->debugHtml($info, $var);
 716          }
 717      }
 718  
 719      /**
 720       * print debug info as HTML
 721       *
 722       * @param string $info
 723       * @param mixed  $var
 724       */
 725      protected function debugHtml($info, $var=null){
 726          print '<b>'.$info.'</b> '.($this->time() - $this->start).'s<br />';
 727          if(!is_null($var)){
 728              ob_start();
 729              print_r($var);
 730              $content = htmlspecialchars(ob_get_contents());
 731              ob_end_clean();
 732              print '<pre>'.$content.'</pre>';
 733          }
 734      }
 735  
 736      /**
 737       * prints debug info as plain text
 738       *
 739       * @param string $info
 740       * @param mixed  $var
 741       */
 742      protected function debugText($info, $var=null){
 743          print '*'.$info.'* '.($this->time() - $this->start)."s\n";
 744          if(!is_null($var)) print_r($var);
 745          print "\n-----------------------------------------------\n";
 746      }
 747  
 748      /**
 749       * Return current timestamp in microsecond resolution
 750       *
 751       * @return float
 752       */
 753      protected static function time(){
 754          list($usec, $sec) = explode(" ", microtime());
 755          return ((float)$usec + (float)$sec);
 756      }
 757  
 758      /**
 759       * convert given header string to Header array
 760       *
 761       * All Keys are lowercased.
 762       *
 763       * @author Andreas Gohr <andi@splitbrain.org>
 764       *
 765       * @param string $string
 766       * @return array
 767       */
 768      protected function parseHeaders($string){
 769          $headers = array();
 770          $lines = explode("\n",$string);
 771          array_shift($lines); //skip first line (status)
 772          foreach($lines as $line){
 773              @list($key, $val) = explode(':',$line,2);
 774              $key = trim($key);
 775              $val = trim($val);
 776              $key = strtolower($key);
 777              if(!$key) continue;
 778              if(isset($headers[$key])){
 779                  if(is_array($headers[$key])){
 780                      $headers[$key][] = $val;
 781                  }else{
 782                      $headers[$key] = array($headers[$key],$val);
 783                  }
 784              }else{
 785                  $headers[$key] = $val;
 786              }
 787          }
 788          return $headers;
 789      }
 790  
 791      /**
 792       * convert given header array to header string
 793       *
 794       * @author Andreas Gohr <andi@splitbrain.org>
 795       *
 796       * @param array $headers
 797       * @return string
 798       */
 799      protected function buildHeaders($headers){
 800          $string = '';
 801          foreach($headers as $key => $value){
 802              if($value === '') continue;
 803              $string .= $key.': '.$value.HTTP_NL;
 804          }
 805          return $string;
 806      }
 807  
 808      /**
 809       * get cookies as http header string
 810       *
 811       * @author Andreas Goetz <cpuidle@gmx.de>
 812       *
 813       * @return string
 814       */
 815      protected function getCookies(){
 816          $headers = '';
 817          foreach ($this->cookies as $key => $val){
 818              $headers .= "$key=$val; ";
 819          }
 820          $headers = substr($headers, 0, -2);
 821          if ($headers) $headers = "Cookie: $headers".HTTP_NL;
 822          return $headers;
 823      }
 824  
 825      /**
 826       * Encode data for posting
 827       *
 828       * @author Andreas Gohr <andi@splitbrain.org>
 829       *
 830       * @param array $data
 831       * @return string
 832       */
 833      protected function postEncode($data){
 834          return http_build_query($data,'','&');
 835      }
 836  
 837      /**
 838       * Encode data for posting using multipart encoding
 839       *
 840       * @fixme use of urlencode might be wrong here
 841       * @author Andreas Gohr <andi@splitbrain.org>
 842       *
 843       * @param array $data
 844       * @return string
 845       */
 846      protected function postMultipartEncode($data){
 847          $boundary = '--'.$this->boundary;
 848          $out = '';
 849          foreach($data as $key => $val){
 850              $out .= $boundary.HTTP_NL;
 851              if(!is_array($val)){
 852                  $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"'.HTTP_NL;
 853                  $out .= HTTP_NL; // end of headers
 854                  $out .= $val;
 855                  $out .= HTTP_NL;
 856              }else{
 857                  $out .= 'Content-Disposition: form-data; name="'.urlencode($key).'"';
 858                  if($val['filename']) $out .= '; filename="'.urlencode($val['filename']).'"';
 859                  $out .= HTTP_NL;
 860                  if($val['mimetype']) $out .= 'Content-Type: '.$val['mimetype'].HTTP_NL;
 861                  $out .= HTTP_NL; // end of headers
 862                  $out .= $val['body'];
 863                  $out .= HTTP_NL;
 864              }
 865          }
 866          $out .= "$boundary--".HTTP_NL;
 867          return $out;
 868      }
 869  
 870      /**
 871       * Generates a unique identifier for a connection.
 872       *
 873       * @param  string $server
 874       * @param  string $port
 875       * @return string unique identifier
 876       */
 877      protected function uniqueConnectionId($server, $port) {
 878          return "$server:$port";
 879      }
 880  
 881      /**
 882       * Should the Proxy be used for the given URL?
 883       *
 884       * Checks the exceptions
 885       *
 886       * @param string $url
 887       * @return bool
 888       */
 889      protected function useProxyForUrl($url) {
 890          return $this->proxy_host && (!$this->proxy_except || !preg_match('/' . $this->proxy_except . '/i', $url));
 891      }
 892  }