So far my program passes all but 2 of check50 server2:

:( Requesting cat.jpg returns 200, image/jpeg, and correct image

:( Requesting two files in a row (cat.html then cat.jpg) succeeds

I think they result from the same problem: loading jpg file.

I have gone through some question replies on this website and found out fgetc() can not successfully deal with binary file, such as jpg. But I have yet seen suggestions regarding how to fix it. And because requesting a jpg via curl is a mess, gdb can't help to inspect.

I'd be grateful for advice!

Here is my load function:

    /**
     * Loads a file into memory dynamically allocated on heap.
     * Stores address thereof in *content and length thereof in *length.
     */
    bool load(FILE* file, BYTE** content, size_t* length)
    {
        if (file == NULL)
        {
            return false;
        }
        
        char* buffer = malloc(1);
        *length = 0;
       
        for (int read = fgetc(file); read != EOF; read = fgetc(file))
        {
            buffer[*length] = read;    
            *length = *length + 1;
            buffer = realloc(buffer, *length + 1);
        }
     
        *content = &buffer[0];
    
        return true;
    }