http_upload_file.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. 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. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "utils_httpc.h"
  19. #include "lite-utils.h"
  20. #include "ca.h"
  21. #include "utils_sha2.h"
  22. int IOT_GET_URL_AND_AUTH(const char *product_sn, const char *device_sn, const char *device_sercret, char *file_name, char *upload_buffer, uint32_t upload_buffer_len, char *md5, char *authorization, char *put_url)
  23. {
  24. int ret = SUCCESS_RET;
  25. http_client_t *http_client_post = (http_client_t *)HAL_Malloc(sizeof(http_client_t));
  26. if(NULL == http_client_post)
  27. {
  28. LOG_ERROR("http_client_post malloc fail\n");
  29. return FAILURE_RET;
  30. }
  31. http_client_data_t *http_data_post = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t));
  32. if(NULL == http_data_post)
  33. {
  34. HAL_Free(http_client_post);
  35. LOG_ERROR("http_data_post malloc fail\n");
  36. return FAILURE_RET;
  37. }
  38. memset(http_client_post, 0, sizeof(http_client_t));
  39. memset(http_data_post, 0, sizeof(http_client_data_t));
  40. http_data_post->response_buf = (char *)HAL_Malloc(1024);
  41. if(NULL == http_data_post->response_buf)
  42. {
  43. HAL_Free(http_client_post);
  44. HAL_Free(http_data_post);
  45. LOG_ERROR("http_data_post->response_buf malloc fail\n");
  46. return FAILURE_RET;
  47. }
  48. memset(http_data_post->response_buf, 0, 1024);
  49. http_data_post->response_buf_len = 1024;
  50. http_data_post->post_content_type = (char *)"application/json";
  51. http_data_post->post_buf = (unsigned char *)HAL_Malloc(1024);
  52. if(NULL == http_data_post->post_buf)
  53. {
  54. HAL_Free(http_client_post);
  55. HAL_Free(http_data_post->response_buf);
  56. HAL_Free(http_data_post);
  57. LOG_ERROR("http_data_post->post_buf malloc fail\n");
  58. return FAILURE_RET;
  59. }
  60. memset(http_data_post->post_buf, 0, 1024);
  61. HAL_Snprintf((char *)http_data_post->post_buf, 1024, "{\"ProductSN\":\"%s\",\"DeviceSN\":\"%s\"," \
  62. "\"FileName\":\"%s\",\"FileSize\":%d,\"MD5\":\"%s\",\"Content-Type\":\"plain/text\"}", \
  63. product_sn, device_sn, file_name, upload_buffer_len, md5);
  64. http_data_post->post_buf_len = strlen((char *)http_data_post->post_buf);
  65. uint8_t mac_output_hex[32] = {0};
  66. char mac_output_char[65] = {0};
  67. utils_hmac_sha256((const uint8_t *)http_data_post->post_buf, http_data_post->post_buf_len, (const uint8_t *)device_sercret, strlen(device_sercret), mac_output_hex);
  68. LITE_hexbuf_convert((unsigned char *)mac_output_hex, mac_output_char, 32, 0);
  69. LOG_DEBUG("hmac:%s\r\n",mac_output_char);
  70. http_client_post->header = (char *)HAL_Malloc(1024);
  71. if(NULL == http_client_post->header)
  72. {
  73. HAL_Free(http_client_post);
  74. HAL_Free(http_data_post->response_buf);
  75. HAL_Free(http_data_post->post_buf);
  76. HAL_Free(http_data_post);
  77. LOG_ERROR("http_client_post->header malloc fail\n");
  78. return FAILURE_RET;
  79. }
  80. memset(http_client_post->header, 0, 1024);
  81. HAL_Snprintf(http_client_post->header, 1024, "Content-Type: application/json\r\nAuthorization:%s\r\n",mac_output_char);
  82. const char *ca_crt = iot_https_ca_get();
  83. char *url = (char *)"https://file-cn-sh2.iot.ucloud.cn/api/v1/url";
  84. ret = http_client_common(http_client_post, url, 443, ca_crt, HTTP_POST, http_data_post,5000);
  85. if(SUCCESS_RET != ret)
  86. {
  87. LOG_ERROR("HTTP_POST error\n");
  88. goto end;
  89. }
  90. ret = http_client_recv_data(http_client_post, 5000, http_data_post);
  91. if(SUCCESS_RET != ret)
  92. {
  93. LOG_ERROR("http_client_recv_data error\n");
  94. goto end;
  95. }
  96. LOG_DEBUG("response_buf:%s\n",http_data_post->response_buf);
  97. char *temp_auth = LITE_json_value_of((char *)"Authorization", http_data_post->response_buf);
  98. if(NULL == temp_auth)
  99. {
  100. LOG_ERROR("parse Authorization error\n");
  101. goto end;
  102. }
  103. strncpy(authorization,temp_auth,strlen(temp_auth));
  104. authorization[strlen(temp_auth)+1] = '\0';
  105. LOG_DEBUG("authorization:%s\n",authorization);
  106. HAL_Free(temp_auth);
  107. char *temp_url = LITE_json_value_of((char *)"URL", http_data_post->response_buf);
  108. if(NULL == temp_url)
  109. {
  110. LOG_ERROR("parse URL error\n");
  111. goto end;
  112. }
  113. strncpy(put_url,temp_url,strlen(temp_url));
  114. put_url[strlen(temp_url)+1] = '\0';
  115. LOG_DEBUG("put_url:%s\n",put_url);
  116. HAL_Free(temp_url);
  117. end:
  118. http_client_close(http_client_post);
  119. HAL_Free(http_client_post->header);
  120. HAL_Free(http_client_post);
  121. HAL_Free(http_data_post->response_buf);
  122. HAL_Free(http_data_post->post_buf);
  123. HAL_Free(http_data_post);
  124. return ret;
  125. }
  126. int IOT_HTTP_UPLOAD_FILE(char *file_name, char *upload_buffer, uint32_t upload_buffer_len, char *md5, char *authorization, char *url, uint32_t timeout_ms)
  127. {
  128. int ret = SUCCESS_RET;
  129. http_client_t *http_client_put = (http_client_t *)HAL_Malloc(sizeof(http_client_t));
  130. if(NULL == http_client_put)
  131. {
  132. LOG_ERROR("http_client_put malloc fail\n");
  133. return FAILURE_RET;
  134. }
  135. http_client_data_t *http_data_put = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t));
  136. if(NULL == http_data_put)
  137. {
  138. HAL_Free(http_client_put);
  139. LOG_ERROR("http_data_put malloc fail\n");
  140. return FAILURE_RET;
  141. }
  142. memset(http_client_put, 0, sizeof(http_client_t));
  143. memset(http_data_put, 0, sizeof(http_client_data_t));
  144. http_data_put->response_buf = (char *)HAL_Malloc(512);
  145. if(NULL == http_data_put)
  146. {
  147. HAL_Free(http_client_put);
  148. HAL_Free(http_data_put);
  149. LOG_ERROR("http_data_put->response_buf malloc fail\n");
  150. return FAILURE_RET;
  151. }
  152. memset(http_data_put->response_buf, 0, 512);
  153. http_data_put->response_buf_len = 512;
  154. http_client_put->header = (char *)HAL_Malloc(1024);
  155. if(NULL == http_data_put)
  156. {
  157. HAL_Free(http_client_put);
  158. HAL_Free(http_data_put->response_buf);
  159. HAL_Free(http_data_put);
  160. LOG_ERROR("http_client_put->header malloc fail\n");
  161. return FAILURE_RET;
  162. }
  163. memset(http_client_put->header, 0, 1024);
  164. HAL_Snprintf(http_client_put->header, 1024, "Authorization:%s\r\nContent-Type:plain/text\r\nContent-MD5:%s\r\n",authorization,md5);
  165. LOG_DEBUG("header:%s\n", http_client_put->header);
  166. http_data_put->post_content_type = (char *)"plain/text";
  167. http_data_put->post_buf = (unsigned char *)HAL_Malloc(upload_buffer_len + 1);
  168. if(NULL == http_data_put->post_buf)
  169. {
  170. HAL_Free(http_data_put->response_buf);
  171. HAL_Free(http_data_put);
  172. HAL_Free(http_client_put->header);
  173. HAL_Free(http_client_put);
  174. LOG_ERROR("http_data_put->post_buf malloc fail\n");
  175. return FAILURE_RET;
  176. }
  177. memset(http_data_put->post_buf, 0, upload_buffer_len + 1);
  178. strncpy((char *)http_data_put->post_buf, upload_buffer, upload_buffer_len);
  179. http_data_put->post_buf[upload_buffer_len]= '\0';
  180. http_data_put->post_buf_len = upload_buffer_len;
  181. const char *ca_crt = iot_https_ca_get();
  182. ret = http_client_common(http_client_put, url, 443, ca_crt, HTTP_PUT, http_data_put, timeout_ms);
  183. if(SUCCESS_RET != ret)
  184. {
  185. LOG_ERROR("http_client_common error\n");
  186. goto end;
  187. }
  188. ret = http_client_recv_data(http_client_put, timeout_ms, http_data_put);
  189. if(SUCCESS_RET != ret)
  190. {
  191. LOG_ERROR("http_client_recv_data error\n");
  192. goto end;
  193. }
  194. LOG_DEBUG("content_len:%d response_received_len:%d\n",http_data_put->response_content_len, http_data_put->response_received_len);
  195. LOG_DEBUG("response_buf:%s\n",http_data_put->response_buf);
  196. end:
  197. http_client_close(http_client_put);
  198. HAL_Free(http_data_put->post_buf);
  199. HAL_Free(http_data_put->response_buf);
  200. HAL_Free(http_data_put);
  201. HAL_Free(http_client_put->header);
  202. HAL_Free(http_client_put);
  203. return ret;
  204. }