0

I am working on pset6 and I am having a hard time in parse function. I consistently take Error 400, so if anyone can give me a direction where my mistake is hiding it will be great! Below I have my code. Thanks in advance!

bool parse(const char* line, char* abs_path, char* query)

{

//creating pointers to the beginning of path, query and end of line
char *abspath = strstr(line, "/");
char *beg_query = strstr(line, "?");
char *lastspace = strrchr(line, ' ');

//where query ends
int end_of_query= beg_query-lastspace; 

//Connecting query and absolute path to pointers
if (beg_query!=NULL)
{
    strncpy(abs_path, abspath, LimitRequestLine+1); 
    strncpy(query, beg_query+1, LimitRequestLine+1);
}
else 
{
    strncpy(abs_path, abspath, LimitRequestLine+1);
    query[0]='\0'; 
}
if (abs_path[0]!='/')
{
    error(501);
    return false;
}

for (int i=0; i<end_of_query; ++i)
{
    if (query[i]=='"')
    {
        error(400);
        return false;
    }
}

if (strncmp(line, "GET", 3)!=0)
{
    error(405);
    return false;
}

//Testing if the HTTP version is HTTP/1.1

char *http_version=strstr("HTTP/1.1", line);

if (http_version==NULL)
{
    error(505);
    return false;
}

return true; 

}

0

A couple of things are giving you trouble. Think about this line:

int end_of_query= beg_query-lastspace;.

First, if there is a query, this will give you a negative number, since beg_query is before (less than) lastspace. That's trouble. And what if there is no query and beg_query is NULL? The result is meaningless, and not what you want, and that's trouble too. Which leads to trouble here:

for (int i=0; i<end_of_query; ++i)  
{        if (query[i]=='"')
    {
        error(400);
        return false;
    }
}

Since query is a string, treat it like a string. No reason to check char by char when you could strchr(query,'"'); If that doesn't return NULL, give your error(400).

I hope that gets you beyond the 400 problem. Spoiler alert: you are going to have to work on the size argument in your strncpy calls. abs_path, for instance, is going to be everything in line from the first '/'.

  • Now it's working! Thanks for your help! I also changed the strncpy functions as you mentioned using strlen to calculate the length of path and query each time, so now the page is loading without any problem – dean.d Mar 11 '16 at 19:44

You must log in to answer this question.

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