tinyhttpd源码剖析

这是一个简单的服务器程序,只有五百多行。每次来一个请求就开一个线程去处理它,可以解析POST,GET而且可以执行CGI程序。

服务器程序想要把自己收到的参数传给某些别的程序去执行,并且把这些程序产生的输出拿走,发给某个目标对象。CGI所处的位置就在“服务器把收到的参数传给别的程序”这个环节。它规定了一个标准的借口,我们只需要调用setenv()把收到的东西设置成条件变量,然后再通过execl执行某个CGI程序就可以了,具体看代码。

/* J. David's webserver */
/* This is a simple webserver.
* Created November 1999 by J. David Blackstone.
* CSE 4344 (Network concepts), Prof. Zeigler
* University of Texas at Arlington
*/
/* This program compiles for Sparc Solaris 2.6.
* 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.
*/
#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>

#define ISspace(x) isspace((int)(x))

#define SERVER_STRING "Server: jdbhttpd/0.1.0\r\n"

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);

/**********************************************************************
处理请求函数,被一个子线程调用
**********************************************************************/
void accept_request(int client)
{
	char buf[1024];
	int numchars;
	char method[255];
	char url[255];
	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;
	
	numchars = get_line(client, buf, sizeof(buf));//这个服务器的模型是每个请求都开一个新的线程去单独处理,也就是说它可以保证你读的第一行一定是请求的第一行
	i = 0; j = 0;
	//以下开始处理第一行 也就是请求行 比如 "GET /index.html HTTP/1.1\n"
	while (!ISspace(buf[j]) && (i < sizeof(method) - 1))//这个循环负责把POST 或者 GET取出来
	{
		method[i] = buf[j];
		i++; j++;
	}
	method[i] = '\0';

	if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))//判断方法,如果都不是就return
	{
		unimplemented(client);
		return;
	}

	if (strcasecmp(method, "POST") == 0)		//post方法一定是动态的,需要运行cgi脚本
		cgi = 1;

	i = 0;
	while (ISspace(buf[j]) && (j < sizeof(buf)))//到这里请求行还剩下" /index.html /HTTP/1.1\n"于是跳过空格
		j++;
	while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)))
	{											//解析url
		url[i] = buf[j];
		i++; j++;
	}
	url[i] = '\0';

	if (strcasecmp(method, "GET") == 0)
	{
		query_string = url;
		while ((*query_string != '?') && (*query_string != '\0'))
			query_string++;						//有问号和post同理,也是有需要处理的参数从客户端发过来
		if (*query_string == '?')
		{
			cgi = 1;
			*query_string = '\0';
			query_string++;
		}
	}

	sprintf(path, "htdocs%s", url);				//路径写到path
	if (path[strlen(path) - 1] == '/')			//如果路径只有/默认返回index.html
		strcat(path, "index.html");				//如果没有这个文件,就把这个http报文全部读出来,然后返回notfound
	if (stat(path, &st) == -1) {
		while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
			numchars = get_line(client, buf, sizeof(buf));
		not_found(client);
	}
	else										
	{
		if ((st.st_mode & S_IFMT) == S_IFDIR)	//如果这是个目录,再把默认文件填充到后边
			strcat(path, "/index.html");
		if ((st.st_mode & S_IXUSR) ||			//如果这是个可执行文件,则执行他
			(st.st_mode & S_IXGRP) ||
			(st.st_mode & S_IXOTH)    )
			cgi = 1;
		if (!cgi)
			serve_file(client, path);			//如果有这个文件,而且不是cgi文件,就普通的发送到用户
		else
			execute_cgi(client, path, method, query_string);
	}

	close(client);
}

/**********************************************************************
客户发来的报文有问题
**********************************************************************/
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);
}

/**********************************************************************
读取文件中的所有内容,把它们写入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);
	}
}

/**********************************************************************
服务器上的CGI脚本无法运行时
**********************************************************************/
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. */
/**********************************************************************/
void error_die(const char *sc)
{
	perror(sc);
	exit(1);
}

