private function createResponseObject(RequestInterface $request, array $headers, TransactionInterface $transaction, StreamInterface $stream) { $parts = explode(' ', array_shift($headers), 3); $options = ['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 InflateStream($stream); break; } } // Drain the stream immediately if 'stream' was not enabled. if (!$request->getConfig()['stream']) { $stream = $this->getSaveToBody($request, $stream); } $response->setBody($stream); $transaction->setResponse($response); RequestEvents::emitHeaders($transaction); return $response; }
/** * Handles the process of processing a response received from a ring * handler. The created response is added to the transaction, and any * necessary events are emitted based on the ring response. * * @param Transaction $trans Owns request and response. * @param array $response Ring response array * @param MessageFactoryInterface $messageFactory Creates response objects. * @param callable $fsm Request FSM function. */ public static function completeRingResponse(Transaction $trans, array $response, MessageFactoryInterface $messageFactory, callable $fsm) { $trans->state = 'complete'; $trans->transferInfo = isset($response['transfer_stats']) ? $response['transfer_stats'] : []; if (!empty($response['status'])) { $options = []; if (isset($response['version'])) { $options['protocol_version'] = $response['version']; } if (isset($response['reason'])) { $options['reason_phrase'] = $response['reason']; } $trans->response = $messageFactory->createResponse($response['status'], isset($response['headers']) ? $response['headers'] : [], isset($response['body']) ? $response['body'] : null, $options); if (isset($response['effective_url'])) { $trans->response->setEffectiveUrl($response['effective_url']); } } elseif (empty($response['error'])) { // When nothing was returned, then we need to add an error. $response['error'] = self::getNoRingResponseException($trans->request); } if (isset($response['error'])) { $trans->state = 'error'; $trans->exception = $response['error']; } // Complete the lifecycle of the request. $fsm($trans); }
/** * 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 RequestEvents::emitHeaders($this->transaction); } return $length; }
private function createResponseObject(array $headers, TransactionInterface $transaction, $stream) { $parts = explode(' ', array_shift($headers), 3); $options = ['protocol_version' => substr($parts[0], -3)]; if (isset($parts[2])) { $options['reason_phrase'] = $parts[2]; } $response = $this->messageFactory->createResponse($parts[1], $this->headersFromLines($headers), $stream, $options); $transaction->setResponse($response); RequestEvents::emitHeaders($transaction); return $response; }
/** * {@inheritdoc} */ protected function readNext() { if (!$this->iterator->valid()) { return; } $message = $this->iterator->current(); $code = $this->response->getStatusCode(); $content = sprintf("HTTP/1.1 300\r\n %s", $message); $element = $this->parser->parseResponse($content); $current = $this->factory->createResponse($code, $element['headers'], $element['body']); return $current; }
private function createResponseObject(array $headers, TransactionInterface $transaction, $stream) { $parts = explode(' ', array_shift($headers), 3); $options = ['protocol_version' => substr($parts[0], -3)]; if (isset($parts[2])) { $options['reason_phrase'] = $parts[2]; } // Set the size on the stream if it was returned in the response $responseHeaders = []; foreach ($headers as $header) { $headerParts = explode(':', $header, 2); $responseHeaders[$headerParts[0]] = isset($headerParts[1]) ? $headerParts[1] : ''; } $response = $this->messageFactory->createResponse($parts[1], $responseHeaders, $stream, $options); $transaction->setResponse($response); RequestEvents::emitHeaders($transaction); return $response; }