includes/http.php
changeset 1224 a54e149f4a78
parent 1135 56c0abbdad3e
child 1227 bdac73ed481e
equal deleted inserted replaced
1223:7dca925b0209 1224:a54e149f4a78
   186    */
   186    */
   187   
   187   
   188   var $socket = false;
   188   var $socket = false;
   189   
   189   
   190   /**
   190   /**
       
   191    * True if SSL is on, defaults to false
       
   192    * @var bool
       
   193    */
       
   194   
       
   195   var $ssl = false;
       
   196   
       
   197   /**
   191    * The state of our request. 0 means it hasn't been made yet. 1 means the socket is open, 2 means the socket is open and the request has been written, 3 means the headers have been fetched, and 4 means the request is completed.
   198    * The state of our request. 0 means it hasn't been made yet. 1 means the socket is open, 2 means the socket is open and the request has been written, 3 means the headers have been fetched, and 4 means the request is completed.
   192    * @var int
   199    * @var int
   193    */
   200    */
   194   
   201   
   195   var $state = 0;
   202   var $state = 0;
   198    * Constructor.
   205    * Constructor.
   199    * @param string Hostname to send to
   206    * @param string Hostname to send to
   200    * @param string URI (/index.php)
   207    * @param string URI (/index.php)
   201    * @param string Request method - GET or POST.
   208    * @param string Request method - GET or POST.
   202    * @param int Optional. The port to open the request on. Defaults to 80.
   209    * @param int Optional. The port to open the request on. Defaults to 80.
   203    */
   210    * @param bool If true, uses SSL (and defaults the port to 443)
   204   
   211    */
   205   function Request_HTTP($host, $uri, $method = 'GET', $port = 80)
   212   
       
   213   function Request_HTTP($host, $uri, $method = 'GET', $port = 'default', $ssl = false)
   206   {
   214   {
   207     if ( !preg_match('/^(?:(([a-z0-9-]+\.)*?)([a-z0-9-]+)|\[[a-f0-9:]+\])$/', $host) )
   215     if ( !preg_match('/^(?:(([a-z0-9-]+\.)*?)([a-z0-9-]+)|\[[a-f0-9:]+\])$/', $host) )
   208       throw new Exception(__CLASS__ . ': Invalid hostname');
   216       throw new Exception(__CLASS__ . ': Invalid hostname');
       
   217     if ( $ssl )
       
   218     {
       
   219       $this->ssl = true;
       
   220       $port = ( $port === 'default' ) ? 443 : $port;
       
   221     }
       
   222     else
       
   223     {
       
   224       $this->ssl = false;
       
   225       $port = ( $port === 'default' ) ? 80 : $port;
       
   226     }
   209     // Yes - this really does support IPv6 URLs!
   227     // Yes - this really does support IPv6 URLs!
   210     $this->host = $host;
   228     $this->host = $host;
   211     $this->uri = $uri;
   229     $this->uri = $uri;
   212     if ( is_int($port) && $port >= 1 && $port <= 65535 )
   230     if ( is_int($port) && $port >= 1 && $port <= 65535 )
   213       $this->port = $port;
   231       $this->port = $port;
   331    */
   349    */
   332   
   350   
   333   function _sock_open(&$connection)
   351   function _sock_open(&$connection)
   334   {
   352   {
   335     // Open connection
   353     // Open connection
   336     $connection = fsockopen($this->host, $this->port, $errno, $errstr);
   354     $ssl_prepend = ( $this->ssl ) ? 'ssl://' : '';
       
   355     $connection = fsockopen($ssl_prepend . $this->host, $this->port, $errno, $errstr);
   337     if ( !$connection )
   356     if ( !$connection )
   338       throw new Exception(__METHOD__ . ": Could not make connection"); // to {$this->host}:{$this->port}: error $errno: $errstr");
   357       throw new Exception(__METHOD__ . ": Could not make connection"); // to {$this->host}:{$this->port}: error $errno: $errstr");
   339     
   358     
   340     // 1 = socket open
   359     // 1 = socket open
   341     $this->state = 1;
   360     $this->state = 1;
   407       echo htmlspecialchars($this->response);
   426       echo htmlspecialchars($this->response);
   408       echo '</pre></div><hr />';
   427       echo '</pre></div><hr />';
   409     }
   428     }
   410     
   429     
   411     fclose($connection);
   430     fclose($connection);
       
   431     $this->state = 0;
   412   }
   432   }
   413   
   433   
   414   /**
   434   /**
   415    * Internal function to grab the response code and status string
   435    * Internal function to grab the response code and status string
   416    * @access string
   436    * @access string