0

Below I'm validating the request-line conforms to the spec (printf statements were telling me the errors were from these if statements).

The first 3 blocks worked fine independently and together. (e.g. I could send a method other than GET and get 405 error or I could add a " in the request-target and get a 400).

As soon as I added the 4th block, to check the HTTP version, anything out of spec triggers the 405 error block... if I add a " to the request-target I get 405. If I make the wrong version of HTTP I get 405. I can't figure out how a later if statement can affect a preceding one.

   // TODO: validate request-line

        // Make request line only respond to GET
        char* exist = malloc(5 * sizeof(char)); // Creates variable to test for method; makes it the size of GET plus null terminator
        strncpy (exist, line, strlen("GET ")); // Copies the first 3 characters of line into test variable
        if(strcasecmp(exist, "GET ") != 0) // If the test variable is ET then it doesn't rigger if statement
        {
            printf("This\n");
            free(exist);
            error(405);
            continue;
        }
        free(exist);

        // Validate that request-target starts with /
        if(strstr(line, "/") != line + (sizeof(char) * 4))
        {
            printf("This\n");
            error(501);
            continue;
        }

        // Validate that there are no quotes
        if(strstr(line, "\""))
        {
            printf("This\n");
            error(400);
            continue;
        }

        // Validate that we're using HTTP/1.1
        char* testVersion = malloc(strlen("HTTP/1.1" + 1));
        strncpy(testVersion, strrchr(line, ' ') + 1, strlen("HTTP/1.1")); // Uses strchr to basically find the last space and start copying text of an amount equal to HTTP/1.1
        printf("This is testVersion: %s\n", testVersion);
        printf("This is length of testVersion: %d\n", strlen(testVersion));
        testVersion[8] = '\0';
        if(strcasecmp(testVersion, "HTTP/1.1") != 0) // If that text copied matches HTTP/1.1 then we don't trigger 
        {
            printf("This is the pointer to second space: %p\n", strrchr(line, ' '));
            printf("This is string: %s\n", testVersion);
            free(testVersion);
            error(505);
            continue;
        }
        free(testVersion);
5
  • Sanity checks: server is compiled. server is restarted. only one server running. If you're testing in a browser, you refreshed or telnet restarted. – DinoCoderSaurus Dec 30 '15 at 13:57
  • These are good ones. I did them all and still seem to get the error. – Norcim133 Dec 30 '15 at 21:52
  • Just a theory: you didn't only add an if clause, you added a variable to the stack (testVersion). The man page for strncopy includes this warning: " If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated." You are copying 4 bytes, so it's possible exist is not null-terminated. – DinoCoderSaurus Dec 30 '15 at 22:23
  • You've nailed it! I added the null terminator to exist (having done it for testVersion without effect) and it all works now. If you want, submit it as an answer and I'll accept. – Norcim133 Dec 30 '15 at 23:41
  • Cool beans. Happy New Year! – DinoCoderSaurus Dec 31 '15 at 3:08
0

You didn't only add an if clause, you added a variable to the stack (testVersion). The man page for strncopy includes this warning:

If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

You are copying 4 bytes, so exist may not be null-terminated.

You must log in to answer this question.

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