Esempio n. 1
0
 /**
  * Send a request to the server and return a JHttpResponse object with the response.
  *
  * @param   RokUpdater_Uri $uri        The URI to the resource to request.
  * @param   string         $data       Either an associative array or a string to be sent with the request.
  * @param   array          $headers    An array of request headers to send with the request.
  * @param   integer        $timeout    Read timeout in seconds.
  * @param   string         $userAgent  The optional user agent string to send with the request.
  *
  * @throws ProtocolBuffers_Exception
  * @return  string
  */
 protected function transportRequest(RokUpdater_Uri $uri, $data = null, array $headers = array(), $timeout = null, $userAgent = null)
 {
     // Create the stream context options array with the required method offset.
     $options = array('method' => strtoupper('POST'));
     // If data exists let's encode it and make sure our Content-type header is set.
     if (isset($data)) {
         $options['content'] = 'message=' . urlencode($data);
         JLog::add(sprintf('%s sending body %s', get_class($this), $options['content']), JLog::DEBUG, 'rokupdater');
         if (!isset($headers['Content-type'])) {
             $headers['Content-type'] = 'application/x-www-form-urlencoded';
         }
         $headers['Content-length'] = strlen($options['content']);
     }
     // Build the headers string for the request.
     $headerString = null;
     if (isset($headers)) {
         foreach ($headers as $key => $value) {
             $headerString .= $key . ': ' . $value . "\r\n";
         }
         // Add the headers string into the stream context options array.
         $options['header'] = trim($headerString, "\r\n");
     }
     // If an explicit timeout is given user it.
     if (isset($timeout)) {
         $options['timeout'] = (int) $timeout;
     }
     // If an explicit user agent is given use it.
     if (isset($userAgent)) {
         $options['user_agent'] = $userAgent;
     }
     // Ignore HTTP errors so that we can capture them.
     $options['ignore_errors'] = 1;
     // Create the stream context for the request.
     $context = stream_context_create(array('http' => $options));
     if ($uri->getScheme() == self::HTTPS_SCHEME) {
         $cacert_path = dirname(__FILE__) . '/cacert.pem';
         JLog::add(sprintf('%s : setting ssl:cacert option to %s', get_class($this), $cacert_path), JLog::INFO, 'rokupdater');
         stream_context_set_option($context, 'ssl', 'cafile', $cacert_path);
         stream_context_set_option($context, 'ssl', 'verify_peer', true);
     }
     if (array_key_exists('streamsocket.options', $this->options) && is_array($this->options['streamsocket.options'])) {
         foreach ($this->options['streamsocket.options'] as $wrapper => $settings) {
             foreach ($settings as $setting_name => $setting_value) {
                 JLog::add(sprintf('%s : Stream socket override option set %s:%s:%s.', get_class($this), $wrapper, $setting_name, $setting_value), JLog::INFO, 'rokupdater');
                 stream_context_set_option($context, $wrapper, $setting_name, $setting_value);
             }
         }
     }
     // Open the stream for reading.
     $stream = fopen($uri->getAbsoluteUri(), 'r', false, $context);
     // Get the contents from the stream.
     $content = stream_get_contents($stream);
     JLog::add(sprintf('%s : Response is : %s', get_class($this), $content), JLog::DEBUG, 'rokupdater');
     // Close the stream.
     fclose($stream);
     return $content;
 }
