Tiny Http源码解析

相关概念

源码
HTTP
CGI
getsockname()

函数说明

     accept_request:  处理从套接字上监听到的一个 HTTP 请求,在这里可以很大一部分地体现服务器处理请求流程。
     bad_request: 返回给客户端这是个错误请求,HTTP 状态吗 400 BAD REQUEST.
     cat: 读取服务器上某个文件写到 socket 套接字。
     cannot_execute: 主要处理发生在执行 cgi 程序时出现的错误。
     error_die: 把错误信息写到 perror 并退出。
     execute_cgi: 运行 cgi 程序的处理,也是个主要函数。
     get_line: 读取套接字的一行,把回车换行等情况都统一为换行符结束。
     headers: 把 HTTP 响应的头部写到套接字。
     not_found: 主要处理找不到请求的文件时的情况。
     sever_file: 调用 cat 把服务器文件返回给浏览器。
     startup: 初始化 httpd 服务,包括建立套接字,绑定端口,进行监听等。
     unimplemented: 返回给浏览器表明收到的 HTTP 请求所用的 method 不被支持。

解析

main


int main(void)
{
 int server_sock = -1;
 u_short port = 0;
 int client_sock = -1;
 struct sockaddr_in client_name;

 //这边要为socklen_t类型
 socklen_t client_name_len = sizeof(client_name);
 pthread_t newthread;

 //建立TCP连接,监听连接请求
 server_sock = startup(&port);
 printf("httpd running on port %d\n", port);

 while (1)
 {
	 //接受请求,函数原型
	 //#include 
	 //#include   
	 //int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  client_sock = accept(server_sock,
                       (struct sockaddr *)&client_name,
                       &client_name_len);
  if (client_sock == -1)
	  error_die("accept");
 /* accept_request(client_sock); */

  //每次收到请求,创建一个线程来处理接受到的请求
  //把client_sock转成地址作为参数传入pthread_create
  if (pthread_create(&newthread, NULL, (void *)accept_request, (void *)(intptr_t)client_sock) != 0)
	  perror("pthread_create");
 }

 close(server_sock);

 return(0);
}

accept_request

void accept_request(void *arg)
{
  //socket
 int client = (intptr_t)arg;
 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;
 //根据上面的Get请求,可以看到这边就是取第一行
 //这边都是在处理第一条http信息
 //"GET / HTTP/1.1\n"
 numchars = get_line(client, buf, sizeof(buf));
 i = 0; j = 0;

 //第一行字符串提取Get
 while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
 {
  method[i] = buf[j];
  i++; j++;
 }
 //结束
 method[i] = '\0';

 //判断是Get还是Post
 if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
 {
  unimplemented(client);
  return;
 }

 //如果是POST,cgi置为1
 if (strcasecmp(method, "POST") == 0)
  cgi = 1;

 i = 0;
 //跳过空格
 while (ISspace(buf[j]) && (j < sizeof(buf)))
  j++;

 //得到 "/"   注意:如果你的http的网址为http://192.168.0.23:47310/index.html
 //               那么你得到的第一条http信息为GET /index.html HTTP/1.1,那么
 //               解析得到的就是/index.html
 while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)))
 {
  url[i] = buf[j];
  i++; j++;
 }
 url[i] = '\0';

 //判断Get请求
 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);

 //默认地址,解析到的路径如果为/,则自动加上index.html
 if (path[strlen(path) - 1] == '/')
  strcat(path, "index.html");

 //获得文件信息
 if (stat(path, &st) == -1) {
  //把所有http信息读出然后丢弃
  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");
  //如果你的文件默认是有执行权限的,自动解析成cgi程序,如果有执行权限但是不能执行,会接受到报错信号
  if ((st.st_mode & S_IXUSR) ||
      (st.st_mode & S_IXGRP) ||
      (st.st_mode & S_IXOTH)    )
   cgi = 1;
  if (!cgi)
   //接读取文件返回给请求的http客户端
   serve_file(client, path);
  else
   //执行cgi文件
   execute_cgi(client, path, method, query_string);
 }
 //执行完毕关闭socket
 close(client);
}

execute_cgi

void execute_cgi(int client, const char *path,
                 const char *method, const char *query_string)
{
//缓冲区
 char buf[1024];

 //2根管道
 int cgi_output[2];
 int cgi_input[2];

 //进程pid和状态
 pid_t pid;
 int status;

 int i;
 char c;
 
 //读取的字符数
 int numchars = 1;

 //http的content_length
 int content_length = -1;

 //默认字符
 buf[0] = 'A'; buf[1] = '\0';

 //忽略大小写比较字符串
 if (strcasecmp(method, "GET") == 0)
 //读取数据,把整个header都读掉,以为Get写死了直接读取index.html,没有必要分析余下的http信息了
  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))
  {
   //如果是POST请求,就需要得到Content-Length,Content-Length:这个字符串一共长为15位,所以
   //取出头部一句后,将第16位设置结束符,进行比较
   //第16位置为结束
   buf[15] = '\0';
   if (strcasecmp(buf, "Content-Length:") == 0)
   //内存从第17位开始就是长度,将17位开始的所有字符串转成整数就是content_length
    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);
 //建立output管道
 if (pipe(cgi_output) < 0) {
  cannot_execute(client);
  return;
 }

 //建立input管道
 if (pipe(cgi_input) < 0) {
  cannot_execute(client);
  return;
 }
 //       fork后管道都复制了一份,都是一样的
 //       子进程关闭2个无用的端口,避免浪费             
 //       ×<------------------------->1    output
 //       0<-------------------------->×   input 

 //       父进程关闭2个无用的端口,避免浪费             
 //       0<-------------------------->×   output
 //       ×<------------------------->1    input
 //       此时父子进程已经可以通信


 //fork进程,子进程用于执行CGI
 //父进程用于收数据以及发送子进程处理的回复数据
 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];

  //子进程输出重定向到output管道的1端
  dup2(cgi_output[1], 1);
  //子进程输入重定向到input管道的0端
  dup2(cgi_input[0], 0);

  //关闭无用管道口
  close(cgi_output[0]);
  close(cgi_input[1]);

  //CGI环境变量
  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);
  }
  //替换执行path
  execl(path, path, NULL);
  //int m = execl(path, path, NULL);
  //如果path有问题,例如将html网页改成可执行的,但是执行后m为-1
  //退出子进程,管道被破坏,但是父进程还在往里面写东西,触发Program received signal SIGPIPE, Broken pipe.
  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请求数据,写到input管道中,供子进程使用
    recv(client, &c, 1, 0);
    write(cgi_input[1], &c, 1);
   }
  //从output管道读到子进程处理后的信息,然后send出去
  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,网络协议,网络,c语言)