U E D R , A S I H C RSS

데블스캠프2006/월요일/연습문제/웹서버작성/변형진

HJ's WebServer 0.3

  • 는 물론고, , 멀미디 , 로드 모두 가능
  • PHP ( : html, htm, php)
  • 디렉리명 index.html, index.htm, index.php 로 가
  • 결 방법 , 된 디렉

To Do..

  • GET Method Query String를 다.
  • POST Method, COOKIE 면 더 다.

~php
<?

$index_file = array("index.html", "index.htm", "index.php");
$host = "IP  또는  ";
$port = 8080;


set_time_limit(0);

if(!function_exists("mime_content_type"))
{
	function mime_content_type($f)
	{
		return exec(trim("file -bi ".escapeshellarg($f)));
	}
}

//echo str_repeat(" ",300);

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $host, $port);
socket_setopt($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_listen($socket);

$i = 100;
while($i--)
{
	$client_socket = socket_accept($socket);
	$result = "";
	while(false!==($read = socket_read($client_socket, 100, PHP_NORMAL_READ)))
	{
		echo "> ".$read;
		if(!$read = trim($read)) $cnt++;
		else $cnt=0;
		if($cnt==3) break;
		flush();
		if(preg_match("/(GET|POST) (\/[^ \/]*) (HTTP\/[0-9]+.[0-9]+)/i", $read, $t))
		{
			$file = "/home/kesarr".$t[2];
			if(preg_match("/\/$/", $file))
			{
				foreach($index_file as $idxf)
				{
					if(is_readable($file.$idxf))
					{
						$file .= $idxf;
						break;
					}
				}
			}
			if(is_readable($file))
			{
				$size = @getimagesize($file);
				if($size['mime']) $mime = $size['mime'];
				else $mime = mime_content_type($file);
				$res[] = "HTTP/1.1 200 OK";
				$res[] = "Server: Kesarr's WebServer";
				$res[] = "Connection: close";
				$to_read = $file;
			}
			else
			{
				$res[] = "HTTP/1.1 404 Not Found";
				$res[] = "Server: Kesarr's WebServer";
				$res[] = "Content-Length: ".strlen("   다.");
				$res[] = "Connection: close";
				$res[] = "Content-Type: $size[mime];charset=UTF-8;";
				$res[] = "";
				$res[] = "   다.";
			}
		}
		if($read == "close")
		{
			break;
		}
		elseif($read == "a")
		{
			$result = $read;
		}
		elseif($read == "exit")
		{
			socket_close($client_socket);
			socket_close($socket);
			exit;
		}
	}
	if($res)
	{
		if($to_read)
		{
			if(preg_match("/\.(html|htm|php)$/", $to_read))
			{
				$buffer = shell_exec(trim("php ".escapeshellarg($to_read)));
				$res[] = "Content-Length: ".strlen($buffer);
				$res[] = "";
				$result = join("\r\n", $res);
				@socket_write($client_socket, $result);
				echo $result;
				@socket_send($client_socket, $buffer, strlen($buffer), 0);
				$buf = explode("\r\n\r\n", $buffer);
				echo $buf[0]."\r\n\r\nPHP Parse: ";
			}
			else
			{
				$res[] = "Content-Length: ".filesize($file);
				$res[] = "Content-Type: ".$mime;
				$res[] = "";
				$res[] = "";
				$result = join("\r\n", $res);
				@socket_write($client_socket, $result);
				echo $result;
				@socket_send($client_socket, $buffer, strlen($buffer), 0);
				if(preg_match("/^text\//", $mime))
				{
					$fp = fopen($to_read, "r");
					echo "ASCII Read: ";
				}
				else
				{
					$fp = fopen($to_read, "rb");
					echo "Binary-safe Read: ";
				}
				while(!feof($fp)){
					$buffer=fread($fp,128);
					@socket_send($client_socket, $buffer, strlen($buffer), 0);
				}
				fclose($fp);
			}
			echo $to_read;
		}
		else
		{
			$result = join("\r\n", $res);
			socket_write($client_socket, $result);
			echo $result;
		}
		echo "\r\n__EOF__\r\n\r\n";
		flush();
	}
	socket_close($client_socket);
	unset($res);
	unset($to_read);
	unset($result);
}

socket_close($socket);

?>

----
다 만들. ㅋㅋ 다. 로 먹고 라면 를 C나 Java로 바꾸는 게 것 같다. - eternalbleu
  • PHP로 로그래밍 겁다고나 ? 그런 느낌 PHP를 만, UI를 면 대부 로 같 만들 라고 . 그래 C++, Java, C# 고민다 C# 봤는데, C# 려면 Windows Vista가 것 같고, C/C++ 더 대 . 가 '대량 데 기반 검 '과 ' 기반 리'로 DB 데, DB 리는 단 RDBMS PHP 고, 문 리는 Perl 다른 다보 그런 . ㅋㅋ -
  • 내가 PHP 보고, JSP 나 Java 듯.. 나 JSP랑 Java DB 개발 보기는 PHP로 는게 가 게 느껴든. 그래 DB PHP Java 렇게 려고 는데 PHP가 Oracle 5.x 부 Jsp로 게 됐. 둘다 는 뭐 PHP로 나 Jsp로 같고, 다만 결 개발경및 Jsp 는 java 를 가다가 다는 . Jsp 면 Junit Unit 고, 또 Visual Studio 다는게 . 또 java 를 가다가 고.(를 들면 Lucene 같 바로 만든 ..). Eclipse 라는 강력 개발 Visual Studio 보다 더 개발 ..
    • 내가 무리 렇게 말 라고 고.. 다만 보길 바래. 내가 되는것 를 Jsp,Java 로 고 PHP로 .. 그리고 나를 는 것 데, 나는 고 배 는 능력 다고 . 그리고 검 다면 Lucene 갖고 놀봐. 그리고 보고 MemeHarvester 되고. - namsang
느껴는군 ㅋㅋ ㅡ_ㅡb 가 기가 가 공부를 끝까 나가는 것 듯. 들리면 결국 닌 그냥 딩만 다가 끝나 까. 는 공부만 만 말 것. 가능면 본 고, 관 가는 과 다고 . 대 로 만고, 노력 단련 가는 과 를 늘기는 겠다. (결론럼 놀말라는 기 ㅡㅡ;; 나 다 ㅋㅋ) - eternalbleu
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:02
Processing time 0.1060 sec