tinyhttpd是一个轻量级的Http服务器,加上注释才500行,代码量较小,适合Unix网络编程的初级学者,tinyhttpd包括了基本的socket编程所用到的函数。百度即可下载源码。
下载解压后,编辑Makefile文件:
httpd: httpd.c
gcc -W -Wall -lpthread -g -o httpd httpd.c
make编译httpd.c运行即可
在学习过程中看到网上的一个流程图写的比较详细,用来对整个服务器的总体流程能有一个清晰的认识。
1、源码中主要函数:
void accept_request(int);
void bad_request(int);
void cat(int, FILE *); //
void cannot_execute(int);
void error_die(const char *);
void execute_cgi(int, const char *, const char *, const char *);
int get_line(int, char *, int);
void headers(int, const char *);
void not_found(int);
void serve_file(int, const char *);
int startup(u_short *);
void unimplemented(int);
作用:
accept_request:处理从套接字上监听到的一个 HTTP 请求
bad_request:请求错误状态码400 – 返回错误请求
cat: 读取服务器文件内容,通过send发送到客户端
cannot_execute:请求错误状态码500 – 本源码中表示在创建、使用管道时发生错误
error_die:基于当前的 errno 值,在标准错误上产生一条错误消息
execute_cgi:运行cgi程序 – 动态cgi
get_line:读取套接字一行,将‘\r’和‘\r\n’等回车换行符统一用换行符结束
headers:将HTTP响应(200)的头部发送到客户端
not_found:求错误状态码404 — 未找到文件
serve_file:调用cat将服务器文件返回给客户端 – 静态cgi
startup:初始化http服务器,包括建立套接字socket、绑定bind、监听listen
unimplemented:请求错误状态码501
对几个主要函数分析:
2、main函数
int main(void)
{
int server_sock = -1;
u_short port = 0;
int client_sock = -1;
struct sockaddr_in client_name;
int client_name_len = sizeof(client_name);
pthread_t newthread;
server_sock = startup(&port);
printf("httpd running on port %d\n", port);
while (1)
{
client_sock = accept(server_sock,
(struct sockaddr *)&client_name,
&client_name_len);
if (client_sock == -1)
error_die("accept");
//响应客户端,但此方式为阻塞式,无法立即响应其他请求
if (pthread_create(&newthread , NULL, accept_request, &client_sock) != 0) //响应客户端,此方式为异步式,创建线程单独处理,可以快速响应其他请求
perror("pthread_create");
}
close(server_sock);
return(0);
}
可以看到main函数主要包括startup函数–>创建socket、bind、listen等,while循环里用pthread_create创建线程处理客户端请求。
3、accept_request函数
void accept_request(int client)
{
char buf[1024];
int numchars;
char method[255]; //GET或POST
char url[255]; //请求的文件路径,如GET /adder?123&333 url表示其中的 /adder
char path[512]; //文件的相对路径
size_t i, j;
struct stat st;
int cgi = 0; /* becomes true if server decides this is a CGI
* program */
char *query_string = NULL; //客户端用GET方式通过url发送过来的参数信息,如GET /adder?123&333 中的 123&333
numchars = get_line(client, buf, sizeof(buf));//读http 请求的第一行数据(request line),把请求方法存进 method 中
i = 0; j = 0;
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
{
method[i] = buf[j];
i++; j++;
}
method[i] = '\0';
//如果请求的方法不是 GET 或 POST 任意一个的话就直接发送 response 告诉客户端没实现该方法
if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) //忽略大小写的字符串比较
{
unimplemented(client);
return;
}
if (strcasecmp(method, "POST") == 0)//如果是 POST 方法就将 cgi 标志变量置1
cgi = 1;
i = 0;
while (ISspace(buf[j]) && (j < sizeof(buf))) //跳过所有的空白字符(空格)
j++;
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf))) //获得url
{
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
//如果这个请求是一个 GET 方法的话
if (strcasecmp(method, "GET") == 0)
{
//用一个指针指向 url
query_string = url;
//去遍历这个 url,跳过字符 ?前面的所有字符,如果遍历完毕也没找到字符 ?则退出循环
while ((*query_string != '?') && (*query_string != '\0'))
query_string++;
//退出循环后检查当前的字符是 ?还是字符串(url)的结尾
if (*query_string == '?')
{
//如果是 ? 的话,证明这个请求需要调用 cgi,将 cgi 标志变量置1
cgi = 1;
//从字符 ? 处把字符串 url 给分隔会两份
*query_string = '\0';
//使指针指向字符 ?后面的那个字符
query_string++;
}
}
//将前面分隔两份的前面那份字符串,拼接在字符串htdocs的后面之后就输出存储到数组 path 中。相当于现在 path 中存储着一个字符串
sprintf(path, "htdocs%s", url);
//如果 path 数组中的这个字符串的最后一个字符是以字符 / 结尾的话,就拼接上一个"index.html"的字符串。首页的意思
if (path[strlen(path) - 1] == '/')
strcat(path, "index.html");
//在系统上去查询该文件是否存在
if (stat(path, &st) == -1) {
//如果不存在,那把这次 http 的请求后续的内容(head 和 body)全部读完并忽略
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
//然后返回一个找不到文件的 response 给客户端
not_found(client);
}
else
{
//文件存在,那去跟常量S_IFMT相与,相与之后的值可以用来判断该文件是什么类型的
if ((st.st_mode & S_IFMT) == S_IFDIR) //如果这个文件是个目录,那就需要再在 path 后面拼接一个"/index.html"的字符串
strcat(path, "/index.html");
if ((st.st_mode & S_IXUSR) ||
(st.st_mode & S_IXGRP) ||
(st.st_mode & S_IXOTH) )
//如果这个文件是一个可执行文件,不论是属于用户/组/其他这三者类型的,就将 cgi 标志变量置一
cgi = 1;
if (!cgi)
serve_file(client, path); //静态CGI
else
execute_cgi(client, path, method, query_string); // 动态CGI
}
close(client);
}
accept_request函数的主要工作流程为:
(1)从http的请求中获取请求方式GET 或POST 、url。
(2)对应GET方式,如果有参数(?)将参数存放在query_string字符串中
(3)获取请求文件的路径。如果路径只有‘/’,将默认路径加到path上,访问index.html
(4)查看路径属性。如果文件类型为目录,则path加上index.html
(5)对于GET无参数的请求(静态页面的请求),执行serve_file函数
(6)对于动态请求,调用execute_cgi执行cgi脚本
4、serve_file函数
void serve_file(int client, const char *filename)
{
FILE *resource = NULL;
int numchars = 1;
char buf[1024];
//确保 buf 里面有东西,能进入下面的 while 循环
buf[0] = 'A'; buf[1] = '\0';
//循环作用是读取并忽略掉这个 http 请求后面的所有内容
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
//打开这个传进来的这个路径所指的文件
resource = fopen(filename, "r");
if (resource == NULL)
not_found(client);
else
{
//打开成功后,将这个文件的基本信息封装成 response 的头部(header)并返回
headers(client, filename);
//接着把这个文件的内容读出来作为 response 的 body 发送到客户端
cat(client, resource);
}
fclose(resource);
}
serve_file函数主要用来执行静态请求。
5、execute_cgi函数
void execute_cgi(int client, const char *path,
const char *method, const char *query_string)
{
char buf[1024];
int cgi_output[2];
int cgi_input[2];
pid_t pid;
int status;
int i;
char c;
int numchars = 1;
int content_length = -1;
//往 buf 中填东西以保证能进入下面的 while
buf[0] = 'A'; buf[1] = '\0';
//如果是 http 请求是 GET 方法的话读取并忽略请求剩下的内容
if (strcasecmp(method, "GET") == 0)
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
else /* POST */
{
//只有 POST 方法才继续读内容
numchars = get_line(client, buf, sizeof(buf));
//这个循环的目的是读出指示 body 长度大小的参数,并记录 body 的长度大小。其余的 header 里面的参数一律忽略
//注意这里只读完 header 的内容,body 的内容没有读
while ((numchars > 0) && strcmp("\n", buf))
{
buf[15] = '\0';
if (strcasecmp(buf, "Content-Length:") == 0)
content_length = atoi(&(buf[16])); //记录 body 的长度大小
numchars = get_line(client, buf, sizeof(buf));
}
//如果 http 请求的 header 没有指示 body 长度大小的参数,则报错返回
if (content_length == -1) {
bad_request(client);
return;
}
}
sprintf(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
//下面这里创建两个管道,用于两个进程间通信
if (pipe(cgi_output) < 0) {
cannot_execute(client);
return;
}
if (pipe(cgi_input) < 0) {
cannot_execute(client);
return;
}
//创建一个子进程
if ( (pid = fork()) < 0 ) {
cannot_execute(client);
return;
}
//子进程用来执行 cgi 脚本
if (pid == 0) /* child: CGI script */
{
char meth_env[255];
char query_env[255];
char length_env[255];
//dup2()包含<unistd.h>中,参读《TLPI》P97
//将子进程的输出由标准输出重定向到 cgi_ouput 的管道写端上
dup2(cgi_output[1], 1);
//将子进程的输出由标准输入重定向到 cgi_ouput 的管道读端上
dup2(cgi_input[0], 0);
//关闭 cgi_ouput 管道的读端与cgi_input 管道的写端
close(cgi_output[0]);
close(cgi_input[1]);
//构造一个环境变量
sprintf(meth_env, "REQUEST_METHOD=%s", method);
//putenv()包含于<stdlib.h>中,参读《TLPI》P128
//将这个环境变量加进子进程的运行环境中
putenv(meth_env);
//根据http 请求的不同方法,构造并存储不同的环境变量
if (strcasecmp(method, "GET") == 0) {
sprintf(query_env, "QUERY_STRING=%s", query_string);
putenv(query_env);
}
else { /* POST */
sprintf(length_env, "CONTENT_LENGTH=%d", content_length);
putenv(length_env);
}
//execl()包含于<unistd.h>中,参读《TLPI》P567
//最后将子进程替换成另一个进程并执行 cgi 脚本
execl(path, path, NULL);
exit(0);
} else { /* parent */
//父进程则关闭了 cgi_output管道的写端和 cgi_input 管道的读端
close(cgi_output[1]);
close(cgi_input[0]);
//如果是 POST 方法的话就继续读 body 的内容,并写到 cgi_input 管道里让子进程去读
if (strcasecmp(method, "POST") == 0)
for (i = 0; i < content_length; i++) {
recv(client, &c, 1, 0);
write(cgi_input[1], &c, 1);
}
//然后从 cgi_output 管道中读子进程的输出,并发送到客户端去
while (read(cgi_output[0], &c, 1) > 0)
send(client, &c, 1, 0);
//关闭管道
close(cgi_output[0]);
close(cgi_input[1]);
//等待子进程的退出
waitpid(pid, &status, 0);
}
}
execute_cgi函数的主要流程:
(1)首先判断method是GET还是POST。对于GET请求忽略;对于POST请求,找到Ccontent-Length。然后将HTTP 200 状态码写到客户端。
(2)创建2个管道:cgi_input和cgi_output– 用于两个进程间通信。并fork一个子进程。子进程用来执行cgi脚本。
(3)在子进程中,把STDOUT从定向到cgi_output的写入端,把STDIN重定向到cgi_input的读取端,关闭cgi_input的写入端和cgi_output的读取端,设置request_method的环境变量,GET方式设置query_stirng环境变量,POST方式设置content_length环境变量,这些环境变量都是为给cgi脚本调用,然后用execl运行cgi程序。
(4)在父进程中,关闭cgi_input的读取端和cgi_output的写入端。如果是 POST 方法的话就继续读 body 的内容,并写到 cgi_input 管道里让子进程去读。然后从 cgi_output 管道中读子进程的输出,并发送到客户端去。
说明:CGI是公共网关接口(Common Gateway Interface),是在CGI程序和Web服务器之间传递信息的规则,CGI运行Web服务器执行外部程序,并将把它们的输出发送给浏览器。这样就提供了动态交互的能力。
GET方式的CGI规范实现原理:
服务器通过URL获取传递的参数,传给CGI程序,设置环境变量QUERY_STIRNG,并将标准输出从定向到文件描述符,然后通过EXEC函数簇执行外部CGI程序。外部CGI程序获取QUERY_STIRNG并处理,处理完后输出结果。
POSR方式的CGI规范实现原理:
由于POST方式不是通过URL传递参数,所以与GET方式不一样。
POST方式获取浏览器发送过来的参数长度设置为环境变量CONTENT-LENGTH。并将参数重定向到CGI的标识输入,这主要通过pipe管道实现。CGI程序从标准输入读取 CONTENT-LENGTH个字符就获取了浏览器传送的参数,并将处理结果输出到标准输出。
6、错误处理函数– 以cannot_execute函数为例
void cannot_execute(int client)
{
char buf[1024];
sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-type: text/html\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<P>Error prohibited CGI execution.\r\n");
send(client, buf, strlen(buf), 0);
}
有此函数可看到发送到客户端的信息是一条一条的发送的。
差不多就这样了。
未用浏览器访问服务器的效果:
系统虚拟机CentOS 6.3
运行服务器:
访问服务器—GET方式: