Hi3861实现http的response数据解析 原创 精华
再见南丫岛
发布于 2022-3-4 09:55
浏览
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;
}
}
3、代码概要说明
http_parser库使用的时候,需要先初始化,audio_http_parser_init();需要在函数中实现回调。这样就可以解析数据了。
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
赞
2
收藏 3
回复
相关推荐
赞,连接和解析http都是必须要了解的一部分