/**
  * Test the isSSL method.
  *
  * @return  void
  *
  * @since   11.1
  * @covers  JUri::isSSL
  */
 public function testIsSSL()
 {
     $this->object->parse('https://*****:*****@www.example.com:80/path/file.html?var=value#fragment');
     $this->assertThat($this->object->isSSL(), $this->equalTo(true));
     $this->object->parse('http://*****:*****@www.example.com:80/path/file.html?var=value#fragment');
     $this->assertThat($this->object->isSSL(), $this->equalTo(false));
 }
Beispiel #2
0
 /**
  * Method to connect to a server and get the resource.
  *
  * @param   JUri   $uri  The URI to connect with.
  *
  * @return  mixed  Connection resource on success or boolean false on failure.
  *
  * @since   11.1
  */
 protected function _connect(JUri $uri)
 {
     // Initialize variables.
     $errno = null;
     $err = null;
     // Get the host from the uri.
     $host = $uri->isSSL() ? 'ssl://' . $uri->getHost() : $uri->getHost();
     // If the port is not explicitly set in the URI detect it.
     if (!$uri->getPort()) {
         $port = $uri->getScheme() == 'https' ? 443 : 80;
     } else {
         $port = $uri->getPort();
     }
     // Build the connection key for resource memory caching.
     $key = md5($host . $port);
     // If the connection already exists, use it.
     if (!empty($this->_connections[$key]) && is_resource($this->_connections[$key])) {
         // Make sure the connection has not timed out.
         $meta = stream_get_meta_data($this->_connections[$key]);
         if (!$meta['timed_out']) {
             return $this->_connections[$key];
         }
     }
     // Attempt to connect to the server.
     if ($this->_connections[$key] = fsockopen($host, $port, $errno, $err, $this->_timeout)) {
         stream_set_timeout($this->_connections[$key], $this->_timeout);
     }
     return $this->_connections[$key];
 }
