0

I'm so close to finishing this, my only problem is that images won't display! Instead I get the broken image icon. The first check50 works fine, but the second gives me this:

:) 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
:) Requesting directory containing index.php outputs index.php
:( Requesting two files in a row (cat.html then cat.jpg) succeeds

I'm guessing it's something to do with my load function? But then how come all other types of file still work?

Here's the code for my load function:

bool load(FILE* file, BYTE** content, size_t* length)
{
    int memlength = 2;

    *content = malloc(memlength);
    if (*content == NULL)
        return false;

    **content = '\0';

    BYTE buffer[1];

    int count = 0;

    while (fread(buffer, 1, 1, file) == 1)
    {
        if (feof(file))
            break;

        if (count != 0 && count >= memlength - 1)    
        {
            memlength = 2 * memlength;
            *content = realloc(*content, memlength);
            if (*content == NULL)
                return false;
        }

        strcat(*content, buffer);
        buffer[0] = '\0';

        count++;

        *((*content) + count) = '\0';
    }

    *length = count;

    return true;
}
0

That approach won't work because binary files have zero bytes all over them, which is the NULL terminator for strings (0 == '\0').

This is one implementation of strcat():

char * strcat(char *dest, const char *source)
{
    strcpy(&dest[strlen(dest)], source);
    return dest;
}

This is one implementation of strcpy():

char *strcpy(char *dest, const char *source)
{
    char *s = dest;
    while ((*s++ = *source++) != 0)
    ;
    return (dest);
}

Can you see how zeroes in the wrong place will mess up with strlen(), strcpy() etc?

Using them you'll basically lose all the zeroes... they never stay inside *content.

You'll have to look into other approaches.

Hint: maybe make it simpler by using the syntax you've already learned how to use here: *((*content) + count) = '\0';? You could always do *((*content) + count) = buffer[0]; instead of strcat()


If this answers your question, please click the check mark to accept!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .