使用《深入理解電腦群組成》書裡面的Web編程裡的代碼,瞭解HTTP和HTML和CGI的原理
**********************************************<br />TELNET<br />**********************************************<br />vadmin@vadmin:~$ telnet localhost 1000<br />Trying ::1...<br />Trying 127.0.0.1...<br />Connected to localhost.<br />Escape character is '^]'.<br />GET / HTTP/1.1</p><p>HTTP/1.0 200 OK<br />Server: Tiny Web Server<br />Content-length: 112<br />Content-type: text/html</p><p><html><br /><head><br /><title>Google</title><br /></head><br /><body><br /><p>Hi, <p>this is the <B>home.html</B> file.<br /></body><br /></html><br />Connection closed by foreign host.<br />vadmin@vadmin:~$ </p><p>*******************************************<br />SERVER<br />*******************************************<br />^C<br />root@vadmin:/home/vadmin/york/code/netp/tiny# ./tiny 1000<br />***buf:<br />GET / HTTP/1.1<br />end of buf***<br />***method:GET; uri:/; version:HTTP/1.1***<br />*****************************<br />***hdrs:</p><p>end of hdrs***<br />*****************************<br />uri:/; filename:./home.html; cgiargs:.<br />***HTTP response heders:<br />HTTP/1.0 200 OK<br />Server: Tiny Web Server<br />Content-length: 112<br />Content-type: text/html</p><p>end response***<br />***file:<br /><html><br /><head><br /><title>Google</title><br /></head><br /><body><br /><p>Hi, <p>this is the <B>home.html</B> file.<br /></body><br /></html><br />end of file***</p><p>
使用Telnet可以登入伺服器,並且,直接使用發送HTTP請求
GET / HTTP/1.1
後面是一個空行,說明HTTP request header結束
請求uri是/,方法為GET,請求home.html
伺服器,會響應HTTP response header
HTTP/1.0 200 OK
Server: Tiny Web Server
Content-length: 112
Content-type: text/html
最後一個空行表示HTTP response header結束,
先顯示版本,然後是狀態代碼200,代表成功OK
然後是一些其他的HTTP response header
最後空行代表HTTP response header結束
接下來是HTTP response body,即一個html檔案。
<html>
<head>
<title>Google</title>
</head>
<body>
<p>Hi, <p>this is the <B>home.html</B> file.
</body>
</html>
如果使用瀏覽器的話,就可以在網頁上顯示內容了,
比如在瀏覽器輸入
http://localhost:1000/
瀏覽器會向伺服器發送上面的
GET / HTTP/1.1
下面的就一樣了,最後,伺服器吧html檔案傳送過來,瀏覽器就把html解析了,然後顯示出來。
照這麼看,貌似自己都可以寫一個簡單的瀏覽器了,呵呵~~~~
當然,也可以請求一些圖片什麼的
比如
GET /logo.gif HTTP/1.1
當然得要求伺服器目錄下面有這個檔案,不然就會報錯
404,not find
還可以請求動態內容,比如
***********************************************<br />TELNET<br />***********************************************<br />vadmin@vadmin:~$ telnet localhost 1000<br />Trying ::1...<br />Trying 127.0.0.1...<br />Connected to localhost.<br />Escape character is '^]'.<br />GET /cgi-bin/adder?1&3 HTTP/1.1</p><p>HTTP/1.0 200 OK<br />Server: Tiny Web Server<br />Content-length: 105<br />Content-type: text/html</p><p>Welcome to add.com: THE Internet addition portal.<br /><p>The answer is: 1 + 3 = 4<br /><p>Thanks for visiting!<br />Connection closed by foreign host.<br />vadmin@vadmin:~$ </p><p>***********************************************<br />SERVER<br />***********************************************<br />root@vadmin:/home/vadmin/york/code/netp/tiny# ./tiny 1000<br />***buf:<br />GET /cgi-bin/adder?1&3 HTTP/1.1<br />end of buf***<br />***method:GET; uri:/cgi-bin/adder?1&3; version:HTTP/1.1***<br />*****************************<br />***hdrs:</p><p>end of hdrs***<br />*****************************<br />uri:/cgi-bin/adder; filename:./cgi-bin/adder; cgiargs:1&3.<br />***HTTP response header:<br />HTTP/1.0 200 OK<br />Server: Tiny Web Server<br />end of HTTP response header***<br />
GET /cgi-bin/adder?1&3 HTTP/1.1
伺服器會把參數讀取,並求出結果,通過HTTP發送過來html檔案。
參數格式為app?var1&var2