I thought my parse() function was looking pretty good, but I keep consistently getting the same check50 (server1) error:
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 501 Not Implemented\r\nContent..."
I'm failing almost everything on server2 which is not really surprising I guess! Thank you for any advice you can provide.
Here is my code:
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
// iterate char by char or string by string to make sure it meets specification
// deal with query if any to store a string
// check for GET method
if (strncmp(line, "GET ", 4) != 0)
{
error(405);
return false;
}
if (line[4] != '/')
{
error(501);
return false;
}
// find request-target
char* req_beg = strchr(line, '/');
char* req_end = strchr(req_beg, ' ');
if ((req_beg == NULL) || (req_end == NULL))
{
error(400);
return false;
}
size_t reqlength = req_end - req_beg;
char request_target[reqlength];
strncpy(request_target, req_beg, reqlength);
request_target[reqlength] = '\0';
// check for '"' in request_target
char* quotecheck = strchr(request_target, '\"');
if (quotecheck != NULL)
{
error(400);
return false;
}
char* querybegin = strchr(request_target, '?');
// no ? found
if (querybegin == NULL)
{
strcpy(abs_path, request_target);
query[0] = '\0';
}
else
{
size_t pathlength = (querybegin - request_target) + 1;
char path[pathlength];
strncpy(path, request_target, pathlength);
path[pathlength] = '\0';
strcpy(query, querybegin + 1);
}
// find HTTP version
char* version = req_end + 1;
// size_t version_length = strlen(version);
// // check for length of "HTTP/1.1CRLF"
// if (version_length != 12)
// {
// error(505);
// return false;
// }
// // check that there are no more spaces after the HTTP version
// char* extra_version = strchr(version, ' ');
// if (extra_version != NULL)
// {
// error(505);
// return false;
// }
// check to make sure version is exactly as shown
char* goodhttp = strstr(version, "HTTP/1.1");
if (goodhttp == NULL)
{
error(505);
return false;
}
error(501);
return false;
}