I have been working on this pset for a long time. When I run check50 now, there are three remaining errors: :( Requesting hello.php? returns 200, text/html, and correct output \ expected output, not an exit code of 0 :( Requesting /test/ outputs /test/index.html :( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
All errors relate to requests for php files but I am not sure what function is giving rise to the issue. I think it could be my parse or my indexes functions (see below). Any help on where the errors may lie would be greatly appreciated. My indexes function is as follows:
char* indexes(const char* path)
{
char* html = "/index.html";
char* php = "/index.php";
char* html_file = malloc(strlen(html) + strlen(path) + 1);
strcpy(html_file, path);
strcat(html_file, html);
char* php_file = malloc(strlen(php) + strlen(path) + 1);
strcpy(php_file, path);
strcat(php_file, php);
if(access(html_file, F_OK) == 1)
{
return html_file;
}
else if(access(php_file, F_OK) == 1)
{
return php_file;
}
return NULL;
And my parse function is:
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
//check that method is GET.
//First create a usable copy of line
char req_line[strlen(line)];
//make copy of line and store in req_line
strcpy(req_line, line);
//parse the line to the first space to get method
char* method = strtok(req_line, " ");
//check whether method is get and if not return error and false
if (strcmp(method, "GET") != 0)
{
error(405);
return false;
}
//we extract the request target which should run to the next space
char* req_tgt = strtok(NULL, " ");
// check that request target begins with /
if (req_tgt[0] != '/')
{
error(501);
return false;
}
//check whether request target contains a " and if it does return an error and false
if (strchr(req_tgt, '"') != NULL)
{
error(400);
return false;
}
//make sure that the correct version of HTTP is being used
char* http_version = strtok(NULL, " ");
if(strcmp(http_version, "HTTP/1.1\r\n") != 0)
{
error(505);
return false;
}
//check whether there is a ? in req_tgt
const char* quest_mark = strchr (req_tgt, '?');
//const char* second_space = strrchr(req_tgt, ' ');
//if there is none then store abs_path and query
if (quest_mark == NULL)
{
char* temp = strtok (req_tgt, " ");
strcpy(abs_path, temp);
strcat(abs_path, "\0");
strcpy (query, "");
strcat(query, "\0");
printf ("absolut path when no ? or ? but no query is.......%s\n", abs_path);
printf ("query when no ? or ? but no query is .......%s\n", query);
}
else if (strcmp(quest_mark + 1, " ") == 0)
{
char* temp = strtok (req_tgt, "?");
strcpy(abs_path, temp);
strcat(abs_path, "\0");
strcpy (query, "");
strcat(query, "\0");
printf ("absolut path when no ? or ? but no query is.......%s\n", abs_path);
printf ("query when no ? or ? but no query is .......%s\n", query);
}
else
{
char* temp = strtok (req_tgt, "?");
strcpy(abs_path, temp);
strcat(abs_path, "\0");
char* query_temp = strtok (NULL, " ");
strcpy(query, query_temp);
strcat(query, "\0");
printf ("absolut path when ? is.......%s\n", abs_path);
printf ("query when ? ois .......%s\n", query);
}
return true;
}