Tinyhttpd源码解析

简介

Tinyhttpd是一轻量级的web服务器,它由美国学生J. David Blackstone于1999年在学习网络课程时编写。源码不到500行,非常适合学习Web编程和Linux/Unix编程接口。

其源码见Tinyhttpd

分析

Tinyhttpd执行流程如下图:
主线程完成socket的创建、绑定、端口监听、创建子线程处理每一个请求。代码如下:
int main(void)
{
	int server_sock = -1;
	u_short port = 8000;
	int client_sock = -1;
	struct sockaddr_in client_name;
	int client_name_len = sizeof(client_name);
	pthread_t newthread;

	/* 在指定端口上建立http服务 */
	server_sock = startup(&port);
	printf("httpd-socket:%d\n", server_sock);
	printf("httpd running on port %d\n", port);

	int count = 0;
	while (1) {
		/* accpet成功后,可获得客户端socket和sockaddr */
		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");
		//printf("\b%d", ++count); fflush(stdout); //统计连接请求数
	}

	close(server_sock);
	return(0);
}
其中startup()函数完成socket的bind/listen。代码实现如下:
/**********************************************************************/
/* 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;
	struct sockaddr_in name;

	/* 建立socket */
	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);

	/* 绑定socket */
	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);
	}
	/* 监听socket */
	if (listen(httpd, 5) < 0)
		error_die("listen");

	return(httpd);
}
子线程accep_request()处理连接请求,请求处理完成后关闭连接。当然这种多线程方式,不可能做到高并发。每来一个请求都会创建一个子线程来处理,处理完成后结束子线程,这必然会导致服务器有不少性能开销,一般用线程池技术。当然Web服务也可以使用多进程如nginx。多线程共享父进程的数据空间,可能导致问题蔓延。多进程有很好的隔离性。accep_request()代码如下:
/**********************************************************************/
/* 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(int client)
{
	char buf[1024];
	int numchars;
	char method[255]; //请求方法
	char url[255]; //URL
	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));
	printf("Request-line:%s\n", buf); //debug
	i = 0; j = 0;
	/* 读取请求行中的请求方法至method */
	while (!ISspace(buf[j]) && (i < sizeof(method) - 1)) {
		method[i] = buf[j];
		i++; j++;
	}
	method[i] = '\0';
	printf("Method:%s ", method);

	/* 仅支持GET和POST请求方法 */
	if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) {
		unimplemented(client); //通知客户客,该请求方法未实现
		return;
	}

	/* POST时开启CGI */
	if (strcasecmp(method, "POST") == 0)
		cgi = 1;

	/* 读取请求行中URL */
	i = 0;
	while (ISspace(buf[j]) && (j < sizeof(buf)))
		j++;
	while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf))) {
		url[i] = buf[j];
		i++; j++;
	}
	url[i] = '\0';
	printf("URL:%s \n", url);

	/* 处理GET方法 */
	if (strcasecmp(method, "GET") == 0) { 
		query_string = url;
		while ((*query_string != '?') && (*query_string != '\0'))
			query_string++;
		/* GET方法的特点,? 后面为参数 */
		if (*query_string == '?') {
			/* 开启CGI */
			cgi = 1;
			*query_string = '\0';
			query_string++;
		}
	}

	/* 格式化URL到path数组, html在htdocs中,从客户端发过来的URL至少是/ */
	sprintf(path, "htdocs%s", url);
	if (path[strlen(path) - 1] == '/')
		/* 默认情况为index.html */
		strcat(path, "index.html");

	/* 根据路径找到对应的文件 */
	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);
		else
			execute_cgi(client, path, method, query_string);
	}
	/* 关闭连接,假定不是keepalive */
	close(client);
}
浏览器是按照http协议请求的格式给服务器发送请求的,这个函数完成了http协议请求解析。目前只支持GET和POST请求。其中get_line()函数并且不管原来是以\n还是\r\n结束,均转化为以\n再加\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) */
/**********************************************************************/
int get_line(int sock, char *buf, int size)
{
	int i = 0;
	char c = '\0';
	int n;

	/*把终止条件统一为\n换行符,标准化buf数组*/
	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') {
				/*MSG_PEEK使下一次读取的内容和本次读取的内容相同,可认为接收窗口不滑动*/
				n = recv(sock, &c, 1, MSG_PEEK); //提前偷窥下次内容
				/* DEBUG printf("%02X\n", c); */
				if ((n > 0) && (c == '\n'))
					recv(sock, &c, 1, 0);
				else
					c = '\n';
			}
			buf[i] = c;
			i++;
		}
		else
			c = '\n';
	}
	buf[i] = '\0';

	return(i);
}
get_line完后,就是开始解析第一行,判断是GET方法还是POST方法,目前只支持这两种。如果是POST,还是把cgi置1,表明要运行CGI程序;如果是GET方法且附带以?开头的参数时,也认为是执行CGI程序。
获取URL得到文件在服务上的访问路径,获取访问文件的属性,并判断文件是否具有可执行权限。如果有可执行权限,则认为是要执行CGI程序。否则访问静态文件,访问方法为 serve_file(),其实现如下:
 /**********************************************************************/
