oss_util.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. #include "aos_string.h"
  2. #include "aos_util.h"
  3. #include "aos_log.h"
  4. #include "aos_status.h"
  5. #include "oss_auth.h"
  6. #include "oss_util.h"
  7. #ifndef WIN32
  8. #include<sys/socket.h>
  9. #include<netinet/in.h>
  10. #include<arpa/inet.h>
  11. #endif
  12. static char *default_content_type = "application/octet-stream";
  13. static oss_content_type_t file_type[] = {
  14. {"html", "text/html"},
  15. {"htm", "text/html"},
  16. {"shtml", "text/html"},
  17. {"css", "text/css"},
  18. {"xml", "text/xml"},
  19. {"gif", "image/gif"},
  20. {"jpeg", "image/jpeg"},
  21. {"jpg", "image/jpeg"},
  22. {"js", "application/x-javascript"},
  23. {"atom", "application/atom+xml"},
  24. {"rss", "application/rss+xml"},
  25. {"mml", "text/mathml"},
  26. {"txt", "text/plain"},
  27. {"jad", "text/vnd.sun.j2me.app-descriptor"},
  28. {"wml", "text/vnd.wap.wml"},
  29. {"htc", "text/x-component"},
  30. {"png", "image/png"},
  31. {"tif", "image/tiff"},
  32. {"tiff", "image/tiff"},
  33. {"wbmp", "image/vnd.wap.wbmp"},
  34. {"ico", "image/x-icon"},
  35. {"jng", "image/x-jng"},
  36. {"bmp", "image/x-ms-bmp"},
  37. {"svg", "image/svg+xml"},
  38. {"svgz", "image/svg+xml"},
  39. {"webp", "image/webp"},
  40. {"jar", "application/java-archive"},
  41. {"war", "application/java-archive"},
  42. {"ear", "application/java-archive"},
  43. {"hqx", "application/mac-binhex40"},
  44. {"doc ", "application/msword"},
  45. {"pdf", "application/pdf"},
  46. {"ps", "application/postscript"},
  47. {"eps", "application/postscript"},
  48. {"ai", "application/postscript"},
  49. {"rtf", "application/rtf"},
  50. {"xls", "application/vnd.ms-excel"},
  51. {"ppt", "application/vnd.ms-powerpoint"},
  52. {"wmlc", "application/vnd.wap.wmlc"},
  53. {"kml", "application/vnd.google-earth.kml+xml"},
  54. {"kmz", "application/vnd.google-earth.kmz"},
  55. {"7z", "application/x-7z-compressed"},
  56. {"cco", "application/x-cocoa"},
  57. {"jardiff", "application/x-java-archive-diff"},
  58. {"jnlp", "application/x-java-jnlp-file"},
  59. {"run", "application/x-makeself"},
  60. {"pl", "application/x-perl"},
  61. {"pm", "application/x-perl"},
  62. {"prc", "application/x-pilot"},
  63. {"pdb", "application/x-pilot"},
  64. {"rar", "application/x-rar-compressed"},
  65. {"rpm", "application/x-redhat-package-manager"},
  66. {"sea", "application/x-sea"},
  67. {"swf", "application/x-shockwave-flash"},
  68. {"sit", "application/x-stuffit"},
  69. {"tcl", "application/x-tcl"},
  70. {"tk", "application/x-tcl"},
  71. {"der", "application/x-x509-ca-cert"},
  72. {"pem", "application/x-x509-ca-cert"},
  73. {"crt", "application/x-x509-ca-cert"},
  74. {"xpi", "application/x-xpinstall"},
  75. {"xhtml", "application/xhtml+xml"},
  76. {"zip", "application/zip"},
  77. {"wgz", "application/x-nokia-widget"},
  78. {"bin", "application/octet-stream"},
  79. {"exe", "application/octet-stream"},
  80. {"dll", "application/octet-stream"},
  81. {"deb", "application/octet-stream"},
  82. {"dmg", "application/octet-stream"},
  83. {"eot", "application/octet-stream"},
  84. {"iso", "application/octet-stream"},
  85. {"img", "application/octet-stream"},
  86. {"msi", "application/octet-stream"},
  87. {"msp", "application/octet-stream"},
  88. {"msm", "application/octet-stream"},
  89. {"mid", "audio/midi"},
  90. {"midi", "audio/midi"},
  91. {"kar", "audio/midi"},
  92. {"mp3", "audio/mpeg"},
  93. {"ogg", "audio/ogg"},
  94. {"m4a", "audio/x-m4a"},
  95. {"ra", "audio/x-realaudio"},
  96. {"3gpp", "video/3gpp"},
  97. {"3gp", "video/3gpp"},
  98. {"mp4", "video/mp4"},
  99. {"mpeg", "video/mpeg"},
  100. {"mpg", "video/mpeg"},
  101. {"mov", "video/quicktime"},
  102. {"webm", "video/webm"},
  103. {"flv", "video/x-flv"},
  104. {"m4v", "video/x-m4v"},
  105. {"mng", "video/x-mng"},
  106. {"asx", "video/x-ms-asf"},
  107. {"asf", "video/x-ms-asf"},
  108. {"wmv", "video/x-ms-wmv"},
  109. {"avi", "video/x-msvideo"},
  110. {"ts", "video/MP2T"},
  111. {"m3u8", "application/x-mpegURL"},
  112. {"apk", "application/vnd.android.package-archive"},
  113. {NULL, NULL}
  114. };
  115. static int starts_with(const aos_string_t *str, const char *prefix) {
  116. uint32_t i;
  117. if(NULL != str && prefix && str->len > 0 && strlen(prefix)) {
  118. for(i = 0; str->data[i] != '\0' && prefix[i] != '\0'; i++) {
  119. if(prefix[i] != str->data[i]) return 0;
  120. }
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. static void generate_proto(const oss_request_options_t *options,
  126. aos_http_request_t *req)
  127. {
  128. const char *proto;
  129. proto = starts_with(&options->config->endpoint, AOS_HTTP_PREFIX) ?
  130. AOS_HTTP_PREFIX : "";
  131. proto = starts_with(&options->config->endpoint, AOS_HTTPS_PREFIX) ?
  132. AOS_HTTPS_PREFIX : proto;
  133. req->proto = apr_psprintf(options->pool, "%.*s", (int)strlen(proto), proto);
  134. }
  135. static void generate_rtmp_proto(const oss_request_options_t *options,
  136. aos_http_request_t *req)
  137. {
  138. const char *proto = AOS_RTMP_PREFIX;
  139. req->proto = apr_psprintf(options->pool, "%.*s", (int)strlen(proto), proto);
  140. }
  141. int is_valid_ip(const char *str)
  142. {
  143. if (INADDR_NONE == inet_addr(str) || INADDR_ANY == inet_addr(str)) {
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. oss_config_t *oss_config_create(aos_pool_t *p)
  149. {
  150. return (oss_config_t *)aos_pcalloc(p, sizeof(oss_config_t));
  151. }
  152. void oss_config_resolve(aos_pool_t *pool, oss_config_t *config, aos_http_controller_t *ctl)
  153. {
  154. if(!aos_is_null_string(&config->proxy_host)) {
  155. // proxy host:port
  156. if (config->proxy_port == 0) {
  157. ctl->options->proxy_host = apr_psprintf(pool, "%.*s", config->proxy_host.len, config->proxy_host.data);
  158. } else {
  159. ctl->options->proxy_host = apr_psprintf(pool, "%.*s:%d", config->proxy_host.len, config->proxy_host.data,
  160. config->proxy_port);
  161. }
  162. // authorize user:passwd
  163. if (!aos_is_null_string(&config->proxy_user) && !aos_is_null_string(&config->proxy_passwd)) {
  164. ctl->options->proxy_auth = apr_psprintf(pool, "%.*s:%.*s", config->proxy_user.len,
  165. config->proxy_user.data, config->proxy_passwd.len, config->proxy_passwd.data);
  166. }
  167. }
  168. }
  169. oss_request_options_t *oss_request_options_create(aos_pool_t *p)
  170. {
  171. int s;
  172. oss_request_options_t *options;
  173. if(p == NULL) {
  174. if ((s = aos_pool_create(&p, NULL)) != APR_SUCCESS) {
  175. aos_fatal_log("aos_pool_create failure.");
  176. return NULL;
  177. }
  178. }
  179. options = (oss_request_options_t *)aos_pcalloc(p, sizeof(oss_request_options_t));
  180. options->pool = p;
  181. return options;
  182. }
  183. void oss_get_object_uri(const oss_request_options_t *options,
  184. const aos_string_t *bucket,
  185. const aos_string_t *object,
  186. aos_http_request_t *req)
  187. {
  188. int32_t proto_len;
  189. const char *raw_endpoint_str;
  190. aos_string_t raw_endpoint;
  191. generate_proto(options, req);
  192. proto_len = strlen(req->proto);
  193. req->resource = apr_psprintf(options->pool, "%.*s/%.*s",
  194. bucket->len, bucket->data,
  195. object->len, object->data);
  196. raw_endpoint_str = aos_pstrdup(options->pool,
  197. &options->config->endpoint) + proto_len;
  198. raw_endpoint.len = options->config->endpoint.len - proto_len;
  199. raw_endpoint.data = options->config->endpoint.data + proto_len;
  200. if (options->config->is_cname) {
  201. req->host = apr_psprintf(options->pool, "%.*s",
  202. raw_endpoint.len, raw_endpoint.data);
  203. req->uri = object->data;
  204. } else if (is_valid_ip(raw_endpoint_str)) {
  205. req->host = apr_psprintf(options->pool, "%.*s",
  206. raw_endpoint.len, raw_endpoint.data);
  207. req->uri = apr_psprintf(options->pool, "%.*s/%.*s",
  208. bucket->len, bucket->data,
  209. object->len, object->data);
  210. } else {
  211. req->host = apr_psprintf(options->pool, "%.*s.%.*s",
  212. bucket->len, bucket->data,
  213. raw_endpoint.len, raw_endpoint.data);
  214. req->uri = object->data;
  215. }
  216. }
  217. void oss_get_bucket_uri(const oss_request_options_t *options,
  218. const aos_string_t *bucket,
  219. aos_http_request_t *req)
  220. {
  221. int32_t proto_len;
  222. const char *raw_endpoint_str;
  223. aos_string_t raw_endpoint;
  224. generate_proto(options, req);
  225. proto_len = strlen(req->proto);
  226. raw_endpoint_str = aos_pstrdup(options->pool,
  227. &options->config->endpoint) + proto_len;
  228. raw_endpoint.len = options->config->endpoint.len - proto_len;
  229. raw_endpoint.data = options->config->endpoint.data + proto_len;
  230. if (is_valid_ip(raw_endpoint_str)) {
  231. req->resource = apr_psprintf(options->pool, "%.*s",
  232. bucket->len, bucket->data);
  233. } else {
  234. req->resource = apr_psprintf(options->pool, "%.*s/",
  235. bucket->len, bucket->data);
  236. }
  237. if (options->config->is_cname ||
  238. is_valid_ip(raw_endpoint_str))
  239. {
  240. req->host = apr_psprintf(options->pool, "%.*s",
  241. raw_endpoint.len, raw_endpoint.data);
  242. req->uri = apr_psprintf(options->pool, "%.*s", bucket->len,
  243. bucket->data);
  244. } else {
  245. req->host = apr_psprintf(options->pool, "%.*s.%.*s",
  246. bucket->len, bucket->data,
  247. raw_endpoint.len, raw_endpoint.data);
  248. req->uri = apr_psprintf(options->pool, "%s", "");
  249. }
  250. }
  251. void oss_get_rtmp_uri(const oss_request_options_t *options,
  252. const aos_string_t *bucket,
  253. const aos_string_t *live_channel_id,
  254. aos_http_request_t *req)
  255. {
  256. int32_t proto_len = 0;
  257. const char *raw_endpoint_str = NULL;
  258. aos_string_t raw_endpoint;
  259. generate_rtmp_proto(options, req);
  260. proto_len = strlen(req->proto);
  261. req->resource = apr_psprintf(options->pool, "%.*s/%.*s", bucket->len, bucket->data,
  262. live_channel_id->len, live_channel_id->data);
  263. raw_endpoint_str = aos_pstrdup(options->pool,
  264. &options->config->endpoint) + proto_len;
  265. raw_endpoint.len = options->config->endpoint.len - proto_len;
  266. raw_endpoint.data = options->config->endpoint.data + proto_len;
  267. if (options->config->is_cname) {
  268. req->host = apr_psprintf(options->pool, "%.*s",
  269. raw_endpoint.len, raw_endpoint.data);
  270. req->uri = apr_psprintf(options->pool, "live/%.*s",
  271. live_channel_id->len, live_channel_id->data);
  272. } else if (is_valid_ip(raw_endpoint_str)) {
  273. req->host = apr_psprintf(options->pool, "%.*s",
  274. raw_endpoint.len, raw_endpoint.data);
  275. req->uri = apr_psprintf(options->pool, "%.*s/live/%.*s",
  276. bucket->len, bucket->data,
  277. live_channel_id->len, live_channel_id->data);
  278. } else {
  279. req->host = apr_psprintf(options->pool, "%.*s.%.*s",
  280. bucket->len, bucket->data,
  281. raw_endpoint.len, raw_endpoint.data);
  282. req->uri = apr_psprintf(options->pool, "live/%.*s",
  283. live_channel_id->len, live_channel_id->data);
  284. }
  285. }
  286. void oss_write_request_body_from_buffer(aos_list_t *buffer,
  287. aos_http_request_t *req)
  288. {
  289. aos_list_movelist(buffer, &req->body);
  290. req->body_len = aos_buf_list_len(&req->body);
  291. }
  292. int oss_write_request_body_from_file(aos_pool_t *p,
  293. const aos_string_t *filename,
  294. aos_http_request_t *req)
  295. {
  296. int res = AOSE_OK;
  297. aos_file_buf_t *fb = aos_create_file_buf(p);
  298. res = aos_open_file_for_all_read(p, filename->data, fb);
  299. if (res != AOSE_OK) {
  300. aos_error_log("Open read file fail, filename:%s\n", filename->data);
  301. return res;
  302. }
  303. req->body_len = fb->file_last;
  304. req->file_path = filename->data;
  305. req->file_buf = fb;
  306. req->type = BODY_IN_FILE;
  307. req->read_body = aos_read_http_body_file;
  308. return res;
  309. }
  310. int oss_write_request_body_from_upload_file(aos_pool_t *p,
  311. oss_upload_file_t *upload_file,
  312. aos_http_request_t *req)
  313. {
  314. int res = AOSE_OK;
  315. aos_file_buf_t *fb = aos_create_file_buf(p);
  316. res = aos_open_file_for_range_read(p, upload_file->filename.data,
  317. upload_file->file_pos, upload_file->file_last, fb);
  318. if (res != AOSE_OK) {
  319. aos_error_log("Open read file fail, filename:%s\n",
  320. upload_file->filename.data);
  321. return res;
  322. }
  323. req->body_len = fb->file_last - fb->file_pos;
  324. req->file_path = upload_file->filename.data;
  325. req->file_buf = fb;
  326. req->type = BODY_IN_FILE;
  327. req->read_body = aos_read_http_body_file;
  328. return res;
  329. }
  330. void oss_fill_read_response_body(aos_http_response_t *resp,
  331. aos_list_t *buffer)
  332. {
  333. if (NULL != buffer) {
  334. aos_list_movelist(&resp->body, buffer);
  335. }
  336. }
  337. int oss_init_read_response_body_to_file(aos_pool_t *p,
  338. const aos_string_t *filename,
  339. aos_http_response_t *resp)
  340. {
  341. int res = AOSE_OK;
  342. aos_file_buf_t *fb = aos_create_file_buf(p);
  343. res = aos_open_file_for_write(p, filename->data, fb);
  344. if (res != AOSE_OK) {
  345. aos_error_log("Open write file fail, filename:%s\n", filename->data);
  346. return res;
  347. }
  348. resp->file_path = filename->data;
  349. resp->file_buf = fb;
  350. resp->write_body = aos_write_http_body_file;
  351. resp->type = BODY_IN_FILE;
  352. return res;
  353. }
  354. int oss_init_read_response_body_to_fb(aos_file_buf_t *fb,
  355. const aos_string_t *filename,
  356. aos_http_response_t *resp)
  357. {
  358. int res = AOSE_OK;
  359. resp->file_path = filename->data;
  360. resp->file_buf = fb;
  361. resp->write_body = aos_write_http_body_file;
  362. resp->type = BODY_IN_FILE;
  363. return res;
  364. }
  365. void oss_fill_read_response_header(aos_http_response_t *resp,
  366. aos_table_t **headers)
  367. {
  368. if (NULL != headers && NULL != resp) {
  369. *headers = resp->headers;
  370. }
  371. }
  372. void *oss_create_api_result_content(aos_pool_t *p, size_t size)
  373. {
  374. void *result_content = aos_palloc(p, size);
  375. if (NULL == result_content) {
  376. return NULL;
  377. }
  378. aos_list_init((aos_list_t *)result_content);
  379. return result_content;
  380. }
  381. oss_list_object_content_t *oss_create_list_object_content(aos_pool_t *p)
  382. {
  383. return (oss_list_object_content_t *)oss_create_api_result_content(
  384. p, sizeof(oss_list_object_content_t));
  385. }
  386. oss_list_object_common_prefix_t *oss_create_list_object_common_prefix(aos_pool_t *p)
  387. {
  388. return (oss_list_object_common_prefix_t *)oss_create_api_result_content(
  389. p, sizeof(oss_list_object_common_prefix_t));
  390. }
  391. oss_list_multipart_upload_content_t *oss_create_list_multipart_upload_content(aos_pool_t *p)
  392. {
  393. return (oss_list_multipart_upload_content_t*)oss_create_api_result_content(
  394. p, sizeof(oss_list_multipart_upload_content_t));
  395. }
  396. oss_list_part_content_t *oss_create_list_part_content(aos_pool_t *p)
  397. {
  398. oss_list_part_content_t *list_part_content = NULL;
  399. list_part_content = (oss_list_part_content_t*)oss_create_api_result_content(p,
  400. sizeof(oss_list_part_content_t));
  401. return list_part_content;
  402. }
  403. oss_complete_part_content_t *oss_create_complete_part_content(aos_pool_t *p)
  404. {
  405. oss_complete_part_content_t *complete_part_content = NULL;
  406. complete_part_content = (oss_complete_part_content_t*)oss_create_api_result_content(
  407. p, sizeof(oss_complete_part_content_t));
  408. return complete_part_content;
  409. }
  410. oss_list_object_params_t *oss_create_list_object_params(aos_pool_t *p)
  411. {
  412. oss_list_object_params_t * params;
  413. params = (oss_list_object_params_t *)aos_pcalloc(
  414. p, sizeof(oss_list_object_params_t));
  415. aos_list_init(&params->object_list);
  416. aos_list_init(&params->common_prefix_list);
  417. aos_str_set(&params->prefix, "");
  418. aos_str_set(&params->marker, "");
  419. aos_str_set(&params->delimiter, "");
  420. params->truncated = 1;
  421. params->max_ret = OSS_PER_RET_NUM;
  422. return params;
  423. }
  424. oss_list_upload_part_params_t *oss_create_list_upload_part_params(aos_pool_t *p)
  425. {
  426. oss_list_upload_part_params_t *params;
  427. params = (oss_list_upload_part_params_t *)aos_pcalloc(
  428. p, sizeof(oss_list_upload_part_params_t));
  429. aos_list_init(&params->part_list);
  430. aos_str_set(&params->part_number_marker, "");
  431. params->max_ret = OSS_PER_RET_NUM;
  432. params->truncated = 1;
  433. return params;
  434. }
  435. oss_list_multipart_upload_params_t *oss_create_list_multipart_upload_params(aos_pool_t *p)
  436. {
  437. oss_list_multipart_upload_params_t *params;
  438. params = (oss_list_multipart_upload_params_t *)aos_pcalloc(
  439. p, sizeof(oss_list_multipart_upload_params_t));
  440. aos_list_init(&params->upload_list);
  441. aos_str_set(&params->prefix, "");
  442. aos_str_set(&params->key_marker, "");
  443. aos_str_set(&params->upload_id_marker, "");
  444. aos_str_set(&params->delimiter, "");
  445. params->truncated = 1;
  446. params->max_ret = OSS_PER_RET_NUM;
  447. return params;
  448. }
  449. oss_upload_part_copy_params_t *oss_create_upload_part_copy_params(aos_pool_t *p)
  450. {
  451. return (oss_upload_part_copy_params_t *)aos_pcalloc(
  452. p, sizeof(oss_upload_part_copy_params_t));
  453. }
  454. oss_lifecycle_rule_content_t *oss_create_lifecycle_rule_content(aos_pool_t *p)
  455. {
  456. oss_lifecycle_rule_content_t *rule;
  457. rule = (oss_lifecycle_rule_content_t *)aos_pcalloc(
  458. p, sizeof(oss_lifecycle_rule_content_t));
  459. aos_str_set(&rule->id, "");
  460. aos_str_set(&rule->prefix, "");
  461. aos_str_set(&rule->status, "");
  462. aos_str_set(&rule->date, "");
  463. rule->days = INT_MAX;
  464. return rule;
  465. }
  466. oss_upload_file_t *oss_create_upload_file(aos_pool_t *p)
  467. {
  468. return (oss_upload_file_t *)aos_pcalloc(p, sizeof(oss_upload_file_t));
  469. }
  470. oss_object_key_t *oss_create_oss_object_key(aos_pool_t *p)
  471. {
  472. return (oss_object_key_t *)aos_pcalloc(p, sizeof(oss_object_key_t));
  473. }
  474. oss_live_channel_publish_url_t *oss_create_live_channel_publish_url(aos_pool_t *p)
  475. {
  476. return (oss_live_channel_publish_url_t *)aos_pcalloc(p, sizeof(oss_live_channel_publish_url_t));
  477. }
  478. oss_live_channel_play_url_t *oss_create_live_channel_play_url(aos_pool_t *p)
  479. {
  480. return (oss_live_channel_play_url_t *)aos_pcalloc(p, sizeof(oss_live_channel_play_url_t));
  481. }
  482. oss_live_channel_content_t *oss_create_list_live_channel_content(aos_pool_t *p)
  483. {
  484. oss_live_channel_content_t *list_live_channel_content = NULL;
  485. list_live_channel_content = (oss_live_channel_content_t*)oss_create_api_result_content(p,
  486. sizeof(oss_live_channel_content_t));
  487. aos_list_init(&list_live_channel_content->publish_url_list);
  488. aos_list_init(&list_live_channel_content->play_url_list);
  489. return list_live_channel_content;
  490. }
  491. oss_live_record_content_t *oss_create_live_record_content(aos_pool_t *p)
  492. {
  493. oss_live_record_content_t *live_record_content = NULL;
  494. live_record_content = (oss_live_record_content_t*)oss_create_api_result_content(p,
  495. sizeof(oss_live_record_content_t));
  496. return live_record_content;
  497. }
  498. oss_live_channel_configuration_t *oss_create_live_channel_configuration_content(aos_pool_t *p)
  499. {
  500. oss_live_channel_configuration_t *config;
  501. config = (oss_live_channel_configuration_t *)aos_pcalloc(
  502. p, sizeof(oss_live_channel_configuration_t));
  503. aos_str_set(&config->name, "");
  504. aos_str_set(&config->description, "");
  505. aos_str_set(&config->status, LIVE_CHANNEL_STATUS_ENABLED);
  506. aos_str_set(&config->target.type, LIVE_CHANNEL_DEFAULT_TYPE);
  507. aos_str_set(&config->target.play_list_name, LIVE_CHANNEL_DEFAULT_PLAYLIST);
  508. config->target.frag_duration = LIVE_CHANNEL_DEFAULT_FRAG_DURATION;
  509. config->target.frag_count = LIVE_CHANNEL_DEFAULT_FRAG_COUNT;
  510. return config;
  511. }
  512. oss_checkpoint_t *oss_create_checkpoint_content(aos_pool_t *p)
  513. {
  514. oss_checkpoint_t *cp;
  515. cp = (oss_checkpoint_t *)aos_pcalloc(p, sizeof(oss_checkpoint_t));
  516. cp->parts = (oss_checkpoint_part_t *)aos_pcalloc(p, sizeof(oss_checkpoint_part_t) * OSS_MAX_PART_NUM);
  517. aos_str_set(&cp->md5, "");
  518. aos_str_set(&cp->file_path, "");
  519. aos_str_set(&cp->file_md5, "");
  520. aos_str_set(&cp->object_name, "");
  521. aos_str_set(&cp->object_last_modified, "");
  522. aos_str_set(&cp->object_etag, "");
  523. aos_str_set(&cp->upload_id, "");
  524. return cp;
  525. }
  526. oss_resumable_clt_params_t *oss_create_resumable_clt_params_content(aos_pool_t *p, int64_t part_size, int32_t thread_num,
  527. int enable_checkpoint, const char *checkpoint_path)
  528. {
  529. oss_resumable_clt_params_t *clt;
  530. clt = (oss_resumable_clt_params_t *)aos_pcalloc(p, sizeof(oss_resumable_clt_params_t));
  531. clt->part_size = part_size;
  532. clt->thread_num = thread_num;
  533. clt->enable_checkpoint = enable_checkpoint;
  534. if (enable_checkpoint && NULL != checkpoint_path) {
  535. aos_str_set(&clt->checkpoint_path, checkpoint_path);
  536. }
  537. return clt;
  538. }
  539. oss_list_live_channel_params_t *oss_create_list_live_channel_params(aos_pool_t *p)
  540. {
  541. oss_list_live_channel_params_t *params;
  542. params = (oss_list_live_channel_params_t *)aos_pcalloc(
  543. p, sizeof(oss_list_live_channel_params_t));
  544. aos_list_init(&params->live_channel_list);
  545. aos_str_set(&params->prefix, "");
  546. aos_str_set(&params->marker, "");
  547. params->truncated = 1;
  548. params->max_keys = OSS_PER_RET_NUM;
  549. return params;
  550. }
  551. const char *get_oss_acl_str(oss_acl_e oss_acl)
  552. {
  553. switch (oss_acl) {
  554. case OSS_ACL_PRIVATE:
  555. return "private";
  556. case OSS_ACL_PUBLIC_READ:
  557. return "public-read";
  558. case OSS_ACL_PUBLIC_READ_WRITE:
  559. return "public-read-write";
  560. default:
  561. return NULL;
  562. }
  563. }
  564. void oss_init_request(const oss_request_options_t *options,
  565. http_method_e method,
  566. aos_http_request_t **req,
  567. aos_table_t *params,
  568. aos_table_t *headers,
  569. aos_http_response_t **resp)
  570. {
  571. *req = aos_http_request_create(options->pool);
  572. *resp = aos_http_response_create(options->pool);
  573. (*req)->method = method;
  574. init_sts_token_header();
  575. (*req)->headers = headers;
  576. (*req)->query_params = params;
  577. }
  578. void oss_init_bucket_request(const oss_request_options_t *options,
  579. const aos_string_t *bucket,
  580. http_method_e method,
  581. aos_http_request_t **req,
  582. aos_table_t *params,
  583. aos_table_t *headers,
  584. aos_http_response_t **resp)
  585. {
  586. oss_init_request(options, method, req, params, headers, resp);
  587. oss_get_bucket_uri(options, bucket, *req);
  588. }
  589. void oss_init_object_request(const oss_request_options_t *options,
  590. const aos_string_t *bucket,
  591. const aos_string_t *object,
  592. http_method_e method,
  593. aos_http_request_t **req,
  594. aos_table_t *params,
  595. aos_table_t *headers,
  596. oss_progress_callback cb,
  597. uint64_t init_crc,
  598. aos_http_response_t **resp)
  599. {
  600. oss_init_request(options, method, req, params, headers, resp);
  601. if (HTTP_GET == method) {
  602. (*resp)->progress_callback = cb;
  603. } else if (HTTP_PUT == method || HTTP_POST == method) {
  604. (*req)->progress_callback = cb;
  605. (*req)->crc64 = init_crc;
  606. }
  607. oss_get_object_uri(options, bucket, object, *req);
  608. }
  609. void oss_init_live_channel_request(const oss_request_options_t *options,
  610. const aos_string_t *bucket,
  611. const aos_string_t *live_channel,
  612. http_method_e method,
  613. aos_http_request_t **req,
  614. aos_table_t *params,
  615. aos_table_t *headers,
  616. aos_http_response_t **resp)
  617. {
  618. oss_init_request(options, method, req, params, headers, resp);
  619. oss_get_object_uri(options, bucket, live_channel, *req);
  620. }
  621. void oss_init_signed_url_request(const oss_request_options_t *options,
  622. const aos_string_t *signed_url,
  623. http_method_e method,
  624. aos_http_request_t **req,
  625. aos_table_t *params,
  626. aos_table_t *headers,
  627. aos_http_response_t **resp)
  628. {
  629. *req = aos_http_request_create(options->pool);
  630. *resp = aos_http_response_create(options->pool);
  631. (*req)->method = method;
  632. (*req)->headers = headers;
  633. (*req)->query_params = params;
  634. (*req)->signed_url = signed_url->data;
  635. }
  636. aos_status_t *oss_send_request(aos_http_controller_t *ctl,
  637. aos_http_request_t *req,
  638. aos_http_response_t *resp)
  639. {
  640. aos_status_t *s;
  641. const char *reason;
  642. int res = AOSE_OK;
  643. s = aos_status_create(ctl->pool);
  644. res = aos_http_send_request(ctl, req, resp);
  645. if (res != AOSE_OK) {
  646. reason = aos_http_controller_get_reason(ctl);
  647. aos_status_set(s, res, AOS_HTTP_IO_ERROR_CODE, reason);
  648. } else if (!aos_http_is_ok(resp->status)) {
  649. s = aos_status_parse_from_body(ctl->pool, &resp->body, resp->status, s);
  650. } else {
  651. s->code = resp->status;
  652. }
  653. s->req_id = (char*)(apr_table_get(resp->headers, "x-oss-request-id"));
  654. if (s->req_id == NULL) {
  655. s->req_id = (char*)(apr_table_get(resp->headers, "x-img-request-id"));
  656. if (s->req_id == NULL) {
  657. s->req_id = "";
  658. }
  659. }
  660. return s;
  661. }
  662. aos_status_t *oss_process_request(const oss_request_options_t *options,
  663. aos_http_request_t *req,
  664. aos_http_response_t *resp)
  665. {
  666. int res = AOSE_OK;
  667. aos_status_t *s;
  668. s = aos_status_create(options->pool);
  669. res = oss_sign_request(req, options->config);
  670. if (res != AOSE_OK) {
  671. aos_status_set(s, res, AOS_CLIENT_ERROR_CODE, NULL);
  672. return s;
  673. }
  674. return oss_send_request(options->ctl, req, resp);
  675. }
  676. aos_status_t *oss_process_signed_request(const oss_request_options_t *options,
  677. aos_http_request_t *req,
  678. aos_http_response_t *resp)
  679. {
  680. return oss_send_request(options->ctl, req, resp);
  681. }
  682. void oss_get_part_size(int64_t filesize, int64_t *part_size)
  683. {
  684. if (filesize > (*part_size) * OSS_MAX_PART_NUM) {
  685. *part_size = (filesize + OSS_MAX_PART_NUM -
  686. filesize % OSS_MAX_PART_NUM) / OSS_MAX_PART_NUM;
  687. aos_warn_log("Part number larger than max limit, "
  688. "part size Changed to:%" APR_INT64_T_FMT "\n",
  689. *part_size);
  690. }
  691. }
  692. int part_sort_cmp(const void *a, const void *b)
  693. {
  694. return (((oss_upload_part_t*)a)->part_num -
  695. ((oss_upload_part_t*)b)->part_num > 0 ? 1 : -1);
  696. }
  697. void oss_headers_add_range(apr_pool_t *pool, apr_table_t *headers, int64_t offset, int64_t size)
  698. {
  699. char *range;
  700. range = apr_psprintf(pool, "bytes=%" APR_INT64_T_FMT "-%" APR_INT64_T_FMT,
  701. offset, offset + size - 1);
  702. apr_table_set(headers, "Range", range);
  703. }
  704. char *get_content_type_by_suffix(const char *suffix)
  705. {
  706. oss_content_type_t *content_type;
  707. for (content_type = file_type; content_type->suffix; ++content_type) {
  708. if (strcasecmp(content_type->suffix, suffix) == 0)
  709. {
  710. return content_type->type;
  711. }
  712. }
  713. return default_content_type;
  714. }
  715. char *get_content_type(const char *name)
  716. {
  717. char *begin;
  718. char *content_type = NULL;
  719. begin = strrchr(name, '.');
  720. if (begin) {
  721. content_type = get_content_type_by_suffix(begin + 1);
  722. }
  723. return content_type;
  724. }
  725. void set_content_type(const char* file_name,
  726. const char* key,
  727. aos_table_t *headers)
  728. {
  729. char *user_content_type = NULL;
  730. char *content_type = NULL;
  731. const char *mime_key = NULL;
  732. mime_key = file_name == NULL ? key : file_name;
  733. user_content_type = (char*)apr_table_get(headers, OSS_CONTENT_TYPE);
  734. if (NULL == user_content_type && mime_key != NULL) {
  735. content_type = get_content_type(mime_key);
  736. if (content_type) {
  737. apr_table_set(headers, OSS_CONTENT_TYPE, content_type);
  738. } else {
  739. apr_table_set(headers, OSS_CONTENT_TYPE, default_content_type);
  740. }
  741. }
  742. }
  743. aos_table_t* aos_table_create_if_null(const oss_request_options_t *options,
  744. aos_table_t *table,
  745. int table_size)
  746. {
  747. if (table == NULL) {
  748. table = aos_table_make(options->pool, table_size);
  749. }
  750. return table;
  751. }
  752. int is_enable_crc(const oss_request_options_t *options)
  753. {
  754. return options->ctl->options->enable_crc;
  755. }
  756. int has_crc_in_response(const aos_http_response_t *resp)
  757. {
  758. if (NULL != apr_table_get(resp->headers, OSS_HASH_CRC64_ECMA)) {
  759. return AOS_TRUE;
  760. }
  761. return AOS_FALSE;
  762. }
  763. int has_range_or_process_in_request(const aos_http_request_t *req)
  764. {
  765. if (NULL != apr_table_get(req->headers, "Range") ||
  766. NULL != apr_table_get(req->query_params, OSS_PROCESS)) {
  767. return AOS_TRUE;
  768. }
  769. return AOS_FALSE;
  770. }
  771. static int check_crc(uint64_t crc, const apr_table_t *headers)
  772. {
  773. char * srv_crc = (char*)(apr_table_get(headers, OSS_HASH_CRC64_ECMA));
  774. if (NULL != srv_crc && crc != aos_atoui64(srv_crc)) {
  775. return AOSE_CRC_INCONSISTENT_ERROR;
  776. }
  777. return AOSE_OK;
  778. }
  779. int oss_check_crc_consistent(uint64_t crc, const apr_table_t *resp_headers, aos_status_t *s)
  780. {
  781. int res = check_crc(crc, resp_headers);
  782. if (res != AOSE_OK) {
  783. aos_inconsistent_error_status_set(s, res);
  784. }
  785. return res;
  786. }
  787. int oss_get_temporary_file_name(aos_pool_t *p, const aos_string_t *filename, aos_string_t *temp_file_name)
  788. {
  789. int len = filename->len + 1;
  790. char *temp_file_name_ptr = NULL;
  791. len += strlen(AOS_TEMP_FILE_SUFFIX);
  792. temp_file_name_ptr = aos_pcalloc(p, len);
  793. apr_snprintf(temp_file_name_ptr, len, "%.*s%s", filename->len, filename->data, AOS_TEMP_FILE_SUFFIX);
  794. aos_str_set(temp_file_name, temp_file_name_ptr);
  795. return len;
  796. }
  797. int oss_temp_file_rename(aos_status_t *s, const char *from_path, const char *to_path, apr_pool_t *pool)
  798. {
  799. int res = -1;
  800. if (s != NULL) {
  801. if (aos_status_is_ok(s)) {
  802. res = apr_file_rename(from_path, to_path, pool);
  803. } else {
  804. res = apr_file_remove(from_path, pool);
  805. }
  806. }
  807. return res;
  808. }