<?
// 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;
}
?>
제목 | 날짜 | ||
---|---|---|---|
167 | [X61] 뱅킹에서 빨콩 날아다니는 증상 [1] | 9485 | 2008/12/10 |
166 | [하드웨어] 컴퓨터 관련 포럼 [1] | 9493 | 2009/09/23 |
165 | [윈도우] 마우스키 설정으로 컴퓨터 하기 | 9497 | 2013/07/27 |
164 | [하드웨어] USB가 고장났다. | 9512 | 2009/11/06 |
163 | [웹로그] MySQL 관련 팁 | 9515 | 2010/09/19 |
162 | [하드웨어] 잉크 토너 구매 | 9524 | 2010/03/08 |
161 | 비스타>계속 하려면 사용 권한이 필요합니다. | 9546 | 2008/10/02 |
160 | [미디어/녹음·재생] 프로디지 녹음하기 | 9550 | 2009/01/11 |
159 | [브라우저] 크롬을 좀 더 편리하게 사용할 수 있는 방법은 없을까? | 9567 | 2009/01/06 |
158 | [웹로그] mysql의 최대 성능 향상 방법 | 9612 | 2010/09/19 |
157 | [메신저] MSN 자동 로그인 해제하기 | 9623 | 2009/07/31 |
156 | [하드웨어] 어머니 노트북 구매하기 | 9626 | 2009/10/18 |