我有一个包含多个HTTP标头的char。 现在我想分割标题并将每个标题存储到char []数组中,如
myheader[0] = "POST /index001.html HTTP/1.1\r\nHost:xxx ...";
myheader[1] = "POST /index002.html HTTP/1.1\r\nHost:xxx ..."
每个标头大小都不同,所以我不能计算字节数。我不知道该怎么做。 strstr()只查找第一个子字符串。
缓冲区格式如下:
POST /index001.html HTTP/1.1
Host: xxx
...
POST /index002.html HTTP/1.1
Host: xxx
...
POST /index003.html HTTP/1.1
Host: xxx
...
任何人都可以帮我解决代码问题吗?
编辑:
我编写了一个从缓冲区中提取子字符串的函数。但它只能处理第一对“POST”。 例如,
GetInBetween(“1234www567890rrr777wwwOUEJF99rrr”,“www”,“rrr”)只能获得“567890”。
char * GetInBetween( char* buffer, char* start, char* end)
{
char * subbuffer = NULL;
subbuffer = strstr(buffer, startpoint);
int betweenLen = 0;
if (subbuffer == NULL)
{
return NULL;
}
betweenLen = strcspn(subbuffer, endpoint );
int sbLen=0;
sbLen= strlen(subbuffer);
int spLen =0;
spLen = strlen(startpoint);
int dataLen =0;
dataLen = betweenLen - spLen;
char databuffer[1024];
for (int i =0; i< dataLen; i++)
{
databuffer[0+i] = subbuffer[4+i];
}
databuffer[dataLen] ='\0';
int test = strlen(databuffer);
return databuffer;
}
答案 0 :(得分:1)
你可以改变你的功能,让你有一些早期调用的记忆,比如strtok
。然后,您可以在同一缓冲区中重复调用您的函数,以查找任何后续事件。作为这种方法的草图(自己完成):
char * GetInBetween( char* buffer, char* startstr, char* endstr)
{
static char *lastbuf, *lastbufp, *laststartstr, *lastendstr;
char * subbuffer = NULL;
if (startstr==NULL || endstr==NULL) {
if (lastbuf!=buffer) return NULL; // not same buffer: error;
if (lastbufp==NULL) return NULL; // no previous match, so no next match
subbuffer= strstr(lastbufp, laststartstr);
}
else
{
laststartstr= startstr; // remember the parts to search in between
lastendstr= endstr;
lastbuf= buffer;
subbuffer = strstr(buffer, startpoint);
lastbufp= NULL; // no match yet to remember
}