http.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include "ulog/ulog.h"
  9. #include <aos/kernel.h>
  10. #include <yoc/fota.h>
  11. #include "http.h"
  12. #define TAG "http"
  13. http_t *http_init(const char *path)
  14. {
  15. char *ip, *port_str;
  16. char *end_point;
  17. char *host = NULL;
  18. int port;
  19. // FIXME: only support HTTP now.
  20. if (path == NULL || !strstr(path, "http://")) {
  21. return NULL;
  22. }
  23. http_t *http = aos_zalloc_check(sizeof(http_t));
  24. http->url = strdup(path);
  25. if (http->url == NULL) {
  26. goto error;
  27. }
  28. host = http->url + sizeof("http://") - 1;
  29. if (strlen(host) == 0) {
  30. goto error;
  31. }
  32. http->port = 80;
  33. ip = http->url + strlen("http://");
  34. port_str = strrchr(host, ':');
  35. if (port_str != NULL) {
  36. *port_str = 0;
  37. port_str ++;
  38. port = strtol(port_str, &end_point, 10);
  39. http->port = port;
  40. } else {
  41. port = 80;
  42. end_point = strchr(ip, '/');
  43. }
  44. if (end_point != NULL) {
  45. http->path = strdup(end_point);
  46. LOGD(TAG, "http: path %s", http->path);
  47. if (http->path == NULL) {
  48. goto error;
  49. }
  50. } else {
  51. goto error;
  52. }
  53. http->buffer = (uint8_t *)aos_malloc(BUFFER_SIZE);
  54. if (http->buffer == NULL) {
  55. goto error;
  56. }
  57. memset(http->buffer, 0, BUFFER_SIZE);
  58. *end_point = 0;
  59. http->host = strdup(ip);
  60. if (http->host == NULL) {
  61. goto error;
  62. }
  63. LOGD(TAG, "http connect: %s:%d", http->host, port);
  64. network_init(&http->net);
  65. if (http->net.net_connect(&http->net, http->host, port, SOCK_STREAM) != 0) {
  66. goto error;
  67. }
  68. return http;
  69. error:
  70. if (http) {
  71. if (http->url) aos_free(http->url);
  72. if (http->path) aos_free(http->path);
  73. if (http->host) aos_free(http->host);
  74. if (http->buffer) aos_free(http->buffer);
  75. aos_free(http);
  76. }
  77. return NULL;
  78. }
  79. int http_head_sets(http_t *http, const char *key, const char *value)
  80. {
  81. int len;
  82. if (http->buffer == NULL) {
  83. return -1;
  84. }
  85. len = snprintf((char*)http->buffer + http->buffer_offset, BUFFER_SIZE - http->buffer_offset,
  86. "%s: %s\r\n", key, value);
  87. if (len <= 0) {
  88. return -1;
  89. }
  90. http->buffer_offset += len;
  91. return 0;
  92. }
  93. int http_head_seti(http_t *http, const char *key, int value)
  94. {
  95. int len;
  96. if (http->buffer == NULL) {
  97. return -1;
  98. }
  99. len = snprintf((char*)http->buffer + http->buffer_offset, BUFFER_SIZE - http->buffer_offset,
  100. "%s: %d\r\n", key, value);
  101. if (len <= 0) {
  102. return -1;
  103. }
  104. http->buffer_offset += len;
  105. return 0;
  106. }
  107. int http_post(http_t *http, char *playload, int timeoutms)
  108. {
  109. char *temp_buff;
  110. if ((temp_buff = strdup((char *)http->buffer)) == NULL) {
  111. return -1;
  112. }
  113. // temp_buff = strdup((char *)http->buffer);
  114. memset(http->buffer, 0, BUFFER_SIZE);
  115. snprintf((char *)http->buffer, BUFFER_SIZE, "POST %s HTTP/1.1\r\n", http->path);
  116. strcat((char *)http->buffer, temp_buff);
  117. strcat((char *)http->buffer, "\r\n");
  118. strcat((char *)http->buffer, playload);
  119. // LOGD(TAG, "http post sendbuffer: %s", http->buffer);
  120. aos_free(temp_buff);
  121. return http->net.net_write(&http->net, http->buffer, strlen((char*)http->buffer), timeoutms);
  122. }
  123. int http_get(http_t *http, int timeoutms)
  124. {
  125. char *temp_buff;
  126. if ((temp_buff = strdup((char *)http->buffer)) == NULL) {
  127. return -1;
  128. }
  129. // temp_buff = strdup((char *)http->buffer);
  130. memset(http->buffer, 0, BUFFER_SIZE);
  131. snprintf((char *)http->buffer, BUFFER_SIZE, "GET %s HTTP/1.1\r\n", http->path);
  132. strcat((char *)http->buffer, temp_buff);
  133. strcat((char *)http->buffer, "\r\n");
  134. // LOGD(TAG, "http get sendbuffer: %s", http->buffer);
  135. aos_free(temp_buff);
  136. return http->net.net_write(&http->net, http->buffer, strlen((char*)http->buffer), timeoutms);
  137. }
  138. static int http_parse_head(http_t *http, char **head)
  139. {
  140. char *content;
  141. char *endpoint;
  142. int content_len;
  143. char *code_str;
  144. int code;
  145. char *end_point;
  146. *head = strstr((char*)http->buffer, "\r\n\r\n");
  147. if (*head == NULL) {
  148. return 0;
  149. }
  150. *head += 4;
  151. // FIXME: server connection close, need reconnect
  152. if (strstr((char*)http->buffer, "Connection: close")) {
  153. return -2;
  154. }
  155. if ((content = strstr((char*)http->buffer, "Content-Length: ")) == NULL) {
  156. code_str = strstr((char *)http->buffer, "HTTP/1.1");
  157. code = atoi(code_str + strlen("HTTP/1.1"));
  158. if (code != 206 && code != 200) {
  159. LOGD(TAG, "http code :%d", code);
  160. return -1;
  161. }
  162. content_len = strtol(*head, &end_point, 16);
  163. if (end_point == NULL) {
  164. return 0;
  165. }
  166. } else {
  167. // content_len = atoi(content + strlen("Content-Length: "));
  168. content_len = strtol(content + strlen("Content-Length: "), &endpoint, 10);
  169. if (endpoint == NULL || *endpoint != '\r') {
  170. return -1;
  171. }
  172. }
  173. // LOGD(TAG, "http parse head: %s %d", http->buffer, content_len);
  174. return content_len;
  175. }
  176. static int net_quick_reconnect(http_t *http)
  177. {
  178. LOGW(TAG, "i need reconnect http");
  179. http->net.net_disconncet(&http->net);
  180. if (http->net.net_connect(&http->net, http->host, http->port, SOCK_STREAM) != 0) {
  181. LOGE(TAG, "reconnect failed!");
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. static int net_read(int fd, unsigned char *buffer, int len, int timeout_ms)
  187. {
  188. struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
  189. if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 100)) {
  190. interval.tv_sec = 0;
  191. interval.tv_usec = 10000;
  192. }
  193. if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval,
  194. sizeof(struct timeval))) {
  195. return -1;
  196. }
  197. int rc = recv(fd, buffer, len, 0);
  198. return rc;
  199. }
  200. int http_wait_resp(http_t *http, char **head_end, int timeoutms)
  201. {
  202. int recv_len = 0;
  203. int len;
  204. int content_len;
  205. again:
  206. recv_len = 0;
  207. *head_end = NULL;
  208. content_len = 0;
  209. memset(http->buffer, 0, BUFFER_SIZE);
  210. while(recv_len < BUFFER_SIZE) {
  211. int pre_read = BUFFER_SIZE - recv_len;
  212. if(pre_read > 512) {
  213. pre_read = 512;
  214. }
  215. len = net_read(http->net.fd, http->buffer + recv_len, pre_read, timeoutms);
  216. //printf("pre read %d\n", len);
  217. if (len <= 0) {
  218. LOGE(TAG, "net read len=%d errno=%d", len, errno);
  219. return -1;
  220. }
  221. recv_len += len;
  222. if ((content_len = http_parse_head(http, head_end)) == 0) {
  223. continue;
  224. }
  225. if (content_len < 0) {
  226. if (content_len == -2) {
  227. if (net_quick_reconnect(http) < 0) {
  228. return 0;
  229. }
  230. return -2;
  231. }
  232. return -1;
  233. } else {
  234. break;
  235. }
  236. }
  237. if (content_len <= 0 && !(*head_end)) {
  238. goto again;
  239. }
  240. int head_len = (*head_end - (char *)http->buffer);
  241. int total_len = content_len + head_len;
  242. //int left_len = content_len + head_len - len;
  243. //printf("head len %d, left_len %d, total %d, recv len %d\n", head_len, left_len, total_len, recv_len);
  244. if ( total_len > BUFFER_SIZE ) {
  245. LOGE(TAG, "total len %d", total_len);
  246. return -1;
  247. }
  248. // LOGD(TAG, "buffer: %.*s", *head_end - (char*)http_ins->buffer, http_ins->buffer);
  249. while(recv_len < total_len) {
  250. if (content_len <= recv_len - (*head_end - (char *)http->buffer)) {
  251. break;
  252. }
  253. len = http->net.net_read(&http->net, http->buffer + recv_len, total_len - recv_len, timeoutms);
  254. //printf("left read %d\n", len);
  255. if (len <= 0) {
  256. LOGE(TAG, "net read len=%d errno=%d", len, errno);
  257. return -1;
  258. }
  259. recv_len += len;
  260. }
  261. return content_len;
  262. }
  263. char *http_head_get(http_t *http, char *key, int *length)
  264. {
  265. char *temp_key;
  266. char *temp_value;
  267. char *temp_end;
  268. if ((temp_key = strstr((char *)http->buffer, key)) == NULL) {
  269. LOGD(TAG, "no key %s", key);
  270. return NULL;
  271. }
  272. if ((temp_value = strstr(temp_key, ":")) == NULL) {
  273. LOGD(TAG, "no delimiter");
  274. return NULL;
  275. }
  276. if ((temp_end = strstr(temp_value, "\r\n")) == NULL) {
  277. LOGD(TAG, "no end");
  278. return NULL;
  279. }
  280. *length = (int)(temp_end - temp_value - 2) >= 0 ? (int)(temp_end - temp_value - 2) : 0;// 1 space
  281. return temp_value + 1;
  282. }
  283. char *http_read_data(http_t *http)
  284. {
  285. char *buffer;
  286. buffer = strstr((char *)http->buffer, "\r\n\r\n");
  287. if (buffer == NULL) {
  288. return NULL;
  289. }
  290. return buffer + 4;
  291. }
  292. int http_deinit(http_t *http)
  293. {
  294. if (http) {
  295. http->net.net_disconncet(&http->net);
  296. if (http->url) aos_free(http->url);
  297. if (http->path) aos_free(http->path);
  298. if (http->host) aos_free(http->host);
  299. if (http->buffer) aos_free(http->buffer);
  300. aos_free(http);
  301. }
  302. return 0;
  303. }