0

I finished all four functions to my best knowledge but the server seems not working well. Once I started the program and then tried to access https://ide50-(myusername).cs50.io/, 403 forbidden popped up. However, if I manually add documents following the link, for example, /hello.html, they worked - only /test returned 403 forbidden. Thus I believe there are some problems with my lookup and indexes functions.

Here is a screenshot of check50 server2: enter image description here

Here is my lookup function:

const char* lookup(const char* path)
{
    const char* ext = strrchr(path, '.');

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

and here is my indexes function:

char* indexes(const char* path)
{
    const char* php = "/index.php";
    char* phpcheck = calloc(1, strlen(path) + strlen(php) + 1);
    strcpy(phpcheck, path);
    strcat(phpcheck, php);

    const char* html = "/index.html";
    char* htmlcheck = calloc(1, strlen(path) + strlen(html) + 1);
    strcpy(htmlcheck, path);
    strcat(htmlcheck, html);

    if (access(phpcheck, F_OK))
    {
        free(htmlcheck);
        return phpcheck;
    }
    else if(access(htmlcheck, F_OK))
    {
        free(phpcheck);
        return htmlcheck;
    }
    else
    {
        free(htmlcheck);
        free(phpcheck);
        return NULL;
    }
}

Any suggestions or hints are appreciated!

1

Hint: This line in indexes if (access(phpcheck, F_OK)) (and the other access) is wrong.
Suggestion: Read man access. Pay special attention to the RETURN section. access returns an int. Treat it like an int.

Hint: The pattern of errors from check50 makes load() the likely culprit.
Suggestion: Since the browser is giving "acceptable" output, look for something like length "off-by-one" (use gdb to check that length is returned properly to main). Another popular problem is null terminating content. (It's not string data. Don't treat it like string data).

  • Thank you a lot for the hints - they are all very helpful!! Now the server works fine: no more 403 forbidden and I can access all files in /public successfully! But somehow the check50 still gives errors - only "Requesting directory containing index.php outputs index.php" has been corrected. I know this may sound silly but can you tell me how to use gdb to check the length of something is returned properly to main? – codecodecode Jul 12 '16 at 19:03
  • And the results of the check50 do not show the errors, just "TODO|TODO". – codecodecode Jul 12 '16 at 21:11
  • Check out this post – DinoCoderSaurus Jul 13 '16 at 12:52
  • I used gdb to debug and actually found that only hello.html, hello.php, and test went through the server code (I should have found this earlier). On the other hand, cat.jpg, cat.html, and favicon.ico just skipped the server and directly access the document , i.e., when I open cat.jpg, cat.html and favicon.ico, nothing appeared on the command-line. Do you know where the problems might be? – codecodecode Jul 16 '16 at 1:40
  • Every request from a browser or curl to the server goes "through the server code". – DinoCoderSaurus Jul 16 '16 at 11:06

You must log in to answer this question.

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