Parse 在 check50 pset6 中出错

Parse gets error in check50 pset6

我已经为此 pset 编写了解析函数,但 check50 仅检查以下内容是否有效:

:) server.c exists
:) server compiles
:( HTTP/1.0 returns error code 505
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:( Method of 'abcGET' returns error code 405
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:( Method of 'GETabc' returns error code 405
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:( request-target without starting '/' returns error code 501
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:( request-target of abc/hello.php returns error code 501
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:( Requesting cat.exe returns error code 501
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:( Requesting non-existant file returns error code 404
   \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..."
:) Requesting request-target with " returns error code 400
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code

所以基本上只有空格检查在起作用。在比较字符串时,我想就我的代码中的问题所在提供一些指导。我猜函数的使用有问题,但研究了几个小时后,我仍然没有找到解决方案。我是一个完全的初学者,我真的很感激一些帮助。谢谢

这是我的解析函数的代码

bool parse(const char* line, char* abs_path, char* query)
{
    // TODO
    // Get the lenght of line
    int l = strlen(line);

    // Store the place in the string 
    int a = 0;

    // Stores number of total spaces
    int spaces = 0;

    // Iterate in line
    for (int i = 0; i < l; i++)
    {// Look for spaces
        if (isspace(line[i]) == 0)
        {
            spaces++;
        }
    }
    // If there are more than the 2 required spaces
    if (spaces > 2)
    {
        error(400);
        printf("400 Bad Request\n");
        return false;
    }
    // If request - target doen't begin with "/"

    // Look for the fist space
    for (int i = 0; i < l; i++)
    {// Look for space
        if (isspace(line[i]) == 0)
        {
            a = i + 1;
            break;
        }
    }

    if (line[a] != '/')
    {
        error(501);
        printf("501 not implemented\n");
        return false;
    }


    {
        error(405);
        printf("405 Method Not Allowed\n");
        return false;
    }

    // If request-target contains "
    // Will store position of second space
    int b = 0;
    // Look for the second space
    for (int i = a; i < l; i++)
    {
        if (isspace(line[i]) == 0)
        {
            b = i;
            break;
        }
    }
    // Find if there is a " in request-target
    for (int i = a; i < b; i++)
    {
        if ( line[i] == '"')
        {   
            error(400);
            printf("400 Bad Request\n");
            return false;
        }
    }

    // If HTTP version isn't HTTP/1.1
    char http[7] = "";
    for (int i = b + 1; i < (b + 1) + 7/*Chars 
    from the second space till the end of HTTP/1.1*/;i++)
    {
        http[i] = line[i];
    }

    if (strcmp(http, "HTTP/1.1") != 0)
    {
        error(505);
        printf("505 HTTP Version Not Supported\n");
        return false;
    }

    // Store absolute-path at the address in abs_path and the query string at query

    // Separate the query
    // String to store the query
    char query2[8190 + 1] = "";

    // Store the position in line where the query string starts
    int c = 0;
    // Counter for query array
    int q = 0;
    for (int i = b - 1/*before the space after the query*/; i > a/*after the first space*/;i--)
    {
        if (line[i] == '=')
        {
            c = i - 1;
            break;
        }

    }
    // Pass the string to query2
    for (int i = c; i < (b - 1); i++)
    {
        query2[q] = line[i];
        q++;
    }
    // If there is no query, make query2 empty
    if (query2[0] != 'q')
    {
        strcpy(query2, "");
    }

    // Allocate query2 and query at the heap
    query = malloc(sizeof(query2));

    // Copy query2 to query
    strcpy(query, query2);

    // Separate absolute path
    // Declare char array to store absolute path
    char abs1[8190 + 1] = "";

    // Make space at the heap

    abs_path = malloc(sizeof(abs1));

    for (int i = a /*where the / after the first space starts*/; i < (c - 1);
    i++)
    {
        abs1[i] = line[i];
    }

    // Copy abs to abs_path
    strcpy(abs_path, abs1);

    return true;


}

Parse 总是返回“400 Bad Request”,表示条件:

if (spaces > 2)

总是评估为真,那是因为行有两个或更多 space 但那是因为 http 请求的格式要求 space 将方法与请求分开,并且另一个 space 将请求与 http 版本分开。

您需要搜索两个连续的 space,而不是整行中的两个 space。

并且您应该使用推荐的函数而不是大量的 for 循环。