1

I've been grinding on this pset6 problem for days. I've implemented all four functions thus far. After I had only implemented lookup and parse, I passed the server1 check50 so I assume both of those are not the issue.

However, I can't seem to get images to load correctly since completing the load and indexes functions. I do however get a 200 ok response from the server when running it myself. Here is my output from the server2 check50.

:) 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

Here is the source code for my load function:

bool load(FILE* file, BYTE** content, size_t* length)
{
    //create variables for number in buffer, capacity of buffer and current count in buffer
    unsigned int count = 0;
    unsigned int capacity = 0;
    char* buffer = NULL;
    int c;

    for (c = fgetc(file); c != EOF; c = fgetc(file))
    {
        //determine the new capacity
        if (count + 1 > capacity)
        {
            if (capacity == 0)
                capacity = 2;
            else
                capacity *= 2;

            //extend the capacity of the buffer
            char* temp = realloc(buffer, capacity * sizeof(char));
            if (temp == NULL)
            {
                free(buffer);
                return false;
            }
            buffer = temp;
        }
        buffer[count] = c;
        count++;
    }
    //minimize the buffer to compensate for doubling 
    char* storage = malloc((count + 1) * sizeof(char));
    strncpy(storage, buffer, count);
    free(buffer);


    //set content and lenth to storage and count
    *content = storage;
    *length = count;
    return true;
}

And here is the code for my index function

char* indexes(const char* path)
{
    //get the path length
    int pathLength = strlen(path);

    //malloc enough space on heap for php path 
    char* phpIndexPath = malloc((pathLength + 11) * sizeof(char));

    //check to make sure malloc succeeded 
    if (phpIndexPath == NULL)
    {
        return NULL;
    }

    //append index extension
    strcpy(phpIndexPath, path);
    strcat(phpIndexPath, "/index.php");

    if (access(phpIndexPath, F_OK) == -1)
        free(phpIndexPath);
    else 
        return phpIndexPath;

    char* htmlIndexPath = malloc((pathLength + 12) * sizeof(char));

    //check to make sure malloc succeeded 
    if (htmlIndexPath == NULL)
    {
        return NULL;
    }

    //append index extension
    strcpy(htmlIndexPath, path);
    strcat(htmlIndexPath, "/index.html");

    if (access(htmlIndexPath, F_OK) == -1)
    {
        free(htmlIndexPath);
        return NULL;
    }
    else
    {
        return htmlIndexPath;
    }
}

1 Answer 1

1

EDIT Sorry again for the misdirect. The elusive 1 eludes me at least once a day. This strncpy(storage, buffer, count); is causing a problem. Content can be anything. And content can have bytes with value of 0 (which is what the null-terminator is). (The 5th byte of cat.jpg is one such). In most cases (all cases?) content is not a c string. The strncpy command, meanwhile, expects the dest and src arguments to be strings. So it is only going to copy buffer into storage up to the first null byte. memcpy would be a better function to use. From the man page:

The memcpy() function copies n bytes from memory area src to memory area dest.


ORIGINAL (WRONG) ANSWER Isn't *length one byte bigger than the length of the input file, since you are incrementing count before you detect EOF? It needs to be exact for image files particularly, where every byte is significant.

The title of your question does not jive with the actual question. Would you please fix that when you get a chance? Thanks.

10
  • Hmmmmm I will check that out. Also updated the title. I was originally going to ask that, but then solved it midway through typing my issue.
    – Nicholas S
    Commented May 18, 2016 at 13:59
  • Nope that didn't solve it. Actually made it fail a bunch of the other check50's where it was succeeding earlier. Could it be that I'm reading in chars when I need to be reading in something else. I tried to go through it will GDB but couldn't really see why it wasn't working :/
    – Nicholas S
    Commented May 18, 2016 at 14:14
  • after that loop completed I said count = count -1;
    – Nicholas S
    Commented May 18, 2016 at 14:43
  • and then did *length = count;
    – Nicholas S
    Commented May 18, 2016 at 14:44
  • 1
    Wow it works perfectly now! Thanks so much! :)
    – Nicholas S
    Commented May 18, 2016 at 16:58

You must log in to answer this question.

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