(Translated by https://www.hiragana.jp/)
GitHub - EZLippi/Tinyhttpd: Tinyhttpd 是J. David Blackstone在1999年写的一个不到 500 行的超轻量型 Http Server,用来学习非常不错,可以帮助我们真正理解服务器程序的本质。官网:http://tinyhttpd.sourceforge.net
Skip to content

Tinyhttpd J. David Blackstoneざい1999ねんうつしてき一个不到 500 ぎょうてきちょう轻量がた Http Server,ようらいがく非常ひじょう错,以帮すけわが们真せい理解りかいふく务器ほどじょてきほん质。かん网:http://tinyhttpd.sourceforge.net

License

Notifications You must be signed in to change notification settings

EZLippi/Tinyhttpd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A mirror for tinyhttpd(Tinyhttpdかんかた镜像,ForksourceForge,仅供がく习)

测试CGI时需要じゅようほんつくえあんそうPERL,どう时安そうperl-cgi

Prepare

Compile for Linux

 To compile for Linux:
  1) Comment out the #include <pthread.h> line.
  2) Comment out the line that defines the variable newthread.
  3) Comment out the two lines that run pthread_create().
  4) Uncomment the line that runs accept_request().
  5) Remove -lsocket from the Makefile.

     まい个函すうてき作用さよう

     accept_request:  处理从套せっじょう监听到てきいち个 HTTP 请求,ざい这里以很だい一部分地体现服务器处理请求流程。

     bad_request: かえしかい给客户端这是个错误请もとめ,HTTP じょう态吗 400 BAD REQUEST.

     cat: 读取ふく务器じょうぼう个文けんうつしいた socket 套接

     cannot_execute: 主要しゅよう处理发生ざい执行 cgi ほどじょ时出现的错误。

     error_die: 错误しんいきうつしいた perror 并退出たいしゅつ

     execute_cgi: 运行 cgi ほどじょてき处理,也是个主よう函数かんすう

     get_line: 读取套接てきいちぎょうかい车换ぎょうとうじょう况都统一为换行符结束。

     headers: HTTP 响应てき头部うつしいた套接

     not_found: 主要しゅよう处理找不いた请求てきぶんけん时的じょう况。

     sever_file: 调用 cat ふく务器ぶんけんかえしかい给浏览器。

     startup: はつはじめ httpd ふく务,包括ほうかつ建立こんりゅう套接,绑定はしこう,进行监听とう

     unimplemented: かえしかい给浏览器表明ひょうめいおさむいたてき HTTP 请求所用しょようてき method 支持しじ


     たて议源码阅读顺じょ: main -> startup -> accept_request -> execute_cgi, つう晓主よう工作こうさくりゅうほどきさきさい细把ごと个函すうてきげん码看いち


     工作こうさくりゅうほど

     (1) ふく务器启动,ざい指定していはしこうあるずいつくえ选取はしこう绑定 httpd ふく务。

     (2)おさむいたいち个 HTTP 请求时(其实就是 listen てきはしこう accpet てき时候),派生はせい一个线程运行 accept_request 函数かんすう

     (3)取出とりで HTTP 请求ちゅうてき method (GET ある POST) かず url,。对于 GET 方法ほうほう,如果ゆう携带さんすう,则 query_string ゆび指向しこう url ちゅうきさきめんてき GET さんすう

     (4) 格式かくしき url いた path すう组,表示ひょうじ浏览请求てきふく务器ぶんけんみちざい tinyhttpd ちゅうふく务器ぶんけんざい htdocs ぶんけん夹下。とう url 以 / 结尾,ある url 个目录,则默认在 path ちゅうじょう index.html,表示ひょうじ访问ぬし页。

     (5)如果ぶんけんみち合法ごうほう,对于无参すうてき GET 请求,直接ちょくせつ输出ふく务器ぶんけんいた浏览そくよう HTTP 格式かくしきうつしいた套接じょうとべいた(10)。其他じょう况(带参すう GET,POST 方式ほうしき,url 为可执行文こうぶんけん),则调よう excute_cgi 函数かんすう执行 cgi 脚本きゃくほん

    (6)读取せい个 HTTP 请求并丢弃,如果 POST 则找 Content-Length. HTTP 200  じょう态码うつしいた套接

    (7) 建立こんりゅう两个かんどう,cgi_input cgi_output, 并 fork いち个进ほど

    (8) ざい进程ちゅう STDOUT 重定しげさだこういた cgi_outputt てきうつしにゅうはし STDIN 重定しげさだこういた cgi_input てき读取はし,关闭 cgi_input てきうつしにゅうはし cgi_output てき读取はし,设置 request_method てき环境变量,GET てき话设おけ query_string てき环境变量,POST てき话设おけ content_length てき环境变量,这些环境变量为了给 cgi 脚本きゃくほん调用,接着せっちゃくよう execl 运行 cgi ほどじょ

    (9) ざいちち进程ちゅう,关闭 cgi_input てき读取はし cgi_output てきうつしにゅうはし,如果 POST てき话, POST すうすえうつしにゅう cgi_input,やめ重定しげさだこういた STDIN,读取 cgi_output てきかんどう输出いたきゃく户端,该管どう输入 STDOUT。接着せっちゃく关闭所有しょゆうかんどうとう待子まちこ进程结束。这一部分ぶぶん较乱,见下图说あきら



图 1    かんどうはつはじめじょう


图 2  かんどうさい终状态 


    (10) 关闭与浏览てき连接,完成かんせいりょういち HTTP 请求あずかかい应,いん为 HTTP 无连せってき


以下いか内容ないようみなもと作者さくしゃ:

This software is copyright 1999 by J. David Blackstone. Permission is granted to redistribute and modify this software under the terms of the GNU General Public License, available at http://www.gnu.org/ .

If you use this software or examine the code, I would appreciate knowing and would be overjoyed to hear about it at jdavidb@sourceforge.net .

This software is not production quality. It comes with no warranty of any kind, not even an implied warranty of fitness for a particular purpose. I am not responsible for the damage that will likely result if you use this software on your computer system.

I wrote this webserver for an assignment in my networking class in 1999. We were told that at a bare minimum the server had to serve pages, and told that we would get extra credit for doing "extras." Perl had introduced me to a whole lot of UNIX functionality (I learned sockets and fork from Perl!), and O'Reilly's lion book on UNIX system calls plus O'Reilly's books on CGI and writing web clients in Perl got me thinking and I realized I could make my webserver support CGI with little trouble.

Now, if you're a member of the Apache core group, you might not be impressed. But my professor was blown over. Try the color.cgi sample script and type in "chartreuse." Made me seem smarter than I am, at any rate. :)

Apache it's not. But I do hope that this program is a good educational tool for those interested in http/socket programming, as well as UNIX system calls. (There's some textbook uses of pipes, environment variables, forks, and so on.)

One last thing: if you look at my webserver or (are you out of mind?!?) use it, I would just be overjoyed to hear about it. Please email me. I probably won't really be releasing major updates, but if I help you learn something, I'd love to know!

Happy hacking!

                               J. David Blackstone

About

Tinyhttpd J. David Blackstoneざい1999ねんうつしてき一个不到 500 ぎょうてきちょう轻量がた Http Server,ようらいがく非常ひじょう错,以帮すけわが们真せい理解りかいふく务器ほどじょてきほん质。かん网:http://tinyhttpd.sourceforge.net

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published