<?
// last update : 2005.01.10.001
class http
{
var $debug = 0;
var $socket;
var $host;
var $port;
var $path;
var $method;
var $send_header;
var $send_parameter;
var $receive;
var $cookie;
function http($host, $port)
{
$this->host = $host;
$this->port = $port;
$this->open($host, $port);
$this->send_header["Accept"] = "*/*";
$this->send_header["Content-Type"] = "application/x-www-form-urlencoded";
$this->send_header["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
$this->send_header["Connection"] = "Keep-Alive";
$this->send_header["Cache-Control"] = "no-cache";
$this->send_header["Referer"] = "http://$host";
$this->send_header["Host"] = $host;
}
function getBody()
{
return $this->receive["Body"];
}
function open($host, $port)
{
$this->host = $host;
$this->port = $port;
$this->send_header["Referer"] = "http://$host";
$this->send_header["Host"] = $host;
return $this->socket = fsockopen($host, $port);
}
function close()
{
@fclose($this->socket);
$this->socket = "";
}
function setTarget($path)
{
$this->path = $path;
$this->method = "GET";
$this->send_header["Host"] = $this->host;
unset($this->send_header["Content-Length"]);
}
function setHeader($name, $value)
{
$this->send_header[$name] = $value;
}
function sendData()
{
while( true )
{
if( $this->socket )
{
$data = "$this->method $this->path HTTP/1.1rn";
foreach($this->send_header as $header => $value)
$data .= "$header: $valuern";
$data .= $this->getCookie() . "rn";
if( $this->method == "POST" )
$data .= $this->send_parameter;
fwrite($this->socket, $data);
if( $this->debug )
echo "<div style='text-align: justify; font-size: 9pt; font-family: 굴림; line-height: 150%; word-break: break-all; color: red'>".str_replace("rn", "<br>", $data)."</div>";
$this->readData();
break;
}
else if (!$this->open($this->host, $this->port))
break;
}
}
function readData()
{
unset($this->receive["Connection"]);
unset($this->receive["Content-Length"]);
unset($this->receive["Transfer-Encoding"]);
$this->receive["Status"] = "100";
$this->receive["Body"] = "";
$data = "";
while( $this->receive["Status"] == "100" )
{
while( true )
{
$data = fgets($this->socket, 1024);
if(strlen($data)==0)
return;
if( $data == "rn" )
break;
else if(substr($data, 0, 5) == "HTTP/")
$this->receive["Status"] = substr($data, 9, 3);
else
{
$pos = strpos($data, ":");
$name = substr($data, 0, $pos);
$value = substr($data, $pos+2);
if(strtoupper($name) == "SET-COOKIE")
$this->setCookie($value);
else
$this->receive[$name] = str_replace("rn", "", $value);
}
}
}
if( $this->receive["Content-Length"] )
{
$this->receive["Body"] = fread($this->socket, $this->receive["Content-Length"]);
while( strlen($this->receive["Body"]) < $this->receive["Content-Length"] )
$this->receive["Body"] .= fread($this->socket, $this->receive["Content-Length"]-strlen($this->receive["Body"]));
}
if( $this->receive["Transfer-Encoding"] == "chunked" )
{
while( true )
{
$buff = "";
$length = hexdec(fgets($this->socket, 1024));
if( $length == 0 )
break;
while( strlen($buff) < $length+2 )
$buff .= fread($this->socket, $length+2-strlen($buff));
$this->receive["Body"] .= substr($buff, 0, strlen($buff)-2);
}
}
if( $this->receive["Connection"] == "close" )
{
while( $buff = fgets($this->socket, 8192) )
$this->receive["Body"] .= $buff;
$this->close();
}
if( $this->debug )
$this->dumpAll();
}
function setCookie($value)
{
$buff = split("rn", $value);
foreach($buff as $value)
{
if($value != "")
{
$value = substr($value, 0, strpos($value, ";"));
$pos = strpos($value, "=");
$key = substr($value, 0, $pos);
$val = substr($value, $pos+1);
$this->cookie[$key] = $val;
}
}
}
function getCookie()
{
if( sizeof($this->cookie) == 0 )
return "";
$string = "Cookie: ";
foreach($this->cookie as $key => $value)
$string .= "$key=$value; ";
return substr($string, 0, strlen($string)-2)."rn";
}
function setPost($param)
{
$this->method = "POST";
$this->send_header["Content-Length"] = strlen($param);
$this->send_parameter = $param;
}
function dumpAll()
{
$data = "<div style='text-align: justify; margin-bottom: 30px; font-size: 9pt; font-family: 굴림; line-height: 150%; word-break: break-all'><fieldset><legend> 헤더 </legend>";
foreach($this->receive as $header => $value)
{
if( $header == "Body" )
$body = "<fieldset><legend> 본문 </legend>".htmlspecialchars($value)."</fieldset>";
else
$data .= "$header: ".htmlspecialchars($value)."<br>";
}
echo "$data</fieldset><fieldset><legend> 쿠키 </legend>";
if( $this->cookie )
foreach($this->cookie as $key => $value)
echo "<b>$key</b>: ".htmlspecialchars(urldecode($value))."<br>";
echo "</fieldset>$body</div>";
}
}
function extString($string, $prefix, $postfix, $sequence)
{
for($i=0; $i<$sequence; $i++)
{
if( ($pos = strpos($string, $prefix)) === false )
return "";
else
$string = substr($string, $pos+strlen($prefix));
}
if( $postfix == "" )
$string = substr($string, 0);
else
$string = substr($string, 0, strpos($string, $postfix));
return $string;
}
?>