fclose($socket); function buildRequest($conn) { $request = new\stdClass(); $requestCommand = t 「 im(fgets($conn)); list ($request → method, $ 「 equest → path. $ 「 equest → ve 「 sion) = explode(). $ 「 equestCommand); while (($char = fgetc($conn)) ! = = false) { $line . = $cha 「 ; fo 「 each (explode("\n". $line) as $heade 「 ) { $label = strtouppe 「 (strt 「 ($tmp[l]. ・ $ 「 equest → $label = t 「 im($tmp[2]); if (isset($ 「 equest → CONTENT_LENGTH)) { $ 「 equest → body = f 「 ead($conn, intval($ 「 equst- >CONTENT_LENGTH)); b 「 eak; 「 etu 「 n $ 「 equest; 「これ、パイプの部分が良く分からないけど、なんとなく拡張子が " php " なら プログラムを起動するっていうところが、主な変更点だよね ? 」 「その通りだにや。パイプについては、 Linux の仕組の基本だから、そのうち こでは「プログラムとプログラムの間を通信する仕組」 説明するにや。一旦、 2 7
>path); fclose($conn); fwrite($conn. "HTTP/I.1 404 N0tFound\r\n"); } else { fw 「 ite($conn, $content); fwrite($conn, "\r\n"); fw 「 ite($conn, "Content-Type: text/html\r\n"); と、 png を返せるようになる 〃 HTML 固定にするよ。拡張子とかで判別して img/png とかにする fwrite($conn. "Content-Length:" . strlen($content) . "\ 「 \n"); fwrite($conn. "HTTP/I.1 200 OK\ 「 \n"); if ($content) { $content = file_get_contents($path); if ($ 「 equest → method = = ・ GET' & & is—file($path)) { 〃 GET で来ていて、ファイルが有ったら読む 2 0 ン explode( ・・ , $ 「 equestCommand); list ($ 「 equest → meth0d. $ 「 equest → path. $ 「 equest → ve 「 sion) = 〃スペース区切りでメソッド [GETIPOSTIDELETEIPUT] 、パス、パージョ $requestCommand = t 「 im(fgets($conn)); / / 1 行だけ読む、 1 行目はリクエスト内容そのもの $request = new\stdClass(); function buildRequest($conn) { fclose($socket);
「え ? bot ってプログラムでしょ ? プログラムが猫耳ショタ ? えっ ? どういう メカニズム ? 」 説明を受けたら、より一層混乱をしていると部屋の隅に、派手で悪趣味な格 好をした猫耳が座って芋焼酎を飲んでいた。 「いちいち、うるせえなー。 bot が猫耳ショタになるくらい、よく有ることだろ。 そんなことイチイチ気にしてるんじゃねえよ」「え ? 二人目、プログラムって 猫耳ショタになるものなの ? えっ ? どういうメカニズム ? 」 「そっちは、おだいった。そっちは元々はモバファクがやっていた wassr って いうサービスで、大喜利のお題を出すための bot だったんだけど。今は止まっ ているし、このパソコン借りて呼び出しておいたにや。よろしくにや。ところ で、昨日の Web サーバーの続きなんだけど。 CGI が動くようにしてみたにや」 $ 「 equest = buildRequest($conn); $buffers = while ($conn = stream socket_accept($socket)) { } else { ech0 "$errstr ( $ e 「「 no ) く b 「 / > \ n " ・ if (!$socket) { $er 「 str); $socket = st 「 eam_socket se 「 ve 「 ( " tcp : / / 0.0.0.0 : 8080 " く ?php php $content = null; $path = >path); 引 R 2 5 if (p 「 eg-match( ・ #\. php# ・ . $path)) { if ($request → method = = ・ GET ・ & & is_file($path)) { putenv( ・ QUERY_STRING=• . $que 「 Y[1ル 〃ファイル名の " ? " よりあとがクエリストリングだよ $ 「 equest-