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; }
/** * 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; }
public function send(TransactionInterface $transaction) { RequestEvents::emitBefore($transaction); if (!$transaction->getResponse()) { $response = is_callable($this->response) ? call_user_func($this->response, $transaction) : $this->response; if (!$response instanceof ResponseInterface) { throw new \RuntimeException('Invalid mocked response'); } // Read the request body if it is present if ($transaction->getRequest()->getBody()) { $transaction->getRequest()->getBody()->__toString(); } $transaction->setResponse($response); RequestEvents::emitHeaders($transaction); RequestEvents::emitComplete($transaction); } return $transaction->getResponse(); }
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; }