Esempio n. 2
0
 /**
  * wrapper to send a request to the server and return a string with the response body.
  *
  * @param   RokUpdater_Uri $uri        The URI to the resource to request.
  * @param   string         $data       Either an associative array or a string to be sent with the request.
  * @param   array          $headers    An array of request headers to send with the request.
  * @param   integer        $timeout    Read timeout in seconds.
  * @param   string         $userAgent  The optional user agent string to send with the request.
  *
  * @throws ProtocolBuffers_Exception
  * @throws Exception
  * @return string
  */
 public function request(RokUpdater_Uri $uri, $data = null, array $headers = array(), $timeout = null, $userAgent = null)
 {
     JLog::add(sprintf('%s request called for %s', get_class($this), $uri->getAbsoluteUri()), JLog::DEBUG, 'rokupdater');
     JLog::add(sprintf('%s request data passed %s', get_class($this), $data), JLog::DEBUG, 'rokupdater');
     if ($uri->getScheme() == self::HTTPS_SCHEME && !$this->isSSLSupported() && array_key_exists('http_fallback', $this->options)) {
         JLog::add(sprintf('%s : SSL is not supported in cURL and fallback is set', get_class($this)), JLog::DEBUG, 'rokupdater');
         if ($this->options['http_fallback']) {
             $uri->setScheme(self::HTTP_SCHEME);
             if ((int) $uri->getPort() == self::HTTPS_PORT) {
                 $uri->setPort(self::HTTP_PORT);
             }
         } else {
             JLog::add('SSL is not supported in cURL and fallback not set', JLog::INFO, 'rokupdater');
             throw new ProtocolBuffers_Exception(sprintf('%s Transport implmentation does not support SSL', get_class($this)), ProtocolBuffers_TransportFactory::PROTOCOL_BUFFERS_ERROR_SSL_STREAM_NOT_REGISTERED);
         }
     }
     JLog::add(sprintf('%s sending request to %s', get_class($this), $uri->getAbsoluteUri()), JLog::INFO, 'rokupdater');
     return $this->transportRequest($uri, $data, $headers, $timeout, $userAgent);
 }
Esempio n. 3
0
 /**
  * @param  RokUpdater_Uri   $uri
  * @param  array $components
  * @param  bool  $esc
  *
  * @return string
  */
 private static function getAbsoluteComponents(RokUpdater_Uri $uri, $components, $esc)
 {
     $result = '';
     if ($components & RokUpdater_Uri_Components::SCHEME) {
         $cmp = $uri->getScheme();
         if (!empty($cmp)) {
             $result .= "{$cmp}:";
         }
     }
     // AUTHORITY_START:
     if ($components & RokUpdater_Uri_Components::AUTHORITY_START) {
         $result .= "//";
     }
     //USER_INFO:
     if ($components & RokUpdater_Uri_Components::USERINFO) {
         $cmp = $uri->getUserInfo();
         if (!empty($cmp)) {
             if ($esc && strpos($cmp, ':') !== false) {
                 $parts = explode(':', $cmp, 2);
                 $result .= rawurlencode($parts[0]) . ':' . rawurlencode($parts[1]);
             } else {
                 if ($esc) {
                     $result .= rawurlencode($cmp);
                 } else {
                     $result .= $cmp;
                 }
             }
             $result .= '@';
         }
     }
     //HOST:
     if ($components & RokUpdater_Uri_Components::HOST) {
         $cmp = $uri->getHost();
         if (!empty($cmp)) {
             $result .= $cmp;
         }
     }
     //PORT:
     if ($components & RokUpdater_Uri_Components::STRONG_PORT || $components & RokUpdater_Uri_Components::PORT) {
         $cmp = $uri->getPort();
         if (!($cmp === -1 || $components & RokUpdater_Uri_Components::PORT && $uri->isDefaultPort())) {
             $result .= ":{$cmp}";
         }
     }
     return $result;
 }
Esempio n. 4
0
 /**
  * Determine if the the passed url is external to the current running platform
  *
  * @param RokUpdater_Uri $uri
  *
  * @return mixed
  */
 public function isExternal($uri)
 {
     if (is_string($uri)) {
         $uri = new RokUpdater_Uri($uri);
     }
     if (@file_exists($uri->getComponents(RokUpdater_Uri_Components::PATH, RokUpdater_Uri_Builder::FORMAT_RAW))) {
         return false;
     }
     //if the url does not have a scheme must be internal
     if (is_null($uri->getHost())) {
         return false;
     }
     if ($uri->getHost() == $this->site_uri->getHost()) {
         if ($uri->getPort() === RokUpdater_Uri::PORT_UNDEFINED) {
             return false;
         }
         if ($uri->getPort() === $this->site_uri->getPort()) {
             return false;
         }
     }
     // if its a vfs url its a unit test and local
     if ($uri->getScheme() == 'vfs') {
         return false;
     }
     //the url has a host and it isn't internal
     return true;
 }