两种另类的解析http响应头的方法

   最近需求需对http响应头进行解析,提供两种思路,方法1是使用开源的库http_parser.c,方法二是使用strstr函数进行解析.

两个方案都比较简单,没有什么难度,这里粘贴一下方法二的部分元代码。strstr在这里起了很大的作用。

 unsigned char  *pread_data_pos = NULL;
    unsigned char *data_blk_buf = NULL;
    int data_blk_size = 0;
   int bodylength,ResponseHeadlength=0;

    if((NULL == strstr(&recData[0], "HTTP/1.1 200 OK")) && (NULL == strstr(&recData[0],"HTTP/1.0 200 OK")))
                                 {
                                      
                                       printf("%s   response  error.\n", "__func__");
                                        return 0 ;
                                 }
    bodylength = atoi((char *)(strstr((const char*)recData,"Content-Length: ")+16));
    printf("bodylength is %d \r\n",bodylength);
    pread_data_pos = (unsigned char*)(strstr((const char*)recData,"\r\n\r\n")+4);
    ResponseHeadlength = pread_data_pos - (unsigned char*)recData;
    //data_blk_buf   +=  ResponseHeadlength;
  //  data_blk_size  -=   ResponseHeadlength;
    printf("ota Response  Head  Length = %d\n",ResponseHeadlength);  
    int http_check = atoi((char *)(recData + 9));
      if((http_check == 200 )|| (http_check == 206))
         {
              printf("ota response ok!\n");
        
         }
      unsigned char data_temp[2048]={0};
    //data_blk_buf;
     memcpy(data_temp,pread_data_pos, bodylength);
     printf("body is data_temp  \r\n%s",data_temp);

 

两种另类的解析http响应头的方法