I can't figure out why I am failing 2 tests in check50.
Here is my code:
bool load(FILE* file, BYTE** content, size_t* length)
{
if (file == NULL) {
return false;
}
int temp = 0;
*length = 0;
BYTE* temporaryByte = malloc(1);
while(fread(temporaryByte, 1, 1, file) == 1) {
temp++;
}
*length = temp;
fseek(file, 0, SEEK_SET);
BYTE* memory = malloc(*length);
for (int i = 0; i < *length; i++) {
fread(memory + i, 1, 1, file);
}
*content = memory;
return true;
}
const char* lookup(const char* path)
{
// TODO
const char* substring = strrchr(path, 46);
printf("substring ->%s<- is substring\n", substring);
if (strcasecmp((substring), ".css") == 0) {
printf("css\n");
return "text/css";
}
else if (strcasecmp((substring), ".html") == 0) {
printf("html\n");
return "text/html";
}
else if (strcasecmp((substring), ".gif") == 0) {
printf("gif\n");
return "image/gif";
}
else if (strcasecmp((substring), ".ico") == 0) {
printf("ico\n");
return "image/x-icon";
}
else if (strcasecmp((substring), ".jpg") == 0) {
printf("jpg\n");
return "image/jpeg";
}
else if (strcasecmp((substring), ".js") == 0) {
printf("js\n");
return "text/javascript";
}
else if (strcasecmp((substring), ".php") == 0) {
printf("php\n");
return "text/x-php";
}
else if (strcasecmp((substring), ".png") == 0) {
printf("png\n");
return "image/png";
}
else {
printf("other\n");
return NULL;
} }
bool parse(const char* line, char* abs_path, char* query)
{
char* placeholder = "/";
if (strncmp(line, "GET ", 4) != 0) {
error(405);
return false;
}
if (strncmp(line + 4, placeholder, 1) != 0) {
error(501);
return false;
}
const char* index = (line + 4);
while(strncmp(index, " ", 1) != 0) {
if (strncmp(index, "\"", 1) == 0) {
error(400);
return false;
}
index++;
}
index++;
if (strncmp(index, "HTTP/1.1", 8) != 0) {
error(505);
return false;
}
int i = 0;
index = line + 4;
while(strncmp(index, " ", 1) != 0 && strncmp(index, "?", 1) != 0) {
*(abs_path + i) = *(index);
index++;
i++;
}
i = 0;
if (strncmp(index, "?", 1) == 0) {
index++;
while(strncmp(index, "\r\n", 2) != 0) {
*(query + i) = *(index);
i++;
index++;
}
}
else {
query = "";
}
return true;
}
char* indexes(const char* path)
{
char* memory = malloc(strlen(path) + 1);
sprintf(memory, "%s/index.html", path);
FILE* file = fopen(memory, "r");
if (file != NULL) {
return memory;
}
sprintf(memory, "%s/index.php", path);
file = fopen(memory, "r");
if (file != NULL) {
return memory;
}
sprintf(memory, "%sindex.html", path);
file = fopen(memory, "r");
if (file != NULL) {
return memory;
}
sprintf(memory, "%sindex.php", path);
file = fopen(memory, "r");
if (file != NULL) {
return memory;
}
printf("memory ->%s<- memory\n", memory);
return NULL;
}`
Here are the errors:
check50 2015.fall.pset6.server2 server.c
:) 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 /test/ outputs /test/index.html
:) Requesting directory containing index.php outputs index.php
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
https://sandbox.cs50.net/checks/a7deaa4858c1480ebc8162b0c5e6f390
Thank-you very much for helping me!
-Jon
:( Requesting /test/ outputs /test/index.html
would be from indexes. This parse does not reflect any change to null-terminate abs_path as discussed in earlier post. That's a potential cause of "two files in a row" failure. Why don't you troubleshoot those two things, and post an answer when you find and fix the problems? – DinoCoderSaurus Aug 11 '16 at 20:35