+
Skip to content

juteman/tinyhttpd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinyhttpd 源码解析

tinyhttpd的源代码分析

流程分析

main()函数分析

int main(void) {
  int server_sock = -1;
  u_short port = 0;
  int client_sock = -1;
  struct sockaddr_in client_name;
  socklen_t client_name_len = sizeof(client_name);
  pthread_t newthread;

  server_sock = startup(&port);
......

sockaddr_in在头文件<netinet/in.h>中定义,定义如下

struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;			/* Port number.  */
    struct in_addr sin_addr;		/* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
			   __SOCKADDR_COMMON_SIZE -
			   sizeof (in_port_t) -
			   sizeof (struct in_addr)];
  };

这是我系统里的sockaddr_in 结构定义,这是ipv4 socket struct定义

startup()分析

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");
  // 初始化sockaddr_in 结构体
  memset(&name, 0, sizeof(name));
  name.sin_family = AF_INET;
  name.sin_port = htons(*port);
  name.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY use for wildcard
  // 将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);
  }
  // 最后开始监听
  if (listen(httpd, 5) < 0)
    error_die("listen");
  return (httpd);
}
socket 函数

socket函数第一个参数代表Protocol family,PF_INET 于AF_INET相同。代表 IPV4 protocol 第二个参数带表socket type,SOCK_STREAM 代表 stream socket 第三个参数0,代表系统默认协议 socket函数返回一个socket 描述符

htons() htonl()

这两函数可以统一大端小端的机器影响,末尾s代表small,l代表long

bind 函数

将本地协议地址绑定到socket上,成功执行返回0,否则返回-1

getsockname 函数

如果成功执行connect 函数,并且没有执行bind函数. getsockname 返回ip地址和端口 执行bind函数后返回端口

listen函数

listen函数将CLOSE状态转换为LISTEN状态,即开始监听

accept()

接受sever_sock,此时接收一个http的请求

phread_create()

NULL参数代表使用系统默认参数,accept_request表示执行的函数,client_sock 为函数的参数

accept_request()

int client = (intptr_t)arg;
  char buf[1024];
  size_t 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;

//这里将buf里的字符串赋给method,并且去掉空白字符
  while (!ISspace(buf[i]) && (i < sizeof(method) - 1)) {
    method[i] = buf[i];
    i++;
  }
  j = i;
  method[i] = '\0';

//这里判断如果既不是"GET",也不是"POST" 的话o,请求失败
  if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) {
    unimplemented(client);
    return;
  }

//若果是"POST"的话,cgi为开启状态
  if (strcasecmp(method, "POST") == 0)
    cgi = 1;

  i = 0;
  while (ISspace(buf[j]) && (j < numchars)) //跳过空白符
    j++;
//读取url地址
  while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars)) {
    url[i] = buf[j];
    i++;
    j++;
  }
  url[i] = '\0';

//如果是"GET",GET的url中带有"?",如果读取到?则cgi开启
  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++;
    }
  }

//将url格式化放入path中
  sprintf(path, "htdocs%s", url);
  if (path[strlen(path) - 1] == '/')
    strcat(path, "index.html"); //将默认设为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开启
      cgi = 1;
    if (!cgi) 
      serve_file(client, path);
    else
      execute_cgi(client, path, method, query_string);
  }

  close(client);
}
get_line()

一个字符一个字符的读取socket的第一行,保存在buf里

get_line()中的recv()函数

recv()与read()函数大致相同,读取时会有一个读取指针偏移, 下一次读取时会从上一次读取出开始

stat()

stat(pathname,buf) 。将buf作为一个指针,指像pathname文件的文件信息结构体上 S_IXUSR S_IXGRP S_IXOTH 分别代表了用户 用户组 和其他的执行权限

execute_cgi()

if ((pid = fork()) < 0) { //创建一个子进程
    cannot_execute(client);
    return;
  }
  sprintf(buf, "HTTP/1.0 200 OK\r\n");
  send(client, buf, strlen(buf), 0);
  if (pid == 0) /* child: CGI script */ //子进程执行if
  {
    char meth_env[255];
    char query_env[255];
    char length_env[255];

    dup2(cgi_output[1], STDOUT);
    dup2(cgi_input[0], STDIN);
    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, 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); //将post信息写入cgi_input
      }
    while (read(cgi_output[0], &c, 1) > 0)
      send(client, &c, 1, 0);   //将STDOUT的数据写入客户端
//关闭管道并等待子进程结束
    close(cgi_output[0]);
    close(cgi_input[1]);
    waitpid(pid, &status, 0);
  }
}

这里用图片解释父进程与子进程的通信(请忽略我的灵魂画风): http://7xsrk7.com1.z0.glb.clouddn.com/tinyhttpd%E8%AF%B4%E6%98%8E%E5%9B%BE.png 有关于进程间通信的内容请阅读《APUE》

最后还要关闭浏览器的链接,因为http是无连接的协议

About

tinyhttpd源码解析

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载