/** * Add a request to the history * * @param RequestInterface $request Request to add * @param Response $response Response of the request * * @return HistoryPlugin */ public function add(RequestInterface $request, Response $response = null) { if (!$response && $request->getResponse()) { $response = $request->getResponse(); } $this->transactions[] = array('request' => $request, 'response' => $response); if (count($this->transactions) > $this->getlimit()) { array_shift($this->transactions); } return $this; }
/** * Write data to the response body of a request * * @param resource $curl Curl handle * @param string $write Data that was received * * @return int */ public function writeResponseBody($curl, $write) { if ($this->emitIo) { $this->request->dispatch('curl.callback.write', array('request' => $this->request, 'write' => $write)); } if ($response = $this->request->getResponse()) { return $response->getBody()->write($write); } else { // Unexpected data received before response headers - abort transfer return 0; } }
/** * Update a request based on the log messages of the CurlHandle * * @param RequestInterface $request Request to update */ public function updateRequestFromTransfer(RequestInterface $request) { if (!$request->getResponse()) { return; } // Update the transfer stats of the response $request->getResponse()->setInfo($this->getInfo()); if (!($log = $this->getStderr(true))) { return; } // Parse the cURL stderr output for outgoing requests $headers = ''; fseek($log, 0); while (($line = fgets($log)) !== false) { if ($line && $line[0] == '>') { $headers = substr(trim($line), 2) . "\r\n"; while (($line = fgets($log)) !== false) { if ($line[0] == '*' || $line[0] == '<') { break; } else { $headers .= trim($line) . "\r\n"; } } } } // Add request headers to the request exactly as they were sent if ($headers) { $parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($headers); if (!empty($parsed['headers'])) { $request->setHeaders(array()); foreach ($parsed['headers'] as $name => $value) { $request->setHeader($name, $value); } } if (!empty($parsed['version'])) { $request->setProtocolVersion($parsed['version']); } } }
/** * @link https://github.com/guzzle/guzzle/issues/710 */ private function validateResponseWasSet(RequestInterface $request) { if ($request->getResponse()) { return true; } $body = $request instanceof EntityEnclosingRequestInterface ? $request->getBody() : null; if (!$body) { $rex = new RequestException('No response was received for a request with no body. This' . ' could mean that you are saturating your network.'); $rex->setRequest($request); $this->removeErroredRequest($request, $rex); } elseif (!$body->isSeekable() || !$body->seek(0)) { // Nothing we can do with this. Sorry! $rex = new RequestException('The connection was unexpectedly closed. The request would' . ' have been retried, but attempting to rewind the' . ' request body failed.'); $rex->setRequest($request); $this->removeErroredRequest($request, $rex); } else { $this->remove($request); // Add the request back to the batch to retry automatically. $this->requests[] = $request; $this->addHandle($request); } return false; }