<?
// 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;
}
?>
제목 | 날짜 | ||
---|---|---|---|
191 | [GitHub] github.io 페이지 만들기 | 157 | 2015/09/04 |
190 | [윈도우] .sit 파일 압축 풀기 | 1124 | 2015/09/06 |
189 | [윈도우] hiberfil | 160 | 2015/09/10 |
188 | [기타] pc에서 인스타 하기 | 420 | 2015/09/14 |
187 | [기타] 인스타그램 pc버전 인스타넷 | 1316 | 2015/09/14 |
186 | [윈도우] 윈도우 8.1 0x80070011 오류 | 458 | 2015/09/21 |
185 | [윈도우] .net Framework 3.5(닷넷프레임워크)이 깔리지 않을 때(0x80071a90)[Windows 8.1] | 1202 | 2015/09/22 |
184 | [Apm] [bitnami] 맥에서 APM 사용하기(비트나미 설치) | 289 | 2015/09/22 |
183 | [브라우저] 브라우저 중복 즐겨찾기 정리 프로그램 | 135 | 2015/10/06 |
182 | [웹 제작] Let's Encrypt 웹호스팅에 적용하기 | 0 | 2015/11/06 |
181 | [Apm] AutoSet(오토셋) vs ApmSetup7 | 294 | 2015/11/17 |
180 | [Apm] WT-NMP - portable Nginx Mysql Php development stack for Windows | 142 | 2015/11/17 |