1

I thought my parse() function was looking pretty good, but I keep consistently getting the same check50 (server1) error:

:( Requesting non-existant file returns error code 404
   \ expected output, but not "HTTP/1.1 501 Not Implemented\r\nContent..."

I'm failing almost everything on server2 which is not really surprising I guess! Thank you for any advice you can provide.

Here is my code:

bool parse(const char* line, char* abs_path, char* query)
{
// TODO

// iterate char by char or string by string to make sure it meets specification
// deal with query if any to store a string

// check for GET method
if (strncmp(line, "GET ", 4) != 0)
{
    error(405);
    return false; 
}

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

// find request-target
char* req_beg = strchr(line, '/');
char* req_end = strchr(req_beg, ' ');

if ((req_beg == NULL) || (req_end == NULL))
{
    error(400);
    return false;
}

size_t reqlength = req_end - req_beg;

char request_target[reqlength];

strncpy(request_target, req_beg, reqlength);

request_target[reqlength] = '\0';

// check for '"' in request_target
char* quotecheck = strchr(request_target, '\"');
if (quotecheck != NULL)
{
    error(400);
    return false;
}

char* querybegin = strchr(request_target, '?');

// no ? found
if (querybegin == NULL)
{
    strcpy(abs_path, request_target);
    query[0] = '\0';
}

else
{
    size_t pathlength = (querybegin - request_target) + 1;
    char path[pathlength];
    strncpy(path, request_target, pathlength);
    path[pathlength] = '\0';

    strcpy(query, querybegin + 1);
}

// find HTTP version
char* version = req_end + 1;
// size_t version_length = strlen(version);

// // check for length of "HTTP/1.1CRLF"
// if (version_length != 12)
// {
//     error(505);
//     return false;
// }

// // check that there are no more spaces after the HTTP version
// char* extra_version = strchr(version, ' ');
// if  (extra_version != NULL)
// {
//     error(505);
//     return false;
// }

// check to make sure version is exactly as shown
char* goodhttp = strstr(version, "HTTP/1.1");
if (goodhttp == NULL)
{
    error(505);
    return false;
}

error(501);
return false;

}

1 Answer 1

2

Have you noticed that every request you send through a browser or curl gives a 501? server returns 501 in 3 places, 2 in this parse and one in main. You might want to take it for a spin in the new debug50 or gdb (tips here) and find out more info. This parse, if it doesn't give any other error, will always throw an error 501 and return false. It should return true if none of the error conditions is met. And it looks like if there is a query, abs_path is not built.

2
  • I had a lot of problems with my code, but I was finally able to solve all of them after using debug50! Thank you for the tip. I now pass all check50 tests on both test servers.
    – ribex
    Sep 15, 2016 at 2:17
  • Congratulations! Sep 15, 2016 at 22:19

You must log in to answer this question.

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