thttpd是一个非常小巧的轻量级web server,它非常简单,仅仅提供了HTTP/1.1和简单的CGI支持,在其官方网站上有一个与其他web server(如Apache, Zeus等)的对比图+Benchmark,可以参考。此外,thttpd 也类似于lighttpd,对于并发请求不使用fork()来派生子进程处理,而是采用多路复用(Multiplex)技术来实现。因此效能很好。
1 编写 web 配置文件 thttpd.conf
主要设置:
dir - www内容的目录
logfile - 日志文件
port - 访问端口
charset - 网页字符集
cgipat - cgi程序访问的url匹配模式
vi /etc/thttpd.conf
# This section overrides defaults
dir=/usr/local/thttpd/www
#chroot
user=root
# default = nobody
logfile=/var/log/thttpd.log
pidfile=/var/run/thttpd.pid
port=80
charset=utf-8
cgipat=/cgi-bin/*
# This section _documents_ defaults in effect
# nosymlink# default = !chroot
# novhost
# nocgipat
# nothrottles
# host=0.0.0.0
2 启动 thttpd
启动脚本 start.sh
#!/bin/sh
thttpd -D -C /etc/thttpd.conf &
停止脚本 thttpd
stop.sh
#!/bin/sh
pkill -9 thttpd
3 开发 cgi 程序
例:生成 cgi程序 test
以下样例,分别实现一个 post 访问,和一个 get 访问。
3.1 post 和 get 的区别
- GET在浏览器回退时是无害的,而POST会再次提交请求。
- GET产生的URL地址可以被Bookmark,而POST不可以。
- GET请求会被浏览器主动cache,而POST不会,除非手动设置。
- GET请求只能进行url编码,而POST支持多种编码方式。
- GET请求参数会被完整保留在浏览器历史记录里,而POST中的参数不会被保留。
- 对参数的数据类型,GET只接受ASCII字符,而POST没有限制。
- GET比POST更不安全,因为参数直接暴露在URL上,所以不能用来传递敏感信息。
- GET参数通过URL传递,POST放在Request body中。
- 有些浏览器对 URL长度有限制,例如IE限制2083字节,那么get方式传递参数不能太长,POST则不受此限制。
- get请求,在服务端使用 QUERY_STRING 变量取出参数,而post请求使用 gets() 函数取出参数。
3.2 源码 test.c 样例
#include <stdlib.h>
int main(int argc, char *argv[])
{
char szStr[512];
char *p ;
strcpy(szStr, "");
printf("Content-type:text/html\n\n");
printf("<html>\n");
printf("<head><title>An html page from a cgi</title></head>\n");
printf("<body>\n");
printf("<h1>Board.Info</h1>\n");
p = getenv("QUERY_STRING");
if(p) {
sprintf(szStr, "get: %s",getenv("QUERY_STRING")); //打印get获取的信息
printf("%s", szStr);
}
else
{
gets(szStr);
if(strcmp(szStr,"")!=0)
printf("post: %s",szStr); //打印post获取的信息
}
printf("</body>\n");
printf("</html>\n");
fflush(stdout);
return 0;
}
3.3 交叉编译
arm-linux-gnueabihf-gcc test.c -o test
将生成的 cgi 程序test发布到www web目录下。
4 准备 web 目录
/usr/local/thttpd/www
结构:
root@am335x:/usr/local/thttpd/www# ls -lh
drwxr-xr-x 2 root root 224 Jan 12 14:13 cgi-bin
-rw-r--r-- 1 root root 829 Jan 12 14:07 index.html
root@am335x:/usr/local/thttpd/www# ls -lh cgi-bin/
-rwxr-xr-x 1 root root 11.8K Jan 12 14:13 test
index.html 样例:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>HPD Home Page</title>
</head>
<body>
<h1>Hello HPD Visitor!</h1>
<p>We are coming from one world, welcome here!</p>
<form id="formpost" name="formpost" method="post" action="cgi-bin/test"> <!-- 这里调用 cgi 程序 test -->
<p>输入内容:
<input type="text" name="user" id="user" />
</p>
<p>
<input type="submit" name="submit post" id="submitpost" value="提交" />
</p>
</form>
<form id="formget" name="formget" method="get" action="cgi-bin/test">
<p>输入内容:
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit" name="submit get" id="submitget" value="提交" />
</p>
<form>
</body>
</html>
访问
浏览器打开服务器:
POST 提交,页面显示收到的参数:
post: user=demo+post&submit+post=%E6%8F%90%E4%BA%A4
GET 提交,页面显示收到的参数:
get: email=demo+get&submit+get=%E6%8F%90%E4%BA%A4