/**
  * Callback function called by cURL for saving the response body
  *
  * @param    resource    cURL handle (not used)
  * @param    string      part of the response body
  * @return   integer     number of bytes saved
  * @see      Diglin_HTTP_Request2_Response::appendBody()
  */
 protected function callbackWriteBody($ch, $string)
 {
     // cURL calls WRITEFUNCTION callback without calling HEADERFUNCTION if
     // response doesn't start with proper HTTP status line (see bug #15716)
     if (empty($this->response)) {
         throw new Diglin_HTTP_Request2_Exception("Malformed response: {$string}");
     }
     if ($this->request->getConfig('store_body')) {
         $this->response->appendBody($string);
     }
     $this->request->setLastEvent('receivedBodyPart', $string);
     return strlen($string);
 }
 /**
  * Reads the remote server's response
  *
  * @return   Diglin_HTTP_Request2_Response
  * @throws   Diglin_HTTP_Request2_Exception
  */
 protected function readResponse()
 {
     $bufferSize = $this->request->getConfig('buffer_size');
     do {
         $response = new Diglin_HTTP_Request2_Response($this->readLine($bufferSize), true);
         do {
             $headerLine = $this->readLine($bufferSize);
             $response->parseHeaderLine($headerLine);
         } while ('' != $headerLine);
     } while (in_array($response->getStatus(), array(100, 101)));
     $this->request->setLastEvent('receivedHeaders', $response);
     // No body possible in such responses
     if (Diglin_HTTP_Request2::METHOD_HEAD == $this->request->getMethod() || Diglin_HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() && 200 <= $response->getStatus() && 300 > $response->getStatus() || in_array($response->getStatus(), array(204, 304))) {
         return $response;
     }
     $chunked = 'chunked' == $response->getHeader('transfer-encoding');
     $length = $response->getHeader('content-length');
     $hasBody = false;
     if ($chunked || null === $length || 0 < intval($length)) {
         // RFC 2616, section 4.4:
         // 3. ... If a message is received with both a
         // Transfer-Encoding header field and a Content-Length header field,
         // the latter MUST be ignored.
         $toRead = $chunked || null === $length ? null : $length;
         $this->chunkLength = 0;
         while (!feof($this->socket) && (is_null($toRead) || 0 < $toRead)) {
             if ($chunked) {
                 $data = $this->readChunked($bufferSize);
             } elseif (is_null($toRead)) {
                 $data = $this->fread($bufferSize);
             } else {
                 $data = $this->fread(min($toRead, $bufferSize));
                 $toRead -= strlen($data);
             }
             if ('' == $data && (!$this->chunkLength || feof($this->socket))) {
                 break;
             }
             $hasBody = true;
             if ($this->request->getConfig('store_body')) {
                 $response->appendBody($data);
             }
             if (!in_array($response->getHeader('content-encoding'), array('identity', null))) {
                 $this->request->setLastEvent('receivedEncodedBodyPart', $data);
             } else {
                 $this->request->setLastEvent('receivedBodyPart', $data);
             }
         }
     }
     if ($hasBody) {
         $this->request->setLastEvent('receivedBody', $response);
     }
     return $response;
 }
Пример #3
0
 /**
  * Creates a new Diglin_HTTP_Request2_Response object from a file
  *
  * @param    resource    file pointer returned by fopen()
  * @return   Diglin_HTTP_Request2_Response
  * @throws   Diglin_HTTP_Request2_Exception
  */
 public static function createResponseFromFile($fp)
 {
     $response = new Diglin_HTTP_Request2_Response(fgets($fp));
     do {
         $headerLine = fgets($fp);
         $response->parseHeaderLine($headerLine);
     } while ('' != trim($headerLine));
     while (!feof($fp)) {
         $response->appendBody(fread($fp, 8192));
     }
     return $response;
 }