0

This is my parse function: I keep getting a 505 error and I can't wrap my head around what is going wrong. Why can't my code figure out that the line is GET / HTTP/1.1

I hope someone could help me out here :)

bool parse(const char* line, char* abs_path, char* query)
{
 char *abspath = strstr(line, "/");
 char *boq= strstr(line, "?");
 char *lastspace = strrchr(line, ' ');

// Where query ends
int eoq = boq-lastspace; 

// Connecting query and absolute path to pointers
if (boq != NULL)
{
    // Copy the strings of absolute path
    strncpy(abs_path, abspath, LimitRequestLine+1); 

    // Copy the strings of query
    strncpy(query, boq + 1, LimitRequestLine+1);
}
else 
{
    strncpy(abs_path, abspath, LimitRequestLine+1);
    query[0]='\0'; 
}

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

// Target
if (abs_path[0]!='/')
{
    error(501);
    return false;
}

// Request
for (int i=0; i<eoq; ++i)
{
    if (strchr(query, '"')!=0)
    {
        error(400);
        return false;
    }
}

// Validate http-version token
if (strncmp(line, "HTTP/1.1", 4))
{
    // reply HTTP Version Not Supported
    error(505);
    return false;
}
return true; 
}
1

Since you are always "getting a 505", this seems like a good place to start:

if (strncmp(line, "HTTP/1.1", 4))
{
    // reply HTTP Version Not Supported
    error(505);
    return false;
}

according to man strncmp

it compares the only first (at most) n bytes of s1 and s2.

and

It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

The first four bytes of line (assuming "GET ") compared to the first four bytes of "HTTP/1.1" will never be equal, therefore will always return a non-zero integer. A non-zero integer is always "true". Your parse always returns 505. QED.

gdb should be your best friend once you get past this problem. Find tips here. Very helpful to set breaks at the function calls and evaluate the returned values, especially since pointers are the point. Your values can look fine inside a function, but if the wrong value is returned to the caller, failures can occur, perhaps not seen in a browser, but always detected by check50.

If you get stuck on a particular function, post a new question about the specific function and specific problem. Good luck!

You must log in to answer this question.

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