0

i want to implement parse function by breaking the line into 3 parts using strtok() function, store those char* in array and check if they complete conditions we have in pset. But something goes wrong. Here's my code for this function, any kind of help will be apriciated.

bool parse(const char* line, char* abs_path, char* query)
{
    char *aray[3];
    char str[strlen(line)+1]; 
    strncpy( str, line, strlen(line)+1 );
    char* pch;
    pch = strtok(str, " ");
    int index = 0;

    while (pch != NULL)
    {
        aray[index] = pch;
        index++;
        pch = strtok(NULL, " ");
    }

    if (strncmp(aray[0], "GET", 9) != 0)
    {
        error(405);
        return false;
    }
    else if (aray[1][0]!='/')
    {
        error(501);
        return false;
    }
    else if (strchr (aray[1], '"')!=NULL)
    {
        error(400);
        return false;
    }
    else if (strncmp(aray[2], "HTTP/1.1", 7) !=0)
    {
        error(505);
        return false;
    }
    else
    {
        // abs_path = aray[1];
        return true;
    }
}

Made a few general changes, now check50 gives me next errors which i dont understand: https://sandbox.cs50.net/checks/020d4c58104f406d91c2bee120da07d3

1

301 errors generally come when abs_path is NULL. As it is from this function, since abs_path is not built. (Nor query, obviously). And this // abs_path = aray[1]; (uncommented) will not work, because you are setting abs_path to a local pointer, which will be destroyed when control returns to main.

The HTTP/1.0 error is just counting. This else if (strncmp(aray[2], "HTTP/1.1", 7) !=0) evaluates to false. I leave it to you to count it out. I also question this line if (strncmp(aray[0], "GET", 9) != 0). It seems to work, ie check50 doesn't complain, but I question why you are comparing 9 bytes.

| improve this answer | |
  • Thanks for your answer, i thought that something was wrong with that counting. The reason i comparing 9 bytes is because the longest method that could be used is OPTIONS so i guessed to have enough space for that. Is it wrong? I'll be thanksfull if you explain me where i'm wrong. One more point is that i don't get how to implement query, as abs_path could be done using copy so the pointer won't be lost. but how to manage with that ?q=something i'm little bit lost. – Alexander Ismagulov Dec 1 '16 at 12:39
  • Then 9 bytes is fine! Isn't "splitting" aray[1] at '?' a lot like "splitting" line at space? Hint hint. Then abs_path and query could both "be done using copy so the pointer won't be lost". – DinoCoderSaurus Dec 1 '16 at 13:24

You must log in to answer this question.

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