|
@@ -15,11 +15,11 @@ uint8_t httpRecvBuffer[512];
|
|
|
|
|
|
|
|
|
|
|
|
- * @brief 浠巗ocket缂撳瓨涓��鍙栦竴琛宧ttp鏁版嵁
|
|
|
- * @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
- * @param buf: 淇濆瓨鏁版嵁鐨刡uffer
|
|
|
- * @param size: buf的最大可用长度
|
|
|
- * @retval 璇诲彇鍒扮殑鏁版嵁鐨勯暱搴︼紝鍖呮嫭涓や釜瀛楄妭鐨勬崲琛岀�
|
|
|
+ * @brief 从socket缓存中读取一行http数据
|
|
|
+ * @param sock: 已经连接到服务器的sock编号
|
|
|
+ * @param buf: 保存数据的buffer
|
|
|
+ * @param size: buf的最大可用长度
|
|
|
+ * @retval 读取到的数据的长度,包括两个字节的换行符
|
|
|
*/
|
|
|
int http_getLine(int sock, uint8_t *buf, int size)
|
|
|
{
|
|
@@ -35,13 +35,13 @@ int http_getLine(int sock, uint8_t *buf, int size)
|
|
|
}
|
|
|
buf[i] = '\0';
|
|
|
|
|
|
- return i;
|
|
|
+ return i;
|
|
|
}
|
|
|
|
|
|
|
|
|
- * @brief 解析http响应行
|
|
|
- * @param pbuf: 鍝嶅簲琛岀殑鏁版嵁
|
|
|
-* @retval 其他值: 返回http请求状态 -1: 解析失败
|
|
|
+ * @brief 解析http响应行
|
|
|
+ * @param pbuf: 响应行的数据
|
|
|
+* @retval 其他值: 返回http请求状态 -1: 解析失败
|
|
|
*/
|
|
|
int http_parseRequestLine(uint8_t *pbuf)
|
|
|
{
|
|
@@ -61,11 +61,11 @@ int http_parseRequestLine(uint8_t *pbuf)
|
|
|
}
|
|
|
|
|
|
|
|
|
- * @brief DNS瑙f瀽鍥炶皟鍑芥暟
|
|
|
- * @note 鍦ㄨВ鏋愬煙鍚嶆垚鍔熷悗锛屼細璋冪敤杩欎釜鍑芥暟锛岀劧鍚庡彲浠ヨ�鍙栧埌瀵瑰簲鐨処P鍦板潃
|
|
|
- * @param name: 鍩熷悕
|
|
|
- * @param host_ip: 鍩熷悕瀵瑰簲鐨刬p鍦板潃
|
|
|
- * @param callback_arg: 浼犻€掔殑鍙傛暟
|
|
|
+ * @brief DNS解析回调函数
|
|
|
+ * @note 在解析域名成功后,会调用这个函数,然后可以读取到对应的IP地址
|
|
|
+ * @param name: 域名
|
|
|
+ * @param host_ip: 域名对应的ip地址
|
|
|
+ * @param callback_arg: 传递的参数
|
|
|
* @retval None
|
|
|
*/
|
|
|
void http_dns_found(const char *name, ip_addr_t *host_ip, void *callback_arg)
|
|
@@ -75,12 +75,12 @@ void http_dns_found(const char *name, ip_addr_t *host_ip, void *callback_arg)
|
|
|
}
|
|
|
|
|
|
|
|
|
- * @brief 杩炴帴鍒癶ttp鏈嶅姟鍣ㄧ殑鍑芥暟
|
|
|
- * @note 连接到http服务器
|
|
|
- * @param host: 鏈嶅姟鍣ㄧ殑鍩熷悕鎴栬€卛p鍦板潃
|
|
|
- * @param port: 鏈嶅姟鍣ㄧ�鍙e彿
|
|
|
- * @param hostIsIp: host代表的是 域名,还是ip地址 0: host为域名 1: host为ip地址
|
|
|
- * @retval -1:连接服务器失败 -2: 域名解析失败 >=0: 连接成功,返回值为 sock编号
|
|
|
+ * @brief 连接到http服务器的函数
|
|
|
+ * @note 连接到http服务器
|
|
|
+ * @param host: 服务器的域名或者ip地址
|
|
|
+ * @param port: 服务器端口号
|
|
|
+ * @param hostIsIp: host代表的是 域名,还是ip地址 0: host为域名 1: host为ip地址
|
|
|
+ * @retval -1:连接服务器失败 -2: 域名解析失败 >=0: 连接成功,返回值为 sock编号
|
|
|
*/
|
|
|
int http_clientConnectToServer(char *host, int port, int hostIsIp)
|
|
|
{
|
|
@@ -89,14 +89,14 @@ int http_clientConnectToServer(char *host, int port, int hostIsIp)
|
|
|
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))
|
|
|
{
|
|
@@ -116,14 +116,14 @@ int http_clientConnectToServer(char *host, int port, int hostIsIp)
|
|
|
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));
|
|
|
|
|
@@ -133,8 +133,8 @@ int http_clientConnectToServer(char *host, int port, int hostIsIp)
|
|
|
}
|
|
|
|
|
|
|
|
|
- * @brief 鍏抽棴socket绔�彛
|
|
|
- * @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
+ * @brief 关闭socket端口
|
|
|
+ * @param sock: 已经连接到服务器的sock编号
|
|
|
*/
|
|
|
void http_clientClose(int sock)
|
|
|
{
|
|
@@ -143,24 +143,24 @@ void http_clientClose(int sock)
|
|
|
|
|
|
|
|
|
|
|
|
- * @brief 杩炴帴鍒板彂閫丟ET璇锋眰
|
|
|
- * @note 组装GET数据包,并将GET请求发送出去
|
|
|
- * @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
- * @param host: 鏈嶅姟鍣ㄧ殑鍩熷悕鎴栬€卛p鍦板潃
|
|
|
- * @param url: 璇锋眰璧勬簮鐨勫湴鍧€
|
|
|
- * @retval -1: 发送请求失败 1:发送成功
|
|
|
+ * @brief 连接到发送GET请求
|
|
|
+ * @note 组装GET数据包,并将GET请求发送出去
|
|
|
+ * @param sock: 已经连接到服务器的sock编号
|
|
|
+ * @param host: 服务器的域名或者ip地址
|
|
|
+ * @param url: 请求资源的地址
|
|
|
+ * @retval -1: 发送请求失败 1:发送成功
|
|
|
*/
|
|
|
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");
|
|
@@ -168,12 +168,12 @@ int http_clientPacketRequest_GET(int sock, char *host, char *url)
|
|
|
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)
|
|
|
{
|
|
@@ -185,35 +185,35 @@ int http_clientPacketRequest_GET(int sock, char *host, char *url)
|
|
|
|
|
|
|
|
|
* @brief http_clientReadResponse_GET
|
|
|
- * @note 绛夊緟鏈嶅姟鍣ㄨ繑鍥濭ET鍝嶅簲
|
|
|
- * @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
- * @retval -1: 发送请求失败 1:发送成功
|
|
|
+ * @note 等待服务器返回GET响应
|
|
|
+ * @param sock: 已经连接到服务器的sock编号
|
|
|
+ * @retval -1: 发送请求失败 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;
|
|
|
+ if(len == 2) break;
|
|
|
else return -1;
|
|
|
}
|
|
|
}while(len > 0);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
length = 0;
|
|
|
do
|
|
|
{
|
|
@@ -232,13 +232,13 @@ int http_clientReadResponse_GET(int sock, uint8_t *pbuf, int *datlen)
|
|
|
|
|
|
|
|
|
* @brief http_clientPacketRequest_POST
|
|
|
-* @note 缁勫缓鍜屽彂閫丳OST璇锋眰澶存暟鎹�寘
|
|
|
-* @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
-* @param host: 鏈嶅姟鍣ㄥ煙鍚嶆垨鑰匢P鍦板潃
|
|
|
-* @param url: 请求的资源位置
|
|
|
-* @param pbuf: 需要post的数据缓存
|
|
|
-* @param datalen 需要post的数据长度
|
|
|
-* @retval -1: 发送失败 1:发送成功
|
|
|
+* @note 组建和发送POST请求头数据包
|
|
|
+* @param sock: 已经连接到服务器的sock编号
|
|
|
+* @param host: 服务器域名或者IP地址
|
|
|
+* @param url: 请求的资源位置
|
|
|
+* @param pbuf: 需要post的数据缓存
|
|
|
+* @param datalen 需要post的数据长度
|
|
|
+* @retval -1: 发送失败 1:发送成功
|
|
|
*/
|
|
|
|
|
|
char httpTmpBuffer[64];
|
|
@@ -247,13 +247,13 @@ 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);
|
|
@@ -265,13 +265,13 @@ int http_clientPacketRequest_POST(int sock, char *host, char *url, int datalen)
|
|
|
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)
|
|
|
{
|
|
@@ -284,11 +284,11 @@ int http_clientPacketRequest_POST(int sock, char *host, char *url, int datalen)
|
|
|
|
|
|
|
|
|
* @brief http_clientPacketBody_POST
|
|
|
-* @note 鍙戦€乸ost鏁版嵁
|
|
|
-* @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
-* @param pbuf: 需要post的数据缓存
|
|
|
-* @param datalen 需要post的数据长度
|
|
|
-* @retval -1: 发送失败 1:发送成功
|
|
|
+* @note 发送post数据
|
|
|
+* @param sock: 已经连接到服务器的sock编号
|
|
|
+* @param pbuf: 需要post的数据缓存
|
|
|
+* @param datalen 需要post的数据长度
|
|
|
+* @retval -1: 发送失败 1:发送成功
|
|
|
*/
|
|
|
int http_clientPacketBody_POST(int sock, uint8_t *pbuf, int datalen)
|
|
|
{
|
|
@@ -320,35 +320,35 @@ int http_clientPacketBody_POST(int sock, uint8_t *pbuf, int datalen)
|
|
|
|
|
|
|
|
|
* @brief http_clientReadResponse_POST
|
|
|
-* @note 读取和解析POST返回的响应
|
|
|
-* @param sock: 宸茬粡杩炴帴鍒版湇鍔″櫒鐨剆ock缂栧彿
|
|
|
-* @retval 响应的返回值
|
|
|
+* @note 读取和解析POST返回的响应
|
|
|
+* @param sock: 已经连接到服务器的sock编号
|
|
|
+* @retval 响应的返回值
|
|
|
*/
|
|
|
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;
|
|
|
+ if(len == 2) break;
|
|
|
else return -1;
|
|
|
}
|
|
|
}while(len > 0);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
length = 0;
|
|
|
do
|
|
|
{
|
|
@@ -368,14 +368,14 @@ int http_clientReadResponse_POST(int sock, uint8_t *pbuf, int *datlen)
|
|
|
|
|
|
|
|
|
* @brief http_clientGet
|
|
|
-* @note 瀹㈡埛绔�彂閫丟ET璇锋眰
|
|
|
-* @param host: 鏈嶅姟鍣ㄧ殑鍩熷悕鎴栬€匢P鍦板潃
|
|
|
-* @param url: 璁块棶鏈嶅姟鍣ㄨ祫婧愮殑浣嶇疆
|
|
|
-* @param port: 服务器的端口号
|
|
|
-* @param hostIsIp: 绗�竴涓�弬鏁版槸鍩熷悕杩樻槸ip鍦板潃 0:鍩熷悕 1: ip鍦板潃
|
|
|
-* @param pbuf: 鎺ユ敹鏈嶅姟鍣ㄥ搷搴旂殑鏁版嵁
|
|
|
-* @param datalen: 鏈嶅姟鍣ㄥ搷搴旀暟鎹�殑闀垮害
|
|
|
-* @retval 负值: GET请求异常, 正值: http协议的返回码
|
|
|
+* @note 客户端发送GET请求
|
|
|
+* @param host: 服务器的域名或者IP地址
|
|
|
+* @param url: 访问服务器资源的位置
|
|
|
+* @param port: 服务器的端口号
|
|
|
+* @param hostIsIp: 第一个参数是域名还是ip地址 0:域名 1: ip地址
|
|
|
+* @param pbuf: 接收服务器响应的数据
|
|
|
+* @param datalen: 服务器响应数据的长度
|
|
|
+* @retval 负值: GET请求异常, 正值: http协议的返回码
|
|
|
*/
|
|
|
int http_clientGet(char *host, char *url, uint16_t port, uint8_t hostIsIp, uint8_t *pbuf, int *datalen)
|
|
|
{
|
|
@@ -399,16 +399,16 @@ __httpError:
|
|
|
|
|
|
|
|
|
* @brief http_clientGet
|
|
|
-* @note 瀹㈡埛绔�彂閫丟ET璇锋眰
|
|
|
-* @param host: 鏈嶅姟鍣ㄧ殑鍩熷悕鎴栬€匢P鍦板潃
|
|
|
-* @param url: 璁块棶鏈嶅姟鍣ㄨ祫婧愮殑浣嶇疆
|
|
|
-* @param port: 服务器的端口号
|
|
|
-* @param hostIsIp: 绗�竴涓�弬鏁版槸鍩熷悕杩樻槸ip鍦板潃 0:鍩熷悕 1: ip鍦板潃
|
|
|
-* @param postbuf: 闇€瑕佸彂寰€鏈嶅姟鍣ㄧ殑鏁版嵁
|
|
|
-* @param postlen: 鍙戦€佹暟鎹�殑闀垮害
|
|
|
-* @param rtnbuf: 鎺ユ敹鏈嶅姟鍣ㄥ搷搴旂殑鏁版嵁
|
|
|
-* @param rtnlen: 鏈嶅姟鍣ㄥ搷搴旀暟鎹�殑闀垮害
|
|
|
-* @retval 负值: GET请求异常, 正值: http协议的返回码
|
|
|
+* @note 客户端发送GET请求
|
|
|
+* @param host: 服务器的域名或者IP地址
|
|
|
+* @param url: 访问服务器资源的位置
|
|
|
+* @param port: 服务器的端口号
|
|
|
+* @param hostIsIp: 第一个参数是域名还是ip地址 0:域名 1: ip地址
|
|
|
+* @param postbuf: 需要发往服务器的数据
|
|
|
+* @param postlen: 发送数据的长度
|
|
|
+* @param rtnbuf: 接收服务器响应的数据
|
|
|
+* @param rtnlen: 服务器响应数据的长度
|
|
|
+* @retval 负值: GET请求异常, 正值: http协议的返回码
|
|
|
*/
|
|
|
int http_clientPost(char *host, char *url, uint16_t port, uint8_t hostIsIp, uint8_t *postbuf, int postlen, uint8_t *rtnbuf, int *rtnlen)
|
|
|
{
|