Web服务器:如何验证请求行

时间:2014-10-31 06:21:39

标签: c http get webserver

我正在进行一项任务,我必须在C中实现一个简单的Web服务器。我已经获得了缺少部分的分发代码。其中一个缺失的部分需要验证请求行。经过研究,我发现:

Request-Line = Method SPACE Request-URI SPACE HTTP-Version CRLF

到目前为止,我的理解是我需要:

    size_t length = strrchr(line, '\n');

        if (strrchr(line, '\r') != length - 1)
        {
            error(400); // bad CRLF
        }

        line[length - 1] = 0;

        // now there isn't anymore CRLF
        length = strchr(line, ' ');
        line[length] = 0;

        char *method = line;
        char *request = line + length + 1;

         length = strchr(request, ' ');
         request[length] = 0;
         char * version = request + length + 1;


         char   *(methods[]) = {"GET", NULL};
         int    it = 0;

         while (strstr(method, methods[it]) == NULL)
         {
              ++it;
              if (methods[it] == NULL)
              {
                  error(405);
              }

              if (request[0] != '/' || strrchr(request, '.') == -1)
              {
                  error(501);
              }

              if (strchr(request, ',') != -1)
              {
                  error(400);
              }     

              if (strstr(version, "HTTP/1.1") == NULL || version[8] == '\0')

              {
                  error(505);
              }
          }

我的方法(pseudocode)是否正确?请详细说明这件事。

0 个答案:

没有答案