从零快速解析之Tinyhttp源码解析(三)

4.2.2 execute_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';
 //如果是get方法,直接读取舍弃请求头部                                                                                        
 if (strcasecmp(method, "GET") == 0)                                                                                 
  while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */                                          
   numchars = get_line(client, buf, sizeof(buf));                                                                    
 else    /* POST */                                                                                                  
 {        
  //如果是POST方法,则等待捕获头部中Content-Length行                                                                                                       
  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));                                                                    
  }
  //如果参数长度= -1,则说明请求无效                                                                                                                  
  if (content_length == -1) {                                                                                        
   bad_request(client);                                                                                              
   return;                                                                                                           
  }                                                                                                                  
 }                                                                                                                   
 
 //给client回复状态信息                                                                                                                    
 sprintf(buf, "HTTP/1.0 200 OK\r\n");                                                                                
 send(client, buf, strlen(buf), 0);                                                                                  
 
 //创建两个pipe:cgi_output, cgi_input 使父子进程之间可以双向通信                                                                                                        
 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];                                                                                              
  //子进程标准输出,输入分别重定向到cgi_output[1],cgi_input[0]                                                                                                                  
  dup2(cgi_output[1], 1);                                                                                            
  dup2(cgi_input[0], 0);                                                                                             
  close(cgi_output[0]);                                                                                              
  close(cgi_input[1]);
  //meth_env=REQUEST_METHOD=GET或者POST                                                                                              
  sprintf(meth_env, "REQUEST_METHOD=%s", method);                                                                    
  putenv(meth_env);
  //GET从刚刚的URL?后面获取相应参数给环境变量query_env,POST直接设置参数长度给环境变量length_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);                                                                                               
  }
  //执行PREL脚本,注意其通过环境变量传递给脚本参数                                                                                                                  
  execl(path, path, NULL);                                                                                           
  exit(0);                                                                                                           
 } else {    /* parent */ 
                                                                                           
  close(cgi_output[1]);                                                                                              
  close(cgi_input[0]);
  //如果是POST请求方法,则继续读取请求数据,并将改数据传递给子进程,也就是CGI脚本                                                                                              
  if (strcasecmp(method, "POST") == 0)                                                                               
   for (i = 0; i < content_length; i++) {                                                                            
    recv(client, &c, 1, 0);                                                                                          
    write(cgi_input[1], &c, 1);                                                                                      
   }
  //将CGI脚本的输出发送给client,也就是生产的html网页的内容                                                                                                                 
  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);                                                                                          
 }                                                                                                                   
}

这样除了错误处理信息之外,整个TinyHttp的代码逻辑就很清楚了,但是上面的函数也需要强调几个地方:

  • 通过设置环境变量的方式,传递给CGI脚本参数,当然这里也可以选择使用exel系列函数传递参数。(如果你的脚本语言支持获取exel传递的参数的话)
  • waitpid(pid, &status, 0);的作用这里并不是很明显。
  • fork是在线程中调用的,因为没有涉及到锁的使用,所以这里没有任何额外处理。
  • Tinyhttp并没有显示的设置每个子线程为detached state

4.2.3 一些错误处理函数

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

这些都比较简单,属于应用层协议报文请求错误的时候,发送给client的对应信息。这样来说整个Tinyhttp的解析就完全做完了。

5. 最后

TinyHttp非常简单,作为解析的步骤到此结束。之后笔者将会对TinyHttp的功能进行改进,稍微修改部分代码,不断完善下TinyHttp不合理的部分。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值