0
bool parse(const char* line, char* abs_path, char* query)
{
    const char* sp1 = strchr(line, ' ');
    const char* sp2 = strrchr(line, ' ');
    const char* slash = "/";
    const char* crlf = strchr(line, '\r');
    const char* method = malloc(sp1 - line)+1;
    const char* req = malloc(sp2 - sp1)+1;
    const char* ver = malloc(crlf - sp2)+1;

if (method == NULL)
{
    return 1;
}

if (strcasecmp(method, "GET") != 0)
{
    error(405);
    return false;
}

if (strcmp(req, slash) != 0)
{
    error(501);
    return false;
}

if (strchr(req, '"') != NULL)
{
    error(400);
    return false;
}

if (strcasecmp(ver, "HTTP/1.1") != 0)
{
    error(505);
    return false;
}

if (strchr(req, '?') == NULL)
{
    strcpy(abs_path, req);
    return abs_path;
}

else if (strchr(req, '?') != NULL)
{
    const char* qm = strchr(req, '?');
    const char* quer = malloc(sp2 - qm)+1;
    if (strcasecmp(quer, "") == 0)
    {
        query = "";
        return query;
    }
    else if (strcasecmp(quer,"") != 0)
    {
        strcpy(query, quer);
        return query;
    }
}
error(501);
return false;
}

When I run "check50 2015.fall.pset6.server1 server.c", I get the following errors:

:) server.c exists
:) server compiles
:( HTTP/1.0 returns error code 505
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:( request-target without starting '/' returns error code 501
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( request-target of abc/hello.php returns error code 501
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:( Requesting request-target with " returns error code 400
\ expected output, but not "HTTP/1.1 405 Method Not Allowed\r\nCont..."
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code

Any hints on how I am using pointers incorrectly and/or using strcmp, strchr incorrectly?

1 Answer 1

0

Since check50 overwhelmingly returns 405 error, attention is drawn here if (strcasecmp(method, "GET") != 0). The strcasemp syntax looks good. It looks like method is allocated ok. But where is it populated? This const char* method = malloc(sp1 - line)+1; allocates the memory for method, but nothing is ever stored in that memory.

As you work through parse and the rest of the server functions, you will more than likely find debug50 and/or gdb invaluable tools.

4
  • After I posted this I realized that I probably need to copy parts of the string into the allocated memory (which is exactly what you have suggested!) If I use strcpy, can I use the const char* method I malloc'd space for even though the man page for strcpy shows a char* destination and not a const char* as the destination? Commented Dec 9, 2016 at 1:27
  • I didn't even notice the const char* and no. If it's const it cannot be changed. Commented Dec 9, 2016 at 1:57
  • OK. I re-worked my code and have gotten parts of parse to work (identifying the method "GET", and checking if " is included in request). Now I am working on checking the http-version and have hit a wall. I am using memcpy or strncpy. Question: strncpy(*dest, *src, size_t n) - does this function include the character pointed to by src when copying or does it start copying from the pointer src? Commented Dec 9, 2016 at 6:07
  • I'm sorry, I don't understand the distinction in your question. It copies the first n bytes of src to dest. Commented Dec 12, 2016 at 0:40

You must log in to answer this question.

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