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);