Skip to main content
1 of 7

pset6 server trapped in if (connected())

I am so frustrated by pset6, so much unfamiliar stuff here...... please help!

I have written all the 4 to-do functions, however if I go to the web server, no matter what I clicked, it shows no more than < ?. Except the cat.exe shows error 500 in stead of 501. It should return 501, I cant figure out why 500 here.

It seems a lot goes wrong.

So I go to gdb, setting the breakpoint at main, and find out the server.c falls into the line if(connected()) and does not show anything anymore.

I am confused because by if(connected()), it has not yet touched any of the 4 functions I wrote, there shouldn't be anything wrong....supposedly?

I really need advice, thanks!

Here are lookup and load:

bool load(FILE* file, BYTE** content, size_t* length)
{
    // bsic settings
    int counter = 0;
    char* buffer = malloc(sizeof(BYTES));
    * content = malloc(1);
    
    // Realloc *content to the size needed.
    if (fread(buffer, 1, 1, file) == 1)
    {
        counter = counter + 1;
        
        if (counter > BYTES - 1)
        {
            buffer = realloc(buffer, sizeof(buffer) + sizeof(BYTES));
        }
    }
    
    * content = realloc (* content, counter + 1);
    memset(*content, '\0', counter + 1);

    // Read and store all the bytes to *content.
    memcpy(* content, buffer, counter);
    if (* content == NULL)
    {
        return false;
    }
    
    * length = strlen(* content) + 1;
    if (length == NULL)
    {
        return false;
    }
    
    free(buffer);
    return true;
}


const char* lookup(const char* path)
{
    // To return MIME types
    char* MIME_type = strrchr(path, '.');
    char buffer[strlen(MIME_type) + 1];
    memset(buffer, '\0', strlen(MIME_type) + 1);
    strcpy(buffer, MIME_type);
    if (strcasecmp(buffer, ".css"))
        return "text/css";
    
    else if (strcasecmp(buffer, ".html"))
        return "text/html";
    
    else if (strcasecmp(buffer, ".gif"))
        return "image/gif";
        
    else if (strcasecmp(buffer, ".ico"))
        return "image/x-icon";
    
    else if (strcasecmp(buffer, ".jpg"))
        return "image/jpg";
    
    else if(strcasecmp(buffer, ".js"))
        return "text/javascript";
    
    else if(strcasecmp(buffer, ".php"))
        return "text/x-php";
    
    else if(strcasecmp(buffer, ".png"))
        return "image/png";
    
    else
    return NULL;
}

Here is parse:

bool parse(const char* line, char* abs_path, char* query)
{
    // Initialize abs_path and query.
    memset(abs_path, '\0', LimitRequestLine + 1);
    memset(query, '\0', LimitRequestLine + 1);
    
    // Find where /,? and space are.
    char* first_space = strchr(line, ' ');
    char* question_mark = strchr(line, '?');
    char* HTTP_version = strstr(line, "HTTP");
    char* target_end = HTTP_version - 1;
    
    // Extract absolute-path and query.
    if (question_mark == NULL)
    {
        memcpy(abs_path, (first_space + 1), target_end - (first_space + 1));
    }
    
    else
    {
        memcpy(abs_path, (first_space + 1), question_mark - (first_space + 1));
        memcpy(query, (question_mark + 1), target_end - (question_mark + 1));
    }
    
    // Check line's consistency with the rules.
    // find substrings in line.
    
    // for testing but FAIL************************
    if (strncmp((first_space + 1), "/cat.exe", 8) == 0)
    {
        error(500);
        return false;
    }
    
    char* get = strstr(line, "GET ");
    if (get == NULL || get != line)
    {
        error(405);
        return false;
    }
    
    if(strstr(line, "  ") != NULL)
    {
        error(400);
        return false;
    }

    if (strncmp(HTTP_version, "HTTP/1.1", 8) != 0)
    {
        error(505);
        return false;
    }
    
    char* quotation = strchr(line, '"');
    if(first_space < quotation && quotation < target_end)
    {
        error(400);
        return false;
    }
    
    if (* (first_space + 1) != '/')
    {
        error(501);
        return false;
    }
    
    return true;
}

and indexes:

/**
 * 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)
{
    // TODO
    char* indexhtml = "/index.html";
    char* indexphp = "/index.php";
    char* path_html = malloc(strlen(path) + strlen(indexhtml) + 1);
    char* path_php = malloc(strlen(path) + strlen(indexphp) + 1);
    memset(path_html, '\0', strlen(path) + strlen(indexhtml) + 1);
    memset(path_php, '\0', strlen(path) + strlen(indexphp) + 1);
    strcpy(path_html, path);
    strcpy(path_php, path);
    strcat(path_html, indexhtml);
    strcat(path_php, indexphp);
    
    if (access(path_html, F_OK) == 0)
    {
        return path_html;
    }
    
    else if (access(path_php, F_OK) == 0)
    {
        return path_php;
    }
    
    else
    return NULL;
}