Hi3861实现http的response数据解析 原创 精华

再见南丫岛
发布于 2022-3-4 09:55
1259浏览
3收藏

Hi3861的代码实例库中,介绍了http的连接,但是没有说明如何解析。当然,解析http返回值的方法是一个通用方法,不只限于在openharmony中使用。

1、http_parser库

http-parser是一个用C编写的HTTP消息解析器,可以解析请求和响应,被设计用于高性能HTTP应用程序。它不会进行任何系统调用及内存分配,它不会缓冲数据,它可以被随时中断。

2、源代码

/*
 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hi_stdlib.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "lwip/netifapi.h"
#include "lwip/netdb.h"
#include "lwip/netifapi.h"
#include <string.h>
#include <stdlib.h>
#include "lwip/sockets.h"
#include "hi_mem.h"
#include "hi_config.h"
#include "audio_http_client.h"
#include "../http_parser/http_parser.h"
#include "hi_i2s.h"
#include "hi_time.h"

#define HTTPC_DEMO_RECV_BUFSIZE 2048*2
#define SOCK_TARGET_PORT  80

//#define ADDRESS "192.168.1.91" //"192.168.0.200"
#define ADDRESS "121.36.121.226"

bool bParsed = false;
int total_len = 10902;
int frame_len = 2560;

char current_header_key[64];

void audio_http_parser_init();
void audio_http_parser_exec(char *buf,int len);
/*****************************************************************************
* Func description: demo for http get action
*****************************************************************************/
unsigned int audio_http_clienti_get()
{
    struct sockaddr_in addr = {0};
    int s, r;
    char recv_buf[HTTPC_DEMO_RECV_BUFSIZE];
    addr.sin_family = AF_INET;
    addr.sin_port = PP_HTONS(SOCK_TARGET_PORT);
    //addr.sin_port =  my_htons(SOCK_TARGET_PORT);
    addr.sin_addr.s_addr = ipaddr_addr(ADDRESS);
    printf("addr=%d,%d,%d\r\n",addr.sin_family,addr.sin_port,addr.sin_addr.s_addr);
    s = my_socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0) {
        return 1;
    }
    DEBUG_printf("... allocated socket %d\r\n",s);
    if (my_connect(s, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
        DEBUG_printf("... socket connect failed.\r\n");
        my_closesocket(s);
        return 1;
    }
    DEBUG_printf("... connected\r\n");

    int start = 0;
    int length = frame_len -1;
    int end = start + length;

    char get_request[512];
    char header_bytes[256];

    int body_recv_len = 0;

    int down_start_time = hi_get_milli_seconds();
    while (1)
    {
        //DEBUG_printf("******start********\r\n");
        sprintf(header_bytes,"Range:bytes=%d-%d\r\n",start,end);
        sprintf(get_request,"GET /sis/tts/test02.pcm HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded;charset=UTF-8\r\nConnection: Keep-Alive\r\nHost: hqx-default-sis.obs.cn-north-4.myhuaweicloud.com\r\n%s\r\n",
        header_bytes);

        if (lwip_write(s, get_request, strlen(get_request)) < 0) {
            my_closesocket(s);
            DEBUG_printf("my_closesocket\r\n");
            return 1;
        }
        //DEBUG_printf("... socket send success. %s\r\n",header_bytes);
        struct timeval receiving_timeout;
        
        /* 5S Timeout */
        /*
        receiving_timeout.tv_sec = 1;
        receiving_timeout.tv_usec = 0;
        if (my_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout, sizeof(receiving_timeout)) < 0) {
            printf("setsockopt = %d,%d,%d\r\n",s,SOL_SOCKET,SO_RCVTIMEO);
            DEBUG_printf("... failed to set socket receiving timeout\r\n");
            my_closesocket(s);
            return 1;
        }
        DEBUG_printf("... set socket receiving timeout success\r\n");
        */
        audio_http_parser_init();
        /* Read HTTP response */
        do {
            (void)memset_s(recv_buf, sizeof(recv_buf), 0, sizeof(recv_buf));
            r = lwip_read(s, recv_buf, sizeof(recv_buf) - 1);
            
            //for (int i = 0; i < r; i++) {
            //    putchar(recv_buf[i]);
            //}
            DEBUG_printf("-%d-",r);
            audio_http_parser_exec(recv_buf,r);
            if (bParsed)
            {
                bParsed = false;
                break;
            }
        } while (r > 0);

        start = start + length + 1;
        end = start + length;
        if(end > total_len - 1)
            end = total_len - 1;
        if(start > total_len - 1)
            break;
    }
    //DEBUG_printf("... done reading from socket. Last read return=%d\r\n", r);
    my_closesocket(s);
    int down_end_time = hi_get_milli_seconds();
    DEBUG_printf("down load time = %d ms\r\n",down_end_time - down_start_time);
    return 0;
}

