剖析tinyhttpd服务器

tinyhttpd是一个小型的http服务器,虽然小但是功能也多,剖析tinyhttpd有助于理解http服务器的工作流程,加深对web服务器的认识。
一、原理图
这里写图片描述

这里写图片描述

二、源码剖析
下面是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))                   //ISspace宏用来判断是否为空格

#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];                                 //保存http请求行中使用的方法
char url[255];                                    //保存请求资源的路径
char path[512];                             
size_t i, j;
struct stat st;                                   //用来记录客户端所请求的资源的信息
int cgi = 0;                                      //用来标记是不是需要运行cgi程序
char *query_string = NULL;                        //记录参数的地址

numchars = get_line(client, buf, sizeof(buf));     //获取请求行(method url http版本号)
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"))   //判断是不是GET或POST方法(只实现了GET和POST方法)
{
  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)              //判断是不是GET方法(GET方法可以带参也可以不带参,路径和参数之间以?分隔)
{
  query_string = url;
  while ((*query_string != '?') && (*query_string != '\0'))     //寻找"?"字符
   query_string++;
  if (*query_string == '?')                       //如果有“?”则表示GET是带参的
  {
   cgi = 1;                                       //带参数的GET方法使用CGI进行处理
   *query_string = '\0';                
   query_string++;                                //用来记录参数的起始位置
  }
}

sprintf(path, "htdocs%s", url);                  //将请求资
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值