/** * Gets the stopwatch name. * * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. * * @return string The stopwatch name. */ private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest) { return sprintf('ivory.http_adapter.%s (%s)', $httpAdapter->getName(), (string) $internalRequest->getUri()); }
/** * {@inheritdoc} */ public function getName() { return $this->httpAdapter->getName(); }
/** * {@inheritdoc} */ public function createRedirectRequest(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { if ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400 || !$response->hasHeader('Location')) { return false; } if ($internalRequest->getParameter(self::REDIRECT_COUNT) >= $this->max) { if ($this->throwException) { $rootRequest = $this->getRootRequest($internalRequest); $exception = HttpAdapterException::maxRedirectsExceeded((string) $rootRequest->getUri(), $this->max, $httpAdapter->getName()); $exception->setRequest($rootRequest); throw $exception; } return false; } $strict = $response->getStatusCode() === 303 || !$this->strict && $response->getStatusCode() <= 302; $headers = $internalRequest->getHeaders(); foreach ($headers as $key => $value) { if (strtolower($key) === 'host') { unset($headers[$key]); } } $redirect = $httpAdapter->getConfiguration()->getMessageFactory()->createInternalRequest($response->getHeaderLine('Location'), $strict ? InternalRequestInterface::METHOD_GET : $internalRequest->getMethod(), $internalRequest->getProtocolVersion(), $headers, $strict ? array() : $internalRequest->getDatas(), $strict ? array() : $internalRequest->getFiles(), $internalRequest->getParameters()); if ($strict) { $redirect = $redirect->withoutHeader('Content-Type')->withoutHeader('Content-Length'); } else { $redirect = $redirect->withBody($internalRequest->getBody()); } return $redirect->withParameter(self::PARENT_REQUEST, $internalRequest)->withParameter(self::REDIRECT_COUNT, $internalRequest->getParameter(self::REDIRECT_COUNT) + 1); }
/** * Creates a status code exception. * * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * * @return \Ivory\HttpAdapter\HttpAdapterException The status code exception. */ private function createStatusCodeException(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { $exception = HttpAdapterException::cannotFetchUri((string) $internalRequest->getUri(), $httpAdapter->getName(), sprintf('Status code: %d', $response->getStatusCode())); $exception->setRequest($internalRequest); $exception->setResponse($response); return $exception; }
/** * Logs error. * * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. * * @return \Ivory\HttpAdapter\Message\InternalRequestInterface The logged request. */ private function error(HttpAdapterInterface $httpAdapter, HttpAdapterException $exception) { $request = $this->getTimer()->stop($exception->getRequest()); $this->logger->error(sprintf('Unable to send "%s %s".', $exception->getRequest()->getMethod(), (string) $exception->getRequest()->getUri()), array('adapter' => $httpAdapter->getName(), 'exception' => $this->getFormatter()->formatException($exception), 'request' => $this->getFormatter()->formatRequest($request), 'response' => $exception->hasResponse() ? $this->getFormatter()->formatResponse($exception->getResponse()) : null)); return $request; }
/** * Collects an exception. * * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter The http adapter. * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception. */ private function collectException(HttpAdapterInterface $httpAdapter, HttpAdapterException $exception) { $this->getTimer()->stop($exception->getRequest()); $this->datas['exceptions'][] = array('adapter' => $httpAdapter->getName(), 'exception' => $this->getFormatter()->formatException($exception), 'request' => $this->getFormatter()->formatRequest($exception->getRequest()), 'response' => $exception->hasResponse() ? $this->getFormatter()->formatResponse($exception->getResponse()) : null); }
/** * {@inheritdoc} */ public function createRedirectRequest(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { if ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400 || !$response->hasHeader('Location')) { return false; } if ($internalRequest->getParameter(self::REDIRECT_COUNT) >= $this->max) { if ($this->throwException) { throw HttpAdapterException::maxRedirectsExceeded((string) $this->getRootRequest($internalRequest)->getUrl(), $this->max, $httpAdapter->getName()); } return false; } $redirect = $httpAdapter->getConfiguration()->getMessageFactory()->cloneInternalRequest($internalRequest); if ($response->getStatusCode() === 303 || !$this->strict && $response->getStatusCode() <= 302) { $redirect->setMethod(InternalRequestInterface::METHOD_GET); $redirect->removeHeaders(array('Content-Type', 'Content-Length')); $redirect->clearRawDatas(); $redirect->clearDatas(); $redirect->clearFiles(); } $redirect->setUrl($response->getHeader('Location')); $redirect->setParameter(self::PARENT_REQUEST, $internalRequest); $redirect->setParameter(self::REDIRECT_COUNT, $internalRequest->getParameter(self::REDIRECT_COUNT) + 1); return $redirect; }