优化代码

This commit is contained in:
HEYAHONG 2023-12-26 17:12:17 +08:00
parent 2cf528144f
commit 19144c3024
No known key found for this signature in database
GPG Key ID: 97E3E469FE2C920B

View File

@ -13,62 +13,7 @@ void httpclient::luat_http_callback(int status, void *data, uint32_t data_len, v
std::string http_get(const char *url,std::string ca_crt,std::string client_crt,std::string key,std::string key_pass)
{
std::string data;
if(url==NULL || strlen(url) < 5)
{
return data;
}
httpclient client;
if(url[4]!='s' && url[4] !='S')
{
//http
if(!client.base())
{
//由于库函数错误未返回0此处不处理
//return data;
}
}
else
{
//https
if(!client.ssl(ca_crt,client_crt,key,key_pass))
{
return data;
}
}
bool is_end=false;
client.set_callback(
[&](int status)
{
(void)status;
is_end=true;
},
[&](void *_data,uint32_t _data_len)
{
if(data.length() > 40*1024)
{
//超过40K,由于内存限制,不可下载较大数据
is_end=true;
client.stop();
}
if(_data!=NULL && _data_len != 0)
{
data+=std::string((char *)_data,_data_len);
}
else
{
is_end=true;
}
});
if(!client.start(url))
{
return data;
}
while(!is_end)
{
luat_rtos_task_sleep(1000);
}
return data;
return http_post(url,std::string(),ca_crt,client_crt,key,key_pass);
}
@ -127,7 +72,7 @@ std::string http_post(const char *url,std::string post,std::string ca_crt,std::s
post=post.substr(256);
return ret;
});
if(!client.start(url,1))
if(!client.start(url,post.empty()?0:1))
{
return data;
}