0

I am working on the parse function. My code gives the following results in check50 1.

:) request-target of abc/hello.php returns error code 501

:) Requesting cat.exe returns error code 501

:( request-target without starting '/' returns error code 501

\ expected output, not an exit code of 0

I have tried a couple of different codes, but the results are all the same. I can not see why my parse function detects the error in the first 2 cases while fails the third one, since the first 2 cases do not start with '/' either.

Here is part of my code, please advise.

bool parse(const char* line, char* abs_path, char* query)
{
    // Initialize abs_path and query.
    memset(abs_path, '\0', LimitRequestLine + 1);
    memset(query, '\0', LimitRequestLine + 1);

    // Find where /,? and space are.
    char* first_space = strchr(line, '/');
    char* question_mark = strchr(line, '?');
    char* HTTP_version = strstr(line, "HTTP");
    char* target_end = HTTP_version - 1;

    // Extract absolute-path and query.
    if (question_mark == NULL)
    {
        memcpy(abs_path, (first_space + 1), target_end - (first_space + 1));
    }

    else
    {
        memcpy(abs_path, (first_space + 1), question_mark - (first_space + 1));
        memcpy(query, question_mark + 1, target_end - (question_mark + 1));
    }

    // Check line's consistency to the rules.
    // find substrings in line.
    char* get = strstr(line, "GET ");
    if (get == NULL || get != line)
    {
        error(405);
        return false;
    }

    if(strstr(line, "  ") != NULL)
    {
        error(400);
        return false;
    }

    if (strncmp(HTTP_version, "HTTP/1.1", 8) != 0)
    {
        error(505);
        return false;
    }

    char* quotation = strchr(line, '"');
    if(first_space < quotation && quotation < target_end)
    {
        error(400);
        return false;
    }

    if (* (first_space + 1) != '/')
    {
        error(501);
        return false;
    }

    return true;
}
0

At a quick glance, your trouble starts here char* first_space = strchr(line, '/'); Assuming a request-line of "GET /cat.jpg HTTP/1.1\r\n", first_space is "/cat.jpg HTTP/1.1\r\n". What will happen here?

if (* (first_space + 1) != '/') { error(501); return false; }

This message \ expected output, not an exit code of 0 from check50 indicates a seg fault. Probably because in that case first_space is NULL.

If you are looking for the first space, then look for the first space :). There may be other problems in the function, but correcting this problem should change the check50 result.

debug50 and/or gdb [tips here] are the goto tools for troubleshooting server.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .