I'm still struggling with my parse function. The only check50 test I now fail is the one for error 404, where I get 501 not implemented. I've been on it for hours and hours and can't find the bug. I triple-checked with printfs where the terminating \0 byte is added, and it's always the end of abs_path string.
bool parse(const char* line, char* abs_path, char* query)
{
//check for more than 2 spaces in line
int spaces = 0;
for (int i = 0, lenght = strlen(line); i < lenght; i++)
{
if (line[i] == ' ')
spaces++;
if (spaces > 2)
{
error(400);
return false;
}
}
//check for method
if (strncmp (line, "GET ", 4) != 0)
{
error(405);
return false;
}
//check for '/' at the start request target
char* SP = strchr(line, ' ');
if (SP[1] != '/')
{
error(501);
return false;
}
//check for " in the request target
//otherwise copy to abs_path
int iter = 1;
while (SP[iter] != ' ' && SP[iter] != '?')
{
if (SP[iter] == '"')
{
error(400);
return false;
}
abs_path[iter - 1] = SP[iter];
iter++;
}
abs_path[iter - 1] = '\0';
//if query exists, copy to query
if (SP[iter] == '?')
{
int query_iter = 0;
while (SP[iter] != ' ')
{
if (SP[iter] == '"')
{
error(400);
return false;
}
query[query_iter] = SP[iter];
iter++;
query_iter++;
}
query[query_iter] = '\0';
}
else
query[0] = '\0';
//check for http version
SP = strrchr(line, ' ');
if (strncmp(SP, " HTTP/1.1", 9) != 0)
{
error(505);
return false;
}
error(501);
return false;
}
error(501)
in your server, 2 in your parse, 1 in main after it returns from lookup. Find out (with gdb or printf) which one is being called. Then you can focus your attention at the right point in the code, and you can work it backwards from there. When you find the problem, be sure to write an answer so others can benefit and learn from your experience.