コード例 #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()));
 }
コード例 #2
0
ファイル: CakeHttpAdapter.php プロジェクト: lamenath/fbp
 /**
  * {@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()));
 }
コード例 #3
0
ファイル: SocketHttpAdapter.php プロジェクト: lamenath/fbp
 /**
  * {@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()));
 }
コード例 #4
0
 /**
  * {@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()));
 }
コード例 #5
0
ファイル: HttpfulHttpAdapter.php プロジェクト: lamenath/fbp
 /**
  * {@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()));
 }
コード例 #6
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::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()));
 }