Skip to main content
3 of 5
added 2 characters in body

Server.c, errors in Check50, probably a problem with load function

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 recieve are:

  1. :( Requesting cat.jpg returns 200, image/jpeg, and correct image
  2. :( Requesting /test/ outputs /test/index.html
  3. :( Requesting directory containing index.php outputs index.php
  4. :( 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 Function:

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:

char* indexes(const char* path)
{
    // ensure path is readable and executable
    if (access(path, R_OK | X_OK) == -1)
    {
        error(403);
        return NULL;
    }
    char* path1 = malloc(60);
    if (path1 == NULL)
    {
        return NULL;
    }
    strcpy(path1, path);
    char* path2 = malloc(50);
    if (path2 == NULL)
    {
        return NULL;
    }
    strcpy(path2, path);
    strcat(path1, "index.php");
    strcat(path2, "index.html");
    // check if index.html or index.php exists and returns else returns NULL
    if (access(path1, R_OK) != -1)
    {
        return path1;
    }
    else if (access(path2, R_OK) != -1)
    {
        return path2;
    }
    else
    {
        return NULL;   
    }    
}