httpclient.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "stdint.h"
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "ucos_ii.h"
  5. #include "lwip/opt.h"
  6. #include "lwip/arch.h"
  7. #include "lwip/api.h"
  8. #include "lwip/inet.h"
  9. #include "lwip/sockets.h"
  10. #include "lwip/dns.h"
  11. #include "httpclient.h"
  12. uint8_t httpSendBuffer[1024];
  13. uint8_t httpRecvBuffer[512];
  14. /**
  15. * @brief 从socket缓存中读取一行http数据
  16. * @param sock: 已经连接到服务器的sock编号
  17. * @param buf: 保存数据的buffer
  18. * @param size: buf的最大可用长度
  19. * @retval 读取到的数据的长度,包括两个字节的换行符
  20. */
  21. int http_getLine(int sock, uint8_t *buf, int size)
  22. {
  23. int i = 0;
  24. char c = '\0';
  25. int n;
  26. while((i < (size - 1)) && (c != '\n'))
  27. {
  28. n = recv(sock, &c, 1, 0);
  29. if(n <= 0) c = '\n';
  30. buf[i++] = c;
  31. }
  32. buf[i] = '\0';
  33. return i; //返回读取的到的数据长度
  34. }
  35. /**
  36. * @brief 解析http响应行
  37. * @param pbuf: 响应行的数据
  38. * @retval 其他值: 返回http请求状态 -1: 解析失败
  39. */
  40. int http_parseRequestLine(uint8_t *pbuf)
  41. {
  42. int b, s, g;
  43. if((strncmp((char *)pbuf, "HTTP/1.1 ", strlen("HTTP/1.1 ")) == 0) || (strncmp((char *)pbuf, "http/1.1 ", strlen("http/1.1 ")) == 0))
  44. {
  45. pbuf += strlen("HTTP/1.1 ");
  46. b = pbuf[0] - '0';
  47. s = pbuf[1] - '0';
  48. g = pbuf[2] - '0';
  49. return (b * 100 + s * 10 + g);
  50. }
  51. return -1;
  52. }
  53. /**
  54. * @brief DNS解析回调函数
  55. * @note 在解析域名成功后,会调用这个函数,然后可以读取到对应的IP地址
  56. * @param name: 域名
  57. * @param host_ip: 域名对应的ip地址
  58. * @param callback_arg: 传递的参数
  59. * @retval None
  60. */
  61. void http_dns_found(const char *name, ip_addr_t *host_ip, void *callback_arg)
  62. {
  63. *(ip_addr_t *)callback_arg = *host_ip;
  64. HTTP_PRINTF("%s:%s\r\n",name, ipaddr_ntoa(host_ip));
  65. }
  66. /**
  67. * @brief 连接到http服务器的函数
  68. * @note 连接到http服务器
  69. * @param host: 服务器的域名或者ip地址
  70. * @param port: 服务器端口号
  71. * @param hostIsIp: host代表的是 域名,还是ip地址 0: host为域名 1: host为ip地址
  72. * @retval -1:连接服务器失败 -2: 域名解析失败 >=0: 连接成功,返回值为 sock编号
  73. */
  74. int http_clientConnectToServer(char *host, int port, int hostIsIp)
  75. {
  76. int timeout;
  77. struct sockaddr_in serverAddr;
  78. int sock = socket(AF_INET, SOCK_STREAM, 0);
  79. if(sock < 0) return -2;
  80. //如果传入的host参数是域名,解析域名
  81. if(hostIsIp == 0)
  82. {
  83. ip_addr_t addr;
  84. addr.addr = 0;
  85. dns_gethostbyname(host, &addr, http_dns_found, &addr);
  86. //等待dns解析完成
  87. timeout = 0;
  88. while((addr.addr == 0) && (timeout < 2000))
  89. {
  90. OSTimeDly(10);
  91. timeout += 10;
  92. }
  93. if(timeout >= 2000)
  94. {
  95. HTTP_PRINTF(("dns get failure \n"));
  96. return -2;
  97. }
  98. serverAddr.sin_addr.s_addr = inet_addr(inet_ntoa(addr));
  99. }
  100. else serverAddr.sin_addr.s_addr = inet_addr((char*)host);
  101. serverAddr.sin_len = sizeof(serverAddr);
  102. serverAddr.sin_family = AF_INET;
  103. serverAddr.sin_port = htons(port);
  104. memset(&serverAddr.sin_zero, 0, sizeof(serverAddr.sin_zero));
  105. //连接服务器
  106. if(connect(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0)
  107. {
  108. HTTP_PRINTF("connect server error \r\n");
  109. return -1;
  110. }
  111. //设置接收数据超时时间
  112. timeout = 3000;
  113. setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(int));
  114. HTTP_PRINTF("connect server success \r\n");
  115. return sock;
  116. }
  117. /**
  118. * @brief 关闭socket端口
  119. * @param sock: 已经连接到服务器的sock编号
  120. */
  121. void http_clientClose(int sock)
  122. {
  123. close(sock);
  124. }
  125. /**
  126. * @brief 连接到发送GET请求
  127. * @note 组装GET数据包,并将GET请求发送出去
  128. * @param sock: 已经连接到服务器的sock编号
  129. * @param host: 服务器的域名或者ip地址
  130. * @param url: 请求资源的地址
  131. * @retval -1: 发送请求失败 1:发送成功
  132. */
  133. int http_clientPacketRequest_GET(int sock, char *host, char *url)
  134. {
  135. int len;
  136. memset(httpSendBuffer, 0, sizeof(httpSendBuffer));
  137. //组建请求行
  138. sprintf((char *)httpSendBuffer, "GET ");
  139. if(url == NULL) strcat((char *)httpSendBuffer, "/");
  140. else strcat((char *)httpSendBuffer, url);
  141. strcat((char *)httpSendBuffer, " HTTP/1.1\r\n");
  142. //组建请求头部
  143. strcat((char *)httpSendBuffer, "Host: ");
  144. strcat((char *)httpSendBuffer, host);
  145. strcat((char *)httpSendBuffer, "\r\n");
  146. strcat((char *)httpSendBuffer, "Connection: close\r\n");
  147. strcat((char *)httpSendBuffer, "Accept: application/json\r\n");
  148. strcat((char *)httpSendBuffer, "User-Agent: stm32f207\r\n");
  149. strcat((char *)httpSendBuffer, "Cache-Control: no-cache\r\n");
  150. //添加一个空白行
  151. strcat((char *)httpSendBuffer, "\r\n");
  152. len = strlen((char *)httpSendBuffer);
  153. HTTP_PRINTF("%s", (char *)httpSendBuffer); //
  154. //发送请求报文
  155. len = write(sock, httpSendBuffer, len);
  156. if(len <= 0)
  157. {
  158. return -1;
  159. }
  160. return 1;
  161. }
  162. /**
  163. * @brief http_clientReadResponse_GET
  164. * @note 等待服务器返回GET响应
  165. * @param sock: 已经连接到服务器的sock编号
  166. * @retval -1: 发送请求失败 1:发送成功
  167. */
  168. int http_clientReadResponse_GET(int sock, uint8_t *pbuf, int *datlen)
  169. {
  170. int len, ret;
  171. int length = 0;
  172. //读取响应行
  173. len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
  174. if(len <= 0) return -1;
  175. HTTP_PRINTF("%s", (char *)httpRecvBuffer);
  176. ret = http_parseRequestLine(httpRecvBuffer);
  177. //读取响应头
  178. do
  179. {
  180. len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
  181. HTTP_PRINTF("%s", (char *)httpRecvBuffer);
  182. if(len <= 2)
  183. {
  184. if(len == 2) break; //读取到了空行
  185. else return -1;
  186. }
  187. }while(len > 0);
  188. //读取响应主体内容
  189. length = 0;
  190. do
  191. {
  192. len = recv(sock, httpRecvBuffer, sizeof(httpRecvBuffer), 0);
  193. if(len > 0)
  194. {
  195. memcpy(pbuf + length, httpRecvBuffer, len);
  196. length += len;
  197. }
  198. }while(len > 0);
  199. *datlen = length;
  200. return ret;
  201. }
  202. /*
  203. * @brief http_clientPacketRequest_POST
  204. * @note 组建和发送POST请求头数据包
  205. * @param sock: 已经连接到服务器的sock编号
  206. * @param host: 服务器域名或者IP地址
  207. * @param url: 请求的资源位置
  208. * @param pbuf: 需要post的数据缓存
  209. * @param datalen 需要post的数据长度
  210. * @retval -1: 发送失败 1:发送成功
  211. */
  212. char httpTmpBuffer[64];
  213. int http_clientPacketRequest_POST(int sock, char *host, char *url, int datalen)
  214. {
  215. int len;
  216. memset(httpSendBuffer, 0, sizeof(httpSendBuffer));
  217. //组建请求行
  218. sprintf((char *)httpSendBuffer, "POST ");
  219. if(url == NULL) strcat((char *)httpSendBuffer, "/");
  220. else strcat((char *)httpSendBuffer, url);
  221. strcat((char *)httpSendBuffer, " HTTP/1.1\r\n");
  222. //组建请求头部
  223. memset(httpTmpBuffer, 0, sizeof(httpTmpBuffer));
  224. sprintf(httpTmpBuffer, "Host: %s\r\n", host);
  225. strcat((char *)httpSendBuffer, httpTmpBuffer);
  226. strcat((char *)httpSendBuffer, "Connection: close\r\n");
  227. strcat((char *)httpSendBuffer, "Accept: application/json\r\n"); //
  228. strcat((char *)httpSendBuffer, "User-Agent: stm32f207\r\n");
  229. strcat((char *)httpSendBuffer, "Cache-Control: no-cache\r\n");
  230. strcat((char *)httpSendBuffer, "Content-Type: application/json\r\n");
  231. memset(httpTmpBuffer, 0, sizeof(httpTmpBuffer));
  232. sprintf(httpTmpBuffer, "Content-Length: %d\r\n", datalen);
  233. strcat((char *)httpSendBuffer, httpTmpBuffer);
  234. //添加一个空白行
  235. strcat((char *)httpSendBuffer, "\r\n");
  236. len = strlen((char *)httpSendBuffer);
  237. HTTP_PRINTF("%s", (char *)httpSendBuffer);
  238. //发送请求报文
  239. len = write(sock, httpSendBuffer, len);
  240. if(len <= 0)
  241. {
  242. return -1;
  243. }
  244. return 1;
  245. }
  246. /*
  247. * @brief http_clientPacketBody_POST
  248. * @note 发送post数据
  249. * @param sock: 已经连接到服务器的sock编号
  250. * @param pbuf: 需要post的数据缓存
  251. * @param datalen 需要post的数据长度
  252. * @retval -1: 发送失败 1:发送成功
  253. */
  254. int http_clientPacketBody_POST(int sock, uint8_t *pbuf, int datalen)
  255. {
  256. int len;
  257. while(datalen > 1000)
  258. {
  259. len = write(sock, pbuf, 1000);
  260. if(len <= 0)
  261. {
  262. return -1;
  263. }
  264. datalen -= 1000;
  265. pbuf += 1000;
  266. }
  267. if(datalen > 0)
  268. {
  269. len = write(sock, pbuf, datalen);
  270. if(len <= 0)
  271. {
  272. return -1;
  273. }
  274. }
  275. return 1;
  276. }
  277. /*
  278. * @brief http_clientReadResponse_POST
  279. * @note 读取和解析POST返回的响应
  280. * @param sock: 已经连接到服务器的sock编号
  281. * @retval 响应的返回值
  282. */
  283. int http_clientReadResponse_POST(int sock, uint8_t *pbuf, int *datlen)
  284. {
  285. int len, ret;
  286. int length = 0;
  287. //读取响应行
  288. len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
  289. if(len <= 0) return -1;
  290. HTTP_PRINTF("%s", (char *)httpRecvBuffer);
  291. ret = http_parseRequestLine(httpRecvBuffer);
  292. //读取响应头
  293. do
  294. {
  295. len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
  296. HTTP_PRINTF("%s", (char *)httpRecvBuffer);
  297. if(len <= 2)
  298. {
  299. if(len == 2) break; //读取到了空行
  300. else return -1;
  301. }
  302. }while(len > 0);
  303. //读取响应主体内容
  304. length = 0;
  305. do
  306. {
  307. len = recv(sock, httpRecvBuffer, sizeof(httpRecvBuffer), 0);
  308. if(len > 0)
  309. {
  310. memcpy(pbuf + length, httpRecvBuffer, len);
  311. length += len;
  312. }
  313. }while(len > 0);
  314. *datlen = length;
  315. return ret;
  316. }
  317. /*
  318. * @brief http_clientGet
  319. * @note 客户端发送GET请求
  320. * @param host: 服务器的域名或者IP地址
  321. * @param url: 访问服务器资源的位置
  322. * @param port: 服务器的端口号
  323. * @param hostIsIp: 第一个参数是域名还是ip地址 0:域名 1: ip地址
  324. * @param pbuf: 接收服务器响应的数据
  325. * @param datalen: 服务器响应数据的长度
  326. * @retval 负值: GET请求异常, 正值: http协议的返回码
  327. */
  328. int http_clientGet(char *host, char *url, uint16_t port, uint8_t hostIsIp, uint8_t *pbuf, int *datalen)
  329. {
  330. int sock = -1, ret;
  331. sock = http_clientConnectToServer(host, port, hostIsIp);
  332. if(sock < 0) goto __httpError;
  333. ret = http_clientPacketRequest_GET(sock, host, url);
  334. if(sock < 0) goto __httpError;
  335. ret = http_clientReadResponse_GET(sock, pbuf, datalen);
  336. if(sock < 0) goto __httpError;
  337. __httpError:
  338. if(sock >= 0)
  339. http_clientClose(sock);
  340. return ret;
  341. }
  342. /*
  343. * @brief http_clientGet
  344. * @note 客户端发送GET请求
  345. * @param host: 服务器的域名或者IP地址
  346. * @param url: 访问服务器资源的位置
  347. * @param port: 服务器的端口号
  348. * @param hostIsIp: 第一个参数是域名还是ip地址 0:域名 1: ip地址
  349. * @param postbuf: 需要发往服务器的数据
  350. * @param postlen: 发送数据的长度
  351. * @param rtnbuf: 接收服务器响应的数据
  352. * @param rtnlen: 服务器响应数据的长度
  353. * @retval 负值: GET请求异常, 正值: http协议的返回码
  354. */
  355. int http_clientPost(char *host, char *url, uint16_t port, uint8_t hostIsIp, uint8_t *postbuf, int postlen, uint8_t *rtnbuf, int *rtnlen)
  356. {
  357. int sock, ret;
  358. sock = http_clientConnectToServer(host, port, hostIsIp);
  359. if(sock < 0) goto __httpError;
  360. ret = http_clientPacketRequest_POST(sock, host, url, postlen);
  361. if(ret < 0) goto __httpError;
  362. ret = http_clientPacketBody_POST(sock, postbuf, postlen);
  363. if(ret < 0) goto __httpError;
  364. ret = http_clientReadResponse_POST(sock, rtnbuf, rtnlen);
  365. __httpError:
  366. if(sock >= 0)
  367. http_clientClose(sock);
  368. return ret;
  369. }