I'm almost done with server.c but can't seem to find the last error which doesn't allow me to pass check50.
I tried to analyse the existing responses to similar questions but couldn't use them to find my bug. Any help would be very much appreciated!
The errors I receive are:
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
:( 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
Given these errors I suppose there is some existing problem with jpg's and php's. The second one is strange though because I checked with GDB that the indexes function returns the correct path as a result when asked for index.html.
load()
bool load(FILE* file, BYTE** content, size_t* length)
{
*content = NULL;
*length = 0;
*content = malloc(512 * sizeof(BYTE));
if (*content == NULL)
{
return false;
}
size_t size = fread(*content, sizeof(BYTE), 512, file);
while (size > 0)
{
*length += size;
*content = realloc(*content, *length * sizeof(BYTE));
if (*content == NULL)
{
return false;
}
size = fread(*content + *length, sizeof(BYTE), 512, file);
}
return true;
}
EDIT #1 My jpg convertion in the lookup function:
else if (strcasecmp(final, ".jpg") == 0)
{
mime = "image/jpeg";
}
The error in Check50 is:
expected an exit code of 0, not standard error of "======= Backtrace: =========\n======= M..."
except for index.php where the error is:
expected output, not standard error of "======= Backtrace: =========\n======= M..."
My indexes function seems to be ok, here it is:
// ensure path is readable and executable
if (access(path, R_OK | X_OK) == -1)
{
error(403);
return NULL;
}
char *new_path = malloc(strlen(path) + 2);
strcpy (new_path, path);
char* path1 = malloc(51);
if (path1 == NULL)
{
return NULL;
}
strcpy(path1, new_path);
char* path2 = malloc(52);
if (path2 == NULL)
{
return NULL;
}
strcpy(path2, new_path);
strcat(path1, "/index.php");
strcat(path2, "/index.html");
path1[strlen(new_path) + strlen("/index.php")] = '\0';
path2[strlen(new_path) + strlen("/index.html")] = '\0';
// check if index.php or index.html exists and returns. Else return NULL
if (access(path1, R_OK) != -1)
{
free(path2);
return path1;
}
else if (access(path2, R_OK) != -1)
{
free(path1);
return path2;
}
else
{
free(path1);
free(path2);
return NULL;
}