<?
// 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;
}
?>
제목 | 날짜 | ||
---|---|---|---|
635 | [미디어/녹음·재생] 동영상 WMV 인코딩 방법 | 9422 | 2007/09/04 |
634 | [생활] 고등학교 졸업증명서 인터넷으로 출력가능합니다(공인인증서 필요!!!!).~~ | 18823 | 2007/09/10 |
633 | [네트워크] FON FAQ [1] | 8319 | 2007/09/12 |
632 | [기타] 네이버 카페 자료 백업을 위한 프로그램 - 까네 2.0 (GPL 라이센스 버전) | 12009 | 2007/09/17 |
631 | 네이버 오픈API 책검색 | 6784 | 2007/09/17 |
630 | [웹로그] Play Street 에 참여할 의사가 있으신지요? | 5964 | 2007/10/02 |
629 | 구글 어플리케이션으로 자신이 소유한 도메인의 메일주소 사용하기 | 6097 | 2007/10/03 |
628 | [브라우저] 파이어폭스 팁입니다. | 12590 | 2007/10/04 |
627 | [하드웨어] AMD 939소켓 사용하시는 분 계신가요? | 7016 | 2007/10/04 |
626 | 개인도메인 활용하기 (작성 중) | 5396 | 2007/10/05 |
625 | [하드웨어] 컴퓨터 고장 뭐가 문제일까요? | 7095 | 2007/10/05 |
624 | [하드웨어] 아버지 컴퓨터 사양 | 7888 | 2007/10/07 |