
tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注解),附带一个简单的Client,可以借助阅读这段代码理解一个 Http Server 的本质。下载链接:
分析这段源码前,需要对网络协议,Linux编程,以及HTTP有必定的知道,这里假定你们对http有必定的知道,如果有时间,会额外介绍下Http。
HTTP协议详解
这里,我先让代码跑起来,再分析源码
注:下载的源码不能直接在Linux下运行,需要改动一些小地方,因为今天Linux以及支持pthread,不需要再根据注解给出的更改方式。
修改1
33行改为 void* accept_request(void *); 相应的该函数的谋求也要作出更改。
主要是为了适应pthread_create()函数参数。
void* accept_request(void *pclient)
{
int client = *(int*)pclient;
……
unimplemented(client);
return NULL;//r77
……
close(client);//r128
return NULL;//
}
修改2 438行和483行(源码436行和481行)的函数类型改为 socklen_t
修改3
(源码495行)改为
if (pthread_create(&newthread , NULL, accept_request,(void*)&client_sock) != 0)

修改4
修改makefile,
gcc -W -Wall -o httpd httpd.c -lpthread
修改5
如果上述修改后,不能运行,那就应该修改5了,
可能是htdocs文件下的index.html可能具备可执行权限了,
修改其权限,chmod 444 index.html
运行程序
tinyhttpd总共包括下面变量:
int startup(u_short *);//开启http服务,包括绑定端口,,开启线程处理链接
void* accept_request(void *pclient) ;//处理从套接字上到的一个 HTTP 请求
void execute_cgi(int, const char *, const char *, const char *);//运行cgi脚本,这个非常重要,涉及动态解析
//前三个是主要函数,后面的都是辅助函数
void bad_request(int);//返回给客户端这是个错误请求,400响应码
void cat(int, FILE *);//读取服务器上某个文件写到 socket 套接字
void cannot_execute(int);//处理发生在执行 cgi 程序时出现的错误
void error_die(const char *);//把错误信息写到 perror
int get_line(int, char *, int);//读取一行HTTP报文
void headers(int, const char *);//返回HTTP响应头
void not_found(int);//返回找不到请求文件
void serve_file(int, const char *);//调用 cat 把服务器文件内容返回给浏览器。
void unimplemented(int);//返回给浏览器表明收到的 HTTP 请求所用的 method 不被支持。
先把整体的模式理明白,再探讨源码。

工作流程:
1>服务器启动,在选定端口或随机选择端口绑定httpd服务。
2>收到一个http请求时(其实就是listen端口accept的之后),派生一个线程运行accept_request函数。
3>取出http请求中method(get或post)和url,对于get方式,如果有携带参数,则query_string指针指向url中?后面的get参数。
4>格式化url到path变量,表示浏览器请求的文件模式,在tinyhttpd中服务器文件是在htdocs文件夹下。当url以/结尾,或者url是个目录,则默认在path中加上index.thml,表示访问主页。
5>如果文件模式合法,对于无参数的get请求,直接输出服务器文件到浏览器,即用http格式写到套接字上,跳到(10)。其他情况(带参数get,post方式,url为科执行文件),则读取execute_cgi函数执行cgi脚本。
6>读取整个http请求并丢弃,如果是post则找出content-lengthpython s ixusr,把http状态码200写到套接字里面。
7>建立两个管道,cgi_input和cgi_output,并fork一个子进程。
8>在子进程中,把stdout重定向到cgi_output的写入端,把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数据读取到cgo_input,已被重定向到stdin读入cgi_output的管道输出到客户端,等待子进程结束。
10>关闭与浏览器的链接,完成一次http请求与回应,因为http是无连接的。
网上找的一张图,对比着看,

这样眼里一般就有一个思路,接下来分下代码
建议源码阅读顺序:main —> startup —> accept_request —> excute_cgi
先介绍几个中间辅助函数:
从文件描述符sock中python s ixusr,读取一行信息:get_line
//读取一行http报文,以\r 或\r\n为行结束符
//注:只是把\r\n之前的内容读取到buf中,最后再加一个\n\0
int get_line(int sock, char *buf, int size)
{
int i = 0;
char c = '\0';
int n;
//读取到的字符个数大于size或者读到\n结束循环
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0); //接收一个字符
/* DEBUG printf("X\n", c); */
if (n > 0)
{
if (c == '\r')//回车字符
{
/*使用 MSG_PEEK 标志使下一次读取依然可以得到这次读取的内容,可认为接收窗口不滑动*/
n = recv(sock, &c, 1, MSG_PEEK);
/* DEBUG printf("X\n", c); */
//读取到回车换行
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);//还需要读取,因为之前一次的读取,相当于没有读取
else
c = '\n';//如果只读取到\r,也要终止读取
}
//没有读取到\r,则把读取到内容放在buf中
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0'
return(i);//返回读取到的字符个数,不包括\0
}
请求错误处理函数 :
bad_requeset , cannot_execute ,error_dieerror_die, not_found
//告知客户端该请求有错误400
void bad_request(int client)
{
char buf[1024];
/*将字符串存入缓冲区,再通过send函数发送给客户端*/
sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n");
send(client, buf, sizeof(buf), 0);
sprintf(buf, "Content-type: text/html\r\n");
send(client, buf, sizeof(buf), 0);
sprintf(buf, "\r\n");
send(client, buf, sizeof(buf), 0);
sprintf(buf, "<P>Your browser sent a bad request, ");
send(client, buf, sizeof(buf), 0);
sprintf(buf, "such as a POST without a Content-Length.\r\n");
send(client, buf, sizeof(buf), 0);
}
//告知客户端CGI脚本不能被执行,错误代码500
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);
}
//打印出错误信息并结束程序。
void error_die(const char *sc)
{
perror(sc);
exit(1);
}
/告知客户端404错误(没有找到)
void not_found(int client)
{
char buf[1024];
sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, SERVER_STRING);
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, "<HTML><TITLE>Not Found</TITLE>\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<BODY><P>The server could not fulfill\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "your request because the resource specified\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "is unavailable or nonexistent.\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-121791-1.html
清者自清