Ejemplo n.º 1
0
 /**
  * {@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()));
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function doSendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $context = stream_context_create(array('http' => array('follow_location' => false, 'max_redirects' => 1, 'ignore_errors' => true, 'timeout' => $this->getConfiguration()->getTimeout(), 'protocol_version' => $internalRequest->getProtocolVersion(), 'method' => $internalRequest->getMethod(), 'header' => $this->prepareHeaders($internalRequest, false), 'content' => $this->prepareBody($internalRequest))));
     list($body, $headers) = $this->process($url = (string) $internalRequest->getUrl(), $context);
     if ($body === false) {
         $error = error_get_last();
         throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $error['message']);
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse(StatusCodeExtractor::extract($headers), ReasonPhraseExtractor::extract($headers), ProtocolVersionExtractor::extract($headers), HeadersNormalizer::normalize($headers), BodyNormalizer::normalize($body, $internalRequest->getMethod()));
 }
Ejemplo n.º 3
0
 /**
  * {@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()));
 }
Ejemplo n.º 4
0
 /**
  * {@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()));
 }
Ejemplo n.º 5
0
 /**
  * {@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()));
 }
Ejemplo n.º 6
0
 /**
  * {@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()));
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 protected function doSendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $request = Request::init($internalRequest->getMethod())->whenError(function () {
     })->addOnCurlOption(CURLOPT_HTTP_VERSION, $this->prepareProtocolVersion($internalRequest))->timeout($this->getConfiguration()->getTimeout())->uri($url = (string) $internalRequest->getUrl())->addHeaders($this->prepareHeaders($internalRequest))->body($this->prepareContent($internalRequest));
     if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
         $request->addOnCurlOption(CURLOPT_CONNECTTIMEOUT_MS, $this->getConfiguration()->getTimeout() * 1000);
     } else {
         // @codeCoverageIgnoreStart
         $request->addOnCurlOption(CURLOPT_CONNECTTIMEOUT, $this->getConfiguration()->getTimeout());
     }
     // @codeCoverageIgnoreEnd
     if ($internalRequest->hasFiles()) {
         $request->mime(Mime::UPLOAD);
     }
     try {
         $response = $request->send();
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->code, ReasonPhraseExtractor::extract($response->raw_headers), ProtocolVersionExtractor::extract($response->raw_headers), $response->headers->toArray(), BodyNormalizer::normalize($response->body, $internalRequest->getMethod()));
 }
Ejemplo n.º 8
0
 /**
  * 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;
 }
Ejemplo n.º 9
0
 /**
  * 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()));
 }
Ejemplo n.º 10
0
 /**
  * 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;
 }