Beispiel #3
0
 /**
  * Method to connect to a server and get the resource.
  *
  * @param   JUri     $uri      The URI to connect with.
  * @param   integer  $timeout  Read timeout in seconds.
  *
  * @return  resource  Socket connection resource.
  *
  * @since   11.3
  * @throws  RuntimeException
  */
 protected function connect(JUri $uri, $timeout = null)
 {
     // Initialize variables.
     $errno = null;
     $err = null;
     // Get the host from the uri.
     $host = $uri->isSSL() ? 'ssl://' . $uri->getHost() : $uri->getHost();
     // If the port is not explicitly set in the URI detect it.
     if (!$uri->getPort()) {
         $port = $uri->getScheme() == 'https' ? 443 : 80;
     } else {
         $port = $uri->getPort();
     }
     // Build the connection key for resource memory caching.
     $key = md5($host . $port);
     // If the connection already exists, use it.
     if (!empty($this->connections[$key]) && is_resource($this->connections[$key])) {
         // Connection reached EOF, cannot be used anymore
         $meta = stream_get_meta_data($this->connections[$key]);
         if ($meta['eof']) {
             if (!fclose($this->connections[$key])) {
                 throw new RuntimeException('Cannot close connection');
             }
         } elseif (!$meta['timed_out']) {
             return $this->connections[$key];
         }
     }
     if (!is_numeric($timeout)) {
         $timeout = ini_get("default_socket_timeout");
     }
     // Attempt to connect to the server.
     $connection = fsockopen($host, $port, $errno, $err, $timeout);
     if (!$connection) {
         throw new RuntimeException($err, $errno);
     }
     // Since the connection was successful let's store it in case we need to use it later.
     $this->connections[$key] = $connection;
     // If an explicit timeout is set, set it.
     if (isset($timeout)) {
         stream_set_timeout($this->connections[$key], (int) $timeout);
     }
     return $this->connections[$key];
 }
 /**
  * Method to connect to a server and get the resource.
  *
  * @param   JUri     $uri      The URI to connect with.
  * @param   integer  $timeout  Read timeout in seconds.
  *
  * @return  resource  Socket connection resource.
  *
  * @since   11.3
  * @throws  RuntimeException
  */
 protected function connect(JUri $uri, $timeout = null)
 {
     $errno = null;
     $err = null;
     // Get the host from the uri.
     $host = $uri->isSSL() ? 'ssl://' . $uri->getHost() : $uri->getHost();
     // If the port is not explicitly set in the URI detect it.
     if (!$uri->getPort()) {
         $port = $uri->getScheme() == 'https' ? 443 : 80;
     } else {
         $port = $uri->getPort();
     }
     // Build the connection key for resource memory caching.
     $key = md5($host . $port);
     // If the connection already exists, use it.
     if (!empty($this->connections[$key]) && is_resource($this->connections[$key])) {
         // Connection reached EOF, cannot be used anymore
         $meta = stream_get_meta_data($this->connections[$key]);
         if ($meta['eof']) {
             if (!fclose($this->connections[$key])) {
                 throw new RuntimeException('Cannot close connection');
             }
         } elseif (!$meta['timed_out']) {
             return $this->connections[$key];
         }
     }
     if (!is_numeric($timeout)) {
         $timeout = ini_get('default_socket_timeout');
     }
     // Capture PHP errors
     $php_errormsg = '';
     $track_errors = ini_get('track_errors');
     ini_set('track_errors', true);
     // PHP sends a warning if the uri does not exists; we silence it and throw an exception instead.
     // Attempt to connect to the server
     $connection = @fsockopen($host, $port, $errno, $err, $timeout);
     if (!$connection) {
         if (!$php_errormsg) {
             // Error but nothing from php? Create our own
             $php_errormsg = sprintf('Could not connect to resource: %s', $uri, $err, $errno);
         }
         // Restore error tracking to give control to the exception handler
         ini_set('track_errors', $track_errors);
         throw new RuntimeException($php_errormsg);
     }
     // Restore error tracking to what it was before.
     ini_set('track_errors', $track_errors);
     // Since the connection was successful let's store it in case we need to use it later.
     $this->connections[$key] = $connection;
     // If an explicit timeout is set, set it.
     if (isset($timeout)) {
         stream_set_timeout($this->connections[$key], (int) $timeout);
     }
     return $this->connections[$key];
 }
Beispiel #5
0
 /**
  * Method to connect to a server and get the resource.
  *
  * @param   JUri     $uri      The URI to connect with.
  * @param   integer  $timeout  Read timeout in seconds.
  *
  * @return  resource  Socket connection resource.
  *
  * @since   11.3
  * @throws  JMapExceptionRuntime
  */
 protected function connect(JUri $uri, $timeout = null)
 {
     // Initialize variables.
     $errno = null;
     $err = null;
     // Get the host from the uri.
     $host = $uri->isSSL() ? 'ssl://' . $uri->getHost() : $uri->getHost();
     // If the port is not explicitly set in the URI detect it.
     if (!$uri->getPort()) {
         $port = $uri->getScheme() == 'https' ? 443 : 80;
     } else {
         $port = $uri->getPort();
     }
     // Build the connection key for resource memory caching.
     $key = md5($host . $port);
     // If the connection already exists, use it.
     if (!empty($this->connections[$key]) && is_resource($this->connections[$key])) {
         // Make sure the connection has not timed out.
         $meta = stream_get_meta_data($this->connections[$key]);
         if (!$meta['timed_out']) {
             return $this->connections[$key];
         }
     }
     // Attempt to connect to the server.
     $connection = @fsockopen($host, $port, $errno, $err, $timeout);
     if (!$connection) {
         throw new JMapExceptionRuntime($err, 'error', $errno);
     }
     // Since the connection was successful let's store it in case we need to use it later.
     $this->connections[$key] = $connection;
     // If an explicit timeout is set, set it.
     if (isset($timeout)) {
         stream_set_timeout($this->connections[$key], (int) $timeout);
     }
     return $this->connections[$key];
 }