oss_util.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #ifndef LIBOSS_UTIL_H
  2. #define LIBOSS_UTIL_H
  3. #include "aos_string.h"
  4. #include "aos_transport.h"
  5. #include "aos_status.h"
  6. #include "oss_define.h"
  7. #include "oss_resumable.h"
  8. OSS_CPP_START
  9. #define init_sts_token_header() do { \
  10. if (options->config->sts_token.data != NULL) {\
  11. apr_table_set(headers, OSS_STS_SECURITY_TOKEN, options->config->sts_token.data);\
  12. }\
  13. } while(0)
  14. /**
  15. * @brief check hostname ends with specific oss domain suffix.
  16. **/
  17. int is_oss_domain(const aos_string_t *str);
  18. /**
  19. * @brief check hostname is ip.
  20. **/
  21. int is_valid_ip(const char *str);
  22. /**
  23. * @brief get oss acl str according oss_acl
  24. * @param[in] oss_acl the oss bucket acl
  25. * @return oss acl str
  26. **/
  27. const char *get_oss_acl_str(oss_acl_e oss_acl);
  28. /**
  29. * @brief create oss config including host, port, access_key_id, access_key_secret, is_oss_domain
  30. **/
  31. oss_config_t *oss_config_create(aos_pool_t *p);
  32. /**
  33. * @brief evaluate config to curl
  34. **/
  35. void oss_config_resolve(aos_pool_t *pool, oss_config_t *config, aos_http_controller_t *ctl);
  36. /**
  37. * @brief create oss request options
  38. * @return oss request options
  39. **/
  40. oss_request_options_t *oss_request_options_create(aos_pool_t *p);
  41. /**
  42. * @brief init oss request
  43. **/
  44. void oss_init_request(const oss_request_options_t *options, http_method_e method,
  45. aos_http_request_t **req, aos_table_t *params, aos_table_t *headers, aos_http_response_t **resp);
  46. /**
  47. * @brief init oss bucket request
  48. **/
  49. void oss_init_bucket_request(const oss_request_options_t *options, const aos_string_t *bucket,
  50. http_method_e method, aos_http_request_t **req, aos_table_t *params, aos_table_t *headers,
  51. aos_http_response_t **resp);
  52. /**
  53. * @brief init oss object request
  54. **/
  55. void oss_init_object_request(const oss_request_options_t *options, const aos_string_t *bucket,
  56. const aos_string_t *object, http_method_e method, aos_http_request_t **req,
  57. aos_table_t *params, aos_table_t *headers, oss_progress_callback cb, uint64_t initcrc,
  58. aos_http_response_t **resp);
  59. /**
  60. * @brief init oss live channel request
  61. **/
  62. void oss_init_live_channel_request(const oss_request_options_t *options,
  63. const aos_string_t *bucket, const aos_string_t *live_channel,
  64. http_method_e method, aos_http_request_t **req, aos_table_t *params,
  65. aos_table_t *headers, aos_http_response_t **resp);
  66. /**
  67. * @brief init oss request with signed_url
  68. **/
  69. void oss_init_signed_url_request(const oss_request_options_t *options, const aos_string_t *signed_url,
  70. http_method_e method, aos_http_request_t **req,
  71. aos_table_t *params, aos_table_t *headers, aos_http_response_t **resp);
  72. /**
  73. * @brief oss send request
  74. **/
  75. aos_status_t *oss_send_request(aos_http_controller_t *ctl, aos_http_request_t *req,
  76. aos_http_response_t *resp);
  77. /**
  78. * @brief process oss request including sign request, send request, get response
  79. **/
  80. aos_status_t *oss_process_request(const oss_request_options_t *options,
  81. aos_http_request_t *req, aos_http_response_t *resp);
  82. /**
  83. * @brief process oss request with signed_url including send request, get response
  84. **/
  85. aos_status_t *oss_process_signed_request(const oss_request_options_t *options,
  86. aos_http_request_t *req, aos_http_response_t *resp);
  87. /**
  88. * @brief get object uri using third-level domain if hostname is oss domain, otherwise second-level domain
  89. **/
  90. void oss_get_object_uri(const oss_request_options_t *options,
  91. const aos_string_t *bucket,
  92. const aos_string_t *object,
  93. aos_http_request_t *req);
  94. /**
  95. * @brief bucket uri using third-level domain if hostname is oss domain, otherwise second-level domain
  96. **/
  97. void oss_get_bucket_uri(const oss_request_options_t *options,
  98. const aos_string_t *bucket,
  99. aos_http_request_t *req);
  100. /**
  101. * @brief get rtmp uri using third-level domain if hostname is oss domain, otherwise second-level domain
  102. **/
  103. void oss_get_rtmp_uri(const oss_request_options_t *options,
  104. const aos_string_t *bucket,
  105. const aos_string_t *live_channel_id,
  106. aos_http_request_t *req);
  107. /**
  108. * @brief write body content into oss request body from buffer
  109. **/
  110. void oss_write_request_body_from_buffer(aos_list_t *buffer, aos_http_request_t *req);
  111. /**
  112. * @brief write body content into oss request body from file
  113. **/
  114. int oss_write_request_body_from_file(aos_pool_t *p, const aos_string_t *filename, aos_http_request_t *req);
  115. /**
  116. * @brief write body content into oss request body from multipart upload file
  117. **/
  118. int oss_write_request_body_from_upload_file(aos_pool_t *p, oss_upload_file_t *upload_file, aos_http_request_t *req);
  119. /**
  120. * @brief read body content from oss response body to buffer
  121. **/
  122. void oss_fill_read_response_body(aos_http_response_t *resp, aos_list_t *buffer);
  123. /**
  124. * @brief read body content from oss response body to file
  125. **/
  126. int oss_init_read_response_body_to_file(aos_pool_t *p, const aos_string_t *filename, aos_http_response_t *resp);
  127. /**
  128. * @brief read body content from oss response body to file buffer
  129. **/
  130. int oss_init_read_response_body_to_fb(aos_file_buf_t *fb, const aos_string_t *filename, aos_http_response_t *resp);
  131. /**
  132. * @brief read response header if headers is not null
  133. **/
  134. void oss_fill_read_response_header(aos_http_response_t *resp, aos_table_t **headers);
  135. /**
  136. * @brief create oss api result content
  137. * @return oss api result content
  138. **/
  139. void *oss_create_api_result_content(aos_pool_t *p, size_t size);
  140. oss_list_object_content_t *oss_create_list_object_content(aos_pool_t *p);
  141. oss_list_object_common_prefix_t *oss_create_list_object_common_prefix(aos_pool_t *p);
  142. oss_list_part_content_t *oss_create_list_part_content(aos_pool_t *p);
  143. oss_list_multipart_upload_content_t *oss_create_list_multipart_upload_content(aos_pool_t *p);
  144. oss_complete_part_content_t *oss_create_complete_part_content(aos_pool_t *p);
  145. /**
  146. * @brief create oss api list parameters
  147. * @return oss api list parameters
  148. **/
  149. oss_list_object_params_t *oss_create_list_object_params(aos_pool_t *p);
  150. oss_list_upload_part_params_t *oss_create_list_upload_part_params(aos_pool_t *p);
  151. oss_list_multipart_upload_params_t *oss_create_list_multipart_upload_params(aos_pool_t *p);
  152. oss_list_live_channel_params_t *oss_create_list_live_channel_params(aos_pool_t *p);
  153. /**
  154. * @brief create upload part copy params
  155. * @return upload part copy params struct for upload part copy
  156. **/
  157. oss_upload_part_copy_params_t *oss_create_upload_part_copy_params(aos_pool_t *p);
  158. /**
  159. * @brief create upload file struct for range multipart upload
  160. * @return upload file struct for range multipart upload
  161. **/
  162. oss_upload_file_t *oss_create_upload_file(aos_pool_t *p);
  163. /**
  164. * @brief create lifecycle rule content
  165. * @return lifecycle rule content
  166. **/
  167. oss_lifecycle_rule_content_t *oss_create_lifecycle_rule_content(aos_pool_t *p);
  168. /**
  169. * @brief create oss object content for delete objects
  170. * @return oss object content
  171. **/
  172. oss_object_key_t *oss_create_oss_object_key(aos_pool_t *p);
  173. /**
  174. * @brief create oss live channel publish url content for delete objects
  175. * @return oss live channel publish url content
  176. **/
  177. oss_live_channel_publish_url_t *oss_create_live_channel_publish_url(aos_pool_t *p);
  178. /**
  179. * @brief create oss live channel play url content for delete objects
  180. * @return oss live channel play url content
  181. **/
  182. oss_live_channel_play_url_t *oss_create_live_channel_play_url(aos_pool_t *p);
  183. /**
  184. * @brief create oss list live channel content for delete objects
  185. * @return oss list live channel content
  186. **/
  187. oss_live_channel_content_t *oss_create_list_live_channel_content(aos_pool_t *p);
  188. /**
  189. * @brief create oss live recored content for delete objects
  190. * @return oss live record content
  191. **/
  192. oss_live_record_content_t *oss_create_live_record_content(aos_pool_t *p);
  193. /**
  194. * @brief create live channel configuration content
  195. * @return live channel configuration content
  196. **/
  197. oss_live_channel_configuration_t *oss_create_live_channel_configuration_content(aos_pool_t *p);
  198. /**
  199. * @brief create oss checkpoint content
  200. * @return oss checkpoint content
  201. **/
  202. oss_checkpoint_t *oss_create_checkpoint_content(aos_pool_t *p);
  203. /**
  204. * @brief create oss resumable clt params content
  205. * @return oss checkpoint content
  206. **/
  207. oss_resumable_clt_params_t *oss_create_resumable_clt_params_content(aos_pool_t *p, int64_t part_size, int32_t thread_num,
  208. int enable_checkpoint, const char *checkpoint_path);
  209. /**
  210. * @brief get part size for multipart upload
  211. **/
  212. void oss_get_part_size(int64_t filesize, int64_t *part_size);
  213. /**
  214. * @brief compare function for part sort
  215. **/
  216. int part_sort_cmp(const void *a, const void *b);
  217. /**
  218. * @brief add range in http request
  219. **/
  220. void oss_headers_add_range(apr_pool_t *pool, apr_table_t *headers, int64_t offset, int64_t size);
  221. /**
  222. * @brief set content type for object according to objectname
  223. * @return oss content type
  224. **/
  225. char *get_content_type(const char *name);
  226. char *get_content_type_by_suffix(const char *suffix);
  227. /**
  228. * @brief set content type for object according to filename
  229. **/
  230. void set_content_type(const char* filename, const char* key, aos_table_t *headers);
  231. aos_table_t* aos_table_create_if_null(const oss_request_options_t *options,
  232. aos_table_t *table, int table_size);
  233. int is_enable_crc(const oss_request_options_t *options);
  234. int has_crc_in_response(const aos_http_response_t *resp);
  235. int has_range_or_process_in_request(const aos_http_request_t *req) ;
  236. /**
  237. * @brief check crc consistent between client and server
  238. **/
  239. int oss_check_crc_consistent(uint64_t crc, const apr_table_t *resp_headers, aos_status_t *s);
  240. int oss_get_temporary_file_name(aos_pool_t *p, const aos_string_t *filename, aos_string_t *temp_file_name);
  241. int oss_temp_file_rename(aos_status_t *s, const char *from_path, const char *to_path, apr_pool_t *pool);
  242. OSS_CPP_END
  243. #endif