xref: /OK3568_Linux_fs/external/rknpu2/examples/3rdparty/zlmediakit/include/mk_httpclient.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3  *
4  * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5  *
6  * Use of this source code is governed by MIT license that can be found in the
7  * LICENSE file in the root of the source tree. All contributing project authors
8  * may be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MK_HTTPCLIENT_H_
12 #define MK_HTTPCLIENT_H_
13 
14 #include "mk_common.h"
15 #include "mk_events_objects.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 ///////////////////////////////////////////HttpDownloader/////////////////////////////////////////////
22 
23 typedef void *mk_http_downloader;
24 
25 /**
26  * @param user_data 用户数据指针
27  * @param code 错误代码,0代表成功
28  * @param err_msg 错误提示
29  * @param file_path 文件保存路径
30  */
31 typedef void(API_CALL *on_mk_download_complete)(void *user_data, int code, const char *err_msg, const char *file_path);
32 
33 /**
34  * 创建http[s]下载器
35  * @return 下载器指针
36  */
37 API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create();
38 
39 /**
40  * 销毁http[s]下载器
41  * @param ctx 下载器指针
42  */
43 API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx);
44 
45 /**
46  * 开始http[s]下载
47  * @param ctx 下载器指针
48  * @param url http[s]下载url
49  * @param file 文件保存路径
50  * @param cb 回调函数
51  * @param user_data 用户数据指针
52  */
53 API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data);
54 
55 
56 ///////////////////////////////////////////HttpRequester/////////////////////////////////////////////
57 typedef void *mk_http_requester;
58 
59 /**
60  * http请求结果回调
61  * 在code == 0时代表本次http会话是完整的(收到了http回复)
62  * 用户应该通过user_data获取到mk_http_requester对象
63  * 然后通过mk_http_requester_get_response等函数获取相关回复数据
64  * 在回调结束时,应该通过mk_http_requester_release函数销毁该对象
65  * 或者调用mk_http_requester_clear函数后再复用该对象
66  * @param user_data 用户数据指针
67  * @param code 错误代码,0代表成功
68  * @param err_msg 错误提示
69  */
70 typedef void(API_CALL *on_mk_http_requester_complete)(void *user_data, int code, const char *err_msg);
71 
72 /**
73  * 创建HttpRequester
74  */
75 API_EXPORT mk_http_requester API_CALL mk_http_requester_create();
76 
77 /**
78  * 在复用mk_http_requester对象时才需要用到此方法
79  */
80 API_EXPORT void API_CALL mk_http_requester_clear(mk_http_requester ctx);
81 
82 /**
83  * 销毁HttpRequester
84  * 如果调用了mk_http_requester_start函数且正在等待http回复,
85  * 也可以调用mk_http_requester_release方法取消本次http请求
86  */
87 API_EXPORT void API_CALL mk_http_requester_release(mk_http_requester ctx);
88 
89 /**
90  * 设置HTTP方法,譬如GET/POST
91  */
92 API_EXPORT void API_CALL mk_http_requester_set_method(mk_http_requester ctx,const char *method);
93 
94 /**
95  * 批量设置设置HTTP头
96  * @param header 譬如 {"Content-Type","text/html",NULL} 必须以NULL结尾
97  */
98 API_EXPORT void API_CALL mk_http_requester_set_header(mk_http_requester ctx, const char *header[]);
99 
100 /**
101  * 添加HTTP头
102  * @param key 譬如Content-Type
103  * @param value 譬如 text/html
104  * @param force 如果已经存在该key,是否强制替换
105  */
106 API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,const char *key,const char *value,int force);
107 
108 /**
109  * 设置消息体,
110  * @param body mk_http_body对象,通过mk_http_body_from_string等函数生成,使用完毕后请调用mk_http_body_release释放之
111  */
112 API_EXPORT void API_CALL mk_http_requester_set_body(mk_http_requester ctx, mk_http_body body);
113 
114 /**
115  * 在收到HTTP回复后可调用该方法获取状态码
116  * @return 譬如 200 OK
117  */
118 API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx);
119 
120 /**
121  * 在收到HTTP回复后可调用该方法获取响应HTTP头
122  * @param key HTTP头键名
123  * @return  HTTP头键值
124  */
125 API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key);
126 
127 /**
128  * 在收到HTTP回复后可调用该方法获取响应HTTP body
129  * @param length 返回body长度,可以为null
130  * @return body指针
131  */
132 API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requester ctx, size_t *length);
133 
134 /**
135  * 在收到HTTP回复后可调用该方法获取响应
136  * @return 响应对象
137  */
138 API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx);
139 
140 /**
141  * 设置回调函数
142  * @param cb 回调函数,不能为空
143  * @param user_data 用户数据指针
144  */
145 API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data);
146 
147 /**
148  * 开始url请求
149  * @param url 请求url,支持http/https
150  * @param timeout_second 最大超时时间
151  */
152 API_EXPORT void API_CALL mk_http_requester_start(mk_http_requester ctx,const char *url, float timeout_second);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif /* MK_HTTPCLIENT_H_ */
159