123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- #include "stdint.h"
- #include "stdio.h"
- #include "string.h"
- #include "lwip/opt.h"
- #include "lwip/arch.h"
- #include "lwip/api.h"
- #include "lwip/inet.h"
- #include "lwip/sockets.h"
- #include "lwip/dns.h"
- #include "httpclient.h"
- uint8_t httpSendBuffer[1024];
- uint8_t httpRecvBuffer[512];
- int http_getLine(int sock, uint8_t *buf, int size)
- {
- int i = 0;
- char c = '\0';
- int n;
-
- while((i < (size - 1)) && (c != '\n'))
- {
- n = recv(sock, &c, 1, 0);
- if(n <= 0) c = '\n';
- buf[i++] = c;
- }
- buf[i] = '\0';
-
- return i;
- }
- int http_parseRequestLine(uint8_t *pbuf)
- {
- int b, s, g;
-
- if((strncmp((char *)pbuf, "HTTP/1.1 ", strlen("HTTP/1.1 ")) == 0) || (strncmp((char *)pbuf, "http/1.1 ", strlen("http/1.1 ")) == 0))
- {
- pbuf += strlen("HTTP/1.1 ");
- b = pbuf[0] - '0';
- s = pbuf[1] - '0';
- g = pbuf[2] - '0';
- return (b * 100 + s * 10 + g);
- }
-
- return -1;
- }
- void http_dns_found(const char *name, ip_addr_t *host_ip, void *callback_arg)
- {
- *(ip_addr_t *)callback_arg = *host_ip;
- HTTP_PRINTF("%s:%s\r\n",name, ipaddr_ntoa(host_ip));
- }
- int http_clientConnectToServer(char *host, int port, int hostIsIp)
- {
- int timeout;
- struct sockaddr_in serverAddr;
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- if(sock < 0) return -2;
-
- if(hostIsIp == 0)
- {
- ip_addr_t addr;
-
- addr.addr = 0;
- dns_gethostbyname(host, &addr, http_dns_found, &addr);
-
- timeout = 0;
- while((addr.addr == 0) && (timeout < 2000))
- {
- vTaskDelay(100);
- timeout += 10;
- }
- if(timeout >= 2000)
- {
- HTTP_PRINTF(("dns get failure \n"));
- return -2;
- }
- serverAddr.sin_addr.s_addr = inet_addr(inet_ntoa(addr));
- }
- else serverAddr.sin_addr.s_addr = inet_addr((char*)host);
- serverAddr.sin_len = sizeof(serverAddr);
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(port);
- memset(&serverAddr.sin_zero, 0, sizeof(serverAddr.sin_zero));
-
-
- if(connect(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0)
- {
- HTTP_PRINTF("connect server error \r\n");
- return -1;
- }
-
-
- timeout = 3000;
- setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(int));
-
- HTTP_PRINTF("connect server success \r\n");
-
- return sock;
- }
- void http_clientClose(int sock)
- {
- close(sock);
- }
- int http_clientPacketRequest_GET(int sock, char *host, char *url)
- {
- int len;
-
- memset(httpSendBuffer, 0, sizeof(httpSendBuffer));
-
- sprintf((char *)httpSendBuffer, "GET ");
- if(url == NULL) strcat((char *)httpSendBuffer, "/");
- else strcat((char *)httpSendBuffer, url);
- strcat((char *)httpSendBuffer, " HTTP/1.1\r\n");
-
- strcat((char *)httpSendBuffer, "Host: ");
- strcat((char *)httpSendBuffer, host);
- strcat((char *)httpSendBuffer, "\r\n");
- strcat((char *)httpSendBuffer, "Connection: close\r\n");
- strcat((char *)httpSendBuffer, "Accept: application/json\r\n");
- strcat((char *)httpSendBuffer, "User-Agent: stm32f207\r\n");
- strcat((char *)httpSendBuffer, "Cache-Control: no-cache\r\n");
-
- strcat((char *)httpSendBuffer, "\r\n");
- len = strlen((char *)httpSendBuffer);
- HTTP_PRINTF("%s", (char *)httpSendBuffer);
-
-
- len = write(sock, httpSendBuffer, len);
- if(len <= 0)
- {
- return -1;
- }
-
- return 1;
- }
- int http_clientReadResponse_GET(int sock, uint8_t *pbuf, int *datlen)
- {
- int len, ret;
- int length = 0;
-
-
- len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
- if(len <= 0) return -1;
- HTTP_PRINTF("%s", (char *)httpRecvBuffer);
-
- ret = http_parseRequestLine(httpRecvBuffer);
-
-
- do
- {
- len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
- HTTP_PRINTF("%s", (char *)httpRecvBuffer);
- if(len <= 2)
- {
- if(len == 2) break;
- else return -1;
- }
- }while(len > 0);
-
-
- length = 0;
- do
- {
- len = recv(sock, httpRecvBuffer, sizeof(httpRecvBuffer), 0);
- if(len > 0)
- {
- memcpy(pbuf + length, httpRecvBuffer, len);
- length += len;
- }
- }while(len > 0);
-
- *datlen = length;
-
- return ret;
- }
- char httpTmpBuffer[64];
- int http_clientPacketRequest_POST(int sock, char *host, char *url, int datalen)
- {
- int len;
-
- memset(httpSendBuffer, 0, sizeof(httpSendBuffer));
-
- sprintf((char *)httpSendBuffer, "POST ");
- if(url == NULL) strcat((char *)httpSendBuffer, "/");
- else strcat((char *)httpSendBuffer, url);
- strcat((char *)httpSendBuffer, " HTTP/1.1\r\n");
-
-
- memset(httpTmpBuffer, 0, sizeof(httpTmpBuffer));
- sprintf(httpTmpBuffer, "Host: %s\r\n", host);
- strcat((char *)httpSendBuffer, httpTmpBuffer);
- strcat((char *)httpSendBuffer, "Connection: close\r\n");
- strcat((char *)httpSendBuffer, "Accept: application/json\r\n");
- strcat((char *)httpSendBuffer, "User-Agent: stm32f207\r\n");
- strcat((char *)httpSendBuffer, "Cache-Control: no-cache\r\n");
- strcat((char *)httpSendBuffer, "Content-Type: application/json\r\n");
- memset(httpTmpBuffer, 0, sizeof(httpTmpBuffer));
- sprintf(httpTmpBuffer, "Content-Length: %d\r\n", datalen);
- strcat((char *)httpSendBuffer, httpTmpBuffer);
-
- strcat((char *)httpSendBuffer, "\r\n");
-
- len = strlen((char *)httpSendBuffer);
- HTTP_PRINTF("%s", (char *)httpSendBuffer);
-
- len = write(sock, httpSendBuffer, len);
- if(len <= 0)
- {
- return -1;
- }
- return 1;
- }
- int http_clientPacketBody_POST(int sock, uint8_t *pbuf, int datalen)
- {
- int len;
-
- while(datalen > 1000)
- {
- len = write(sock, pbuf, 1000);
- if(len <= 0)
- {
- return -1;
- }
-
- datalen -= 1000;
- pbuf += 1000;
- }
-
- if(datalen > 0)
- {
- len = write(sock, pbuf, datalen);
- if(len <= 0)
- {
- return -1;
- }
- }
-
- return 1;
- }
- int http_clientReadResponse_POST(int sock, uint8_t *pbuf, int *datlen)
- {
- int len, ret;
- int length = 0;
-
-
- len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
- if(len <= 0) return -1;
- HTTP_PRINTF("%s", (char *)httpRecvBuffer);
-
- ret = http_parseRequestLine(httpRecvBuffer);
-
-
- do
- {
- len = http_getLine(sock, httpRecvBuffer, sizeof(httpRecvBuffer));
- HTTP_PRINTF("%s", (char *)httpRecvBuffer);
- if(len <= 2)
- {
- if(len == 2) break;
- else return -1;
- }
- }while(len > 0);
-
-
- length = 0;
- do
- {
- len = recv(sock, httpRecvBuffer, sizeof(httpRecvBuffer), 0);
- if(len > 0)
- {
- memcpy(pbuf + length, httpRecvBuffer, len);
- length += len;
- }
- }while(len > 0);
-
- *datlen = length;
-
- return ret;
- }
- int http_clientGet(char *host, char *url, uint16_t port, uint8_t hostIsIp, uint8_t *pbuf, int *datalen)
- {
- int sock = -1, ret;
-
- sock = http_clientConnectToServer(host, port, hostIsIp);
- if(sock < 0) goto __httpError;
-
- ret = http_clientPacketRequest_GET(sock, host, url);
- if(sock < 0) goto __httpError;
-
- ret = http_clientReadResponse_GET(sock, pbuf, datalen);
- if(sock < 0) goto __httpError;
- __httpError:
- if(sock >= 0)
- http_clientClose(sock);
- return ret;
- }
- int http_clientPost(char *host, char *url, uint16_t port, uint8_t hostIsIp, uint8_t *postbuf, int postlen, uint8_t *rtnbuf, int *rtnlen)
- {
- int sock, ret;
-
- sock = http_clientConnectToServer(host, port, hostIsIp);
- if(sock < 0) goto __httpError;
- ret = http_clientPacketRequest_POST(sock, host, url, postlen);
- if(ret < 0) goto __httpError;
-
- ret = http_clientPacketBody_POST(sock, postbuf, postlen);
- if(ret < 0) goto __httpError;
-
- ret = http_clientReadResponse_POST(sock, rtnbuf, rtnlen);
- __httpError:
- if(sock >= 0)
- http_clientClose(sock);
-
- return ret;
- }
|