Esempio n. 1
0
 private function createResponseObject(puzzle_message_RequestInterface $request, array $headers, puzzle_adapter_TransactionInterface $transaction, puzzle_stream_StreamInterface $stream)
 {
     $parts = explode(' ', array_shift($headers), 3);
     $options = array('protocol_version' => substr($parts[0], -3));
     if (isset($parts[2])) {
         $options['reason_phrase'] = $parts[2];
     }
     $response = $this->messageFactory->createResponse($parts[1], $this->headersFromLines($headers), null, $options);
     // Automatically decode responses when instructed.
     if ($request->getConfig()->get('decode_content')) {
         switch ($response->getHeader('Content-Encoding')) {
             case 'gzip':
             case 'deflate':
                 $stream = new puzzle_stream_InflateStream($stream);
                 break;
         }
     }
     // Drain the stream immediately if 'stream' was not enabled.
     $config = $request->getConfig();
     if (!$config['stream']) {
         $stream = $this->getSaveToBody($request, $stream);
     }
     $response->setBody($stream);
     $transaction->setResponse($response);
     puzzle_event_RequestEvents::emitHeaders($transaction);
     return $response;
 }
Esempio n. 2
0
 /**
  * Receive a response header from curl
  *
  * @param resource $curl   Curl handle
  * @param string   $header Received header
  *
  * @return int
  */
 public function receiveResponseHeader($curl, $header)
 {
     static $normalize = array("\r", "\n");
     $length = strlen($header);
     $header = str_replace($normalize, '', $header);
     if (strpos($header, 'HTTP/') === 0) {
         $startLine = explode(' ', $header, 3);
         // Only download the body to a target body when a successful
         // response is received.
         if ($startLine[1][0] != '2') {
             $this->body = null;
         }
         $this->statusCode = $startLine[1];
         $this->reasonPhrase = isset($startLine[2]) ? $startLine[2] : null;
         $this->protocolVersion = substr($startLine[0], -3);
         $this->headers = array();
     } elseif ($pos = strpos($header, ':')) {
         $this->headers[substr($header, 0, $pos)][] = substr($header, $pos + 1);
     } elseif ($header == '' && $this->statusCode >= 200) {
         $response = $this->messageFactory->createResponse($this->statusCode, $this->headers, $this->body, array('protocol_version' => $this->protocolVersion, 'reason_phrase' => $this->reasonPhrase));
         $this->headers = $this->body = null;
         $this->transaction->setResponse($response);
         // Allows events to react before downloading any of the body
         puzzle_event_RequestEvents::emitHeaders($this->transaction);
     }
     return $length;
 }