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,%drn",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 %drn",s);
if (my_connect(s, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
DEBUG_printf("... socket connect failed.rn");
my_closesocket(s);
return 1;
}
DEBUG_printf("... connectedrn");
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********rn");
sprintf(header_bytes,"Range:bytes=%d-%drn",start,end);
sprintf(get_request,"GET /sis/tts/test02.pcm HTTP/1.1rnContent-Type: application/x-www-form-urlencoded;charset=UTF-8rnConnection: Keep-AlivernHost: hqx-default-sis.obs.cn-north-4.myhuaweicloud.comrn%srn",
header_bytes);
if (lwip_write(s, get_request, strlen(get_request)) < 0) {
my_closesocket(s);
DEBUG_printf("my_closesocketrn");
return 1;
}
//DEBUG_printf("... socket send success. %srn",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,%drn",s,SOL_SOCKET,SO_RCVTIMEO);
DEBUG_printf("... failed to set socket receiving timeoutrn");
my_closesocket(s);
return 1;
}
DEBUG_printf("... set socket receiving timeout successrn");
*/
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=%drn", r);
my_closesocket(s);
int down_end_time = hi_get_milli_seconds();
DEBUG_printf("down load time = %d msrn",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: %.*sn", (int)length, at);
strncpy(current_header_key,at,length);
current_header_key[length] = ' ';
return 0;
}
int on_header_value(http_parser* _, const char* at, size_t length) {
(void)_;
//DEBUG_printf("Header %s value: %.*sn",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,%drn",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: %.*sn", (int)length, at);
memcpy(audio_data_buff,at,length);
audio_data_len = length;
//DEBUG_printf("Body: %dn", (int)length);
return 0;
}
int on_message_begin(http_parser* _) {
(void)_;
//DEBUG_printf("n***MESSAGE BEGIN***nn");
return 0;
}
int on_message_complete(http_parser* _) {
(void)_;
//DEBUG_printf("n***MESSAGE COMPLETE***nn");
bParsed = true;
return 0;
}
int on_headers_complete(http_parser* _) {
(void)_;
//DEBUG_printf("n***HEADERS COMPLETE***nn");
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 = %Xn", ret);
}
//bParsed = false;
}
}
3、代码概要说明
http_parser库使用的时候,需要先初始化,audio_http_parser_init();需要在函数中实现回调。这样就可以解析数据了。
文章版权声明
1 原创文章作者:5739,如若转载,请注明出处: https://www.52hwl.com/94194.html
2 温馨提示:软件侵权请联系469472785#qq.com(三天内删除相关链接)资源失效请留言反馈
3 下载提示:如遇蓝奏云无法访问,请修改lanzous(把s修改成x)
4 免责声明:本站为个人博客,所有软件信息均来自网络 修改版软件,加群广告提示为修改者自留,非本站信息,注意鉴别