So I've been fumbling over this for a little while now. When I copy the first i
bytes of line
to method
, the server responds OK
when the request_target
is /
. You can see I have a printf
statement right after I copy to method
, which confirms that method
is indeed GET
when request_target
is /
.
However, when the request_target
is /favicon.ico
, method prints with some unexpected values that fill the rest of the array after GET
: GET22%24last_name%22%3L%20%22LLL��
. I can't quite figure out where these values are coming from and how to stop the issue. If I add method[3] = '\0';
right where my printf
statement is, then I get a seg fault.
Can anyone give me some hints or explain why this is happening? If you need to see more code to diagnose the problem, I'll be happy to add more of this function.
// get substrings ready
char method[25];
char version[25];
// get pointers ready
const char* version_ptr = NULL;
const char* request_target_ptr = NULL;
const char* qptr = NULL;
// iterate through line to first space
int i;
for (i = 0; line[i] != ' '; i++);
// request target begins after this space
request_target_ptr = &line[i + 1];
// copy everything up to this point to method substring, exclude space
strncpy(method, line, i);
printf("%s\n", method);
// ensure that method is get
if (strcasecmp(method, "get") != 0)
{
error(405);
return false;
}
// ...