如何架一个http服务器? Tinyhttpd 源码学习

tinyhttpd是一个超轻量型Http Server 使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通过阅读这段代码理解一个 Http Server 的本质。 

下载链接:http://sourceforge.net/projects/tinyhttpd/

函数和宏声明

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

按照函数调用顺序 进行分析首先是主函数  注释@n 的函数在第n部分

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);    //@1  startup 开启服务并返回一个服务器套接字
 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");
 if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0) //@2 开启一个新线程接受连接
   perror("pthread_create");
 }

 close(server_sock);
 return(0);
}

1 开启服务函数 startup

int startup(u_short *port)
{
 int httpd = 0;
 struct sockaddr_in name;
 httpd = socket(PF_INET, SOCK_STREAM, 0); //建立套接字,TCP协议,面向连接的流
 if (httpd == -1)
  error_die("socket");
 memset(&name, 0, sizeof(name));  //填充 socket地址
 name.sin_family = AF_INET;
 name.sin_port = htons(*port);
 name.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY 表示所有IP地址,当服务器又不止一个网卡时使用可以同时监听多个ip地址

 if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0) //毕竟端口是手动设置的,这个实际上是防止端口撞车
  error_die("bind");
 if (*port == 0)  // 如果port是0,则由内核自动分配一个端口,只有使用getsockname才能知道自动分配的是多少
 {
  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)  //5是等待队列的最大值,不是最大连接数
  error_die("listen");
 return(httpd);
}

2 接受请求函数accep_request

在这里首先给出url的结构,例如我们在百度上搜索http得到的url

https://www.baidu.com/s?wd=http&rsv_spt=1&rsv_iqid=0xc94cb34200007344&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&oq=linux&inputT=7086&rsv_t=e997SilGN55zLKxW%2B0RanHCQFsOQ9m89M4A4DeQ3O8%2FVJ8ltOs%2F425FTuD9J%2BIyRXCYy&rsv_pq=c216f9ba00006fcc&rsv_sug3=22&rsv_sug1=23&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7086

其中https://www.baidu.com/s是百度搜索,s实际上是search的意思,问号后面是要search的参数,比如wd=http就是说我们要搜索http这个关键词等等

再比如打开csdn首页 https://www.csdn.net/ 这里就没有? 可以理解为,这里是将储存的html直接send 而上一个是先用本地进程search了一下,然后再给你返回来search的结果,这种功能就叫cgi

void accept_request(int client)
{
 char buf[1024];
 int numchars;
 char method[255];
 char url[255];  //有些小,url应该有很长的,所以buf,path 和url最好都加长
 char path[512];
 size_t i, j;
 struct stat st;
 int cgi = 0;    //cgi 标识,CGI实际上是一个脚本或者命令,
                 //比如我要搜索http个关键字,服务器首先执行相关命令,然后再返回所需页面
 char *query_string = NULL;
 numchars = get_line(client, buf, sizeof(buf));//@3 读取一行
 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")) //这里只考虑http1.0没有新增的那些
 {
  unimplemented(client);
  return;
 }

 if (strcasecmp(method, "POST") == 0) //post 一定是cgi
  cgi = 1;
 i = 0;
 while (ISspace(buf[j]) && (j < sizeof(buf))) //去掉非打印字符
  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++;
  if (*query_string == '?')  //将get与参数分隔
  {
   cgi = 1;
   *query_string = '\0';      
   query_string++;
  }
 }

 sprintf(path, "htdocs%s", url);  // htdocs 主机文件,应该是linux 下的一个路径别名就像~,用于存放网页
 if (path[strlen(path) - 1] == '/')  //默认返回 index.html
  strcat(path, "index.html");

 if (stat(path, &st) == -1) {    //如果找不到文件,返回not_found 报文
  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) //如果是目录,添加默认的/index.html
   strcat(path, "/index.html");
  if ((st.st_mode & S_IXUSR) ||
      (st.st_mode & S_IXGRP) ||
      (st.st_mode & S_IXOTH)    )  //如果可执行,证明是一个cgi程序
   cgi = 1;
  if (!cgi)
   serve_file(client, path); //@4返回请求页面
  else
   execute_cgi(client, path, method, query_string);//@6 执行命令之后,再返回页面
 }
 close(client);
}

3. get_line 从socket中读取一行, 这里值的关注的是预读取来解决 不同的换行符\r  和\r\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);   //一次接收一个字符
  if (n > 0)
  {
   if (c == '\r')
   {
    n = recv(sock, &c, 1, MSG_PEEK);// MSG_PEEK 预读取, 即读取后该字符仍在缓冲区,下次读的还是它
    if ((n > 0) && (c == '\n')) //这里是为了同时处理 \r\n 和\r
     recv(sock, &c, 1, 0);
    else
     c = '\n';
   }
   buf[i] = c;
   i++;
  }
  else
   c = '\n';
 }
 buf[i] = '\0';
 return(i);
}

4  返回请求页面

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);   //发送一个报头
  cat(client, resource);      //@ 5 发送整个文件
 }
 fclose(resource);
}

5. 发送文件 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);
 }
}

6 执行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));
  /*
  读取content_length,其他丢弃 Content-Length描述HTTP消息实体的长度,
  对于POST方法,这个域是必须存在的,因为服务器需要这个长度来查找客户端请求的参数。
  */
  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");
 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);   //把output管道的写端 设为 标准输出 即向管道中输出
  dup2(cgi_input[0], 0);    //把input管道的读端 设为标准输入 即从管道中读取输入
  close(cgi_output[0]);     //关闭 输出读端 和 输入写端
  close(cgi_input[1]);

  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 */
   sprintf(length_env, "CONTENT_LENGTH=%d", content_length);
   putenv(length_env);
  }

  execl(path, path, NULL);  //执行 cgi 脚本
  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);
   }

  while (read(cgi_output[0], &c, 1) > 0)  //从输出管道读取cgi的处理结果
   send(client, &c, 1, 0);               //发送

  close(cgi_output[0]);
  close(cgi_input[1]);
  waitpid(pid, &status, 0);  //等待子进程结束
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值