http_test.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #include "board.h"
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "FreeRTOS.h"
  6. #define HOST_NAME "www.baidu.com"
  7. const static char get_buf[]= "GET / HTTP/1.1\r\n" /* HOST, host-port, URI */\
  8. "Host: www.baidu.com\r\n" /* server name */ \
  9. "\r\n";
  10. /*
  11. *行长度不包含行尾的\r\n
  12. */
  13. static int https_content_get_line_len(const char *https_content, int content_len)
  14. {
  15. int ret = -1;
  16. int step = 0;
  17. int cnt;
  18. if (!https_content || (content_len <= 0))
  19. goto exit;
  20. for (cnt = 0; cnt < content_len; cnt++) {
  21. if (https_content[cnt] == '\r') {
  22. step = 1;
  23. } else if (https_content[cnt] == '\n') {
  24. if (step == 1) {
  25. ret = cnt - 1;
  26. break;
  27. }
  28. } else {
  29. step = 0;
  30. }
  31. }
  32. exit:
  33. return ret;
  34. }
  35. static int https_content_get_line_pos(const char *https_content, int content_len, uint32_t line)
  36. {
  37. int pos = -1;
  38. int cur_pos;
  39. int step = 0;
  40. int line_cnt = 0;
  41. if (!https_content || (content_len <= 0))
  42. goto exit;
  43. for (cur_pos = 0; cur_pos < content_len; cur_pos++) {
  44. if (step == 2) {
  45. line_cnt++;
  46. step = 0;
  47. if (line_cnt == line) {
  48. pos = cur_pos;
  49. break;
  50. }
  51. }
  52. if (https_content[cur_pos] == '\r') {
  53. step = 1;
  54. } else if (https_content[cur_pos] == '\n') {
  55. if (step == 1) {
  56. step = 2;
  57. }
  58. } else {
  59. step = 0;
  60. }
  61. }
  62. exit:
  63. return pos;
  64. }
  65. static int https_get_content_length(const char *https_content, int content_len)
  66. {
  67. int ret = -1;
  68. int line_len = 0;
  69. int pos = 0;
  70. int pos_cnt = 0;
  71. const char header_start[] = "HTTP";
  72. const char ContentLength[] = "Content-Length";
  73. if (!https_content || content_len < 4) {
  74. ret = -1;
  75. goto exit;
  76. }
  77. if (memcmp((const void *)https_content, (const void *)header_start, 4) != 0) {
  78. ret = -1;
  79. goto exit;
  80. }
  81. while (1) {
  82. pos = https_content_get_line_pos(&https_content[pos_cnt], content_len - pos_cnt, 1);
  83. if (pos > -1) {
  84. pos_cnt += pos;
  85. line_len = https_content_get_line_len(&https_content[pos_cnt], content_len - pos_cnt);
  86. if (line_len <= 0) {
  87. break;
  88. } else if (line_len >= strlen(ContentLength)) {
  89. if (memcmp((const char *)&https_content[pos_cnt], (const char *)ContentLength, strlen(ContentLength)) == 0) {
  90. sscanf((const char *)&https_content[pos_cnt + strlen(ContentLength) + 2], (const char *)"%d\r\n", &ret);
  91. break;
  92. }
  93. }
  94. } else {
  95. break;
  96. }
  97. }
  98. exit:
  99. return ret;
  100. }
  101. static int https_get_content_pos(const char *https_content, int content_len)
  102. {
  103. int ret = -1;
  104. int line_len = 0;
  105. int pos = 0;
  106. int pos_cnt = 0;
  107. int flag = 0;
  108. const char header_start[] = "HTTP";
  109. if (!https_content || content_len < 4) {
  110. ret = -1;
  111. goto exit;
  112. }
  113. if (memcmp((const void *)https_content, (const void *)header_start, 4) != 0) {
  114. ret = -1;
  115. goto exit;
  116. }
  117. while (1) {
  118. pos = https_content_get_line_pos(&https_content[pos_cnt], content_len - pos_cnt, 1);
  119. if (pos > -1) {
  120. pos_cnt += pos;
  121. line_len = https_content_get_line_len(&https_content[pos_cnt], content_len - pos_cnt);
  122. if (flag) {
  123. ret = pos_cnt;
  124. break;
  125. } else {
  126. if (line_len < 0) {
  127. break;
  128. } else if (line_len == 0) {
  129. flag = 1;
  130. }
  131. }
  132. } else {
  133. break;
  134. }
  135. }
  136. exit:
  137. return ret;
  138. }
  139. #if !USE_LWIP
  140. #include "FreeRTOS_Sockets.h"
  141. #include "FreeRTOS_DHCP_Server.h"
  142. void http_client_test(void *thread_param)
  143. {
  144. Socket_t sock = NULL;
  145. char *rbuf = NULL;
  146. int total_recv_len = 0;
  147. uint32_t pg_size = 1024 * 16;
  148. int ContentLength = -1;
  149. int content_start_pos = -1;
  150. int recv_len = 0;
  151. uint32_t server_ip;
  152. char pucBuffer[32] = {0};
  153. struct freertos_sockaddr servaddr = {0};
  154. printf("-------------- freertos-plus-tcp http client test ---------------\n");
  155. server_ip = FreeRTOS_gethostbyname(HOST_NAME);
  156. if (server_ip == 0) {
  157. printf("-------------- FreeRTOS_gethostbyname fail! --------------\n");
  158. goto exit;
  159. }
  160. FreeRTOS_inet_ntoa(server_ip, pucBuffer);
  161. printf("host name : %s , host_ip : %s\n", HOST_NAME, pucBuffer);
  162. sock = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);
  163. if (!sock) {
  164. printf("Socket error\n");
  165. goto exit;
  166. }
  167. servaddr.sin_port = htons(80);
  168. FreeRTOS_inet_pton(FREERTOS_AF_INET, FreeRTOS_inet_ntoa(server_ip, pucBuffer), &servaddr.sin_addr);
  169. if (0 > FreeRTOS_connect(sock, &servaddr, sizeof(servaddr))) {
  170. printf("-------------- https connect socket fail! --------------\n");
  171. goto exit;
  172. }
  173. printf("Connect to server successful!\n");
  174. printf("\n***************************************************\n");
  175. FreeRTOS_send(sock, get_buf, sizeof(get_buf), NULL);
  176. //接收
  177. for(;;) {
  178. if (total_recv_len == 0)
  179. rbuf = (char *)pvPortRealloc(rbuf, pg_size);
  180. else
  181. rbuf = (char *)pvPortRealloc(rbuf, total_recv_len + pg_size);
  182. if (!rbuf) {
  183. printf("realloc fail!\n");
  184. goto exit;
  185. }
  186. if ((ContentLength > 0) && (content_start_pos > 0)) {
  187. if (((ContentLength + content_start_pos) - total_recv_len) >= pg_size) {
  188. recv_len = FreeRTOS_recv(sock, rbuf + total_recv_len, pg_size, 0);
  189. } else {
  190. recv_len = FreeRTOS_recv(sock, rbuf + total_recv_len, ((ContentLength + content_start_pos) - total_recv_len), 0);
  191. }
  192. } else {
  193. recv_len = FreeRTOS_recv(sock, rbuf + total_recv_len, pg_size, 0);
  194. }
  195. if(recv_len <= 0) {
  196. break;
  197. }
  198. total_recv_len += recv_len;
  199. if ((ContentLength > 0) && (content_start_pos > 0)) {
  200. if ((ContentLength + content_start_pos) <= total_recv_len)
  201. break;
  202. }
  203. if (ContentLength == -1) {
  204. ContentLength = https_get_content_length((const char *)rbuf, total_recv_len);
  205. if (ContentLength > 0)
  206. printf("ContentLength = %d\n", ContentLength);
  207. }
  208. if ((ContentLength > 0) && (content_start_pos == -1)) {
  209. content_start_pos = https_get_content_pos((const char *)rbuf, total_recv_len);
  210. if (content_start_pos > 0)
  211. printf("content_start_pos = %d\n", content_start_pos);
  212. }
  213. }
  214. printf("\n ========== \n\n");
  215. printf("https recv data : recv_len = 0x%x!\n",total_recv_len);
  216. printf("addr:0x%x\n", (uint32_t)rbuf);
  217. printf("\n ========== \n\n");
  218. printf("\n**************************************************\n");
  219. exit:
  220. if (sock)
  221. FreeRTOS_closesocket(sock);
  222. if (rbuf)
  223. vPortFree((void *)rbuf);
  224. }
  225. #else
  226. /* lwip includes. */
  227. #include "api.h"
  228. #include "sockets.h"
  229. void http_client_test(void *thread_param)
  230. {
  231. err_t err = ERR_OK;
  232. int sock = -1;
  233. struct sockaddr_in client_addr;
  234. char *rbuf = NULL;
  235. int total_recv_len = 0;
  236. uint32_t pg_size = 1024 * 16;
  237. int ContentLength = -1;
  238. int content_start_pos = -1;
  239. int recv_len = 0;
  240. printf("-------------- lwip http client test ---------------\n");
  241. char* host_ip;
  242. ip4_addr_t dns_ip;
  243. err = netconn_gethostbyname(HOST_NAME, &dns_ip);
  244. if (err != ERR_OK) {
  245. printf("-------------- netconn_gethostbyname fail! --------------\n");
  246. goto exit;
  247. }
  248. host_ip = ip_ntoa(&dns_ip);
  249. printf("host name : %s , host_ip : %s\n",HOST_NAME,host_ip);
  250. sock = socket(AF_INET, SOCK_STREAM, 0);
  251. if (sock < 0) {
  252. printf("Socket error\n");
  253. goto exit;
  254. }
  255. client_addr.sin_family = AF_INET;
  256. client_addr.sin_port = htons(80);
  257. client_addr.sin_addr.s_addr = inet_addr(host_ip);
  258. memset(&(client_addr.sin_zero), 0, sizeof(client_addr.sin_zero));
  259. if (connect(sock,
  260. (struct sockaddr *)&client_addr,
  261. sizeof(client_addr)) == -1)
  262. {
  263. printf("Connect failed!\n");
  264. goto exit;
  265. }
  266. printf("Connect to server successful!\n");
  267. printf("\n***************************************************\n");
  268. write(sock,get_buf,sizeof(get_buf));
  269. //接收
  270. for(;;) {
  271. if (total_recv_len == 0)
  272. rbuf = (char *)pvPortRealloc(rbuf, pg_size);
  273. else
  274. rbuf = (char *)pvPortRealloc(rbuf, total_recv_len + pg_size);
  275. if (!rbuf) {
  276. printf("realloc fail!\n");
  277. goto exit;
  278. }
  279. if ((ContentLength > 0) && (content_start_pos > 0)) {
  280. if (((ContentLength + content_start_pos) - total_recv_len) >= pg_size) {
  281. recv_len = recv(sock, rbuf + total_recv_len, pg_size, 0);
  282. } else {
  283. recv_len = recv(sock, rbuf + total_recv_len, ((ContentLength + content_start_pos) - total_recv_len), 0);
  284. }
  285. } else {
  286. recv_len = recv(sock, rbuf + total_recv_len, pg_size, 0);
  287. }
  288. if(recv_len <= 0) {
  289. break;
  290. }
  291. total_recv_len += recv_len;
  292. if ((ContentLength > 0) && (content_start_pos > 0)) {
  293. if ((ContentLength + content_start_pos) <= total_recv_len)
  294. break;
  295. }
  296. if (ContentLength == -1) {
  297. ContentLength = https_get_content_length((const char *)rbuf, total_recv_len);
  298. if (ContentLength > 0)
  299. printf("ContentLength = %d\n", ContentLength);
  300. }
  301. if ((ContentLength > 0) && (content_start_pos == -1)) {
  302. content_start_pos = https_get_content_pos((const char *)rbuf, total_recv_len);
  303. if (content_start_pos > 0)
  304. printf("content_start_pos = %d\n", content_start_pos);
  305. }
  306. }
  307. printf("\n ========== \n\n");
  308. printf("https recv data : recv_len = 0x%x!\n",total_recv_len);
  309. printf("addr:0x%x\n", (uint32_t)rbuf);
  310. printf("\n ========== \n\n");
  311. printf("\n**************************************************\n");
  312. exit:
  313. if (sock >= 0)
  314. closesocket(sock);
  315. if (rbuf)
  316. vPortFree((void *)rbuf);
  317. }
  318. #endif