/* 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];

	/* 读取并丢弃headers */
	buf[0] = 'A'; buf[1] = '\0';
	while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
		numchars = get_line(client, buf, sizeof(buf));

	/* 打开serve的文件 */
	resource = fopen(filename, "r");
	if (resource == NULL)
		not_found(client);
	else {
		/* 写http头 */
		headers(client, filename);
		/* 复制文件 */
		cat(client, resource);
	}
	fclose(resource);
}
其中在服务上读取要访问的文件,并构造http响应格式,以响应客户端。其中,headers()完成响应头的构造,并把响应头信息发送给客户端。cat()函数则负责把访问的文件发送给客户端。
headers()代码如下:
/**********************************************************************/
/* Return the informational HTTP headers about a file. */
/* Parameters: the socket to print the headers on
 *             the name of the file */
/**********************************************************************/
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);
}
cat()代码如下:
/**********************************************************************/
/* 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 */
/**********************************************************************/
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程序的代码如下:
/**********************************************************************/
/* Execute a CGI script.  Will need to set environment variables as
 * appropriate.
 * Parameters: client socket descriptor
 *             path to the CGI script */
/**********************************************************************/
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 */
		/* 读取并丢弃请求headers */
		while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
			numchars = get_line(client, buf, sizeof(buf));
	else {  /* POST */
		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;
		}
	}

	/* 正确,HTTP状态码200 */
	sprintf(buf, "HTTP/1.0 200 OK\r\n");
	send(client, buf, strlen(buf), 0);

	/* 创建两个管道,管道半双工, cig_output[0]读, cig_output[1]写 */
	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];

		/* 把STDOUT重定向到cgi_output的写入端 */ 
		dup2(cgi_output[1], 1);
		/* 把STDIN重定向到cgi_input的读取端 */
		dup2(cgi_input[0], 0);

		/* 关闭cgi_input的写入端和cgi_output的读取端, 把半双工变单工通信 */
		close(cgi_output[0]);
		close(cgi_input[1]);

		/* 设置request_method的环境变量 */
		sprintf(meth_env, "REQUEST_METHOD=%s", method);
		putenv(meth_env);

		if (strcasecmp(method, "GET") == 0) {
			sprintf(query_env, "QUERY_STRING=%s", query_string);
			putenv(query_env);
		}
		else { /* POST */
			/* 设置content_length的环境变量 */ 
			sprintf(length_env, "CONTENT_LENGTH=%d", content_length);
			putenv(length_env);
		}
		/* 用execl运行cgi程序 */
		execl(path, path, NULL);
		exit(0);
	}
	else {    /* parent */
		/* 关闭cgi_input的读取端和cgi_output的写入端 */
		close(cgi_output[1]);
		close(cgi_input[0]);
		/* 接收POST方法过来的数据 */
		if (strcasecmp(method, "POST") == 0)
			for (i = 0; i < content_length; i++) {
				recv(client, &c, 1, 0);
				/*把POST数据写入cgi_input,现在重定向到STDIN */ 
				write(cgi_input[1], &c, 1);
			}
		/* 读取cgi_output的管道输出到客户端,该管道输入是STDOUT */
		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);
	}
}


猜你喜欢

转载自blog.csdn.net/psc928624742/article/details/50086945
今日推荐