int on_header_field(http_parser* _, const char* at, size_t length) {
    (void)_;
    //DEBUG_printf("Header field: %.*s\n", (int)length, at);
    strncpy(current_header_key,at,length);
    current_header_key[length] = '\0';
    return 0;
}
int on_header_value(http_parser* _, const char* at, size_t length) {
    (void)_;
    //DEBUG_printf("Header %s value: %.*s\n",current_header_key, (int)length, at);
    if (strcmp(current_header_key, "Content-Range") == 0) 
    {
        int s=0,e=0,t=0;
        sscanf(at,"bytes %d-%d/%d",&s,&e,&t);
        //DEBUG_printf("parser=%d,%d,%d\r\n",s,e,t);
        total_len = t;
    }
    return 0;
}

unsigned char audio_data_buff[2048];
int audio_data_len = 0;

int on_body(http_parser* _, const char* at, size_t length) {
  (void)_;
  //DEBUG_printf("Body: %.*s\n", (int)length, at);
  memcpy(audio_data_buff,at,length);
  audio_data_len = length;
  //DEBUG_printf("Body: %d\n", (int)length);
  return 0;
}
int on_message_begin(http_parser* _) {
  (void)_;
  //DEBUG_printf("\n***MESSAGE BEGIN***\n\n");
  return 0;
}

int on_message_complete(http_parser* _) {
  (void)_;
  //DEBUG_printf("\n***MESSAGE COMPLETE***\n\n");
  bParsed = true;
  return 0;
}

int on_headers_complete(http_parser* _) {
  (void)_;
  //DEBUG_printf("\n***HEADERS COMPLETE***\n\n");
  return 0;
}

http_parser_settings settings;
http_parser parser;  
void audio_http_parser_init(char *buf,int len)
{
    http_parser_settings_init(&settings);
    settings.on_header_field = on_header_field;
    settings.on_header_value = on_header_value;
    settings.on_body = on_body;
    settings.on_headers_complete = on_headers_complete;
	settings.on_message_complete = on_message_complete;

    http_parser_init(&parser, HTTP_RESPONSE);
}

void audio_http_parser_exec(char *buf,int len)
{
    http_parser_execute(&parser, &settings, buf,len);  //执行解析过程
    if (bParsed)
    {
        DEBUG_printf("#");
        audio_data_len = frame_len;
        
        hi_u32 ret = hi_i2s_write(audio_data_buff,audio_data_len,1000);
        if (ret != HI_ERR_SUCCESS) {
            DEBUG_printf("hi_i2s_write fail, err = %X\n", ret);
        }

        //bParsed = false;
    }
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.

3、代码概要说明

http_parser库使用的时候,需要先初始化,audio_http_parser_init();需要在函数中实现回调。这样就可以解析数据了。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
2
收藏 3
回复
举报
2
1
3
1条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

赞,连接和解析http都是必须要了解的一部分

回复
2022-3-4 10:32:29
回复
    相关推荐