Reputation: 1432
I'm attempting to iterate over a HTTP request line to determine its legitimacy. Ultimately, my server will not know what the request line will look like or how long it will be. Assume a legitimate request is exactly like the one below. Note that it has only two spaces, one ?, and no internal ". I need to iterate through the request line to check for the spaces at a minimum, but since I won't know its length in advance and don't want to declare an array of arbitrary size, it's going to be kind of like reading braille. Below was my attempt to discover whether it contained two spaces or something else. It returned an error that stated I was comparing ints and pointers. I've been told that, in addition to iteration, the following functions may be helpful: strchr, strcpy, strncmp, strncpy, strstr. Any direction would be greatly appreciated!
int main ()
{
const char* line = "GET /path/script.cgi?field1=value1&field2=value2 HTTP/1.0";
parse(line);
}
bool parse(const char* line)
{
int spaces = 0;
int n = strlen(line);
for (int i = 0; i < n; i++)
{
if (line[i] == " ")
spaces++;
}
if (spaces == 2)
{
printf("Only 2 spaces!\n");
return true;
}
else
{
printf("Illegal request! More than two spaces! Hacker! Hacker! Help!\n");
return false;
}
}
Upvotes: 0
Views: 47