<?
// 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;
}
?>
제목 | 날짜 | ||
---|---|---|---|
107 | [Nginx] Nginx 모니터링 툴 | 400 | 2017/10/23 |
106 | [MySQL] MySQL/MariaDB 비밀번호 변경 | 611 | 2017/10/26 |
105 | [Nginx] : /bin/bash^M: bad interpreter: No such file or directory | 63 | 2017/10/28 |
104 | [리눅스서버] 계정등록(adduser) 기능 및 옵션, 계정 변경(usermod) | 325 | 2017/10/28 |
103 | [Nginx] SSL Lets Encrypt certbot | 490 | 2017/11/01 |
102 | [Nginx] certbot 수동 발급 | 0 | 2017/11/02 |
101 | [Nginx] 리노드 서버 다운그레이드 | 0 | 2017/11/08 |
100 | [웹 제작] 모바일에서 쇼핑몰이나 홈페이지 들어가면 자동적으로 바탕화면에 즐찾 생성 되는 방법 | 0 | 2017/12/02 |
99 | [사이트관리] SEO 작업 | 328 | 2017/12/04 |
98 | [GitHub] develop 을 git pull 하기 | 0 | 2017/12/05 |
97 | [텔넷] 터미널에서 ssh 접속하기 | 275 | 2017/12/12 |
96 | HTTrack Website Copier 한글 언어팩 | 603 | 2018/01/03 |