I am working on the parse function. My code gives the following results in check50 1.
:) request-target of abc/hello.php returns error code 501
:) Requesting cat.exe returns error code 501
:( request-target without starting '/' returns error code 501
\ expected output, not an exit code of 0
I have tried a couple of different codes, but the results are all the same. I can not see why my parse function detects the error in the first 2 cases while fails the third one, since the first 2 cases do not start with '/' either.
Here is part of my code, please advise.
bool parse(const char* line, char* abs_path, char* query)
{
// Initialize abs_path and query.
memset(abs_path, '\0', LimitRequestLine + 1);
memset(query, '\0', LimitRequestLine + 1);
// Find where /,? and space are.
char* first_space = strchr(line, '/');
char* question_mark = strchr(line, '?');
char* HTTP_version = strstr(line, "HTTP");
char* target_end = HTTP_version - 1;
// Extract absolute-path and query.
if (question_mark == NULL)
{
memcpy(abs_path, (first_space + 1), target_end - (first_space + 1));
}
else
{
memcpy(abs_path, (first_space + 1), question_mark - (first_space + 1));
memcpy(query, question_mark + 1, target_end - (question_mark + 1));
}
// Check line's consistency to the rules.
// find substrings in line.
char* get = strstr(line, "GET ");
if (get == NULL || get != line)
{
error(405);
return false;
}
if(strstr(line, " ") != NULL)
{
error(400);
return false;
}
if (strncmp(HTTP_version, "HTTP/1.1", 8) != 0)
{
error(505);
return false;
}
char* quotation = strchr(line, '"');
if(first_space < quotation && quotation < target_end)
{
error(400);
return false;
}
if (* (first_space + 1) != '/')
{
error(501);
return false;
}
return true;
}