0
    /**
 * 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)
{
    // TODO
    BYTE* buffer = malloc(sizeof(BYTE));
    int i =  0;
    ;
    while(!EOF){
        fread(&buffer[i], 512, 1, file);
        i += 1;
        buffer = realloc(buffer, sizeof(BYTE) * (i+1));
    }
    *length += 1;
    *content = buffer;
    return true;
}

/**
 * Returns MIME type for supported extensions, else NULL.
 */
const char* lookup(const char* path)
{
    // TODO
    char* ending = strrchr(path,'.');
    char* first_part;
    char* return_value = malloc(sizeof(char)*16);
    //char return_value[16];
    if(strcasecmp(ending, ".css") == 0 || strcasecmp(ending,".html") == 0 || strcasecmp(ending, ".js") == 0 || strcasecmp(ending, ".php") == 0) {
        first_part = "text/";
        strcpy(return_value, first_part);
        if (strcasecmp(ending, ".css") == 0){
            strcat(return_value, "css");
        } else if (strcasecmp(ending, ".html") == 0) {
            strcat(return_value, "html");
        } else if (strcasecmp(ending, ".js") == 0) {
            strcat(return_value, "javascript");
        } else if (strcasecmp(ending, ".php") == 0) {
            strcat(return_value, "x-php");
        }
        return return_value;
    } else if(strcasecmp(ending, ".gif") == 0 || strcasecmp(ending, ".ico") == 0 || strcasecmp(ending, ".jpg") == 0 || strcasecmp(ending, ".png") == 0 ){
        first_part= "image/";
        strcpy(return_value, first_part);
        if (strcasecmp(ending, ".gif") == 0){
            strcat(return_value, "gif");
        } else if (strcasecmp(ending, ".ico") == 0) {
            strcat(return_value, "x-icon");
        } else if (strcasecmp(ending, ".jpg") == 0) {
            strcat(return_value, "jpeg");
        } else if (strcasecmp(ending, ".png") == 0) {
            strcat(return_value, "png");
        }
        return return_value;
    }
    return NULL;
}

/**
 * Parses a request-line, storing its absolute-path at abs_path 
 * and its query string at query, both of which are assumed
 * to be at least of length LimitRequestLine + 1.
 */
bool parse(const char* line, char* abs_path, char* query)
{
    char input[strlen(line)];
    strcpy(input,line);
    char** res = NULL;
    char *p = strtok(input, " ");
    int n_spaces = 0; 
    while (p) {
        res = realloc(res, sizeof(char*) * ++n_spaces);
        if (res == NULL) 
            exit(-1);
        res[n_spaces-1] = p;
        p = strtok(NULL, " ");
    }
    res = realloc(res, sizeof(char*) * (n_spaces + 1));
    res[n_spaces] = 0;
    if (strcmp(res[0], "GET") != 0) {
        error(405);
        return false;
    }
    if (strcmp(res[2], "HTTP/1.1\r\n") != 0) {
        error(505);
        return false;
    } 
    if (line[4] != '/') {
        error(501);
        return false;
    }
    if (strpbrk(res[1], "\"") != 0){
        error(400);
        return false;
    }
    abs_path = malloc(LimitRequestLine +1);
    query = malloc(LimitRequestLine + 1);
    if (strchr(res[1], '?') == NULL) {
        query = "";
    } else {
        query = strchr(res[1], '?') + 1;
    }
    abs_path = strtok(res[1], "?");
    return true;
}

/**
 * Checks, in order, whether index.php or index.html exists inside of path.
 * Returns path to first match if so, else NULL.
 */
char* indexes(const char* path)
{
    char php_path[12] = "index.php";
    char html_path[12] = "index.html";

    char* full_php_path = malloc(sizeof(char) * (strlen(path)+strlen(php_path)+2+1));
    //char full_php_path[strlen(path)+strlen(php_path)+2+1]; 
    strcpy(full_php_path, path);
    strcat(full_php_path, php_path);

    char* full_html_path =malloc(sizeof(char) * (strlen(path)+strlen(html_path)+2+1));
    //char full_html_path[strlen(path)+strlen(html_path)+2+1];
    strcpy(full_html_path, path);
    strcat(full_html_path, html_path);

    if(access(full_php_path, F_OK) != -1){
        return full_php_path;
    } else if(access(full_html_path, F_OK)!= -1){
        return full_html_path;
    }
    return NULL;
}

I have read through all the similar threads I have found and I cannot figure out the issue...

0
0

This abs_path = malloc(LimitRequestLine +1); will definitely cause big problems in parse. First clue is in this comment

   both of which are assumed
 * to be at least of length LimitRequestLine + 1.

Once you allocate the local abs_path, it gets a new address. When you get back to main, it will be looking for abs_path in "the old neighborhood", ie the address it got when it was declared in main. There may be other problems in these functions, you might want to check out the new debug50 or good ol' gdb, tips here.

And query too.

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