tiny server 淺析

tiny server 淺析

一路記錄動手實踐的過程

1 使用browser測試http

http://localhost:12345/cgi-bin/adder?17&39

Welcome to add.com: THE Internet addition portal.
The answer is: 17 + 39 = 56
Thanks for visiting!

2 使用curl方法測試http

嘗試兩種方法,加上雙引號成功!

root@noya-VirtualBox:/media# curl localhost:12345/cgi-bin/adder?10&20
[1] 3635
20: command not found

root@noya-VirtualBox:/media# curl "localhost:12345/cgi-bin/adder?10&20"
Welcome to add.com: THE Internet addition portal.
<p>The answer is: 10 + 20 = 30
<p>Thanks for visiting!
[1]+ Done curl localhost:12345/cgi-bin/adder?10
root@noya-VirtualBox:/media#

more curl progress

command is to added -v option

clip_image002
左邊對應http事務,右邊是實踐

3 code 淺析

rio_readinitb(&rio,fd) :將程序的內部緩存區與描述符相關聯。
rio_readlineb(&rio,buf,MAXLINE) :從內部緩存區讀出一個文本行至buf中,以null字符來結束這個文本行。當然,每行最大的字符數量不能超過MAXLINE。
sscanf(buf,"%s %s %s",method,uri,version) :作爲例子,一般此時buf中存放的是“GET / HTTP/1.1”,所以可知method爲“GET”,uri爲“/”,version爲“HTTP/1.1”。其中sscanf的功能:把buf中的字符串以空格爲分隔符分別傳送到method、uri及version中。
strcasecmp(method,"GET") :忽略大小寫比較method與“GET”的大小,相等的話返回0。
read_requesthdrs(&rio) :讀並忽略請求報頭。
parse_uri(uri,filename,cgiargs) :解析uri,得文件名存入filename中,參數存入cgiargs中。
stat(filename,&sbuf) :將文件filename中的各個元數據填寫進sbuf中,如果找不到文件返回0。
S_ISREG(sbuf,st_mode) :此文件爲普通文件。
S_IRUSR & sbuf.st_mode :有讀取權限。
serve_static(fd,filename,sbuf.st_size) :提供靜態服務。
serve_dynamic(fd,filename,cgiargs) :提供動態服務。

line buffer

  • init line buffer
  • read line buffer

代碼待深入

一個狀態機 貫穿服務器

參考

CSAPP Tiny web 服務器源碼分析及搭建運行

環境搭建

CSAPP-Tiny Web服務器【2】源碼解析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章