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