If the query is name=Alice
, you should return name=Alice
, but you're setting it to lower case and returning name=alice
. Why?
The second problem I'll let you see for yourself. See your code in action here and pay attention to where the null terminator is:
Wait some seconds for it to load, then keep pressing the forward button.
PS: I can't see you handling abs_path anywhere in that snippet you've posted (the pastebin link doesn't work), but I'll assume you're handling it somewhere.
EDIT to check updated code:
1st Problem:
Look at this:
char method[6];
char * get_method = "GET ";
do
{
method[i] = *(line + i);
i++;
}
while(method[i] != ' ' && i < 6);
When you first get to while(method[i] != ' ' && i < 6);
, the variable i
is equal to 1
(since you've just incremented it with i++
right before), so you're comparing method[1]
to ' '
.
But method[1]
is yet uninitialized (you've only filled method[0]
with 'G'
yet and nothing more)! You'll compare whatever garbage value inside method[1]
to ' '
, which causes unpredictable behavior.
2nd problem
Then look here:
//count the size of the request line
int tgt_size = 0;
// Copy using pointers to the absolute path limit
while (*(target_ptr + tgt_size) != ' ' && *(target_ptr + tgt_size) != '?' && tgt_size < LimitRequestLine)
{
*(abs_path + tgt_size) = tolower(*(target_ptr + tgt_size));
tgt_size++;
}
//null terminate abs path
*(abs_path + tgt_size + 1 ) = '\0';
In the last iteration of your loop, tgt_size++
will be incremented. When you do that + 1
here *(abs_path + tgt_size + 1 ) = '\0'
;, you're placing the '\0'
one byte further than it should be and you end up with something like this:
3rd problem:
Other than that, you're again setting abs_path to lower case with tolower()
, but you shouldn't.
4th problem:
And finally, you forgot to return true
inside this block, after setting query to "". If you don't do this, you function will go to the end and return error(501) and false:
else if (*(query_ptr + 1) == ' ')
// check if there is nothing after the question mark - if so, again query is ""
{
*query = '\0';
}