Esempio n. 1
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()));
 }
Esempio n. 2
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()));
 }
Esempio n. 3
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()));
 }
Esempio n. 4
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()));
 }