0

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;
}

When I run manually those unhappy check50 queries, I get good results. I just don't know why check50 is complaining.

SOLVED: strtok transforms "" to NULL!

2 Answers 2

1

I think the problem is here:

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);

Try to test if qry_t is not NULL before strcpy() it to query.

Do this because if rqt was just /hello.php?, the first strtok will consume the ? and strtok(NULL, "?") will return NULL (I think);

So... If qry_tis not NULL, you copy from qry_t, but if qry_t is NULL, set query to "" (or '\0')

3
  • Yes, it is NULL. I'll tell if that fixes everything. Commented Nov 11, 2016 at 22:41
  • There must be someting else too :( Now I pass the hello.php? but not the directories Commented Nov 11, 2016 at 22:47
  • The directories are something to be dealt with inside the indexes() function. Another problem entirely! Commented Nov 11, 2016 at 22:55
1

I'm having a similar problem - not sure what is going on with your code, but the only thing I can see is that if there's a question mark, and only a question mark, you're not making the query "" aka the null terminator.

That's precisely the case with hello.php?

I'm a bit confused on how you're getting cat2.html and cat3.html to pass though as I didn't have those in my directories. Did you just create them?

4
  • It's the first time I use strtok, so I am not sure, but I think it should simply place a '\0' instead of the '?', which would cause a null string Commented Nov 11, 2016 at 21:02
  • check50 has its own files Commented Nov 11, 2016 at 21:02
  • Looks like you're right on null termination with strtok. I'm not sure what's going on - you'll need big league help from other folks like Yuri, Dino and others.
    – borker
    Commented Nov 11, 2016 at 21:13
  • You were right. I didn't expect it to be NULL, but an empty string. Commented Nov 11, 2016 at 22:42

You must log in to answer this question.

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