You are here

function HTTP_Response::process in Flickr API 5

Processes a HTTP response

This extracts response code, headers, cookies and decodes body if it was encoded in some way

@access public

Parameters

bool Whether to store response body in object property, set: this to false if downloading a LARGE file and using a Listener. This is assumed to be true if body is gzip-encoded.

bool Whether the response can actually have a message-body.: Will be set to false for HEAD requests.

Return value

mixed true on success, PEAR_Error in case of malformed response

Throws

PEAR_Error

File

phpFlickr/PEAR/HTTP/Request.php, line 1184

Class

HTTP_Response
Response class to complement the Request class

Code

function process($saveBody = true, $canHaveBody = true) {
  do {
    $line = $this->_sock
      ->readLine();
    if (sscanf($line, 'HTTP/%s %s', $http_version, $returncode) != 2) {
      return PEAR::raiseError('Malformed response', HTTP_REQUEST_ERROR_RESPONSE);
    }
    else {
      $this->_protocol = 'HTTP/' . $http_version;
      $this->_code = intval($returncode);
    }
    while ('' !== ($header = $this->_sock
      ->readLine())) {
      $this
        ->_processHeader($header);
    }
  } while (100 == $this->_code);
  $this
    ->_notify('gotHeaders', $this->_headers);

  // RFC 2616, section 4.4:
  // 1. Any response message which "MUST NOT" include a message-body ...
  // is always terminated by the first empty line after the header fields
  // 3. ... If a message is received with both a
  // Transfer-Encoding header field and a Content-Length header field,
  // the latter MUST be ignored.
  $canHaveBody = $canHaveBody && $this->_code >= 200 && $this->_code != 204 && $this->_code != 304;

  // If response body is present, read it and decode
  $chunked = isset($this->_headers['transfer-encoding']) && 'chunked' == $this->_headers['transfer-encoding'];
  $gzipped = isset($this->_headers['content-encoding']) && 'gzip' == $this->_headers['content-encoding'];
  $hasBody = false;
  if ($canHaveBody && ($chunked || !isset($this->_headers['content-length']) || 0 != $this->_headers['content-length'])) {
    if ($chunked || !isset($this->_headers['content-length'])) {
      $this->_toRead = null;
    }
    else {
      $this->_toRead = $this->_headers['content-length'];
    }
    while (!$this->_sock
      ->eof() && (is_null($this->_toRead) || 0 < $this->_toRead)) {
      if ($chunked) {
        $data = $this
          ->_readChunked();
      }
      elseif (is_null($this->_toRead)) {
        $data = $this->_sock
          ->read(4096);
      }
      else {
        $data = $this->_sock
          ->read(min(4096, $this->_toRead));
        $this->_toRead -= HTTP_REQUEST_MBSTRING ? mb_strlen($data, 'iso-8859-1') : strlen($data);
      }
      if ('' == $data) {
        break;
      }
      else {
        $hasBody = true;
        if ($saveBody || $gzipped) {
          $this->_body .= $data;
        }
        $this
          ->_notify($gzipped ? 'gzTick' : 'tick', $data);
      }
    }
  }
  if ($hasBody) {

    // Uncompress the body if needed
    if ($gzipped) {
      $body = $this
        ->_decodeGzip($this->_body);
      if (PEAR::isError($body)) {
        return $body;
      }
      $this->_body = $body;
      $this
        ->_notify('gotBody', $this->_body);
    }
    else {
      $this
        ->_notify('gotBody');
    }
  }
  return true;
}