/** * {@inheritdoc} */ protected function doSendInternalRequest(InternalRequestInterface $internalRequest) { $loop = EventLoopFactory::create(); $dnsResolverFactory = new DnsResolverFactory(); $httpClientFactory = new HttpClientFactory(); $error = null; $response = null; $body = null; $request = $httpClientFactory->create($loop, $dnsResolverFactory->createCached('8.8.8.8', $loop))->request($internalRequest->getMethod(), $url = (string) $internalRequest->getUrl(), $this->prepareHeaders($internalRequest, true, true, true)); $request->on('error', function (\Exception $onError) use(&$error) { $error = $onError; }); $request->on('response', function (Response $onResponse) use(&$response, &$body) { $onResponse->on('data', function ($data) use(&$body) { $body .= $data; }); $response = $onResponse; }); $request->end($this->prepareBody($internalRequest)); $loop->run(); if ($error !== null) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $error->getMessage()); } return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->getCode(), $response->getReasonPhrase(), $response->getVersion(), $response->getHeaders(), BodyNormalizer::normalize($body, $internalRequest->getMethod())); }
/** * {@inheritdoc} */ protected function doSendInternalRequest(InternalRequestInterface $internalRequest) { $this->client->resetParameters(true)->setConfig(array('httpversion' => $internalRequest->getProtocolVersion(), 'timeout' => $this->getConfiguration()->getTimeout(), 'maxredirects' => 0))->setUri($url = (string) $internalRequest->getUrl())->setMethod($internalRequest->getMethod())->setHeaders($this->prepareHeaders($internalRequest))->setRawData($this->prepareBody($internalRequest)); try { $response = $this->client->request(); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); } return $this->getConfiguration()->getMessageFactory()->createResponse($response->getStatus(), $response->getMessage(), $response->getVersion(), $response->getHeaders(), BodyNormalizer::normalize(function () use($response) { return $response instanceof \Zend_Http_Response_Stream ? $response->getStream() : $response->getBody(); }, $internalRequest->getMethod())); }
/** * {@inheritdoc} */ protected function doSendInternalRequest(InternalRequestInterface $internalRequest) { $this->httpSocket->config['timeout'] = $this->getConfiguration()->getTimeout(); $request = array('version' => $this->getConfiguration()->getProtocolVersion(), 'redirect' => false, 'uri' => $url = (string) $internalRequest->getUrl(), 'method' => $internalRequest->getMethod(), 'header' => $this->prepareHeaders($internalRequest), 'body' => $this->prepareBody($internalRequest)); try { $response = $this->httpSocket->request($request); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); } if (($error = $this->httpSocket->lastError()) !== null) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $error); } return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->code, $response->reasonPhrase, ProtocolVersionExtractor::extract($response->httpVersion), $response->headers, BodyNormalizer::normalize($response->body, $internalRequest->getMethod())); }
/** * {@inheritdoc} */ protected function doSendInternalRequest(InternalRequestInterface $internalRequest) { $this->browser->getClient()->setTimeout($this->getConfiguration()->getTimeout()); $this->browser->getClient()->setMaxRedirects(0); $request = $this->browser->getMessageFactory()->createRequest($internalRequest->getMethod(), $url = (string) $internalRequest->getUrl()); $request->setProtocolVersion($internalRequest->getProtocolVersion()); $request->setHeaders($this->prepareHeaders($internalRequest, false)); $data = $this->browser->getClient() instanceof AbstractCurl ? $this->prepareContent($internalRequest) : $this->prepareBody($internalRequest); $request->setContent($data); try { $response = $this->browser->send($request); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); } return $this->getConfiguration()->getMessageFactory()->createResponse($response->getStatusCode(), $response->getReasonPhrase(), (string) $response->getProtocolVersion(), HeadersNormalizer::normalize($response->getHeaders()), BodyNormalizer::normalize($response->getContent(), $internalRequest->getMethod())); }
/** * {@inheritdoc} */ protected function doSendInternalRequest(InternalRequestInterface $internalRequest) { list($protocol, $host, $port, $path) = $this->parseUrl($url = (string) $internalRequest->getUrl()); $socket = @stream_socket_client($protocol . '://' . $host . ':' . $port, $errno, $errstr, $this->getConfiguration()->getTimeout()); if ($socket === false) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $errstr); } stream_set_timeout($socket, $this->getConfiguration()->getTimeout()); fwrite($socket, $this->prepareRequest($internalRequest, $path, $host, $port)); list($responseHeaders, $body) = $this->parseResponse($socket); $hasTimeout = $this->detectTimeout($socket); fclose($socket); if ($hasTimeout) { throw HttpAdapterException::timeoutExceeded($url, $this->getConfiguration()->getTimeout(), $this->getName()); } return $this->getConfiguration()->getMessageFactory()->createResponse(StatusCodeExtractor::extract($responseHeaders), ReasonPhraseExtractor::extract($responseHeaders), ProtocolVersionExtractor::extract($responseHeaders), $responseHeaders = HeadersNormalizer::normalize($responseHeaders), BodyNormalizer::normalize($this->decodeBody($responseHeaders, $body), $internalRequest->getMethod())); }
/** * {@inheritdoc} */ public function extract(InternalRequestInterface $request, ResponseInterface $response) { foreach ($response->getHeaderAsArray('Set-Cookie') as $header) { $cookie = $this->cookieFactory->parse($header); if (!$cookie->hasAttribute(CookieInterface::ATTR_DOMAIN)) { $cookie->setAttribute(CookieInterface::ATTR_DOMAIN, parse_url((string) $request->getUrl(), PHP_URL_HOST)); } $this->addCookie($cookie); } }
/** * Creates a request. * * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. * @param callable|null $success The success callable. * @param callable|null $error The error callable. * * @return \GuzzleHttp\Message\RequestInterface The request. */ private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null) { $request = $this->client->createRequest($internalRequest->getMethod(), (string) $internalRequest->getUrl(), array('exceptions' => false, 'allow_redirects' => false, 'timeout' => $this->getConfiguration()->getTimeout(), 'connect_timeout' => $this->getConfiguration()->getTimeout(), 'version' => $internalRequest->getProtocolVersion(), 'headers' => $this->prepareHeaders($internalRequest), 'body' => $this->prepareContent($internalRequest))); if (is_callable($success)) { $messageFactory = $this->getConfiguration()->getMessageFactory(); $request->getEmitter()->on('complete', function (CompleteEvent $event) use($success, $internalRequest, $messageFactory) { $response = $messageFactory->createResponse((int) $event->getResponse()->getStatusCode(), $event->getResponse()->getReasonPhrase(), $event->getResponse()->getProtocolVersion(), $event->getResponse()->getHeaders(), BodyNormalizer::normalize(function () use($event) { return new GuzzleHttpStream($event->getResponse()->getBody()); }, $internalRequest->getMethod())); $response->setParameter('request', $internalRequest); call_user_func($success, $response); }); } if (is_callable($error)) { $httpAdapterName = $this->getName(); $request->getEmitter()->on('error', function (ErrorEvent $event) use($error, $internalRequest, $httpAdapterName) { $exception = HttpAdapterException::cannotFetchUrl($event->getException()->getRequest()->getUrl(), $httpAdapterName, $event->getException()->getMessage()); $exception->setRequest($internalRequest); call_user_func($error, $exception); }); } return $request; }
/** * Checks if the request matches the matcher. * * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $request The request. * * @return boolean TRUE if the request matches the matcher else FALSE. */ private function match(InternalRequestInterface $request) { return !$this->hasMatcher() || is_string($this->matcher) && preg_match($this->matcher, (string) $request->getUrl()) || is_callable($this->matcher) && call_user_func($this->matcher, $request); }
/** * {@inheritdoc} */ public function formatRequest(InternalRequestInterface $request) { return array('protocol_version' => $request->getProtocolVersion(), 'url' => (string) $request->getUrl(), 'method' => $request->getMethod(), 'headers' => $request->getHeaders(), 'raw_datas' => $request->getRawDatas(), 'datas' => $request->getDatas(), 'files' => $request->getFiles(), 'parameters' => $request->getParameters()); }
/** * Creates a response. * * @param resource $curl The curl resource. * @param string|boolean|null $data The data. * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request. * * @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred. * * @return \Ivory\HttpAdapter\Message\ResponseInterface The response. */ private function createResponse($curl, $data, InternalRequestInterface $internalRequest) { if (empty($data)) { throw HttpAdapterException::cannotFetchUrl((string) $internalRequest->getUrl(), $this->getName(), curl_error($curl)); } $headers = substr($data, 0, $headersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE)); return $this->getConfiguration()->getMessageFactory()->createResponse(StatusCodeExtractor::extract($headers), ReasonPhraseExtractor::extract($headers), ProtocolVersionExtractor::extract($headers), HeadersNormalizer::normalize($headers), BodyNormalizer::normalize(substr($data, $headersSize), $internalRequest->getMethod())); }
/** * {@inheritdoc} */ public function match(InternalRequestInterface $request) { $parts = parse_url((string) $request->getUrl()); return $this->matchDomain(isset($parts['host']) ? $parts['host'] : null) && $this->matchPath(isset($parts['path']) ? $parts['path'] : null) && $this->matchScheme(isset($parts['scheme']) ? $parts['scheme'] : null); }
/** * 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::cannotFetchUrl((string) $internalRequest->getUrl(), $httpAdapter->getName(), sprintf('Status code: %d', $response->getStatusCode())); $exception->setRequest($internalRequest); $exception->setResponse($response); return $exception; }
/** * 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->getUrl()); }
/** * {@inheritdoc} */ public function prepareResponse(ResponseInterface $response, InternalRequestInterface $internalRequest) { $response->setParameter(self::REDIRECT_COUNT, (int) $internalRequest->getParameter(self::REDIRECT_COUNT)); $response->setParameter(self::EFFECTIVE_URL, (string) $internalRequest->getUrl()); }