Tinyhttpd源码学习(httpd.c)<四>

写完startup,下一个就是accept_request,这里是处理请求的主要逻辑


void accept_request(void *arg)
{
    int client = (intptr_t)arg;

//----------------------  client_sock的fd
    char buf[1024];

//---------------------  读取sock内容的缓冲区
    size_t numchars;

//--------------------    从buf读取一行信息
    char method[255];

//-------------------     从http消息中获得的请求方式
    char url[255];

//-------------------     从http消息中获取的url
    char path[512];

//-------------------      需要访问的本地文件路径
    size_t i, j;
    struct stat st;

//------------------   需要访问的本地文件的状态


    int cgi = 0;      /* becomes true if server decides this is a CGI

//----------------- 是否是cgi请求
                       * program */
    char *query_string = NULL;
//-----------------  查询字符串

//----------------         变量声明结束,开始工作


    numchars = get_line(client, buf, sizeof(buf));

//---------------  读取第一行信息
    i = 0; j = 0;
    while (!ISspace(buf[i]) && (i < sizeof(method) - 1))
    {
        method[i] = buf[i];
        i++;
    }
    j=i;
    method[i] = '\0';
//-------------    http协议,最开始的字符的请求方式如:GET/POST,存入method这个字符数组中

    if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
    {
        unimplemented(client);
        return;
    }
//-------------------- 不是get请求,也不是post请求,则调用unimplemented方法来处理

    if (strcasecmp(method, "POST") == 0)
        cgi = 1;
//---------------------  post请求,是cgi程序

    i = 0;
    while (ISspace(buf[j]) && (j < numchars))
        j++;
    while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars))
    {
        url[i] = buf[j];
        i++; j++;
    }
    url[i] = '\0';
//---------------------  http协议,请求方式后面是空格,空格后面就是url , 将url放入字符数组url中

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

//---------------------  如果是GET请求,url问号后面跟的是请求的参数列表,放入变量query_string
    sprintf(path, "htdocs%s", url);

//---------------------- 为path赋值 这里是htdocs/.../.../,默认需要访问的内容放在htdocs下面

    if (path[strlen(path) - 1] == '/')
        strcat(path, "index.html");

//--------------------  如果path 以 / 结尾, 则是path/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);

//---------------------- 硬盘上找不到 path的文件,则调用not_found处理
    }
    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 = 1;

//---------------------- 应该上找到了path的文件,如果有可执行权限,则认定为cgi程序。
        if (!cgi)
            serve_file(client, path);

//--------------------- 不是cgi程序,调用serve_file响应请求
        else
            execute_cgi(client, path, method, query_string);

//--------------------- cgi程序,则执行cgi程序,返回结果
    }


    close(client);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值