【发布时间】:2021-05-17 18:49:50
【问题描述】:
函数如下:
int parse_headers(c_request *req, char *raw_headers) {
char *command_line;
char *raw_header;
req->headers = NULL;
command_line = strtok_r(raw_headers, "\\n", &raw_headers);
printf("command line = [%s]\n", command_line);
if (parse_command(req, command_line) < 0)
return -1;
while ((raw_header = strtok_r(raw_headers, "\\n", &raw_headers))) {
printf("\nraw header = [%s]\n", raw_header);
parse_header(req, raw_header);
}
return 0;
}
raw_headers 相等:
POST www.google.fr HTTP/1.1\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\nHost: www.tutorialspoint.com\nContent-Type: text/xml; charset=utf-8\nContent-Length: 38\nAccept-Language: en-us\nAccept-Encoding: gzip, deflate\nConnection: Keep-Alive\r\n\r\n<?xml version='1.0' encoding='utf-8'?
所以strtok找到第一个\n(command_line等于POST www.google.fr HTTP/1.1),但是在while循环中,当我打印raw_header时,它打印我raw header = [User-Age]而不是raw header = [User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)]
我能做些什么来解决这个问题?
【问题讨论】:
-
command_line = strtok_r(raw_headers, "\\n", &raw_headers);肯定是错的。