0

My code passes all but 1 test. This is the error:

:( Requesting hello.php? returns 200, text/html, and correct output
   \ expected output, not an exit code of 0

Can you see what the problem is??

bool parse(const char* line, char* abs_path, char* query)
{
    char method[] = "    ";
    strncpy(method, line, 4);
    if (strcasecmp(method, "GET ") != 0) 
    {
        error(405);
        return false;
    }

    // if line includes a '"'
    if (strchr(line, '"') != NULL) {
        error(400);
        return false;
    }

    char* address_start = strchr(line, 47);
    char* address_end = strrchr(line, 32);

    if (strncmp(address_start, "/", 1) != 0) {
        error(501);
        return false;
    }

    if (strcmp(address_end, " HTTP/1.1\r\n") != 0) {
        error(505);
        return false;
    }

    char path[address_end - address_start +1];
    strncpy(path, address_start, address_end - address_start);
    path[address_end - address_start] = '\0';
    // printf("path is first %s\n", path);

    char* name_input = strstr(path, "name=");
    if (name_input != NULL)
    {
        strcpy(query, name_input);
        printf("query is %s\n", query);
        if (strchr(path, '?') != NULL)
        {
            strncpy(abs_path, path, name_input-path-1);
            printf("abs_path is %s\n", abs_path);
            abs_path[name_input-path-1] = '\0';
        }
        else
        {
            strncpy(abs_path, path, name_input-path);
            printf("abs_path is %s\n", abs_path);
            abs_path[name_input-path] = '\0';
        }
    }
    else
    {
        strcpy(abs_path, path);
    }

    return true;
}

1 Answer 1

0

The problem I see here is, you assume if there is a parameter in a request, it will be like "name=something". What if the first parameter is not "name", or there is nothing following question mark? You have to handle those cases as well.

1
  • I should clarify that currently, when I try to open my browser to "/hello.php?", I DO get a "200 OK." I don't see why the test is failing at all.
    – LuNa
    Dec 19, 2016 at 19:51

You must log in to answer this question.

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