0

When running server.c, and requesting a site, I get this output

Using /home/ubuntu/workspace/pset6/public for server's root                                                                                                                    
Listening on port 8080                                                                                                                                                         
GET / HTTP/1.1                                                                                                                                                                 
HTTP/1.1 501 Not Implemented                                                                                                                                                   
*** Error in `/home/ubuntu/workspace/pset6/server': free(): invalid pointer: 0x00007fffffff9dc0 ***     

Could anybody explain to me what is going on? Thanks a lot!

Here is my code:

lookup:

const char* lookup(const char* path)
{
    // TODO

    char* fileEx = strrchr(path, '.');

    if (fileEx != NULL) {
        if(strcasecmp(fileEx, ".css") == 0) {
            return "text/css";
        }
        else if (strcasecmp(fileEx, ".html") == 0) {
            return "text/html";
        }
        else if (strcasecmp(fileEx, ".gif") == 0) {
            return "image/gif";
        }
        else if (strcasecmp(fileEx, ".ico") == 0) {
            return "image/x-icon";
        }
        else if (strcasecmp(fileEx, ".jpg") == 0) {
            return "image/jpeg";
        }
        else if (strcasecmp(fileEx, ".js") == 0) {
            return "text/javascript";
        }
        else if (strcasecmp(fileEx, ".php") == 0) {
            return "text/x-php";
        }
        else if (strcasecmp(fileEx, ".png") == 0) {
            return "image/png";
        }

    }
    return NULL;
}

parse:

bool parse(const char* line, char* abs_path, char* query)
{
    // TODO
    if(strstr(line, "GET") == NULL) {
        error(405);
        return false;
    }
    if (line[4] != '/') {
        error(501);
        return false;
    }
    int placeInPath = 0;
    int currentPlaceInLine = 4;
    bool queryRequest = false;
    for (int i = 4; line[i] != ' '; i++) {
        if (line[i] == '?') {
            queryRequest = true;
            currentPlaceInLine = i;
            break;
        }
        abs_path[placeInPath] = line[i];
        placeInPath++;
        currentPlaceInLine = i;

    }
    int placeInQuery = 0;
    if (queryRequest) {
        for(int j = currentPlaceInLine+1; line[j] != ' '; j++) {
            if (line[j] == '"') {
                error(400);
                return false;
            }
            query[placeInQuery] = line[j];
            currentPlaceInLine = j;
            placeInQuery++;
        }
        query[placeInQuery+1] = '\0';
    }
    else {
        query[0] = '\0';
    }

    if(strstr(line, "HTTP/1.1") == NULL) {
        error(505);
        return false;
    }
    return true;
}

load:

bool load(FILE* file, BYTE** content, size_t* length)
{
    // TODO
    *length = 0;
    *content = NULL;
    fseek(file, 0L, SEEK_END);
    unsigned long sz = ftell(file);
    fseek(file, 0L, SEEK_SET);

    BYTE* loaded = malloc(sizeof(BYTE)*sz);
    fread(loaded, sz, 1, file);

    content = &loaded;
    *length = sz;
    return true;
}

indexes:

char* indexes(const char* path)
{
    // TODO
    int lenOfPath = strlen(path);
    char newPath[lenOfPath];
    char* indexPHP = strcat(newPath, "index.php");
    char* indexHTML = strcat(newPath, "index.html");
    if(access(indexPHP, F_OK)) {
        return indexPHP;
    }
    if(access(indexHTML, F_OK)) {
        return indexHTML;
    }
    else {
        return NULL;
    }
}
1

There are a couple of problems in your indexes function, which will be invoked with this request line GET / HTTP/1.1 because '/' is a directory.

Recall this line from the spec:

this function should dynamically allocate memory on the heap for the returned string

You need to use a malloc(calloc/realloc) to do that. This declaration char newPath[lenOfPath]; allocates memory on the stack. It needs to be on the heap because of how the pointers are set and free'd in the distro code.

Speaking of char newPath[lenOfPath];. lenOfPath is not enough space to hold the contents of path AND "index.[php | html]".

Finally, this is problematic char* indexPHP = strcat(newPath, "index.php"); First, you need to append index.* to path. This syntax loses the path part. Second, strcat modifies its first argument, there's no reason to instantiate a new pointer. You can build your "test" filename with 2 calls to strcat.

As an aside, since this function should return either path/index.php or path/index.html, you don't need to declare two strings, one string will do.

You must log in to answer this question.

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