我正在尝试编写一个接受 HTTP 请求并提取少量数据的函数。我的功能如下所示:
char* handle_request(char * req) {
char * ftoken; // this will be a token that we pull out of the 'path' variable
// for example, in req (below), this will be "f=fib"
char * atoken; // A token representing the argument to the function, i.e., "n=10"
...
// Need to set the 'ftoken' variable to the first arg of the path variable.
// Use the strtok function to do this
ftoken = strtok(req, "&");
printf("ftoken = %s", ftoken);
// TODO: set atoken to the n= argument;
atoken = strtok(NULL, "");
printf("atoken = %s", atoken);
}
req
通常看起来像这样:GET /?f=fib&n=10 HTTP/1.1
目前,在调用 之后strtok()
,ftoken
打印出来GET /?f=fibGET /favicon.ico HTTP/1.1
显然是错误的。理想情况下,它将是f=fib
并且atoken
将是n=10
任何人都可以帮我解决这个问题吗?