くんすとの備忘録

IT系技術メモ

移転しました。

15秒後に自動的にリダイレクトします。

inetdとシェルスクリプトでシンプルなwebサービスを作る実験②

前回の続きで、今回は、メソッド名とURLを受け取ってみる。

telnetでアクセスする場合、一旦アクセス後入力待ちになり、そこで「GET /」みたいな入力をするので、それを真似てみる。

とりあえず、頭に入力待ちを入れてみる

/home/test/hoge.sh

#!/bin/sh
read L

cat <<++EOS
HTTP/1.0 200 OK
Content-type: text/html

<html>
<head><title>test page</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>
++EOS
telnetからアクセス
% telnet 192.168.29.21 80
Trying 192.168.29.21...
Connected to 192.168.29.21.
Escape character is '^]'.

HTTP/1.0 200 OK
Content-type: text/html

<html>
<head><title>test page</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Connection closed by foreign host.

1回入力待ちになった!


ブラウザからアクセス

URL:http://192.168.29.21/aaa/bbb/ccc

<html>
<head><title>test page</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>

特にかわりなし。


入力待ちのときの引数を表示してみる

/home/test/hoge.sh

#!/bin/sh
read L

cat <<++EOS
HTTP/1.0 200 OK
Content-type: text/html

<html>
<head><title>test page</title></head>
<body>
<h1>Hello World!</h1>
<p>params:$L</p>
</body>
</html>
++EOS
telnetからアクセス

「GET /aaa/bbb/ccc」を入力

% telnet 192.168.29.21 80
Trying 192.168.29.21...
Connected to 192.168.29.21.
Escape character is '^]'.
GET /aaa/bbb/ccc
HTTP/1.0 200 OK
Content-type: text/html

<html>
<head><title>test page</title></head>
<body>
<h1>Hello World!</h1>
</p>arams:GET /aaa/bbb/ccc
</body>
</html>
Connection closed by foreign host.

表示がおかしくなってる?でも「GET /aaa/bbb/ccc」は取れている模様。

ブラウザからアクセス

URL:http://192.168.29.21/aaa/bbb/ccc

<html>
<head><title>test page</title></head>
<body>
<h1>Hello World!</h1>
<p>params:GET /aaa/bbb/ccc HTTP/1.1
</p>
</body>
</html>

「GET /aaa/bbb/ccc HTTP/1.1」が取れている!!

メソッド名とURLを取得できました。

なるほど、こーなってるのね・・・


※ちゃんとやろうと思うと、ちゃんとHTTPの仕様を勉強せねばなるまい。