Skip to main content
1 of 4

pset6 server2 parse() bug

This bug was first found here: pset6 server2 load() fseek() return -1 [SOLVED] I posted it here to distinguish it from that other bug.

Check50:

$ check50 2015.fall.pset6.server2 server.c
:) 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
   \ expected output, not an exit code of 0
:) 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
https://sandbox.cs50.net/checks/87a83dbe920d4d5aac13a6ca5fb770f3

parse:

bool parse(const char* line, char* abs_path, char* query)
{
    char line_cp[strlen(line) + 1];
    strcpy(line_cp, line);

    int i;
    int x;
    for (i = 0, x = 0; line[i]; i++) {
        if (line[i] == ' ') {
            x++;
        }
    }
    if (x != 2) {
        error(400);
        return false;
    }

    char *mth_t = strtok(line_cp, " ");
    char mth[strlen(mth_t) + 1];
    strcpy(mth, mth_t);

    char *rqt_t = strtok(NULL, " ");
    char rqt[strlen(rqt_t) + 1];
    strcpy(rqt, rqt_t);

    char *ver_t = strtok(NULL, "\r\n\r\n");
    char ver[strlen(ver_t) + 1];
    strcpy(ver, ver_t);

    if (strcmp(mth, "GET")) {
        error(405);
        return false;
    }

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

    for (i = 0, x = 0; rqt[i]; i++) {
        if (rqt[i] == '"') {
            x++;
        }
    }
    if (x) {
        error(400);
        return false;
    }

    if (strcmp(ver, "HTTP/1.1")) {
        error(505);
        return false;
    }

    for (i = 0, x = 0; rqt[i]; i++) {
        if (rqt[i] == '?') {
            x++;
        }
    }
    if (x == 1) {
        char *pth_t = strtok(rqt, "?");
        //char pth[strlen(pth_t) + 1];
        strcpy(abs_path, pth_t);

        char *qry_t = strtok(NULL, "?");
        //char qry[strlen(qry_t) + 1];
        strcpy(query, qry_t);
        return true;
    } else if (!x) {
        strcpy(abs_path, rqt);
        strcpy(query, "");
        return true;
    } else {
        error(501);
        return false;
    }

    error(501);
    return true;
}