源码分析-简洁的HTTPServer
second60 20180610
1. tinyhttpd 简介
tinyhttpd 可以说是最小最精简的HTTP服务器,C语言编写,全部代码只有五百多行。通过阅读tinyhttpd源码可以了解HTTP服务器搭建的本质和HTTP基础。
下载链接:http://sourceforge.net/projects/tinyhttpd/
2. tinyhttpd 安装和使用
httpd编译: make
在编译时,如果报错,在makefile中注释-lsocket.(源码开头注释)
httpd运行: ./httpd
simpleclient编译: gcc simpleclient.c -o simpleclient
simpleclient运行: ./simpleclient
此程序中的端口是随机生成的,可以在练习时,固定好一个端口,或修改成命令行传入。
3. tinyhttpd 流程
1) 创建服务器套接字,如果端口为0,随机生成一个端口,startup
2) 套接字绑定并监听startup
3) 如果有客户端连接,单独开启一个线程来处理客户端请求accept_request
a) 获取客户端数据
b) 分析客户端请求数据:GET/POST/query_string/path等
c) 非cgi,执行serve_file,读取文件并返回给客户端(非脚本文件)
d) cgi存在执行execute_cgi(脚本文件)
4. http 原理
4.1 HTTP协议流程
1.用户在浏览器中键入需要访问网页的URL或者点击某个网页中链接;
2.浏览器根据URL中的域名,通过DNS解析出目标网页的IP地址;
3.浏览器与网页所在服务器建立TCP连接;
4.浏览器发送HTTP请求报文,获取目标网页的文件;
5.服务器发送HTTP响应报文,将目标网页文件发送给浏览器;
6.释放TCP连接;
7.浏览器将网页的内容包括文本、图像、声音等显示呈现在用户计算机屏幕。
4.2 HTTP请求报文格式
HTTP请求报文的由请求行、请求头部行、空行和请求数据四部分构成,具体格式如下所示:
(请求行)方法名+空格+URL+空格+版本+回车换行(\r\n)
(请求头部行1)关键字+“:”+空格+值+回车换行(\r\n)
...
(请求头部行N)关键字+“:”+空格+值+回车换行(\r\n)
(空行)回车换行(\r\n)
(请求数据)……
4.2.1 请求行
请求行由请求方法字段、URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔。最后由回车和换行表示请求行结束。
例如: GET www.sdu.edu.cn HTTP/1.1 回车换行(\r\n)
HTTP请求报文的主要方法包括:
GET |
|
|
POST |
|
|
HEAD |
|
|
PUT |
|
|
DELETE |
|
|
OPTIONS |
|
|
TRACE |
|
|
CONNECT |
|
|
4.2.2 请求头部行(header)
请求头部行包括若干行,每行由关键字及其值构成的,关键字和值用英文冒号“:”分隔,每一行都由回车换行表示结束。
请求头部通知服务器有关于客户端请求的信息,典型的请求头部关键字有:
User-Agent | 产生请求的浏览器类型 |
|
Accept | 客户端可识别的内容类型列表 |
|
Accept-Language | 客户端可识别的语言类型 |
|
Host | 请求的主机名 |
|
Connection | 告知服务器发送完文档后释放连接还是保持连接 |
|
|
|
|
4.2.3 空行
最后一个请求头部之后是一个空行,发送回车符和换行符,通知服务器以下不再有请求头部了。
4.2.4 请求数据
GET方法中没有请求数据的内容
POST方法使用请求数据,用于客户端向服务器端填写表单等操作。
4.2.5 HTTP请求例子
GET /index.html HTTP/1.1 \r\n
Host:www.baidu.com\r\n
User-Agent:Mozilla/5.0
Accept-Language:cn*/*\r\n
4.3 HTTP响应报文格式
HTTP响应也由四个部分组成,分别是:状态行、消息头部、空行和响应正文。
其具体格式如下:
(状态行)版本+空格+状态码+空格+短语+回车换行
(消息头部1)关键字+“:”+空格+值+回车换行
……
(消息头部N)关键字+“:”+空格+值+回车换行
(空行)回车换行(\r\n)
(响应正文)……
4.3.1 响应状态行
在响应报文的状态行中,版本字的表示服务器HTTP协议的版本,状态码字的表示服务器发回的响应状态代码;短语字段表示状态代码的文本描述。
状态码由三位十进制数字组成,第一个数字定义了响应的类别,有五种可能取值(1-5)
每种状态码的含义如下:
1xx | 指示信息 | 表示请求已接收,继续处理 |
2xx | 成功 | 表示请求已被成功接收、理解、接受 |
3xx | 重定向 | 要完成请求必须进行更进一步的操作 |
4xx | 客户端错误 | 请求有语法错误或请求无法实现 |
5xx | 服务器端错误 | 服务器未能实现合法的请求 |
常见状态码及状态描述的说明如下:
|
|
|
200 | OK | 客户端请求成功 |
400 | Bad Request | 客户端请求有语法错误,不能被服务器所理解 |
401 | Unauthorized | 请求未经授权 |
403 | Forbidden | 服务器收到请求,但是拒绝提供服务 |
404 | Not Found | 请求资源不存在,比如输入了错误的URL |
500 | Internal ServerError | 服务器发生不可预期的错误 |
503 | ServerUnavailable | 服务器当前不能处理客户端的请求,一段时间后可能恢复正常 |
4.3.2 响应消息头部
消息头部与请求头部的格式相似,也是包含若干行,每行由关键字及其值构成
常用的关键字包括:
Date | 表示返回消息的时间 |
|
Content-Type | 表示返回消息的内容类型 |
|
Content-Length | 返回内容的长度(字节数) |
|
Server | 使用的服务器软件及其版本号 |
|
|
|
|
4.3.3 空行
同样,最后一个消息头部之后是一个空行,发送回车符和换行符,通知客户端以下不再有消息头部了。(注:空行必须有)
4.3.4 响应正文
响应正文部分是服务器端根据客户端的请求发回的具体文档内容,以HTML语言表示。
4.4 CGI 简介
CGI(Common Gateway Interface) 是WWW技术中最重要的技术之一,有着不可替代的重要地位。CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将Web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。
5 源码分析
/* 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);
/**********************************************************************/
/* 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];
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;
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
{
method[i] = buf[j];
i++; j++;
}
method[i] = '\0';
if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
{
unimplemented(client);
return;
}
if (strcasecmp(method, "POST") == 0)
cgi = 1;
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';
if (strcasecmp(method, "GET") == 0)
{
query_string = url;
while ((*query_string != '?') && (*query_string != '\0'))
query_string++;
if (*query_string == '?')
{
cgi = 1;
*query_string = '\0';
query_string++;
}
}
sprintf(path, "htdocs%s", url);
if (path[strlen(path) - 1] == '/')
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);
}
close(client);
}
/**********************************************************************/
/* Inform the client that a request it has made has a problem.
* Parameters: client socket */
/**********************************************************************/
// 非法请求时返回给客户端的内容
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 */
/**********************************************************************/
// 把 resource中的内容发送到套接字中
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 脚本不能执行时返回给客户端的内容
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);
}
/**********************************************************************/
/* Execute a CGI script. Will need to set environment variables as
* appropriate.
* Parameters: client socket descriptor
* path to the CGI script */
/**********************************************************************/
// 执行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)
{
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;
}
}
sprintf(buf, "HTTP/1.0 200 OK\r\n");
// 发送http头给客户端
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(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);
//GET方法处理
if (strcasecmp(method, "GET") == 0) {
sprintf(query_env, "QUERY_STRING=%s", query_string);
putenv(query_env);
}
// POST方法处理
else
{ /* POST */
sprintf(length_env, "CONTENT_LENGTH=%d", content_length);
putenv(length_env);
}
// 执行CGI脚本
execl(path, path, NULL);
exit(0);
}
// 父进程处理
else
{ /* parent */
// 关闭输出管道的写描述符
close(cgi_output[1]);
// 关闭输入管道的读描述符
close(cgi_input[0]);
// 从管道中读取或发送数据
if (strcasecmp(method, "POST") == 0)
{
// 从客户端发来的空容中读取数据
// 并写到CGI输入管理中
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);
}
}
/**********************************************************************/
/* 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 读取一行数据
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')
{
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);
}
/**********************************************************************/
/* Return the informational HTTP headers about a file. */
/* Parameters: the socket to print the headers on
* the name of the file */
/**********************************************************************/
// HTTP headers文件
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. */
/**********************************************************************/
// 文件没找到
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
{
// 发送HTTP头
headers(client, filename);
// 发送文件内容
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 */
/**********************************************************************/
// 启动服务端HTTP监听套接字
// 并返回端口
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");
// 如果端口为0
// 自动分配一个端口
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);
}
/**********************************************************************/
/* Inform the client that the requested web method has not been
* implemented.
* Parameter: the client socket */
/**********************************************************************/
// HTTP 501 错误返回
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 = 8888;
// 客户端套接字
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);
}
6 总结
tinyhttpd 是个非常轻量级的HTTP SERVER,对于学习HTTP SEVER实现非常有帮助,而且可以修改或增加源码,来实现其他功能,麻雀虽小,五脏俱全,里面包含:HTTP的整体工作流程,HTTP原理,HTTP请求和响应实现,还包括CGI,可以执行脚本文件等。
HTTP在本质上就是TCP连接,有一定的规范,请求格式和响应格式。按照相应的规范发送和接收消息,最后解析内容并展示到浏览器上。
学编程,最主要的就是学习本质,如果一门技术或语言,如果本质原理都懂了,开发质量会更好,也可以去做更深层次的优化。