/**********************************************************************
fork一个子进程执行CGI脚本,父子进程通过管道通信,子进程在执行CGI之前要
设置相应的条件变量,通过修改标准输入输出,把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[0] = 'A'; buf[1] = '\0';
	if (strcasecmp(method, "GET") == 0)		//如果是get请求就扔掉后边的header,因为传来的信息都已经在解析url的时候解析出来了
		while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
			numchars = get_line(client, buf, sizeof(buf));
	else    /* POST */						//如果是post那需要内容的长度,这些信息都是为了我们设置cgi脚本的条件变量用的
	{
		numchars = get_line(client, buf, sizeof(buf));
		while ((numchars > 0) && strcmp("\n", buf))
		{
			buf[15] = '\0';
			if (strcasecmp(buf, "Content-Length:") == 0)
				content_length = atoi(&(buf[16]));
			numchars = get_line(client, buf, sizeof(buf));
		}
		if (content_length == -1) {
			bad_request(client);
			return;
		}
	}

	sprintf(buf, "HTTP/1.0 200 OK\r\n");	//先发送回复报文头 这里感觉有点问题啊,应该等到确定cgi可以运行了之后在发送
	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;
	}
	if (pid == 0)  /* child: CGI script */
	{
		char meth_env[255];
		char query_env[255];
		char length_env[255];

		dup2(cgi_output[1], 1);
		dup2(cgi_input[0], 0);
		close(cgi_output[0]);
		close(cgi_input[1]);		//上面都是创建子进程,重定向标准输入输出,管道读写,关闭管道写读
		sprintf(meth_env, "REQUEST_METHOD=%s", method);
		putenv(meth_env);			//设置cgi脚本运行时需要的条件变量
		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(path, path, NULL);	//执行,等着它把输出发送到父进程就OK
		exit(0);
	} else {    /* parent */
		close(cgi_output[1]);
		close(cgi_input[0]);
		if (strcasecmp(method, "POST") == 0)
			for (i = 0; i < content_length; i++) {
				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);
	}
}

/**********************************************************************
用来获取一行数据,在http报文里一行的结束表示可能是\n或者是\r或者\r\n,不管是哪种
这个函数都把一行拿出来,结尾变成\0返回。如果数据不足一行,或者缓冲区太小,get_line
不负责处理这个问题,只是把结尾用\0封住,然后返回。
**********************************************************************/
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)									
		{
			if (c == '\r')						//如果是\r说明此时一行已经读完了,剩下要做的事情就是检测是不是\R\N的组合,如果是就把\N也取出来,方便下次读取
			{
				n = recv(sock, &c, 1, MSG_PEEK);//MSG_PEEK并不改变偏移量,先看看下一个是不是\n
				/* DEBUG printf("%02X\n", c); */
				if ((n > 0) && (c == '\n'))		//发现是\n就一并读出来
					recv(sock, &c, 1, 0);
				else
					c = '\n';					//不是\N就直接设置c为\N结束循环
			}
			buf[i] = c;
			i++;
		}
		else
			c = '\n';							//读到没有数据可读也结束循环
	}
	buf[i] = '\0';

	return(i);
}

/**********************************************************************
填写并且发送回复报文头部信息
**********************************************************************/
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);
}

/**********************************************************************
找不到请求的url,返回notfound404
**********************************************************************/
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);
}

/**********************************************************************
用来发送普通文件
**********************************************************************/
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));//简单服务器,不处理一切header

	resource = fopen(filename, "r");
	if (resource == NULL)
		not_found(client);							//打开失败,读取不了文件
	else
	{
		headers(client, filename);					//发送头部
		cat(client, resource);						//发送文件
	}
	fclose(resource);								//关闭文件指针
}

/**********************************************************************
做一系列的初始化工作,包括申请listenfd,bind,listen
**********************************************************************/
int startup(u_short *port)
{
	int httpd = 0;
	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);
	if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)
		error_die("bind");
	if (*port == 0)  /* if dynamically allocating a port */
	{
		int 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);
}

/**********************************************************************
解析不了的请求,解析第一行的时候发现既不是GET也不是POST就会跳转到这里
组合错误报文,发给客户端。
**********************************************************************/
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 = 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");
		/* accept_request(client_sock); */
		if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)
			perror("pthread_create");
	}

	close(server_sock);

	return(0);
}

猜你喜欢

转载自blog.csdn.net/qq_33113661/article/details/89035388
今日推荐