I think these checks are related to indexes but I can't figure out where I have went wrong.
:) server.c exists
:) server compiles
:) Requesting cat.jpg returns 200, image/jpeg, and correct image
:) Requesting cat.html returns 200, text/html, and correct file
:) Requesting cat2.HTML returns 200, text/html, and correct file
:) Requesting cat3.HtMl returns 200, text/html, and correct file
:) Requesting cat.gif returns 200, image/gif, and correct file
:) Requesting favicon.ico returns 200, image/x-icon, and correct file
:) Requesting test.css returns 200, text/css, and correct file
:) Requesting test.js returns 200, text/javascript, and correct file
:) Requesting hello.php returns 200, text/html, and correct output
:) Requesting hello.php? returns 200, text/html, and correct output
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html \ expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
:( Requesting directory containing index.php outputs index.php \ expected output, not standard error of "======= Backtrace: =========\n======= M..."
:) Requesting two files in a row (cat.html then cat.jpg) succeeds https://sandbox.cs50.net/checks/6a7b17061cfe4ae1a19ea8f7f3617c55
INDEXES
char* indexes(const char* path)
{
char* temp_path = malloc(sizeof(path));
memcpy(temp_path, path, strlen(path) + 1);
char* php_index = "/index.php";
char* html_index = "/index.html";
char* php_path = malloc(sizeof(path) + sizeof(php_index) + 1);
char* html_path = malloc(sizeof(path) + sizeof(html_index) + 1);
memcpy(php_path, temp_path, strlen(path) + 1);
memcpy(html_path, temp_path, strlen(path) + 1);
strcat(php_path, php_index);
strcat(html_path, html_index);
if(access(php_path, F_OK) != -1)
{
return php_path;
}
else if(access(html_path, F_OK) != -1)
{
return html_path;
}
else
{
return NULL;
}
}
LOAD
bool load(FILE* file, BYTE** content, size_t* length)
{
fseek(file, 0, SEEK_END);
*length = ftell(file);
rewind(file);
char* fstore = malloc(*length);
fread(fstore, *length, 1, file);
*content = fstore;
return true;
}
LOOKUP
const char* lookup(const char* path)
{
char extension[6];
char* css = ".css";
char* html = ".html";
char* gif = ".gif";
char* xicon = ".ico";
char* jpeg = ".jpg";
char* java = ".js";
char* php = ".php";
char* png = ".png";
char* location = strrchr(path, '.');
int i = 0;
while(location[i] != '\0')
{
extension[i] = location[i];
i++;
}
printf("extension - %s\nLocation - %s\n", extension, location);
if(strcasecmp(css, extension) == 0)
{
return "text/css";
}
else if(strcasecmp(html, extension) == 0)
{
return "text/html";
}
else if(strcasecmp(gif, extension) == 0)
{
return "image/gif";
}
else if(strcasecmp(xicon, extension) == 0)
{
return "image/x-icon";
}
else if(strcasecmp(jpeg, extension) == 0)
{
return "image/jpeg";
}
else if(strcasecmp(java, extension) == 0)
{
return "text/javascript";
}
else if(strcasecmp(php, extension) == 0)
{
return "text/x-php";
}
else if(strcasecmp(png, extension))
{
return "image/png";
}
else
{
return NULL;
}
}
PARSE
bool parse(const char* line, char* abs_path, char* query)
{
char line_copy[LimitRequestLine + 1];
strcpy(line_copy, line); // Copy line for manipulation
char* method = strtok(line_copy, " "); // Parse at first space to get method
char* req_target = strtok(NULL, " "); // Parse at second space to get abs_path
char* version = strtok(NULL, "\r"); // Parse at /r to get version
int compare;
printf("line_copy = %s\nMethod = %s\nreq_target = %s\nVersion = %s\n", line_copy, method, req_target, version);
compare = strcmp(method, "GET"); // Check method is 'GET'. If not return error.
if(compare != 0)
{
error(405);
return false;
}
if(req_target[0] != '/')
{
error(501);
return false;
}
if(strchr(req_target, '"') != NULL)
{
error(400);
return false;
}
compare = strcmp(version, "HTTP/1.1");
if(compare != 0)
{
error(505);
return false;
}
if(strchr(req_target, '?') != NULL)
{
char* temp = strtok(req_target, "?");
strcpy(abs_path, temp);
temp = strtok(NULL, "");
if(temp != NULL)
{
strcpy(query, temp);
}
}
else
{
strcpy(abs_path, req_target);
query = "";
}
return true;
}