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
*
* 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
#include
#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);
//}
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] = ' |