0

I was passing check50 server1. I don't think I changed anything, but now I get errors from check50 server1 that 2 spaces after GET cause error code. I am using sscanf with "%s %s %s\r\n" to extract data. Number of spaces should not matter, why does it not work now? Here is my code for parse.

/**
 * 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* template = "%s %s %s\r\n";
    char method[LimitRequestFieldSize + 1];
    char request_target[LimitRequestFieldSize + 1];
    char http_version[LimitRequestFieldSize + 1];
    // extract method, request target and http version from line
    if (sscanf(line, template, method, request_target, http_version) != 3)
    {
        error(400);
        return false;
    }
    // make sure it is valid
    // must be GET method
    if (strcmp(method, "GET") != 0)
    {
        error(405);
        return false;
    }
    // request target must start with /
    if (request_target[0] != '/')
    {
        error(501);
        return false;
    }
    // request target cannot contain "
    char* ch = strchr(request_target, '"');
    if (ch != NULL)
    {
        error(400);
        return false;
    }
    // version must be http/1.1
    if (strcmp(http_version, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }
    // extract query and abs path from request target
    ch = strchr(request_target, '?');
    // if there was a ?
    if (ch != NULL)
    {
        // copy everything after the ? into query
        strcpy(query, ch + 1);
        // and everything in front of it into abs path
        strncpy(abs_path, request_target, ch - request_target);
        abs_path[ch - request_target] = '\0';
    }
    // there was no query
    else
    {
        query[0] = '\0';
        // extract abs path
        strcpy(abs_path, request_target);
    }

    return true;
}

In debug it looks like it works fine, but I don't know how to send a request with 2 spaces. Someone please help, it looks like it should work to me.

1
  • I must have removed the part that was checking for the spaces. Working too late. But I still could not pass check50 without making it only check for a space behind the front space and a space in front of the last space, otherwise it gives an error on cat.exe, there must be an extra space in that one at the end or something. Nov 14 '16 at 9:48
1

A small problem with the sscanf. From the man page:

A directive is one of the following:

· A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

Since all the white space is "consumed", this will not detect the double space.

You can send a specific method to curl with the -X switch. A call like this curl -i -X "GET " http://localhost:8080/hello.php would duplicate the test.

1
  • Thanks. I actually has a bit of code to check for extra spaces, but I lost it somehow. Maybe when I reverted to previous versions in my history. Got to be careful when doing that. Nov 14 '16 at 22:14

You must log in to answer this question.

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