luat_soc_sdk_2024 V1.0
luat_http.h
浏览该文件的文档.
1#ifndef LUAT_HTTP_H
2#define LUAT_HTTP_H
3
4#ifdef __LUATOS__
5#include "luat_zbuff.h"
6
7
8#if defined(AIR101) || defined(AIR103)
9// #define HTTP_REQ_HEADER_MAX_SIZE (2048)
10#define HTTP_RESP_BUFF_SIZE (2048)
11#else
12// #define HTTP_REQ_HEADER_MAX_SIZE (8192)
13#define HTTP_RESP_BUFF_SIZE (8192)
14#endif
15#else
16#include "http_parser.h"
17#define HTTP_HEADER_BASE_SIZE (1024)
18#define HTTP_RESP_BUFF_SIZE (4096)
19
20#endif
25enum
26{
36};
37
38#ifndef __LUATOS__
39
40/*
41 * http运行过程的回调函数
42 * status >=0 表示运行状态,看HTTP_STATE_XXX <0说明出错停止了,==0表示结束了
43 * data 在获取响应阶段,会回调head数据,如果为NULL,则head接收完成了。如果设置了data_mode,body数据也直接回调。
44 * data_len 数据长度,head接收时,每行会多加一个\0,方便字符串处理
45 * user_param 用户自己的参数
46 */
47typedef void (*luat_http_cb)(int status, void *data, uint32_t data_len, void *user_param);
48
49#define HTTP_GET_DATA (2)
50#define HTTP_POST_DATA (1)
51#endif
52
53#define HTTP_OK (0)
54#define HTTP_ERROR_STATE (-1)
55#define HTTP_ERROR_HEADER (-2)
56#define HTTP_ERROR_BODY (-3)
57#define HTTP_ERROR_CONNECT (-4)
58#define HTTP_ERROR_CLOSE (-5)
59#define HTTP_ERROR_RX (-6)
60#define HTTP_ERROR_DOWNLOAD (-7)
61#define HTTP_ERROR_TIMEOUT (-8)
62#define HTTP_ERROR_FOTA (-9)
63
64#define HTTP_CALLBACK (1)
65
66#define HTTP_RE_REQUEST_MAX (3)
67
68#define HTTP_TIMEOUT (10*60*1000) // 10分钟
69
70typedef struct{
71 network_ctrl_t *netc; // http netc
72 http_parser parser; //解析相关
73 char *host;
75 uint16_t remote_port;
76 uint8_t is_tls; // 是否SSL
77 uint8_t custom_host;
78 uint8_t is_post;
80 uint32_t timeout;
81 uint32_t tx_offset;
82#ifdef __LUATOS__
83 luat_ip_addr_t ip_addr; // http ip
84 // const char *url; // url
85 // const char *uri; // uri
86 // char method[12]; // method
87
88 // 发送相关
89 // uint8_t request_message[HTTP_REQUEST_BUF_LEN_MAX];
90 char *req_header;
91 char *req_body; //发送body
92 size_t req_body_len; //发送body长度
93 luat_zbuff_t *zbuff_body;
94 char *req_auth;
95#ifdef LUAT_USE_FOTA
96 //OTA相关
97 uint8_t isfota; //是否为ota下载
98 uint32_t address;
99 uint32_t length;
100 luat_spi_device_t* spi_device;
101#endif
102 int http_cb; // http lua回调函数
103 int http_cb_userdata; // http lua回调函数用户传参
104 //下载相关
105 uint8_t is_download; //是否下载
106 char *dst; //下载路径
107
108 // http_parser_settings parser_settings;
109 char* headers;
110 uint32_t headers_len; //headers缓存长度
111 char* body;
112 uint32_t body_len; //body缓存长度
113 // uint8_t is_chunk; //是否chunk编码
114 uint8_t re_request_count;
115 // 响应相关
116 int32_t resp_content_len; //content 长度
117 FILE* fd; //下载 FILE
118 uint64_t idp;
119
120 uint8_t headers_complete;
121 uint8_t close_state;
122 char resp_buff[HTTP_RESP_BUFF_SIZE];
123 size_t resp_buff_offset;
124 size_t resp_headers_done;
125#else
126 Buffer_Struct request_head_buffer;
129 Buffer_Struct response_head_buffer;
131 Buffer_Struct response_cache;
132 uint32_t total_len;
133 uint32_t done_len;
134 uint32_t offset;
136 uint8_t retry_cnt;
137 uint8_t state;
138 uint8_t data_mode;
139 uint8_t is_pause;
140 uint8_t debug_onoff;
141 uint8_t new_data;
142#endif
144#ifdef __LUATOS__
145int luat_http_client_init(luat_http_ctrl_t* http, int ipv6);
147#else
148
157luat_http_ctrl_t* luat_http_client_create(luat_http_cb cb, void *user_param, int adapter_index);
167int luat_http_client_base_config(luat_http_ctrl_t* http_ctrl, uint32_t timeout, uint8_t debug_onoff, uint8_t retry_cnt);
183int luat_http_client_ssl_config(luat_http_ctrl_t* http_ctrl, int mode, const char *server_cert, uint32_t server_cert_len,
184 const char *client_cert, uint32_t client_cert_len,
185 const char *client_cert_key, uint32_t client_cert_key_len,
186 const char *client_cert_key_password, uint32_t client_cert_key_password_len);
187
196
205int luat_http_client_set_user_head(luat_http_ctrl_t *http_ctrl, const char *name, const char *value);
206
207
218int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t type, uint8_t ipv6, uint8_t continue_mode);
234
243int luat_http_client_post_body(luat_http_ctrl_t *http_ctrl, void *data, uint32_t len);
258int luat_http_client_pause(luat_http_ctrl_t *http_ctrl, uint8_t is_pause);
266int luat_http_client_set_get_offset(luat_http_ctrl_t *http_ctrl, uint32_t offset);
267// int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t is_post, uint8_t ipv6, uint8_t data_mode);
269#endif
270#endif
int luat_http_client_base_config(luat_http_ctrl_t *http_ctrl, uint32_t timeout, uint8_t debug_onoff, uint8_t retry_cnt)
http客户端的通用配置,创建客户端时已经有默认配置,可以不配置
int luat_http_client_set_get_offset(luat_http_ctrl_t *http_ctrl, uint32_t offset)
GET请求时要求服务器从offset位置开始传输数据,谨慎使用
int luat_http_client_get_status_code(luat_http_ctrl_t *http_ctrl)
http获取状态码
int luat_http_client_pause(luat_http_ctrl_t *http_ctrl, uint8_t is_pause)
http客户端设置暂停
int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t type, uint8_t ipv6, uint8_t continue_mode)
启动一个http请求
int luat_http_client_destroy(luat_http_ctrl_t **p_http_ctrl)
完全释放掉当前的http客户端
int luat_http_client_ssl_config(luat_http_ctrl_t *http_ctrl, int mode, const char *server_cert, uint32_t server_cert_len, const char *client_cert, uint32_t client_cert_len, const char *client_cert_key, uint32_t client_cert_key_len, const char *client_cert_key_password, uint32_t client_cert_key_password_len)
客户端SSL配置,只有访问https才需要配置
luat_http_ctrl_t * luat_http_client_create(luat_http_cb cb, void *user_param, int adapter_index)
创建一个http客户端
int luat_http_client_post_body(luat_http_ctrl_t *http_ctrl, void *data, uint32_t len)
POST请求时发送body数据,如果数据量比较大,可以在HTTP_STATE_SEND_BODY回调里分次发送
int luat_http_client_close(luat_http_ctrl_t *http_ctrl)
停止当前的http请求,调用后不再有http回调了
void(* luat_http_cb)(int status, void *data, uint32_t data_len, void *user_param)
int luat_http_client_set_user_head(luat_http_ctrl_t *http_ctrl, const char *name, const char *value)
设置一条用户的request head参数,Content-Length一般不需要,在设置POST的body时自动生成
int luat_http_client_clear(luat_http_ctrl_t *http_ctrl)
清空用户设置的POST数据和request head参数
@ HTTP_STATE_WAIT_CLOSE
@ HTTP_STATE_SEND_BODY_START
@ HTTP_STATE_GET_BODY
@ HTTP_STATE_GET_HEAD
@ HTTP_STATE_SEND_HEAD
@ HTTP_STATE_IDLE
@ HTTP_STATE_SEND_BODY
@ HTTP_STATE_CONNECT
@ HTTP_STATE_DONE
#define HTTP_RESP_BUFF_SIZE
Buffer_Struct response_cache
Buffer_Struct response_head_buffer
network_ctrl_t * netc
luat_http_cb http_cb
Buffer_Struct request_head_buffer