Linux平台C简单解析Http报文

文章详细介绍了如何用C语言编写一个HttpResponse解析函数,处理HTTP响应头和内容长度,以及内存管理。作者是CSDN博主lchaofan2008。

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <math.h>
#include <pthread.h>
#include <sys/ioctl.h>
#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;
}
————————————————
版权声明:本文为CSDN博主「lichaofan2008」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lichaofan2008/article/details/121990221

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值