lightduer_http_client.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * Copyright (2017) Baidu Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * File: lightduer_http_client.h
  18. * Author: Pan Haijun, Gang Chen(chengang12@baidu.com)
  19. * Desc: HTTP Client Head File
  20. */
  21. #ifndef BAIDU_DUER_LIGHTDUER_HTTP_CLIENT_H
  22. #define BAIDU_DUER_LIGHTDUER_HTTP_CLIENT_H
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "lightduer_timers.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define DUER_HTTP_CONTENT_TYPE_LEN_MAX (32) // the max length of "content type" section
  30. //http client results
  31. typedef enum {
  32. DUER_HTTP_ERR_FAILED = -1,
  33. DUER_HTTP_OK, // Success
  34. DUER_HTTP_PROCESSING, // Processing
  35. DUER_HTTP_ERR_PARSE, // url Parse error
  36. DUER_HTTP_ERR_DNS, // Could not resolve name
  37. DUER_HTTP_ERR_PRTCL, // Protocol error
  38. DUER_HTTP_ERR_NOT_FOUND, // HTTP 404 Error
  39. DUER_HTTP_ERR_REFUSED, // HTTP 403 Error
  40. DUER_HTTP_ERR_TIMEOUT, // Connection timeout
  41. DUER_HTTP_ERR_CONNECT, // Connection error
  42. DUER_HTTP_CLOSED, // Connection was closed by remote host
  43. DUER_HTTP_ERR_NOT_SUPPORT, // not supported feature
  44. DUER_HTTP_REDIRECTTION, //take a redirection when http header contains 'Location'
  45. } duer_http_result_t;
  46. typedef enum {
  47. DUER_HTTP_GET, // Just 'GET' is supported, other methods are reserved
  48. DUER_HTTP_POST, // reserved
  49. DUER_HTTP_PUT, // reserved
  50. DUER_HTTP_DELETE, // reserved
  51. DUER_HTTP_HEAD // reserved
  52. } duer_http_method_t;
  53. typedef struct {
  54. int n_handle;
  55. int (*init)(void *socket_args);
  56. int (*open)(int socket_handle);
  57. int (*connect)(int socket_handle, const char *host, const int port);
  58. void (*set_blocking)(int socket_handle, int blocking);
  59. void (*set_timeout)(int socket_handle, int timeout);
  60. int (*recv)(int socket_handle, void *data, unsigned size);
  61. int (*send)(int socket_handle, const void *data, unsigned size);
  62. int (*close)(int socket_handle);
  63. void (*destroy)(int socket_handle);
  64. } duer_http_socket_ops_t;
  65. //to tell data output callback user that if the current data block is first block or last block
  66. typedef enum {
  67. DUER_HTTP_DATA_FIRST = 0x1,
  68. DUER_HTTP_DATA_MID = 0x2,
  69. DUER_HTTP_DATA_LAST = 0x4
  70. } duer_http_data_pos_t;
  71. /**
  72. *
  73. * DESC:
  74. * the callback to handler the data download by http
  75. *
  76. * PARAM:
  77. * @param[in] p_user_ctx: usr ctx registed by user
  78. * @param[in] pos: to identify if it is data stream's start, or middle , or end of data stream
  79. * @param[in] buf: buffer stored media data
  80. * @param[in] len: data length in 'buf'
  81. * @param[in] type: data type to identify media or others
  82. *
  83. * @RETURN: negative number when failed
  84. */
  85. typedef int (*duer_http_data_handler)(void *p_user_ctx, duer_http_data_pos_t pos,
  86. const char *buf, size_t len, const char *type);
  87. /**
  88. * DESC:
  89. * callback to check whether need to stop getting data
  90. *
  91. * PARAM: none
  92. *
  93. * @RETURN 1: to stop; 0: no stop
  94. */
  95. typedef int (*duer_http_stop_notify_cb_t)();
  96. /**
  97. * DESC:
  98. * sometimes other module need to know which url is used to download data.
  99. * this callback is used to get the url
  100. *
  101. * PARAM:
  102. * @param[in] url: the url used by http to download data
  103. *
  104. * RETURN: none
  105. */
  106. typedef void (*duer_http_get_url_cb_t)(const char *url);
  107. typedef struct {
  108. const char **pps_custom_headers;
  109. size_t sz_headers_count;
  110. int n_http_response_code; // http response code
  111. duer_http_socket_ops_t ops; // socket operations
  112. int scheme; // http or https type,1 is https, 0 is http
  113. duer_http_data_handler data_hdlr_cb; // callback for output data
  114. void *p_data_hdlr_ctx; // users args for data_hdlr_cb
  115. int n_http_content_len; // http content length
  116. // http content type
  117. char p_http_content_type[DUER_HTTP_CONTENT_TYPE_LEN_MAX];
  118. char *p_location; // http header "Location"
  119. // a callback to check that stopping http client getting data or not
  120. duer_http_stop_notify_cb_t check_stop_notify_cb;
  121. // to get the url used to download, the last url will be returned if 302 location happen
  122. duer_http_get_url_cb_t get_url_cb;
  123. int n_is_chunked;
  124. size_t sz_chunk_size;
  125. char *p_chunk_buf;
  126. size_t recv_size; // size of the data have been dwonload
  127. // the count of http continuously try to resume from break-point
  128. int resume_retry_count;
  129. char *buf; // buf used to receive data
  130. duer_http_data_pos_t data_pos; // play position
  131. char *last_host; // the lastest connected host
  132. unsigned short last_port; // the lastest connected port
  133. duer_timer_handler connect_keep_timer; // used to close persistent connect
  134. size_t upload_size; // size of the data have been upload
  135. } duer_http_client_t;
  136. /**
  137. * DESC:
  138. * init the http client
  139. *
  140. * PARAM:
  141. * @param[in] p_client: pointer of the http client
  142. *
  143. * RETURN: DUER_HTTP_OK if success, DUER_HTTP_ERR_FAILED if failed
  144. */
  145. duer_http_result_t duer_http_init(duer_http_client_t *p_client);
  146. /**
  147. * DESC:
  148. * destroy the http client
  149. *
  150. * PARAM:
  151. * @param[in] p_client: pointer of the http client
  152. *
  153. * RETURN: none
  154. */
  155. void duer_http_destroy(duer_http_client_t *p_client);
  156. /**
  157. * DESC:
  158. * to init http client socket operations
  159. *
  160. * PARAM:
  161. * @param[in] p_client: pointer of the http client
  162. * @param[in] p_ops: socket operations set
  163. * @param[in] socket_args: args for ops "get_inst"
  164. *
  165. * RETURN: DUER_HTTP_OK if success, DUER_HTTP_ERR_FAILED if failed
  166. */
  167. duer_http_result_t duer_http_init_socket_ops(duer_http_client_t *p_client,
  168. duer_http_socket_ops_t *p_ops,
  169. void *socket_args);
  170. /**
  171. * DESC:
  172. * register data output handler callback to handle data block
  173. *
  174. * @param[in] p_client: pointer of the http client
  175. * @param[in] data_hdlr_cb: data output handler callback to be registered
  176. * @param[in] p_usr_ctx: user context for data output handler
  177. *
  178. * @RETURN none
  179. */
  180. void duer_http_reg_data_hdlr(duer_http_client_t *p_client,
  181. duer_http_data_handler data_hdlr_cb,
  182. void *p_usr_ctx);
  183. /**
  184. *
  185. * DESC:
  186. * register callback to get the url which is used by http to get data
  187. *
  188. * PARAM:
  189. * @param[in] p_client: pointer point to the duer_http_client_t
  190. * @param[in] cb: the callback to be registered
  191. *
  192. * @RETURN none
  193. */
  194. void duer_http_reg_url_get_cb(duer_http_client_t *p_client, duer_http_get_url_cb_t cb);
  195. /**
  196. * DESC:
  197. * Set custom headers for request.
  198. * Pass NULL, 0 to turn off custom headers.
  199. *
  200. * PARAM:
  201. * @param[in] p_client: pointer point to the duer_http_client_t
  202. * @param[in] headers: an array (size multiple of two) key-value pairs,
  203. * must remain valid during the whole HTTP session
  204. * const char * hdrs[] =
  205. * {
  206. * "Connection", "keep-alive",
  207. * "Accept", "text/html",
  208. * "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)",
  209. * "Accept-Encoding", "gzip,deflate,sdch",
  210. * "Accept-Language", "en-US,en;q=0.8",
  211. * };
  212. *
  213. * duer_http_set_cust_headers(hdrs, 5);
  214. * @param[in] pairs: number of key-value pairs
  215. *
  216. * @RETURN none
  217. */
  218. void duer_http_set_cust_headers(duer_http_client_t *p_client, const char **headers, size_t pairs);
  219. /**
  220. * DESC:
  221. * get data by http client
  222. *
  223. * PARAM:
  224. * @param[in] p_client: pointer point to the duer_http_client_t
  225. * @param[in] url: url resource
  226. * @param[in] pos: the position to receive the http data,
  227. * sometimes user only want to get part of the data
  228. * @param[in] connect_keep_time: how long time the connection should be kept after download finish
  229. *
  230. * RETURN DUER_HTTP_OK if success, other duer_http_result_t type code if failed
  231. */
  232. duer_http_result_t duer_http_get(duer_http_client_t *p_client,
  233. const char *url,
  234. const size_t pos,
  235. const int connect_keep_time);
  236. /**
  237. * DESC:
  238. * get http response code
  239. *
  240. * PARAM:
  241. * @param[in] p_client: pointer point to the duer_http_client_t
  242. *
  243. * @RETURN none
  244. */
  245. int duer_http_get_rsp_code(duer_http_client_t *p_client);
  246. /**
  247. * DESC:
  248. * register callback to check stop flag
  249. *
  250. * @param[in] p_client: pointer of the http client
  251. * @param[in] chk_stp_cb: to notify httpclient to stop
  252. *
  253. * @RETURN none
  254. */
  255. void duer_http_reg_stop_notify_cb(duer_http_client_t *p_client,
  256. duer_http_stop_notify_cb_t chk_stp_cb);
  257. /**
  258. * DESC:
  259. * get http download progress
  260. *
  261. * PARAM:
  262. * @param[in] p_client: pointer point to the duer_http_client_t
  263. * @param[out] total_size: pointer point to a varirable to receive the total size to be download,
  264. * 0 will be returned if it's chunk transfer
  265. * @param[out] recv_size: pointer point to a varirable to receive the data size have been download
  266. *
  267. * RETURN none
  268. */
  269. void duer_http_get_download_progress(duer_http_client_t *p_client,
  270. size_t *total_size,
  271. size_t *recv_size);
  272. #ifdef __cplusplus
  273. }
  274. #endif
  275. #endif // BAIDU_DUER_LIGHTDUER_HTTP_CLIENT_H