/**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(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(), $uri = (string) $internalRequest->getUri(), $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::cannotFetchUri($uri, $this->getName(), $error->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->getCode(), $response->getVersion(), $response->getHeaders(), BodyNormalizer::normalize($body, $internalRequest->getMethod()));
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(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($uri = (string) $internalRequest->getUri(), $context);
     if ($body === false) {
         $error = error_get_last();
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error['message']);
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse(StatusCodeExtractor::extract($headers), ProtocolVersionExtractor::extract($headers), HeadersNormalizer::normalize($headers), BodyNormalizer::normalize($body, $internalRequest->getMethod()));
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $this->client->resetParameters(true)->setConfig(array('httpversion' => $internalRequest->getProtocolVersion(), 'timeout' => $this->getConfiguration()->getTimeout(), 'maxredirects' => 0))->setUri($uri = (string) $internalRequest->getUri())->setMethod($internalRequest->getMethod())->setHeaders($this->prepareHeaders($internalRequest))->setRawData($this->prepareBody($internalRequest));
     try {
         $response = $this->client->request();
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->getStatus(), $response->getVersion(), $response->getHeaders(), BodyNormalizer::normalize(function () use($response) {
         return $response instanceof \Zend_Http_Response_Stream ? $response->getStream() : $response->getBody();
     }, $internalRequest->getMethod()));
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $this->httpSocket->config['timeout'] = $this->getConfiguration()->getTimeout();
     $request = array('version' => $this->getConfiguration()->getProtocolVersion(), 'redirect' => false, 'uri' => $uri = (string) $internalRequest->getUri(), 'method' => $internalRequest->getMethod(), 'header' => $this->prepareHeaders($internalRequest), 'body' => $this->prepareBody($internalRequest));
     try {
         $response = $this->httpSocket->request($request);
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     if (($error = $this->httpSocket->lastError()) !== null) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error);
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->code, ProtocolVersionExtractor::extract($response->httpVersion), $response->headers, BodyNormalizer::normalize($response->body, $internalRequest->getMethod()));
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $body = new Body();
     $body->append($this->prepareBody($internalRequest));
     $request = new Request($internalRequest->getMethod(), $uri = (string) $internalRequest->getUri(), $this->prepareHeaders($internalRequest), $body);
     $httpVersion = $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0 ? \http\Client\Curl\HTTP_VERSION_1_0 : \http\Client\Curl\HTTP_VERSION_1_1;
     $request->setOptions(array('protocol' => $httpVersion, 'timeout' => $this->getConfiguration()->getTimeout()));
     try {
         $this->client->reset()->enqueue($request)->send();
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     $response = $this->client->getResponse();
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->getResponseCode(), $response->getHttpVersion(), $response->getHeaders(), $response->getBody()->getResource());
 }
Beispiel #6
0
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $this->browser->getClient()->setTimeout($this->getConfiguration()->getTimeout());
     $this->browser->getClient()->setMaxRedirects(0);
     $request = $this->browser->getMessageFactory()->createRequest($internalRequest->getMethod(), $uri = (string) $internalRequest->getUri());
     $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::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->getStatusCode(), (string) $response->getProtocolVersion(), HeadersNormalizer::normalize($response->getHeaders()), BodyNormalizer::normalize($response->getContent(), $internalRequest->getMethod()));
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $request = new Request();
     foreach ($this->prepareHeaders($internalRequest) as $name => $value) {
         $request->header($name, $value);
     }
     $request->method($internalRequest->getMethod());
     $request->body($this->prepareBody($internalRequest));
     $request->url($uri = (string) $internalRequest->getUri());
     $request->version($this->getConfiguration()->getProtocolVersion());
     try {
         $response = $this->client->send($request, array('timeout' => $this->getConfiguration()->getTimeout(), 'redirect' => false));
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse((int) $response->statusCode(), $response->version(), $response->headers(), $response->body());
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $uri = $internalRequest->getUri();
     $socket = @stream_socket_client(($uri->getScheme() === 'https' ? 'ssl' : 'tcp') . '://' . $uri->getHost() . ':' . ($uri->getPort() ?: 80), $errno, $errstr, $this->getConfiguration()->getTimeout());
     if ($socket === false) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $errstr);
     }
     stream_set_timeout($socket, $this->getConfiguration()->getTimeout());
     fwrite($socket, $this->prepareRequest($internalRequest));
     list($responseHeaders, $body) = $this->parseResponse($socket);
     $hasTimeout = $this->detectTimeout($socket);
     fclose($socket);
     if ($hasTimeout) {
         throw HttpAdapterException::timeoutExceeded($uri, $this->getConfiguration()->getTimeout(), $this->getName());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse(StatusCodeExtractor::extract($responseHeaders), ProtocolVersionExtractor::extract($responseHeaders), $responseHeaders = HeadersNormalizer::normalize($responseHeaders), BodyNormalizer::normalize($this->decodeBody($responseHeaders, $body), $internalRequest->getMethod()));
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequests(array $internalRequests, $success, $error)
 {
     $requests = array();
     foreach ($internalRequests as $key => $internalRequest) {
         $requests[$key] = $this->createRequest($internalRequest);
     }
     $httpAdapter = $this;
     $pool = new Pool($this->client, $requests, array_merge($this->createOptions(), array('fulfilled' => function ($response, $index) use($success, $internalRequests, $httpAdapter) {
         $response = $httpAdapter->getConfiguration()->getMessageFactory()->createResponse((int) $response->getStatusCode(), $response->getProtocolVersion(), $response->getHeaders(), BodyNormalizer::normalize(function () use($response) {
             return $response->getBody()->detach();
         }, $internalRequests[$index]->getMethod()));
         $response = $response->withParameter('request', $internalRequests[$index]);
         call_user_func($success, $response);
     }, 'rejected' => function ($exception, $index) use($error, $internalRequests, $httpAdapter) {
         $exception = HttpAdapterException::cannotFetchUri($exception->getRequest()->getUri(), $httpAdapter->getName(), $exception->getMessage());
         $exception->setRequest($internalRequests[$index]);
         call_user_func($error, $exception);
     })));
     $pool->promise()->wait();
 }
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $request = Request::init($internalRequest->getMethod())->whenError(function () {
     })->addOnCurlOption(CURLOPT_HTTP_VERSION, $this->prepareProtocolVersion($internalRequest))->timeout($this->getConfiguration()->getTimeout())->uri($uri = (string) $internalRequest->getUri())->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
     $files = $internalRequest->getFiles();
     if (!empty($files)) {
         $request->mime(Mime::UPLOAD);
     }
     try {
         $response = $request->send();
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->code, ProtocolVersionExtractor::extract($response->raw_headers), $response->headers->toArray(), BodyNormalizer::normalize($response->body, $internalRequest->getMethod()));
 }
 /**
  * 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;
 }
 /**
  * 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::cannotFetchUri((string) $internalRequest->getUri(), $this->getName(), curl_error($curl));
     }
     $headers = substr($data, 0, $headersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE));
     return $this->getConfiguration()->getMessageFactory()->createResponse(StatusCodeExtractor::extract($headers), ProtocolVersionExtractor::extract($headers), HeadersNormalizer::normalize($headers), BodyNormalizer::normalize(substr($data, $headersSize), $internalRequest->getMethod()));
 }
 public function test_it_throws_protocol_exception_from_http_adapter()
 {
     $this->given($SUT = new SUT($this->mockAdapter, $this->apiKey), $httpAdapterException = \Ivory\HttpAdapter\HttpAdapterException::cannotFetchUri('http://www.google.com', get_class($this->mockAdapter), 'this is an error message'), $this->calling($this->mockAdapter)->get->throw = $httpAdapterException)->then($this->exception(function () use($SUT) {
         $SUT->executeQuery([]);
     })->isInstanceOf('Rezzza\\GoogleGeocoder\\Exception\\GoogleGeocodeProtocolException')->hasMessage('An error occurred when fetching the URI "http://www.google.com" with the adapter "mock\\Ivory\\HttpAdapter\\CurlHttpAdapter" ("this is an error message").'))->and($this->mock($this->mockAdapter)->call('get')->once());
 }
 /**
  * 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->getUri(), 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()->getProtocolVersion(), $event->getResponse()->getHeaders(), BodyNormalizer::normalize(function () use($event) {
                 return new GuzzleHttpStream($event->getResponse()->getBody());
             }, $internalRequest->getMethod()));
             $response = $response->withParameter('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::cannotFetchUri($event->getException()->getRequest()->getUrl(), $httpAdapterName, $event->getException()->getMessage());
             $exception->setRequest($internalRequest);
             call_user_func($error, $exception);
         });
     }
     return $request;
 }
 /**
  * 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 \Ivory\HttpAdapter\Message\RequestInterface The request.
  */
 private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null)
 {
     $request = $this->client->createRequest($internalRequest->getMethod(), (string) $internalRequest->getUri(), $this->prepareHeaders($internalRequest), $this->prepareContent($internalRequest), array('exceptions' => false, 'allow_redirects' => false, 'timeout' => $this->getConfiguration()->getTimeout(), 'connect_timeout' => $this->getConfiguration()->getTimeout()));
     $request->setProtocolVersion($internalRequest->getProtocolVersion());
     if (is_callable($success)) {
         $messageFactory = $this->getConfiguration()->getMessageFactory();
         $request->getEventDispatcher()->addListener('request.success', function (Event $event) use($messageFactory, $success, $internalRequest) {
             $response = $messageFactory->createResponse($event['response']->getStatusCode(), $event['response']->getProtocolVersion(), $event['response']->getHeaders()->toArray(), BodyNormalizer::normalize(function () use($event) {
                 $resource = $event['response']->getBody()->getStream();
                 $event['response']->getBody()->detachStream();
                 return $resource;
             }, $internalRequest->getMethod()));
             $response = $response->withParameter('request', $internalRequest);
             call_user_func($success, $response);
         });
     }
     if (is_callable($error)) {
         $httpAdapterName = $this->getName();
         $request->getEventDispatcher()->addListener('request.exception', function (Event $event) use($error, $internalRequest, $httpAdapterName) {
             $exception = HttpAdapterException::cannotFetchUri($event['exception']->getRequest()->getUrl(), $httpAdapterName, $event['exception']->getMessage());
             $exception->setRequest($internalRequest);
             call_user_func($error, $exception);
         });
     }
     return $request;
 }