/** * Emits the complete event for a request and emits an error * event if an error is encountered during the after send. * * @param TransactionInterface $transaction Transaction to emit for * @param array $stats Transfer stats * * @throws RequestException */ public static function emitComplete(TransactionInterface $transaction, array $stats = []) { $request = $transaction->getRequest(); $transaction->getResponse()->setEffectiveUrl($request->getUrl()); try { $request->getEmitter()->emit('complete', new CompleteEvent($transaction, $stats)); } catch (RequestException $e) { self::emitError($transaction, $e, $stats); } }
/** * Write data to the response body of a request * * @param resource $curl * @param string $write * * @return int */ public function writeResponseBody($curl, $write) { if (!($response = $this->transaction->getResponse())) { return 0; } // Add a default body on the response if one was not found if (!($body = $response->getBody())) { $body = new Stream(fopen('php://temp', 'r+')); $response->setBody($body); } return $body->write($write); }
/** * This function ensures that a response was set on a transaction. If one * was not set, then the request is retried if possible. This error * typically means you are sending a payload, curl encountered a * "Connection died, retrying a fresh connect" error, tried to rewind the * stream, and then encountered a "necessary data rewind wasn't possible" * error, causing the request to be sent through curl_multi_info_read() * without an error status. * * @param TransactionInterface $transaction * @param BatchContext $context * * @return bool Returns true if it's OK, and false if it failed. * @throws \GuzzleHttp\Exception\RequestException If it failed and cannot * recover. */ private function validateResponseWasSet(TransactionInterface $transaction, BatchContext $context) { if ($transaction->getResponse()) { return true; } $body = $transaction->getRequest()->getBody(); if (!$body) { // This is weird and should probably never happen. RequestEvents::emitError($transaction, new RequestException('No response was received for a request with no body. This' . ' could mean that you are saturating your network.', $transaction->getRequest())); } elseif (!$body->isSeekable() || !$body->seek(0)) { // Nothing we can do with this. Sorry! RequestEvents::emitError($transaction, new RequestException('The connection was unexpectedly closed. The request would' . ' have been retried, but attempting to rewind the' . ' request body failed. Consider wrapping your request' . ' body in a CachingStream decorator to work around this' . ' issue if necessary.', $transaction->getRequest())); } else { $this->retryFailedConnection($transaction, $context); } return false; }