<?php
    
include_once('idna_convert.class.php');
    
/**
    
        Crash course:
        =====================================================================
    
        require("http.php");
    
        $http = new http("http://www.google.com/");
        $http->setMethod("GET");
        $http->setVersion("1.1");
    
        $http->setHeader("User-Agent", $_SERVER['HTTP_USER_AGENT']);
    
        $http->send();
        
        print $http->getRequest();
        print $http->getRawReply();
    
     */

    
class http {

        var 
$url;

        var 
$headers;
        var 
$body;
        var 
$method;
        var 
$version;

        var 
$request;
        var 
$reply;

        var 
$scheme;
        var 
$host;
        var 
$port;
        var 
$user;
        var 
$pass;
        var 
$path;
        var 
$query;
        var 
$fragment;

        function 
http($u$autoheader 0) {
            global 
$port$path;

            
$url $u;

            if (!
$this->is_valid_url($url)) {
                die(
"Error. <code>$url</code> is not a valid URL.");
            }

            
extract(parse_url($url));

            
$IDN  = new idna_convert();
            
$host $IDN->encode($host);


            if (
$user && $pass) {
                
$this->setHeader("Authorization""Basic ".base64_encode($user.":".$pass));
            }

            if (
$query) {
                
$path .= "?$query";
            }

            
$this->setHeader("Host"$host);

            if (!
$autoheader) {
                
$this->setHeader("User-Agent""HTTP query tool/".date("Ymd"filemtime(__FILE__))." (http://mbn.dk/q/)");
                
$this->setHeader("From""http-query-tool@mbn.dk");
            }

            
$this->setHost($host);
            
$this->setPath($path $path "/");
            
$this->setPort($port $port 80);

        }

        function 
setHost($h) {
            global 
$host;
            
$host $h;
        }

        function 
getHost() {
            global 
$host;
            return 
$host;
        }

        function 
setPath($p) {
            global 
$path;
            
$path $p;
        }

        function 
setPort($p) {
            global 
$port;
            
$port $p;
        }

        function 
getPath() {
            global 
$path;
            return 
$path;
        }

        function 
getPort() {
            global 
$port;
            return 
$port;
        }

        function 
setMethod($m) {
            global 
$method;
            
$method $m;
        }

        function 
getMethod() {
            global 
$method;
            return 
$method;
        }

        function 
setVersion($v) {
            global 
$version;
            
$version $v;
        }

        function 
getVersion() {
            global 
$version;
            return 
$version;
        }

        function 
setHeader($h$v) {
            global 
$headers;
            
$headers[$h] = $v;
        }

        function 
setBody($b) {
            global 
$body;
            
$body $b;
        }

        function 
getBody() {
            global 
$body;
            return 
$body;
        }

        function 
getRequestHeaders() {
            global 
$headers;
            foreach(
$headers as $k=>$v) {
                
$str .= "$k$v\r\n";
            }
            return 
"$str\r\n";
        }

        function 
send() {
            global 
$reply$request;

            
$this->setHeader("Connection""close");
            
            
$request  $this->getMethod()." ".$this->getPath()." HTTP/".$this->getVersion()."\r\n";
            
$request .= $this->getRequestHeaders();
            
$request .= $this->getBody();

            
$fp = @fsockopen($this->getHost(), $this->getPort(), $errno$errstr);
            if (
$fp) {
                
fputs($fp$request);
                while (!
feof($fp)) {
                    
$reply .= fgets($fp128);
                }
                
fclose ($fp);
            }
            else {
                die(
"Unable to connect to: '".$this->getHost()."' $errstr");
            }
        }

        function 
getRequest() {
            global 
$request;
            return 
trim($request);
        }

        function 
getRawReply() {
            global 
$reply;
            return 
$reply;
        }

        function 
getReplyHeaders() {
            
$pos strpos($this->getRawReply(), "\r\n\r\n");
            return 
substr($this->getRawReply(), 0$pos);
        }

        function 
getReplyBody() {
            
$pos strpos($this->getRawReply(), "\r\n\r\n");
            return 
substr($this->getRawReply(), $pos+4);
        }

        function 
getReplyStatus($r "statuscode") {
            
$respons substr($this->getReplyHeaders(), 0strpos($this->getReplyHeaders(), "\r\n"));
            
preg_match("/(http)\/([0-9.]+) ([0-9]+) (.+)/i"$respons$regs);

            
$protocol   $regs[1];
            
$version    $regs[2];
            
$status     $regs[4];
            
$statuscode $regs[3];

            switch (
$r) {
            case 
"protocol":
                return 
$protocol;
                break;
            case 
"version":
                return 
$version;
                break;
            case 
"statuscode":
                return 
$statuscode;
                break;
            case 
"status":
                return 
$status;
                break;
            }
        }

        function 
getReplyHeaderArray() {
            
$headers preg_split("/\r\n/i"$this->getReplyHeaders());
            foreach(
$headers as $header) {
                if (
$num++) {
                    
$key strtolower(substr($header0strpos($header": ")));
                    
$val substr($headerstrpos($header": ")+2);
                    
$a[$key] = $val;
                }
            }
            return 
$a;
        }

        function 
getReplyHeader($h) {
            
$a $this->getReplyHeaderArray();
            return 
$a[strtolower($h)];
        }

        function 
getReplyContent() {
            if (
$this->getReplyHeader("Transfer-Encoding") == "chunked") {
                
/*
                 * *** MANGLER ***
                 */
                
return $this->getReplyBody();
            }
            else {
                return 
$this->getReplyBody();
            }
        }

        function 
is_valid_url($url) {
            return 
preg_match("/http:\/\/(.+)\.([a-z]{2,3})/i"$url);
        }

    }

?>