我有一个长字符串,其中包含以下信息:
"GET http://www.google.se/ HTTP/1.1\r\n
Host: www.google.se\r\n
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:2.0) Gecko/20100101 Firefox/4.0\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
Accept-Language: en-us,en;q=0.5\r\n
Accept-Encoding: gzip, deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n
Keep-Alive: 115\r\n
Proxy-Connection: keep-alive\r\n
Cookie: PREF=ID=7d6a62c557413bc8:FF=0:TM=1327968040:LM=1327968040:S=U1A51rCRDDMTF295\r\n
Cache-Control: max-age=0\r\n\r\n"
我想摆脱代理连接下的keep-alive部分.. 这就是我到目前为止所做的:
现在我想摆脱keep-alive部分,然后将标题放在struct的header-part中。
有什么想法吗?
以下是我到目前为止所做的一些代码。我是C的新手,所以可能不是你见过的最漂亮的代码..
struct ParsedHeader header_parser(char * input) {
struct ParsedHeader h;
int status;
regex_t regex;
char * result_begin = NULL;
regcomp(®ex, "host:", REG_EXTENDED|REG_ICASE|REG_NOSUB);
if((status = regexec(®ex, input, (size_t) 0, NULL, 0)) == 0) {
char end = '\r';
char * header = malloc(strlen(input));
char * begin = "host:";
size_t result_size = 0;
memcpy(header, input, strlen(input));
to_lower(input, header, strlen(input));
result_begin = (strstr(header, begin) + 6);
char * result_end = strchr(result_begin, end);
result_end[0] = '\0';
//char result[strlen(result_begin)] = result_begin;
free(header);
}
regfree(®ex);
h.header = input;
h.host = result_begin;
return h;
}
答案 0 :(得分:3)
如果我正确理解您的问题,您只想删除“Keep-Alive”字符串。这样做:
// assume buffer contains the long input string
char *pBegin;
char *pEnd;
pBegin = strstr(buffer, "Keep-Alive:");
if (pBegin)
{
pEnd = strstr(pBegin, "\r\n");
if (pEnd)
strcpy(pBegin, pEnd+2);
}
但是,如果您要执行的操作是从Proxy-Connection标头中删除“keep-alive”字符串,则类似的解决方案有效:
char *pBegin;
char *pEnd;
pBegin = strstr(buffer, "Proxy-Connection:");
if (pBegin)
{
pBegin = strstr(pBegin, "keep-alive");
if (pBegin)
strcpy(pBegin, pBegin+strlen("keep-alive"));
}