includes/http.php
changeset 894 83d180d87f3c
parent 847 28d5049803f1
child 952 d52dfa1f08da
equal deleted inserted replaced
893:b24601274cd2 894:83d180d87f3c
   197    */
   197    */
   198   
   198   
   199   function Request_HTTP($host, $uri, $method = 'GET', $port = 80)
   199   function Request_HTTP($host, $uri, $method = 'GET', $port = 80)
   200   {
   200   {
   201     if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) )
   201     if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) )
   202       die(__CLASS__ . ': Invalid hostname');
   202       throw new Exception(__CLASS__ . ': Invalid hostname');
   203     $this->host = $host;
   203     $this->host = $host;
   204     $this->uri = $uri;
   204     $this->uri = $uri;
   205     if ( is_int($port) && $port >= 1 && $port <= 65535 )
   205     if ( is_int($port) && $port >= 1 && $port <= 65535 )
   206       $this->port = $port;
   206       $this->port = $port;
   207     else
   207     else
   208       die(__CLASS__ . ': Invalid port');
   208       throw new Exception(__CLASS__ . ': Invalid port');
   209     $method = strtoupper($method);
   209     $method = strtoupper($method);
   210     if ( $method == 'GET' || $method == 'POST' )
   210     if ( $method == 'GET' || $method == 'POST' )
   211       $this->method = $method;
   211       $this->method = $method;
   212     else
   212     else
   213       die(__CLASS__ . ': Invalid request method');
   213       throw new Exception(__CLASS__ . ': Invalid request method');
   214       
   214       
   215     $newline = "\r\n";
   215     $newline = "\r\n";
   216     $php_ver = PHP_VERSION;
   216     $php_ver = PHP_VERSION;
   217     $server = ( isset($_SERVER['SERVER_SOFTWARE']) ) ? "Server: {$_SERVER['SERVER_SOFTWARE']}" : "CLI";
   217     $server = ( isset($_SERVER['SERVER_SOFTWARE']) ) ? "Server: {$_SERVER['SERVER_SOFTWARE']}" : "CLI";
   218     $this->add_header('User-Agent', "PHP/$php_ver ({$server}; automated bot request)");
   218     $this->add_header('User-Agent', "PHP/$php_ver ({$server}; automated bot request)");
   237     {
   237     {
   238       $this->cookies_out[$cookiename] = $cookievalue;
   238       $this->cookies_out[$cookiename] = $cookievalue;
   239     }
   239     }
   240     else
   240     else
   241     {
   241     {
   242       die(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   242       throw new Exception(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   243     }
   243     }
   244   }
   244   }
   245   
   245   
   246   /**
   246   /**
   247    * Sets one or more request header values.
   247    * Sets one or more request header values.
   262     {
   262     {
   263       $this->headers[$headername] = $headervalue;
   263       $this->headers[$headername] = $headervalue;
   264     }
   264     }
   265     else
   265     else
   266     {
   266     {
   267       die(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   267       throw new Exception(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   268     }
   268     }
   269   }
   269   }
   270   
   270   
   271   /**
   271   /**
   272    * Adds one or more values to be passed on GET.
   272    * Adds one or more values to be passed on GET.
   287     {
   287     {
   288       $this->parms_get[$getname] = $getvalue;
   288       $this->parms_get[$getname] = $getvalue;
   289     }
   289     }
   290     else
   290     else
   291     {
   291     {
   292       die(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   292       throw new Exception(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   293     }
   293     }
   294   }
   294   }
   295   
   295   
   296   /**
   296   /**
   297    * Adds one or more values to be passed on POST.
   297    * Adds one or more values to be passed on POST.
   312     {
   312     {
   313       $this->parms_post[$postname] = $postvalue;
   313       $this->parms_post[$postname] = $postvalue;
   314     }
   314     }
   315     else
   315     else
   316     {
   316     {
   317       die(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   317       throw new Exception(__CLASS__ . '::' . __METHOD__ . ': Invalid argument(s)');
   318     }
   318     }
   319   }
   319   }
   320   
   320   
   321   /**
   321   /**
   322    * Internal function to open up the socket.
   322    * Internal function to open up the socket.
   326   function _sock_open(&$connection)
   326   function _sock_open(&$connection)
   327   {
   327   {
   328     // Open connection
   328     // Open connection
   329     $connection = fsockopen($this->host, $this->port);
   329     $connection = fsockopen($this->host, $this->port);
   330     if ( !$connection )
   330     if ( !$connection )
   331       die(__CLASS__ . '::' . __METHOD__ . ': Could not make connection');
   331       throw new Exception(__CLASS__ . '::' . __METHOD__ . ': Could not make connection');
   332     
   332     
   333     // 1 = socket open
   333     // 1 = socket open
   334     $this->state = 1;
   334     $this->state = 1;
   335   }
   335   }
   336   
   336