0

I have been working on server.c in Problem Set 6, and it seems to work. I think it runs the server, but for some reason the second Check50 fails. When I run it, I get:

:) server.c exists

:) server compiles

:) Requesting cat.jpg returns 200, image/jpeg, and correct image

:) Requesting cat.html returns 200, text/html, and correct file

:) Requesting cat2.HTML returns 200, text/html, and correct file

:) Requesting cat3.HtMl returns 200, text/html, and correct file

:) Requesting cat.gif returns 200, image/gif, and correct file

:( Requesting favicon.ico returns 200, image/x-icon, and correct file

:) Requesting test.css returns 200, text/css, and correct file

:) Requesting test.js returns 200, text/javascript, and correct file

:) Requesting hello.php returns 200, text/html, and correct output

:) Requesting hello.php? returns 200, text/html, and correct output

:) Requesting hello.php?name=Alice returns 200, text/html, and correct output

:( Requesting /test redirects to /test/

:( Requesting /test/ outputs /test/index.html

:( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."

:) Requesting two files in a row (cat.html then cat.jpg) succeeds

I am not sure how to test for which part of the program is wrong, and therefore, I have been unable to fix it.

These are my four functions:

Lookup

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

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

Parse

bool parse(const char* line, char* abs_path, char* query)
{
    char* temp = (char*) line;

    if (strncmp(temp, "GET ", 4) != 0) {
        error(405);
        return false;
    }

    temp = temp + 4;

    char target[LimitRequestLine + 1];
    int targetIndex;

    for (targetIndex = 0; *temp != ' '; temp++, targetIndex++) {
        target[targetIndex] = *temp;
    }
    target[targetIndex] = '\0';

    if(target[0] != '/') {
        error(501);
        return false;
    }

    for (int i = 0; i < LimitRequestLine + 1; i++) {
        if (target[i] == '"') {
            error(400);
            return false;
        }
    }

    temp++;
    char version[LimitRequestLine + 1];
    int versionIndex;
    for (versionIndex = 0; *temp != '\0'; temp++, versionIndex++) {
        version[versionIndex] = *temp;
    }
    version[versionIndex] = '\0';

    if (strncmp(version, "HTTP/1.1", 8) != 0) {
        error(505);
        return false;
    }

    if (strcmp(version, "HTTP/1.1\r\n") != 0) {
        error(400);
        return false;
    }

    int j;
    for (j = 0; target[j] != '?' && target[j] != ' ' && target[j] != '\0'; j++) {
        abs_path[j] = target[j];
    }
    abs_path[j] = '\0';

    char* question = strchr(target, '?');
    if (question != NULL) {
        question++;
        strcpy(query, question);
    }
    return true;
}

Load

bool load(FILE* file, BYTE** content, size_t* length)
{
    *length = 0;
    *content = NULL;

    if (file == NULL) {
        error(400);
        return false;
    }

    BYTE buffer[BYTES];

    for (size_t i = fread(buffer, 1, BYTES, file); i != 0; i = fread(buffer, 1, 512, file)) {
        *content = realloc(*content, *length + i + 1);

        if (content == NULL) {
            return false;
        }
        memcpy(*content + *length, buffer, i);
        *length = *length + i;
    }
    return true;
}

Indexes

char* indexes(const char* path)
{
    char* html = "/index.html";
    char* php = "/index.php";
    char* indexhtml = malloc(strlen(html) + strlen(path) + 1);
    strcpy(indexhtml, path);
    strcat(indexhtml, html);
    char* indexphp = malloc(strlen(php) + strlen(path) + 1);
    strcpy(indexphp, path);
    strcat(indexphp, php);

    if(access(indexhtml, F_OK) == 1) {
        return indexhtml;
    }
    else if(access(indexphp, F_OK) == 1) {
        return indexphp;
    }
    return NULL;
}

Thank you so much!

2

These three failed tests should give you a hint that it's testing the code you wrote for indexes.

:( Requesting /test redirects to /test/

:( Requesting /test/ outputs /test/index.html

:( Requesting directory containing index.php outputs index.php \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."

From man access:

RETURN VALUE On success (all requested permissions granted, or mode is F_OK and the file exists), zero is returned.

And be sure to review the instructions for the order in which those files should be evaluated.

What response do you get from server and the browser when you request favicon.ico? The lookup function looks ok. parse and load handle other image files properly. You need eyes-on.

| improve this answer | |
  • Thank you so much for responding! However, I am pretty confused as to what the function Indexes is supposed to do, so that's why I don't think I did it right. I was wondering if someone could summarize the point of the function in a different way from how it was explained in the pset specification. Thanks! – user11799 Jul 11 '16 at 22:01
  • You're so close. Using your var names, the pseudo code is something like: 1) Does indexphp exist? return indexphp 2) does indexhtml exist? return return indexhtml. 3) Otherwise, return NULL. Basically, your if-tests are in the wrong order and they are looking for the "wrong" response from access (since "zero is returned" on success). – DinoCoderSaurus Jul 11 '16 at 22:52
  • Thank you for your help! My code now works! – user11799 Jul 13 '16 at 22:35

You must log in to answer this question.

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