Linux平台C语言解析HttpResponse(http协议)

前言

解析http协议返回的数据。

目录

  • 常见的HTTP/1.1返回数据
  • HttpResponse结构体
  • 内存释放函数
  • HttpResponse解析函数

C代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "cJSON/cJSON.h"

/*
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin,Authorization,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Content-Type
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin:
Access-Control-Expose-Headers: Content-Length
Content-Length: 72
Content-Type: application/json; charset=utf-8

{
  ...
  ...
  "status": true
}
*/

// HttpResponse结构体
typedef struct {
    char *version;
    char *code;   // 状态返回码
    char *desc;   // 返回描述
    char *body;
    int bodySize;
} HttpResponse;

// 内存释放函数
void releaseHttpResponse(HttpResponse *httpResponse) {
    if (httpResponse == NULL) {
        return;
    }

    if (httpResponse->version != NULL) {
        free(httpResponse->version);
        httpResponse->version = NULL;
    }

    if (httpResponse->code != NULL) {
        free(httpResponse->code);
        httpResponse->code = NULL;
    }

    if (httpResponse->desc != NULL) {
        free(httpResponse->desc);
        httpResponse->desc = NULL;
    }

    if (httpResponse->body != NULL) {
        free(httpResponse->body);
        httpResponse->body = NULL;
    }

    free(httpResponse);
    httpResponse = NULL;
}

// HttpResponse解析函数
HttpResponse *parseResponse(char *response) {
    HttpResponse *_httpResponseTemp = (HttpResponse *) malloc(sizeof(HttpResponse));
    memset(_httpResponseTemp, 0, sizeof(HttpResponse));
    HttpResponse *_httpResponse = (HttpResponse *) malloc(sizeof(HttpResponse));
    memset(_httpResponse, 0, sizeof(HttpResponse));

    // 解析第一行
    char *_start = response;
    for (; *_start && *_start != '\r'; _start++) {
        if (_httpResponseTemp->version == NULL) {
            _httpResponseTemp->version = _start;
        }

        if (*_start == ' ') {
            if (_httpResponseTemp->code == NULL) {
                _httpResponseTemp->code = _start + 1;
            } else {
                _httpResponseTemp->desc = _start + 1;
            }
            *_start = '\0';
        }
    }
    *_start = '\0'; // \r -> \0
    _start++;   // skip \n
    //LOGD("parseResponse(): VERSION= %s , CODE= %s , DESC= %s", _httpResponseTemp->version, _httpResponseTemp->code, _httpResponseTemp->desc);

    // 解析header
    _start++;
    char *_line = _start;
    while (*_line != '\r' && *_line != '\0') {
        char *_key;
        char *_value;
        while (*(_start++) != ':');
        *(_start - 1) = '\0';
        _key = _line;
        _value = _start + 1;
        while(_start++, *_start != '\0' && *_start != '\r');
        *_start = '\0'; // \r -> \0
        _start++;   // skip \n

        _start++;
        _line = _start;

        //LOGD("parseResponse(): KEY= %s , VALUE= %s", _key, _value);
        if (!strcmp(_key, "Content-Length")) {
            _httpResponseTemp->bodySize = atoi(_value);
        }
    }

    // 解析body 如果最后一行不是空行,说明有body数据
    if (*_line == '\r') {
        _line += 2;
        _httpResponseTemp->body = _line;
        //LOGD("parseResponse(): BODY= %s", _httpResponseTemp->body);
    }

    if (_httpResponseTemp->version != NULL) {
        _httpResponse->version = strdup(_httpResponseTemp->version);
    }
    if (_httpResponseTemp->code != NULL) {
        _httpResponse->code = strdup(_httpResponseTemp->code);
    }
    if (_httpResponseTemp->desc != NULL) {
        _httpResponse->desc = strdup(_httpResponseTemp->desc);
    }
    if (_httpResponseTemp->body != NULL) {
        _httpResponse->body = strdup(_httpResponseTemp->body);
    }
    _httpResponse->bodySize = _httpResponseTemp->bodySize;

    free(_httpResponseTemp);
    _httpResponseTemp = NULL;

    return _httpResponse;
}

觉得有用的话,可以给我点奖励哦!(微信)
在这里插入图片描述

你可能感兴趣的:(Linux平台C语言开发,c语言,linux,开发语言,http)