0

I am currently working on server.c in pset 6. I have completed all the four functions and the first check50 gives all green smilies indicating that lookup and parse function seems to be working fine.

However, I am getting error in the second check50 test as shown in here: https://sandbox.cs50.net/checks/5b5d258bc7794c40a2c78c78e703138e.

Here is my load function:

bool load(FILE* file, BYTE** content, size_t* length)
{
    // allocate memory on the heap
    char* file_content = malloc(sizeof(char)*1000);
    if (file_content == NULL) 
    {
        return false; 
    }

    int i = 0;
    for (int c = fgetc(file); c != EOF; c = fgetc(file))
    {
        if (i % 1000 == 0)
        {
            file_content = realloc(file_content, sizeof(char)*(1000 + i));
        }

        file_content[i] = c;
        i++;
    }
    // add terminating string and increase the size by one
    file_content[i] = '\0';
    i++;

    // stores the address of the first byte of file contant in heap
    *content = file_content;
    *length = i;

    return true;
}

Hers is my indexes function:

char* indexes(const char* path)
{
    // get length of path + /0
    int path_length = strlen(path) + 1;

    // copy path to new variable
    char new_path[path_length];
    strcpy(new_path, path);

    // covert it to lowercase
    for (int j = 0; new_path[j]; j++)
    {
        new_path[j] = tolower(new_path[j]);
    }

    // allocate memory on heap for new paths
    char* filepath1 = malloc(path_length + 12);
    if (filepath1 == NULL)
    {
        return NULL;
    }
    char* filepath2 = malloc(path_length + 12);
    if (filepath2 == NULL)
    {
        return NULL;
    }

    if ((strstr(new_path, "index.php") != NULL) || (strstr(new_path, "index.html") != NULL))
    {
        strcpy(filepath1, new_path);
        if (access(filepath1, F_OK) != -1) 
        {
            free(filepath2);
            return filepath1;
        }
    }
    else if (strcmp(&new_path[path_length - 2], "/") != 0)
    {
        strcpy(filepath1, new_path);
        strcat(filepath1, "/");
        strcpy(filepath2, new_path);
        strcat(filepath2, "/");
    }
    else
    {
        strcpy(filepath1, new_path);
        strcpy(filepath2, new_path);
    }

    strcat(filepath1, "index.php");
    strcat(filepath2, "index.html");

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

In my testing, the code seems to work fine and I can not figure out the where is the problem with my code. Appreciate if any of you can help me with above.

1 Answer 1

0

At least part of your problem is here:

file_content[i] = '\0';
     i++;

You are ostensibly changing the actual contents of content by adding '\0'. content is not string data, so it does not need to be (read: should not be) null terminated. And even though file_content can be "over-allocated" by as much as 999 bytes, it should respond() properly if length is correct.


Regarding indexes:

  • tolower: Since Unix is case-sensitive, you should not do this conversion. TEST is a valid directory name and distinct from test.
  • if ((strstr(new_path, "index.php") != NULL).... is unnecessary since indexes function is only invoked if path is a dir.
  • Since it's an either (index.php) or (index.html) condition, you don't need to allocate separate variables.

Finally, in my experience, "requesting two files in a row" fails if abs_path is not properly null-terminated (in parse()).

3
  • Thanks @DinoCoderSaurus. Removing the code for adding '\0' in load fucntion and removing the tolower i.e. solved the problem. Actually, initially I didn't implement tolower in my code but when I saw that 2nd check50 test checks for cat2.HTML and cat3.HtMl, I thought that the path may need to be case insensitive. I have also removed if ((strstr(new_path, "index.php") != NULL)... as righlty suggested by you.
    – Gautam
    Mar 24, 2016 at 18:23
  • My pseudo code for indexes function was following: 1. Search for index.php or index.html in the supplied path. (Now you made me realise that this is unnecessary). 2. Serch for / at the end of the path and add if it does not there. 3. Add index.php towards the end of the path and check if the revised path exists 4. if 3 is not true then remove index.php and add index.html and check again if it exists and return the value accordingly. But I was not sure how to implement the fourth line without bring in a new variable, hence I decided to do it from the beginning.
    – Gautam
    Mar 24, 2016 at 18:37
  • After searching a bit more I manage to figure it out and updates the solution. Now I am allocating only one variable on the heap in indexes function. Thanks again @Dino for the help.
    – Gautam
    Mar 24, 2016 at 20:03

You must log in to answer this question.

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