Me again. I've basically sorted out most of the issues now. I can load cat.html normally in the browser and indexes definitely works. From testing, I believe that my parse function is still fine.
Check50:
:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat.html returns 200, text/html, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat2.HTML returns 200, text/html, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat3.HtMl returns 200, text/html, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting cat.gif returns 200, image/gif, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting test.css returns 200, text/css, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting test.js returns 200, text/javascript, and correct file
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting hello.php returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:( Requesting hello.php? returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:( Requesting hello.php?name=Alice returns 200, text/html, and correct output
\ expected output, not an exit code of 0
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
:( Requesting directory containing index.php outputs index.php
\ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
\ expected an exit code of 0, not output of "X-Powered-By: PHP/5.5.7\r\nContent-type..."
As can be seen, apparently my program doesn't exit cleanly and instead outputs this everytime.
LOOKUP CODE REMOVED FOR ACADEMIC HONESTY PURPOSES:
SOLUTION:
Thanks to DinoCoderSaurus and others who offered a lot of help, I realised where I went wrong in my lookup function. I got confused between the return value of strcasecmp and strcasestr and so I wasn't getting the proper output. I altered the condition so that it checks whether it's not equal to NULL and it works fine now.
MINOR ISSUE:
This is relating to my parse function. For some reason, this check50 test fails when one section of code is placed in one location, but passes when it is placed in another location.
bool parse(const char* line, char* abs_path, char* query)
{
//no errors in parse.
int length = strlen(line);
int space_count = 0;
for (int i = 0; i < length; i++)
{
if (line[i] == (char) 32)
{
space_count++;
}
}
if (space_count != 2)
{
error(400);
return false;
}
char* method = malloc(5 * sizeof(char));
strncpy(method, line, 4);
if (strstr(method, "GET ") == NULL)
{
error(405);
return false;
}
free(method);
if (line[4] != '/')
{
error(501);
return false;
}
char* question_mark = strchr(line, '?');
char* slash = strchr(line, '/');
char* HTTP = strstr(line, "HTTP/1.1\r\n");
//if another version of HTTP is used, the server can't handle it.
//HTTP check works here.
if (HTTP == NULL)
{
error(505);
return false;
}
char* abs_path_copy = malloc(5000 * sizeof(char));
char* query_copy = malloc(5000 * sizeof(char));
if (question_mark != NULL)
{
strncpy(abs_path_copy, slash, question_mark - slash);
for (int i = question_mark - slash; i < strlen(abs_path + 1); i++)
{
abs_path_copy[i] = '\0';
}
if (question_mark + 1 == NULL)
{
strcpy(query_copy, "");
}
else
{
strncpy(query_copy, question_mark + 1, (HTTP - question_mark - 2));
query_copy[HTTP - 2 - question_mark] = '\0';
}
}
else
{
strncpy(abs_path_copy, slash, HTTP - slash - 1);
for (int i = HTTP - slash - 1; i < strlen(abs_path_copy + 1); i++)
{
abs_path_copy[i] = '\0';
}
}
strcpy(abs_path, abs_path_copy);
strcpy(query, query_copy);
char* quot_mark = strchr(line, '\"');
if (quot_mark != NULL)
{
error(400);
return false;
}
//HTTP check doesn't work here.
return true;
}
The two positions are marked with HTTP check comments. Can anyone explain why my program behaves like this?