TinyHttpd运行与源码注释
1. 运行
1.1 修改makefile
all: httpd
# 注释掉 -lsocket
LIBS = -lpthread #-lsocket
httpd: httpd.c
gcc -g -W -Wall $(LIBS) -o $@ $<
clean:
rm httpd
1.2 修改cgi文件
-
修改文件权限。
sudo chmod 777 *.cgi sudo chmod 666 index.html
修改完后权限如下:
-
修改cgi文件。
修改./htdocs/color.cgi文件,由于tinyhttpd默认cgi脚本是perl脚本。默认#!/usr/local/bin/perl -Tw 需要修改成本机的位置。
本机的位置通过which perl
命令查看。 我的是 usr/bin/perl。
则修改为:
-
颜色不变化,报错如下。
原因:CGI未安装。
解决:
yum安装CGI。 命令:yum install perl-CGI。
查看是否安装成功。命令:perl -MCGI -e 'print "CGI.pm version $CGI::VERSION\n;"'
1.3 解决警告
make时存在此警告。原因在284行中 execl(path, NULL);
中参数个数不够。修改为 execl(path,query_string, NULL);
1.4 运行过程
- make,生成httpd执行文件。
- ./httpd,执行httpd文件,在端口54335.
- 在浏览器中输入127.0.0.1:54335,出现页面如下:
- 输入一个颜色进行跳转,并显示对应颜色。
2. 执行过程
-
代码执行过程:
-
TCP 的CS模型通信过程。
-
http请求格式:请求行、请求头部、空行、请求数据
-
http响应格式:状态行、消息报头、空行、响应正文
PS:HTTP状态码
3. 源码注释
源码阅读顺序: main -> startup -> accept_request -> execute_cgi。
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdint.h>
#define ISspace(x) isspace((int)(x))
#define SERVER_STRING "Server: jdbhttpd/0.1.0\r\n"
#define STDIN 0
#define STDOUT 1
#define STDERR 2
void accept_request(void*);
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);
/**********************************************************************/
/* A request has caused a call to accept() on the server port to
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(void* arg)
{
int client = (intptr_t)arg;
char buf[1024];
size_t numchars;
char method[255];
char url[255];
char path[512];
size_t i, j;
struct stat st; // 获取文件信息
//struct stat{
// dev_t st_dev; /* ID of device containing file */文件使用的设备号
// ino_t st_ino; /* inode number */ 索引节点号
// mode_t st_mode; /* protection */ 文件对应的模式,文件,目录等
// nlink_t st_nlink; /* number of hard links */ 文件的硬连接数
// uid_t st_uid; /* user ID of owner */ 所有者用户识别号
// gid_t st_gid; /* group ID of owner */ 组识别号
// dev_t st_rdev; /* device ID (if special file) */ 设备文件的设备号
// off_t st_size; /* total size, in bytes */ 以字节为单位的文件容量
// blksize_t st_blksize; /* blocksize for file system I/O */ 包含该文件的磁盘块的大小
// blkcnt_t st_blocks; /* number of 512B blocks allocated */ 该文件所占的磁盘块
// time_t st_atime; /* time of last access */ 最后一次访问该文件的时间
// time_t st_mtime; /* time of last modification */ /最后一次修改该文件的时间
// time_t st_ctime; /* time of last status change */ 最后一次改变该文件状态的时间
//};
int cgi = 0; /* becomes true if server decides this is a CGI program */
char* query_string = NULL;
numchars = get_line(client, buf, sizeof(buf));
i = 0; j = 0;
//buf的内容: GET / HTTP/1.1 或
// GET /favicon.ico HTTP/1.1
// POST /color.cgi HTTP/1.1
//获取buf中的method
while (!ISspace(buf[i]) && (i < sizeof(method) - 1))
{
method[i] = buf[i];
i++;
}
j = i;
method[i] = '\0';
// strcasecmp(s1,s2)用忽略大小写比较字符串。
// 返回值 若s1=s2,返回0;s1>s2,返回大于0的值,s1<s2 返回小于0的值。
if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) // 既不是GET也不是POST方法
{
unimplemented(client);
return;
}
// 请求的方法为POST
if (strcasecmp(method, "POST") == 0)
cgi = 1;
i = 0;
// 跳过空格
while (ISspace(buf[j]) && (j < numchars))
j++;
// 获取url
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars))
{
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
// 请求方法为GET
if (strcasecmp(method, "GET") == 0)
{
query_string = url; // 用于记录带参数的GET方法请求中 ‘?’后的参数
while ((*query_string != '?') && (*query_string != '\0'))
query_string++;
if (*query_string == '?')
{
cgi = 1;
*query_string = '\0';
query_string++;
}
}
// 将“htdocs”与url拼接,存储到path中
sprintf(path, "htdocs%s", url);
// 如果解析到的路径为 / ,则自动在后面加上 index.html
// strcat()拼接字符串
if (path[strlen(path) - 1] == '/')
strcat(path, "index.html");
//定义函数:int stat(const char * file_name, struct stat *buf);
//函数说明:stat()用来将参数file_name 所指的文件状态, 复制到参数buf 所指的结构中。
//返回值:执行成功则返回0,失败返回-1,错误代码存于errno。
// 去掉头部信息
// sss_for: Host: 127.0.0.1:4000
// sss_for: User - Agent : Mozilla / 5.0 (X11; Linux x86_64; rv:17.0) Gecko / 20131029 Firefox / 17.0
// sss_for : Accept : text / html, application / xhtml + xml, application / xml; q = 0.9, */*;q=0.8
// sss_for: Accept-Language: en-US,en;q=0.5
// sss_for: Accept-Encoding: gzip, deflate
// sss_for: Connection: keep-alive
// sss_for:
if (stat(path, &st) == -1) {
while ((numchars > 0) && strcmp("\n", buf)) { /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
char* test = buf;
printf("sss_for: %s",test); // 输出如上所示
}
not_found(client);
}
else
{
// 文件存在,那则跟常量S_IFMT相与,相与之后的值可以用来判断该文件是什么类型的
// 此处与上边判断路径是不是以 \ 结尾的地方作用一样,可以省略,留着可重复确认。
if ((st.st_mode & S_IFMT) == S_IFDIR)
// 如果是目录,则将路径后添加 index.html,
strcat(path, "/index.html");
// 判断此文件是否具有执行权限,不论是属于用户/组/其他这三者类型的都将cgi置为1
if ((st.st_mode & S_IXUSR) ||
(st.st_mode & S_IXGRP) ||
(st.st_mode & S_IXOTH))
cgi = 1;
if (!cgi)
//不需要cgi机制,发送文件内容给client
serve_file(client, path);
else
// 如果需要则调用cgi。
// CGI:公共网关接口 CGI 程序是存放在 HTTP 服务器上,为用户和HTTP服务器之外的其他
// 应用程序提供互相“交谈”手段的软件。
execute_cgi(client, path, method, query_string);
}
close(client);
}
/**********************************************************************/
/* Inform the client that a request it has made has a problem.
* Parameters: client socket */
/**********************************************************************/
// 返回给客户端这是个错误请求,HTTP 状态吗 400 BAD REQUEST.
void bad_request(int client)
{
char buf[1024];
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);
}
/**********************************************************************/
/* Put the entire contents of a file out on a socket. This function
* is named after the UNIX "cat" command, because it might have been
* easier just to do something like pipe, fork, and exec("cat").
* Parameters: the client socket descriptor
* FILE pointer for the file to cat */
/**********************************************************************/
// 读取服务器上某个文件写到 socket 套接字。
void cat(int client, FILE* resource)
{
char buf[1024];
fgets(buf, sizeof(buf), resource);
while (!feof(resource))
{
send(client, buf, strlen(buf), 0);
fgets(buf, sizeof(buf), resource);
}
}
/**********************************************************************/
/* Inform the client that a CGI script could not be executed.
* Parameter: the client socket descriptor. */
/**********************************************************************/
// 主要处理发生在执行 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);
}
/**********************************************************************/
/* Print out an error message with perror() (for system errors; based
* on value of errno, which indicates system call errors) and exit the
* program indicating an error. */
/**********************************************************************/
// 把错误信息写到 perror 并退出
void error_die(const char* sc)
{
perror(sc);
exit(1);
}
/**********************************************************************/
/* Execute a CGI script. Will need to set environment variables as
* appropriate.
* Parameters: client socket descriptor
* path to the CGI script */
/**********************************************************************/
// cgi用于动态网页的处理
// execute_cgi函数创建了两个进程,子进程用于cgi文件的处理,父进程用于往socket读写数据
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';
// GET方法,将头部信息删除即可
if (strcasecmp(method, "GET") == 0)
while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
numchars = get_line(client, buf, sizeof(buf));
// POST方法,需要读取Content-Length字段的数据。
else if (strcasecmp(method, "POST") == 0) /*POST*/
{
numchars = get_line(client, buf, sizeof(buf));
while ((numchars > 0) && strcmp("\n", buf))
{
// "Content-Length:"长度为15个字符,所以将前15个字符比较。
buf[15] = '\0';
// 如果是Content-Length: 则读取此字段后的数值,转为int类型
if (strcasecmp(buf, "Content-Length:") == 0)
content_length = atoi(&(buf[16]));
numchars = get_line(client, buf, sizeof(buf));
}
// 无法处理,400错误
if (content_length == -1) {
bad_request(client);
return;
}
}
else/*HEAD or other*/
{
}
// 响应 200,成功
sprintf(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
// 创建管道
// 子进程写管道
if (pipe(cgi_output) < 0) {
// 服务器错误,状态码 500
cannot_execute(client);
return;
}
// 子进程读管道
if (pipe(cgi_input) < 0) {
cannot_execute(client);
return;
}
// 创建子进程
if ((pid = fork()) < 0) {
cannot_execute(client);
return;
}
// 如果是子进程
if (pid == 0) /* child: CGI script */
{
char meth_env[255];
char query_env[255];
char length_env[255];
dup2(cgi_output[1], STDOUT);// 将子进程的输出由标准输出重定向到 cgi_ouput 的管道写端上
dup2(cgi_input[0], STDIN); // 将子进程的输入由标准输入重定向到 cgi_iuput 的管道读端上
close(cgi_output[0]);// 关闭另外两个无用的端
close(cgi_input[1]);
// 通过环境变量传递信息
// 构造一个环境变量(环境变量的格式都是name = value)
sprintf(meth_env, "REQUEST_METHOD=%s", method);
//putenv()包含于<stdlib.h>中,作用是添加环境变量。获取环境变量可通过getenv()
putenv(meth_env);
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()运行cgi程序
execl(path, NULL);
exit(0);
}
// 父进程
else { /* parent */
close(cgi_output[1]);// 父进程关闭管道无用的端,
close(cgi_input[0]);
if (strcasecmp(method, "POST") == 0)
for (i = 0; i < content_length; i++) {
// 是POST方法,说明后续有参数需要读
recv(client, &c, 1, 0);
write(cgi_input[1], &c, 1);
}
// 从cgi子进程读取响应的数据,发送给客户端
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);
}
}
/**********************************************************************/
/* Get a line from a socket, whether the line ends in a newline,
* carriage return, or a CRLF combination. Terminates the string read
* with a null character. If no newline indicator is found before the
* end of the buffer, the string is terminated with a null. If any of
* the above three line terminators is read, the last character of the
* string will be a linefeed and the string will be terminated with a
* null character.
* Parameters: the socket descriptor
* the buffer to save the data in
* the size of the buffer
* Returns: the number of bytes stored (excluding null) */
/**********************************************************************/
// 从socket中获取一行,分割符为 \n
int get_line(int sock, char* buf, int size)
{
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
/* DEBUG printf("%02X\n", c); */
if (n > 0)
{
/*收到 \r 则继续接收下个字节,因为换行符可能是 \r\n */
if (c == '\r')
{
// recv()的原型是ssize_t recv(int sockfd, void* buf, size_t len, int flags);
// 我们在编写函数时,通常flags设置为0,此时recv()函数读取tcp 缓冲区中的数据
// 到buf中,并从tcp 缓冲区中移除已读取的数据。如果把flags设置为MSG_PEEK,仅仅
// 是把tcp 缓冲区中的数据读取到buf中,没有把已读取的数据从tcp 缓冲区中移除,
// 如果再次调用recv()函数仍然可以读到刚才读到的数据。
n = recv(sock, &c, 1, MSG_PEEK);
/* DEBUG printf("%02X\n", c); */
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
/*
如果回车符(\r)的后面不是换行符(\n)
或者读取失败
就把当前读取的字符置为换行,从而终止循环
*/
c = '\n';
}
buf[i] = c;
i++;
}
else
// 如果没有成功接收到字符,以 \n 结尾,结束循环
c = '\n';
}
// 以 \0 结尾,作为字符串
buf[i] = '\0';
return(i);// buf数组的大小
}
/**********************************************************************/
/* Return the informational HTTP headers about a file. */
/* Parameters: the socket to print the headers on
* the name of the file */
/**********************************************************************/
// 把 HTTP 响应的头部写到套接字
void headers(int client, const char* filename)
{
char buf[1024];
(void)filename; /* could use filename to determine file type */
strcpy(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, "\r\n");
send(client, buf, strlen(buf), 0);
}
/**********************************************************************/
/* Give a client a 404 not found status message. */
/**********************************************************************/
// 主要处理找不到请求的文件时的情况。状态码 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);
}
/**********************************************************************/
/* Send a regular file to the client. Use headers, and report
* errors to client if they occur.
* Parameters: a pointer to a file structure produced from the socket
* file descriptor
* the name of the file to serve */
/**********************************************************************/
void serve_file(int client, const char* filename)
{
FILE* resource = NULL;
int numchars = 1;
char buf[1024];
buf[0] = 'A'; buf[1] = '\0';
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
{
// 构造响应头部并写给客户端
headers(client, filename);
// 将文件写给客户端(此处为index.html)
cat(client, resource);
}
fclose(resource);
}
/**********************************************************************/
/* This function starts the process of listening for web connections
* on a specified port. If the port is 0, then dynamically allocate a
* port and modify the original port variable to reflect the actual
* port.
* Parameters: pointer to variable containing the port to connect on
* Returns: the socket */
/**********************************************************************/
/// 建立套接字,绑定端口,进行监听
int startup(u_short* port)
{
int httpd = 0;
int on = 1;
struct sockaddr_in name;
httpd = socket(PF_INET, SOCK_STREAM, 0);
if (httpd == -1)
error_die("socket");
memset(&name, 0, sizeof(name));
name.sin_family = AF_INET;
name.sin_port = htons(*port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
// setsocket(表示一个套接字的描述符, 选项定义的层次, 需设置的选项, 指向存放选项值的缓冲区指针, 缓冲区长度)
// 此处设置为SO_REUSERADDR,表示允许重用本地地址和端口
if ((setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0)
{
error_die("setsockopt failed");
}
if (bind(httpd, (struct sockaddr*)&name, sizeof(name)) < 0)
error_die("bind");
if (*port == 0) /* if dynamically allocating a port */
{
socklen_t namelen = sizeof(name);
if (getsockname(httpd, (struct sockaddr*)&name, &namelen) == -1)
error_die("getsockname");
*port = ntohs(name.sin_port);
}
if (listen(httpd, 5) < 0)
error_die("listen");
return(httpd);
}
/**********************************************************************/
/* Inform the client that the requested web method has not been
* implemented.
* Parameter: the client socket */
/**********************************************************************/
// 返回给浏览器表明收到的http请求中的method不被支持
void unimplemented(int client)
{
char buf[1024];
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\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><HEAD><TITLE>Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</TITLE></HEAD>\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
}
/**********************************************************************/
int main(void)
{
int server_sock = -1;
u_short port = 4000;
int client_sock = -1;
struct sockaddr_in client_name;
socklen_t 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");
/* accept_request(&client_sock); */
if (pthread_create(&newthread, NULL, (void*)accept_request, (void*)(intptr_t)client_sock) != 0)
perror("pthread_create");
}
close(server_sock);
return(0);
}
4. 父子进程的数据处理流程
管道的初始状态(管道中默认,0为读端,1为写端):
完成的状态(绿色为子进程完成的,红色为父进程完成的):
浏览器与服务器数据流动图: