Tinyhttpd README

The project's README from github.com/EZLippi/Tinyhttpd. As one markdown file: README.md.

A mirror for tinyhttpd(Tinyhttpd非官方镜像,Fork自sourceForge,仅供学习)

测试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 [email protected] .

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

Tinyhttpd (EZLippi/Tinyhttpd)

Searching Database…

12.2K ·GitHub
这是什么?Tinyhttpd 是一个用 C 语言编写的极简 HTTP 服务器,最初由 J. David Blackstone 于 1999 年作为课程作业创建;本仓库是 SourceForge 项目的非官方镜像,仅供学习 HTTP 服务器在套接字层面如何工作。
这个东西是用来当正式的生产环境网页服务器的吗?不是。原作者明确提醒它并非生产级质量,也不提供任何担保;它是作为教学工具编写的,并不是用来和 Apache 竞争的。
我该怎么编译和运行它?克隆仓库,按照 README 中 'Prepare / Compile for Linux' 的步骤在 Linux 下编译,然后运行生成的可执行文件来启动服务器。
想试试 CGI 功能,除了 C 编译器还需要别的东西吗?需要。要测试 CGI,你需要在本地安装 Perl 和 perl-cgi 包,因为附带的示例脚本 color.cgi 是一个 Perl CGI 程序。
服务器会在多次请求之间保持连接吗?不会。服务器每处理完一个 HTTP 请求,就直接关闭与浏览器之间的连接,把 HTTP 当作无连接协议来处理,不支持持久连接或 keep-alive 会话。
我能在 Windows 上编译它吗,还是只能用于 Linux?README 只记录了 Linux 下的编译途径和 Makefile,没有 Windows 相关说明,因此应将其视为仅支持 Linux。
CGI 请求在底层是怎么处理的?它会 fork 出一个子进程,把 STDIN/STDOUT 重定向到与客户端套接字相连的管道,设置 REQUESTMETHOD、QUERYSTRING 等环境变量,然后 exec 执行 CGI 脚本,并把输出流式返回给浏览器。
每个进来的请求都有自己的线程吗?是的。startup() 负责创建监听套接字并绑定端口;每个被接受的 HTTP 请求都会启动一个新线程,运行 acceptrequest 来处理它。
阅读源码时,按什么顺序比较好?README 建议先跟踪 main -> startup -> acceptrequest -> executecgi,以了解整体流程,然后再阅读 getline、headers、cat、notfound 等较小的辅助函数。
作者当初为什么要写这个?J. David Blackstone 于 1999 年为一门大学网络课的作业而编写,该作业只要求能提供静态页面;他自己额外加入了 CGI 支持,以争取额外学分。
What are similar projects?h2o, TinyWebServer, caddy, newbee-mall, selfdb, goproxy, nginx, lytlnyblOS
Creator
EZLippi
Size
<0.1 MB
Stars
12.2K
Updated
CommitJan 20
Age
11y
Commits · 90 Days