http_test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "board.h"
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "FreeRTOS.h"
  6. #include "api.h"
  7. #include "sockets.h"
  8. #define HOST_NAME "www.baidu.com"
  9. const static char get_buf[]= "GET / HTTP/1.1\r\n" /* HOST, host-port, URI */\
  10. "Host: www.baidu.com\r\n" /* server name */ \
  11. "\r\n";
  12. /*
  13. *行长度不包含行尾的\r\n
  14. */
  15. static int https_content_get_line_len(const char *https_content, int content_len)
  16. {
  17. int ret = -1;
  18. int step = 0;
  19. int cnt;
  20. if (!https_content || (content_len <= 0))
  21. goto exit;
  22. for (cnt = 0; cnt < content_len; cnt++) {
  23. if (https_content[cnt] == '\r') {
  24. step = 1;
  25. } else if (https_content[cnt] == '\n') {
  26. if (step == 1) {
  27. ret = cnt - 1;
  28. break;
  29. }
  30. } else {
  31. step = 0;
  32. }
  33. }
  34. exit:
  35. return ret;
  36. }
  37. static int https_content_get_line_pos(const char *https_content, int content_len, uint32_t line)
  38. {
  39. int pos = -1;
  40. int cur_pos;
  41. int step = 0;
  42. int line_cnt = 0;
  43. if (!https_content || (content_len <= 0))
  44. goto exit;
  45. for (cur_pos = 0; cur_pos < content_len; cur_pos++) {
  46. if (step == 2) {
  47. line_cnt++;
  48. step = 0;
  49. if (line_cnt == line) {
  50. pos = cur_pos;
  51. break;
  52. }
  53. }
  54. if (https_content[cur_pos] == '\r') {
  55. step = 1;
  56. } else if (https_content[cur_pos] == '\n') {
  57. if (step == 1) {
  58. step = 2;
  59. }
  60. } else {
  61. step = 0;
  62. }
  63. }
  64. exit:
  65. return pos;
  66. }
  67. static int https_get_content_length(const char *https_content, int content_len)
  68. {
  69. int ret = -1;
  70. int line_len = 0;
  71. int pos = 0;
  72. int pos_cnt = 0;
  73. const char header_start[] = "HTTP";
  74. const char ContentLength[] = "Content-Length";
  75. if (!https_content || content_len < 4) {
  76. ret = -1;
  77. goto exit;
  78. }
  79. if (memcmp((const void *)https_content, (const void *)header_start, 4) != 0) {
  80. ret = -1;
  81. goto exit;
  82. }
  83. while (1) {
  84. pos = https_content_get_line_pos(&https_content[pos_cnt], content_len - pos_cnt, 1);
  85. if (pos > -1) {
  86. pos_cnt += pos;
  87. line_len = https_content_get_line_len(&https_content[pos_cnt], content_len - pos_cnt);
  88. if (line_len <= 0) {
  89. break;
  90. } else if (line_len >= strlen(ContentLength)) {
  91. if (memcmp((const char *)&https_content[pos_cnt], (const char *)ContentLength, strlen(ContentLength)) == 0) {
  92. sscanf((const char *)&https_content[pos_cnt + strlen(ContentLength) + 2], (const char *)"%d\r\n", &ret);
  93. break;
  94. }
  95. }
  96. } else {
  97. break;
  98. }
  99. }
  100. exit:
  101. return ret;
  102. }
  103. static int https_get_content_pos(const char *https_content, int content_len)
  104. {
  105. int ret = -1;
  106. int line_len = 0;
  107. int pos = 0;
  108. int pos_cnt = 0;
  109. int flag = 0;
  110. const char header_start[] = "HTTP";
  111. if (!https_content || content_len < 4) {
  112. ret = -1;
  113. goto exit;
  114. }
  115. if (memcmp((const void *)https_content, (const void *)header_start, 4) != 0) {
  116. ret = -1;
  117. goto exit;
  118. }
  119. while (1) {
  120. pos = https_content_get_line_pos(&https_content[pos_cnt], content_len - pos_cnt, 1);
  121. if (pos > -1) {
  122. pos_cnt += pos;
  123. line_len = https_content_get_line_len(&https_content[pos_cnt], content_len - pos_cnt);
  124. if (flag) {
  125. ret = pos_cnt;
  126. break;
  127. } else {
  128. if (line_len < 0) {
  129. break;
  130. } else if (line_len == 0) {
  131. flag = 1;
  132. }
  133. }
  134. } else {
  135. break;
  136. }
  137. }
  138. exit:
  139. return ret;
  140. }
  141. void http_client_test(void *thread_param)
  142. {
  143. err_t err = ERR_OK;
  144. int sock = -1;
  145. struct sockaddr_in client_addr;
  146. char *rbuf = NULL;
  147. int total_recv_len = 0;
  148. uint32_t pg_size = 1024 * 16;
  149. int ContentLength = -1;
  150. int content_start_pos = -1;
  151. int recv_len = 0;
  152. printf("-------------- lwip http client test ---------------\n");
  153. char* host_ip;
  154. ip4_addr_t dns_ip;
  155. err = netconn_gethostbyname(HOST_NAME, &dns_ip);
  156. if (err != ERR_OK) {
  157. printf("-------------- netconn_gethostbyname fail! --------------\n");
  158. goto exit;
  159. }
  160. host_ip = ip_ntoa(&dns_ip);
  161. printf("host name : %s , host_ip : %s\n",HOST_NAME,host_ip);
  162. sock = socket(AF_INET, SOCK_STREAM, 0);
  163. if (sock < 0) {
  164. printf("Socket error\n");
  165. goto exit;
  166. }
  167. client_addr.sin_family = AF_INET;
  168. client_addr.sin_port = htons(80);
  169. client_addr.sin_addr.s_addr = inet_addr(host_ip);
  170. memset(&(client_addr.sin_zero), 0, sizeof(client_addr.sin_zero));
  171. if (connect(sock,
  172. (struct sockaddr *)&client_addr,
  173. sizeof(client_addr)) == -1)
  174. {
  175. printf("Connect failed!\n");
  176. goto exit;
  177. }
  178. printf("Connect to server successful!\n");
  179. printf("\n***************************************************\n");
  180. write(sock,get_buf,sizeof(get_buf));
  181. //接收
  182. for(;;) {
  183. if (total_recv_len == 0)
  184. rbuf = (char *)pvPortRealloc(rbuf, pg_size);
  185. else
  186. rbuf = (char *)pvPortRealloc(rbuf, total_recv_len + pg_size);
  187. if (!rbuf) {
  188. printf("realloc fail!\n");
  189. goto exit;
  190. }
  191. if ((ContentLength > 0) && (content_start_pos > 0)) {
  192. if (((ContentLength + content_start_pos) - total_recv_len) >= pg_size) {
  193. recv_len = recv(sock, rbuf + total_recv_len, pg_size, 0);
  194. } else {
  195. recv_len = recv(sock, rbuf + total_recv_len, ((ContentLength + content_start_pos) - total_recv_len), 0);
  196. }
  197. } else {
  198. recv_len = recv(sock, rbuf + total_recv_len, pg_size, 0);
  199. }
  200. if(recv_len <= 0) {
  201. break;
  202. }
  203. total_recv_len += recv_len;
  204. if ((ContentLength > 0) && (content_start_pos > 0)) {
  205. if ((ContentLength + content_start_pos) <= total_recv_len)
  206. break;
  207. }
  208. if (ContentLength == -1) {
  209. ContentLength = https_get_content_length((const char *)rbuf, total_recv_len);
  210. if (ContentLength > 0)
  211. printf("ContentLength = %d\n", ContentLength);
  212. }
  213. if ((ContentLength > 0) && (content_start_pos == -1)) {
  214. content_start_pos = https_get_content_pos((const char *)rbuf, total_recv_len);
  215. if (content_start_pos > 0)
  216. printf("content_start_pos = %d\n", content_start_pos);
  217. }
  218. }
  219. printf("\n ========== \n\n");
  220. printf("https recv data : recv_len = 0x%x!\n",total_recv_len);
  221. printf("addr:0x%x\n", (uint32_t)rbuf);
  222. printf("\n ========== \n\n");
  223. printf("\n**************************************************\n");
  224. exit:
  225. if (sock >= 0)
  226. closesocket(sock);
  227. if (rbuf)
  228. vPortFree((void *)rbuf);
  229. }