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
。