1

I am having trouble with the pset6 parse function. I used check50 after implementing parse, and everything checks out except I get these two errors:

:( HTTP/1.0 returns error code 505 \ expected output, but not "HTTP/1.1 501 Not Implemented\r\nContent..."

:( Requesting non-existant file returns error code 404 \ expected output, but not "HTTP/1.1 501 Not Implemented\r\nContent..."

I am also not sure if I am setting abs_path and query correctly. It would be fantastic if y'all could help me resolve these problems. Thanks!

Here is my code:

//check if method is GET
if (line[0] != 'G' || line[1] != 'E' || line[2] != 'T' || line[3] != (char) 32)
{
    error(405);
    return false;
}

//check if request target contains "
if (strchr(line, '"') != NULL)
{
    error(400);
    return false;
}

//make sure request-line begins with '/'
for (int i = 0, j = strlen(line); i < j; i++)
{
    if (line[i] == (char) 32)
    {
        if (line[i + 1] != '/')
        {
            error(501);
            return false;
        }
    }
}

//check if http version is HTTP/1.1
for (int i = 4, j = strlen(line); i < j; i++)
{
    if (line[i] == (char) 32)
    {
        if (line[i + 1] != 'H' || line[i + 2] != 'T' || line[i + 3] != 'T' || line[i + 4] != 'P' || line[i + 5] != '/' || line[i + 6] != '1' || line[i + 7] != '.' || line[i + 8] != '1')
        {
            error(505);
            return false;
        }
    }
}

//variable to store abs_path 
char* temp_ab_path = NULL;

//iterate over line to get abs-path
for (int i = 5, j = 32; line[i] != (char) j; i++)
{
    //concatenate characters to abs_path
    if (line[i] == (char) 32)
    {
        error(400);
        return false;
    }
    strcat(temp_ab_path, &line[i]);
}

//variable to store optional query
char* q = NULL;

//iterate over line to see if there is a query
for (int i = 0, j = strlen(line); i < j; i++)
{
    if (strcmp(&line[i], "?") == 0)
    {
        for (int k = i, l = 32; line[k] != (char) l; k++)
        {
            //concatenate the character to the string
            strcat(q, &line[k]);
        }
    }
}

//set query and absolute path
abs_path = temp_ab_path;
query = q;
return true;
1

First problem:

for (int i = 0, j = strlen(line); i < j; i++)
{
    if (line[i] == (char) 32)
    {
        if (line[i + 1] != '/')
        {
            error(501);
            return false;
        }
    }
}

For a well formed request-line this always gives error 501.Be the computer. Take the request "GET /cat.html HTTP/1.1" through the loop. Encounter the first space. Is line[i + 1] = '/'? Yes. What happens next? Loop continues. What happens when it encounters another space (and it will)?.....I do not recall if the course material specifically covers how to break out of a loop. You may find this info helpful.

Problem 2:

You are using strcat to populate temp_ab_path and q. But you have not allocated any memory to them. This will cause unpredictable (read wrong) results.

Problem 3:

Since abs_path is a pointer, this abs_path = temp_ab_path; is changing what the local abs_path points to. Which is a problem, because main is expecting it's abs_path to point to the same memory location as when it called parse. Refer to this hint from the instructions

Odds are you’ll find functions like strchr, strcpy, strncmp, strncpy, and/or strstr of help!

This pset really challenges one to exhibit a solid understanding of pointers and memory allocation and learning to use new functions; coalescing everything learned thus far. It is also an excellent opportunity to become intimately acquainted with gdb. You'll find some specifics on using gdb with server here.

You must log in to answer this question.

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