0

I've been working my way through PSET6, and most of it's been fine, until now. When I check the program in check50, it shows me that the first two (existing and compiling), the 5th (checking if GETabc works) and the last 3 checks (checks with spaces) work. The others, however, all get error 405, (the 4th check gets error 501). I can't seem to find out why. Can someone help me on the way? I've included my parse function, because I can tell that my mistakes are definitely there

bool parse(const char* line, char* abs_path, char* query)
{
// TODO
bool methodize = true;
bool requested = false;
bool switcher = false;
bool versioning = false;
char* method = (char *) malloc (sizeof(char) * 5);
char* httpVersion = (char *) malloc (sizeof(char) * 10);
abs_path = (char *) malloc (sizeof(char) * (LimitRequestLine + 1)) ;
int methOver = 0;

for(int i = 0; i < strlen(line); i++) {
    char* s = strdup(&line[i]);
    if(methodize == true) {
        sprintf(method, "%s", s);
        if(strncmp(s, " ", 1)) {
            methodize = false;
            requested = true;
            methOver = i;
            if(!strncmp(method, "GET ", 1)) {
                error(405);
                method = NULL;
                return false;
            }

        }
    } else if (requested == true) {
        char* t = strdup(&line[methOver+1]);
        if(strncmp(t, "/", 1)) {
            error(501);
            method = NULL;
            return false;
        }
        if(strncmp(s, "\"", 1)) {
            error(400);
            method = NULL;
            abs_path = NULL;
            return false;
        } else if (strncmp(s, " ", 1)) {
            requested = false;
            versioning = true;
        } else if (!strncmp(s, "?", 1) && switcher == false){
            strcpy(abs_path, &line[i]);
        } else if (strncmp(s,"?", 1) && switcher == false) {
            switcher = true;
        } else if (switcher == true) {
            sprintf(query, "%s", s);
        }
    } else if(versioning == true) {
        if(strncmp(s, "\"", 1)) {
            error(400);
            abs_path = NULL;
            method = NULL;
            httpVersion = NULL;
            return false;
        } else if (i+1 == strlen(line)){
            if(strncmp(httpVersion, " HTTP/1.1", strlen(httpVersion))) {
                return true;
            } else {
                error(505);
                abs_path = NULL;
                method = NULL;
                httpVersion = NULL;
                return false;
            }
        } else {
            sprintf(httpVersion, "%s", s);
        }
    }
}
error(501);
return false;
}

Thank you.

1

This test from check50 Requesting cat.exe returns error code 501 could be your key to progress and ultimately, success because program reaches here error(405); and goes no further. Since this is [should be!] be the only error(405); in server, this sounds like a job for gdb. You know exactly the place the program is failing; you only have to find out why/how it gets there. You'll find tips on running server under gdb here.

Once you have gdb ./server running in a terminal, with a breakpoint set at parse, it would probably be easiest to use a curl request in another terminal. Suggest something like
curl -i http://localhost:8080/cat.exe. That won't do anything, until you go back to the gdb terminal and proceed.

Back at gdb, start by setting a break at this line if(methodize == true) { and examining the contents of s. I predict they will not be at all what you expect. Why? Well, you're stuffing a lot of stuff into s, but you haven't allocated any memory to store it. It's a memory leak.

With patience and perseverence you will find gdb invaluable for getting a perfect score on pset6.

Spoiler alert: this line abs_path = (char *) malloc (sizeof(char) * (LimitRequestLine + 1)) ; is a bug waiting to happen. If you allocate memory to the local abs_path, it gets a new address. What happens when control returns to main? Well, it will be looking at the old address.

These are probably not the only problems you will encounter in your parse, but I stopped reading the program at error(405); much like check50 did :)

You must log in to answer this question.

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