0

Please help me. I have passed every check except 1.

Here is my error:

check50 2015.fall.pset6.server1 server.c
:) server.c exists
:) server compiles
:) HTTP/1.0 returns error code 505
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:) request-target without starting '/' returns error code 501
:) request-target of abc/hello.php returns error code 501
:( Requesting cat.exe returns error code 501
   \ expected output, but not "HTTP/1.1 404 Not Found\r\nContent-Type:..."
:) Requesting non-existant file returns error code 404
:) Requesting request-target with " returns error code 400
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code
https://sandbox.cs50.net/checks/787dc95ee89e4fe09d3efcb7404d7566

Here is my code:

const char* lookup(const char* path)
{
    // TODO
    const char* substring = strrchr(path, 46);
    printf("%s is before\n", substring);
    for (int i = 0; i < strlen(path); i++) {
        if (strncmp((path + i), ".", 1) == 0) {
            substring = path + i;
            break;
        }
    }
    printf("%s after\n", substring);
    if (strcasecmp((substring), ".css") == 0) {
        return "text/css";
    }
    else if (strcasecmp((substring), ".html") == 0) {
        return "text/html";
    }
    else if (strcasecmp((substring), ".gif") == 0) {
        return "image/gif";
    }
    else if (strcasecmp((substring), ".ico") == 0) {
        return "image/x-icon";
    }
    else if (strcasecmp((substring), ".jpg") == 0) {
        return "image/jpeg";
    }
    else if (strcasecmp((substring), ".js") == 0) {
        return "text/javascript";
    }
    else if (strcasecmp((substring), ".php") == 0) {
        return "text/x-php";
    }
    else if (strcasecmp((substring), ".png") == 0) {
        return "image/png";
    }
    else {
        return NULL;
    }
}
bool parse(const char* line, char* abs_path, char* query)
{
    char* placeholder = "/";
    if (strncmp(line, "GET ", 4) != 0) {
        error(405);
        return false;
    }
    if (strncmp(line + 4, placeholder, 1) != 0) {
        error(501);
        return false;
    }
    const char* index = line + 4;
    while(strncmp(index, " ", 1) != 0) {
        if (strncmp(index, "\"", 1) == 0) {
            error(400);
            return false;
        }
        index++;
    }
    index++;
    if (strncmp(index, "HTTP/1.1", 8) != 0) {
        error(505);
        return false;
    }
    int i = 0;
    index = line + 4;
    while(strncmp(index, "\r", 1) != 0 && strncmp(index, "?", 1) != 0) {
        *(abs_path + i) = *(index);
        index++;
        i++;
    }
    i = 0;
    if (strncmp(index, "?", 1) == 0) {
        index++;
        while(strncmp(index, "\r", 1) != 0) {
            *(query + i) = *(index);
            i++;
            index++;
        }
    }
    else {
        query = NULL;
    }
    return true;
}

UPDATE:

I tried making a cat.exe file and in still says 404

Thanks again.

Here are some screenshot:

Screen Shot of ide50 Screen Shot of ide50_2

Jon

2

It's a common problem when abs_path and query are built one character at a time. The problem seems to be in parse. abs_path (and query) are not properly null-terminated. So server returns a 404 error at line 209, before it executes lookup. See the link mentioned in the comment for hints on testing.

NB this is not a duplicate of the referenced call. That was a bug in lookup. This is a bug in parse.

  • I tried making a file called cat.exe and now every path ends up at a 404 error. – Jon Aug 11 '16 at 16:29
  • 1
    Your abs_path is not properly null terminated. Remember, every string must end with '\0'. You are not adding that in your parse function. So 404 would be the expected response. Try adding printfs in your parse function after you create abs_path. Use "%s<-\n" as the print format. This will show you that there are characters after ".exe" (or whatever request target you send). So something like this /cat.exe <- is wrong (notice the space between the last 'e' and the '<'.) – DinoCoderSaurus Aug 11 '16 at 16:58
  • Thank-you very much! – Jon Aug 11 '16 at 17:07

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