0

My code compiles, but the cs50 says it's returning 0 when it should be returning error codes. The premise of my function is to break the line into method, request-target, and HTTPversion and then further break up request target into its absolute path and query (which I don't think I coded correctly) and then to perform the checks on those sub strings. However, I can't figure out where the code isn't working.

    /**
    * Parses a request-line, storing its absolute-path at abs_path 
    * and its query string at query, both of which are assumed
    * to be at least of length LimitRequestLine + 1.
    */
 bool parse(const char* line, char* abs_path, char* query)
{
char* method = NULL;
char* requesttarget = NULL;
char* HTTPversion = NULL;
char* linecpy = NULL;
const char* delim = " ";
char* word = NULL;

linecpy = strcpy(linecpy, line);

int element = 1;

word = strtok(linecpy, delim);
strcpy(method, word);

while (word != NULL )
{
    if(element == 1)
    {
        word = strtok(NULL, delim);
        strcpy(requesttarget, word);
    }

    if(element == 2)
    {
        word = strtok(NULL, delim);
        strcpy(HTTPversion, word);
    }
    element++;
}

const char* querybegin = "?";

abs_path = strtok(requesttarget, querybegin);

strcpy(abs_path, query);

//perform checks
if(strcmp(method, "GET") != 0)
{
    //respond 405 Method Not Allowed
    error(405);
    return false;
}

if(requesttarget[0] != '/')
{
    //respond with 501 Not Implemented
    error(501);
    return false;
}  

if(strchr(requesttarget, '"') == NULL)
{
    //respond with 400 Bad Request
    error(400);
    return false;
}

size_t n = 8;
if(strncmp(HTTPversion, "HTTP/1.1", n) != 0)
{
    //respond 505 HTTP Version Not Supported
    error(505);
    return false;
}

error(501);
return false;

}

1

Your code compiles, but does it process a request through the browser or curl? I think it seg faults here strcpy(method, word); every time.

From man strcpy:

the destination string dest must be large enough to receive the copy.

You haven't allocated any memory for method (or Httpversion or requesttarget).

Your approach is spot on.

break the line into method, request-target, and HTTPversion and then further break up request target into its absolute path and query .

Besides the requesttarget "problem", this code

abs_path = strtok(requesttarget, querybegin);
strcpy(abs_path, query);

doesn't do what you said you wanted to do. Its changing the pointer named abs_path to point to somewhere in requesttarget (that's bad because when you get back to main it's going to be pointing to the original location. Week 4 > Section > Pointers and more detail here). And then copying query, which is presumably blank (or whatever happens to be sitting in it's memory), into abs_path.

gdb is an invaluable tool for troubleshooting and debugging server. You'll find some tips here.

A final thing, see this post for a warning about strtok.

2
  • Thanks for your comments! How can I check whether or not my code processes a request through the browser? I think the hardest issue with this problem set is I don't really know what the best method for debugging is (my go-to gdb doesn't seem to be very useful for this). May 12 '16 at 19:19
  • Gosh, I miss the pdf instructions with page numbers. You can test in the browser the same way you "tested" with the staff implementation. Search for ~cs50/pset6/server public in the instructions. gdb should be your go-to for this pset too. There's a link to some tips on using it in the answer. Remember, either gdb is waiting for a request, or the request is waiting for gdb. May 12 '16 at 21:33

You must log in to answer this question.

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