2022-11-10 16:55:14 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2013-05-05 Bernard the first version
|
|
|
|
* 2013-06-10 Bernard fix the slow speed issue when download file.
|
|
|
|
* 2015-11-14 aozima add content_length_remainder.
|
|
|
|
* 2017-12-23 aozima update gethostbyname to getaddrinfo.
|
|
|
|
* 2018-01-04 aozima add ipv6 address support.
|
|
|
|
* 2018-07-26 chenyong modify log information
|
|
|
|
* 2018-08-07 chenyong modify header processing
|
|
|
|
* 2021-06-09 xiangxistu add shard download function
|
|
|
|
*/
|
|
|
|
|
2022-11-12 15:49:50 +08:00
|
|
|
#include "webclient.h"
|
2022-11-10 17:06:54 +08:00
|
|
|
#include <ctype.h>
|
2022-11-10 16:55:14 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DBG_ENABLE
|
2022-11-10 17:06:54 +08:00
|
|
|
#define DBG_SECTION_NAME "web"
|
2022-11-10 16:55:14 +08:00
|
|
|
#ifdef WEBCLIENT_DEBUG
|
2022-11-10 17:06:54 +08:00
|
|
|
#define DBG_LEVEL DBG_LOG
|
2022-11-10 16:55:14 +08:00
|
|
|
#else
|
2022-11-10 17:06:54 +08:00
|
|
|
#define DBG_LEVEL DBG_INFO
|
2022-11-10 16:55:14 +08:00
|
|
|
#endif /* WEBCLIENT_DEBUG */
|
|
|
|
#define DBG_COLOR
|
|
|
|
// #include <rtdbg.h>
|
|
|
|
|
|
|
|
/* default receive or send timeout */
|
2022-11-10 17:06:54 +08:00
|
|
|
#define WEBCLIENT_DEFAULT_TIMEO 6
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
extern long int strtol(const char* nptr, char** endptr, int base);
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_strncasecmp(const char* a, const char* b, size_t n) {
|
2022-11-10 16:55:14 +08:00
|
|
|
uint8_t c1, c2;
|
|
|
|
if (n <= 0)
|
|
|
|
return 0;
|
|
|
|
do {
|
|
|
|
c1 = tolower(*a++);
|
|
|
|
c2 = tolower(*b++);
|
|
|
|
} while (--n && c1 && c1 == c2);
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static const char* webclient_strstri(const char* str, const char* subStr) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int len = strlen(subStr);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (len == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
while (*str) {
|
|
|
|
if (webclient_strncasecmp(str, subStr, len) == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_send(struct webclient_session* session,
|
|
|
|
const void* buffer,
|
|
|
|
size_t len,
|
|
|
|
int flag) {
|
2022-11-10 16:55:14 +08:00
|
|
|
#ifdef WEBCLIENT_USING_MBED_TLS
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return mbedtls_client_write(session->tls_session, buffer, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return __platform_send(session->socket, buffer, len, flag);
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_recv(struct webclient_session* session,
|
|
|
|
void* buffer,
|
|
|
|
size_t len,
|
|
|
|
int flag) {
|
2022-11-10 16:55:14 +08:00
|
|
|
#ifdef WEBCLIENT_USING_MBED_TLS
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return mbedtls_client_read(session->tls_session, buffer, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
return __platform_recv(session->socket, buffer, len, flag);
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_read_line(struct webclient_session* session,
|
|
|
|
char* buffer,
|
|
|
|
int size) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc, count = 0;
|
|
|
|
char ch = 0, last_ch = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(buffer);
|
|
|
|
|
|
|
|
/* Keep reading until we fill the buffer. */
|
2022-11-10 17:06:54 +08:00
|
|
|
while (count < size) {
|
|
|
|
rc = webclient_recv(session, (unsigned char*)&ch, 1, 0);
|
2022-11-10 16:55:14 +08:00
|
|
|
#if defined(WEBCLIENT_USING_MBED_TLS) || defined(WEBCLIENT_USING_SAL_TLS)
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->is_tls && (rc == MBEDTLS_ERR_SSL_WANT_READ ||
|
|
|
|
rc == MBEDTLS_ERR_SSL_WANT_WRITE)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (rc <= 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (ch == '\n' && last_ch == '\r')
|
|
|
|
break;
|
|
|
|
|
|
|
|
buffer[count++] = ch;
|
|
|
|
|
|
|
|
last_ch = ch;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (count > size) {
|
|
|
|
LOG_E(
|
|
|
|
"read line failed. The line data length is out of buffer size(%d)!",
|
|
|
|
count);
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* resolve server address
|
|
|
|
*
|
|
|
|
* @param session http session
|
|
|
|
* @param res the server address information
|
|
|
|
* @param url the input server URI address
|
|
|
|
* @param request the pointer to point the request url, for example, /index.html
|
|
|
|
*
|
|
|
|
* @return 0 on resolve server address OK, others failed
|
|
|
|
*
|
|
|
|
* URL example:
|
|
|
|
* http://www.rt-thread.org
|
|
|
|
* http://www.rt-thread.org:80
|
|
|
|
* https://www.rt-thread.org/
|
|
|
|
* http://192.168.1.1:80/index.htm
|
|
|
|
* http://[fe80::1]
|
|
|
|
* http://[fe80::1]/
|
|
|
|
* http://[fe80::1]/index.html
|
|
|
|
* http://[fe80::1]:80/index.html
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_resolve_address(struct webclient_session* session,
|
|
|
|
struct addrinfo** res,
|
|
|
|
const char* url,
|
|
|
|
const char** request) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
2022-11-10 17:06:54 +08:00
|
|
|
char* ptr;
|
2022-11-10 16:55:14 +08:00
|
|
|
char port_str[6] = "80"; /* default port of 80(http) */
|
2022-11-10 17:06:54 +08:00
|
|
|
const char* port_ptr;
|
|
|
|
const char* path_ptr;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
const char* host_addr = 0;
|
2022-11-10 16:55:14 +08:00
|
|
|
int url_len, host_addr_len = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(res);
|
|
|
|
RT_ASSERT(request);
|
|
|
|
|
|
|
|
/* make sure *res = NULL before getaddrinfo */
|
|
|
|
*res = RT_NULL;
|
|
|
|
url_len = strlen(url);
|
|
|
|
|
|
|
|
/* strip protocol(http or https) */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strncmp(url, "http://", 7) == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
host_addr = url + 7;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else if (strncmp(url, "https://", 8) == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
strncpy(port_str, "443", 4);
|
|
|
|
host_addr = url + 8;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ipv6 address */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (host_addr[0] == '[') {
|
2022-11-10 16:55:14 +08:00
|
|
|
host_addr += 1;
|
|
|
|
ptr = strstr(host_addr, "]");
|
2022-11-10 17:06:54 +08:00
|
|
|
if (!ptr) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
host_addr_len = ptr - host_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
path_ptr = strstr(host_addr, "/");
|
|
|
|
*request = path_ptr ? path_ptr : "/";
|
|
|
|
|
|
|
|
/* resolve port */
|
|
|
|
port_ptr = strstr(host_addr + host_addr_len, ":");
|
2022-11-10 17:06:54 +08:00
|
|
|
if (port_ptr && (!path_ptr || (port_ptr < path_ptr))) {
|
|
|
|
if (!path_ptr) {
|
2022-11-10 16:55:14 +08:00
|
|
|
strcpy(port_str, port_ptr + 1);
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
int port_len = path_ptr - port_ptr - 1;
|
|
|
|
strncpy(port_str, port_ptr + 1, port_len);
|
|
|
|
port_str[port_len] = '\0';
|
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
port_ptr = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (port_ptr && (!path_ptr)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
strcpy(port_str, port_ptr + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ipv4 or domain. */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (!host_addr_len) {
|
|
|
|
if (port_ptr) {
|
2022-11-10 16:55:14 +08:00
|
|
|
host_addr_len = port_ptr - host_addr;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else if (path_ptr) {
|
2022-11-10 16:55:14 +08:00
|
|
|
host_addr_len = path_ptr - host_addr;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
host_addr_len = strlen(host_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((host_addr_len < 1) || (host_addr_len > url_len)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get host address ok. */
|
|
|
|
{
|
2022-11-10 17:06:54 +08:00
|
|
|
char* host_addr_new = web_malloc(host_addr_len + 1);
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (!host_addr_new) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_memcpy(host_addr_new, host_addr, host_addr_len);
|
|
|
|
host_addr_new[host_addr_len] = '\0';
|
|
|
|
session->host = host_addr_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_D("host address: %s , port: %s", session->host, port_str);
|
|
|
|
|
|
|
|
#ifdef WEBCLIENT_USING_MBED_TLS
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
session->tls_session->port = web_strdup(port_str);
|
|
|
|
session->tls_session->host = web_strdup(session->host);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session->port == RT_NULL ||
|
|
|
|
session->tls_session->host == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* resolve the host name. */
|
|
|
|
{
|
|
|
|
struct addrinfo hint;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
web_memset(&hint, 0, sizeof(hint));
|
2022-11-12 13:55:15 +08:00
|
|
|
ret = __platform_getaddrinfo(session->host, port_str, &hint, res);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (ret != 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("getaddrinfo err: %d '%s'.", ret, session->host);
|
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
|
|
|
if (session->host) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(session->host);
|
|
|
|
session->host = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (*res) {
|
2022-11-12 13:55:15 +08:00
|
|
|
__platform_freeaddrinfo(*res);
|
2022-11-10 16:55:14 +08:00
|
|
|
*res = RT_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WEBCLIENT_USING_MBED_TLS
|
|
|
|
/**
|
|
|
|
* create and initialize https session.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
*
|
|
|
|
* @return <0: create failed, no memory or other errors
|
|
|
|
* =0: success
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_open_tls(struct webclient_session* session,
|
|
|
|
const char* URI) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int tls_ret = 0;
|
2022-11-10 17:06:54 +08:00
|
|
|
const char* pers = "webclient";
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
session->tls_session =
|
|
|
|
(MbedTLSSession*)web_calloc(1, sizeof(MbedTLSSession));
|
|
|
|
if (session->tls_session == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->tls_session->buffer_len = WEBCLIENT_RESPONSE_BUFSZ;
|
|
|
|
session->tls_session->buffer = web_malloc(session->tls_session->buffer_len);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session->buffer == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("no memory for tls_session buffer!");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((tls_ret = mbedtls_client_init(session->tls_session, (void*)pers,
|
|
|
|
strlen(pers))) < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("initialize https client failed return: -0x%x.", -tls_ret);
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WEBCLIENT_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* connect to http server.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI the input server URI address
|
|
|
|
*
|
|
|
|
* @return <0: connect failed or other error
|
|
|
|
* =0: connect success
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_connect(struct webclient_session* session,
|
|
|
|
const char* URI) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int socket_handle;
|
|
|
|
struct timeval timeout;
|
2022-11-10 17:06:54 +08:00
|
|
|
struct addrinfo* res = RT_NULL;
|
|
|
|
const char* req_url;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
|
|
|
|
timeout.tv_sec = WEBCLIENT_DEFAULT_TIMEO;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strncmp(URI, "https://", 8) == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
#if defined(WEBCLIENT_USING_SAL_TLS)
|
|
|
|
session->is_tls = RT_TRUE;
|
|
|
|
#elif defined(WEBCLIENT_USING_MBED_TLS)
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_open_tls(session, URI) < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("connect failed, https client open URI(%s) failed!", URI);
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
session->is_tls = RT_TRUE;
|
|
|
|
#else
|
2022-11-10 17:06:54 +08:00
|
|
|
LOG_E(
|
|
|
|
"not support https connect, please enable webclient https "
|
|
|
|
"configure!");
|
2022-11-24 23:28:37 +08:00
|
|
|
LOG_E("||url:%.1024s||", URI);
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check valid IP address and URL */
|
|
|
|
rc = webclient_resolve_address(session, &res, URI, &req_url);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("connect failed, resolve address error(%d).", rc);
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not use 'getaddrinfo()' for https connection */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->is_tls == RT_FALSE && res == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy host address */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (req_url) {
|
2022-11-10 16:55:14 +08:00
|
|
|
session->req_url = web_strdup(req_url);
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("connect failed, resolve request address error.");
|
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WEBCLIENT_USING_MBED_TLS
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int tls_ret = 0;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((tls_ret = mbedtls_client_context(session->tls_session)) < 0) {
|
|
|
|
LOG_E("connect failed, https client context return: -0x%x",
|
|
|
|
-tls_ret);
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((tls_ret = mbedtls_client_connect(session->tls_session)) < 0) {
|
|
|
|
LOG_E("connect failed, https client connect return: -0x%x",
|
|
|
|
-tls_ret);
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_CONNECT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket_handle = session->tls_session->server_fd.fd;
|
|
|
|
|
|
|
|
/* set recv timeout option */
|
2022-11-12 13:55:15 +08:00
|
|
|
__platform_setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(void*)&timeout, sizeof(timeout));
|
|
|
|
__platform_setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO,
|
|
|
|
(void*)&timeout, sizeof(timeout));
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
session->socket = socket_handle;
|
|
|
|
|
|
|
|
return WEBCLIENT_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef WEBCLIENT_USING_SAL_TLS
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->is_tls) {
|
|
|
|
socket_handle =
|
|
|
|
__platform_socket(res->ai_family, SOCK_STREAM, PROTOCOL_TLS);
|
|
|
|
} else {
|
|
|
|
socket_handle =
|
|
|
|
__platform_socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
#else
|
2022-11-10 17:06:54 +08:00
|
|
|
socket_handle =
|
|
|
|
__platform_socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
|
2022-11-10 16:55:14 +08:00
|
|
|
#endif
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (socket_handle < 0) {
|
|
|
|
LOG_E("connect failed, create __platform_socket(%d) error.",
|
|
|
|
socket_handle);
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_NOSOCKET;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set receive and send timeout option */
|
2022-11-12 13:55:15 +08:00
|
|
|
__platform_setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(void*)&timeout, sizeof(timeout));
|
|
|
|
__platform_setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO,
|
|
|
|
(void*)&timeout, sizeof(timeout));
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (__platform_connect(socket_handle, res->ai_addr, res->ai_addrlen) !=
|
|
|
|
0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* connect failed, close socket */
|
2022-11-10 17:06:54 +08:00
|
|
|
LOG_E("connect failed, connect __platform_socket(%d) error.",
|
|
|
|
socket_handle);
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(socket_handle);
|
|
|
|
rc = -WEBCLIENT_CONNECT_FAILED;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->socket = socket_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
2022-11-10 17:06:54 +08:00
|
|
|
if (res) {
|
2022-11-12 13:55:15 +08:00
|
|
|
__platform_freeaddrinfo(res);
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 21:05:05 +08:00
|
|
|
* FIXME: userful func
|
2022-11-10 16:55:14 +08:00
|
|
|
* add fields data to request header data.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param fmt fields format
|
|
|
|
*
|
|
|
|
* @return >0: data length of successfully added
|
|
|
|
* <0: not enough header buffer size
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_header_fields_add(struct webclient_session* session,
|
|
|
|
const char* fmt,
|
|
|
|
...) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rt_int32_t length;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(session->header->buffer);
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
length = web_vsnprintf(session->header->buffer + session->header->length,
|
2022-11-10 17:06:54 +08:00
|
|
|
session->header->size - session->header->length, fmt,
|
|
|
|
args);
|
2022-11-10 16:55:14 +08:00
|
|
|
va_end(args);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (length < 0) {
|
|
|
|
LOG_E("add fields header data failed, return length(%d) error.",
|
|
|
|
length);
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->header->length += length;
|
|
|
|
|
|
|
|
/* check header size */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->header->length >= session->header->size) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("not enough header buffer size(%d)!", session->header->size);
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get fields information from request/response header data.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param fields fields keyword
|
|
|
|
*
|
|
|
|
* @return = NULL: get fields data failed
|
|
|
|
* != NULL: success get fields data
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
const char* webclient_header_fields_get(struct webclient_session* session,
|
|
|
|
const char* fields) {
|
|
|
|
char* resp_buf = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
size_t resp_buf_len = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(session->header->buffer);
|
|
|
|
|
|
|
|
resp_buf = session->header->buffer;
|
2022-11-10 17:06:54 +08:00
|
|
|
while (resp_buf_len < session->header->length) {
|
|
|
|
if (webclient_strstri(resp_buf, fields) == resp_buf) {
|
|
|
|
char* mime_ptr = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
/* jump space */
|
|
|
|
mime_ptr = strstr(resp_buf, ":");
|
2022-11-10 17:06:54 +08:00
|
|
|
if (mime_ptr != NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
mime_ptr += 1;
|
|
|
|
|
|
|
|
while (*mime_ptr && (*mime_ptr == ' ' || *mime_ptr == '\t'))
|
|
|
|
mime_ptr++;
|
|
|
|
|
|
|
|
return mime_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*resp_buf == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
resp_buf += strlen(resp_buf) + 1;
|
|
|
|
resp_buf_len += strlen(resp_buf) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get http response status code.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
*
|
|
|
|
* @return response status code
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_resp_status_get(struct webclient_session* session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
return session->resp_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get http response data content length.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
*
|
|
|
|
* @return response content length
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_content_length_get(struct webclient_session* session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
return session->content_length;
|
|
|
|
}
|
|
|
|
|
2022-11-24 23:28:37 +08:00
|
|
|
static int webclient_send_header(struct webclient_session* session,
|
2022-11-10 17:06:54 +08:00
|
|
|
int method) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
2022-11-10 17:06:54 +08:00
|
|
|
char* header = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
header = session->header->buffer;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->header->length == 0 && method <= WEBCLIENT_GET) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* use default header data */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_header_fields_add(session, "GET %s HTTP/1.1\r\n",
|
|
|
|
session->req_url) < 0)
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_header_fields_add(session, "Host: %s\r\n",
|
|
|
|
session->host) < 0)
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_header_fields_add(
|
|
|
|
session, "User-Agent: RT-Thread HTTP Agent\r\n\r\n") < 0)
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
webclient_write(session, (unsigned char*)session->header->buffer,
|
|
|
|
session->header->length);
|
|
|
|
} else {
|
|
|
|
if (method != WEBCLIENT_USER_METHOD) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* check and add fields header data */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (web_memcmp(header, "HTTP/1.", strlen("HTTP/1."))) {
|
|
|
|
char* header_buffer = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
header_buffer = web_strdup(session->header->buffer);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (header_buffer == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("no memory for header buffer!");
|
|
|
|
rc = -WEBCLIENT_NOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* splice http request header data */
|
|
|
|
if (method == WEBCLIENT_GET)
|
2022-11-10 17:06:54 +08:00
|
|
|
length = web_snprintf(
|
|
|
|
session->header->buffer, session->header->size,
|
|
|
|
"GET %s HTTP/1.1\r\n%s",
|
|
|
|
session->req_url ? session->req_url : "/",
|
|
|
|
header_buffer);
|
2022-11-10 16:55:14 +08:00
|
|
|
else if (method == WEBCLIENT_POST)
|
2022-11-10 17:06:54 +08:00
|
|
|
length = web_snprintf(
|
|
|
|
session->header->buffer, session->header->size,
|
|
|
|
"POST %s HTTP/1.1\r\n%s",
|
|
|
|
session->req_url ? session->req_url : "/",
|
|
|
|
header_buffer);
|
2022-11-10 16:55:14 +08:00
|
|
|
else if (method == WEBCLIENT_HEAD)
|
2022-11-10 17:06:54 +08:00
|
|
|
length = web_snprintf(
|
|
|
|
session->header->buffer, session->header->size,
|
|
|
|
"HEAD %s HTTP/1.1\r\n%s",
|
|
|
|
session->req_url ? session->req_url : "/",
|
|
|
|
header_buffer);
|
2022-11-10 16:55:14 +08:00
|
|
|
session->header->length = length;
|
|
|
|
|
|
|
|
web_free(header_buffer);
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strstr(header, "Host:") == RT_NULL) {
|
|
|
|
if (webclient_header_fields_add(session, "Host: %s\r\n",
|
|
|
|
session->host) < 0)
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strstr(header, "User-Agent:") == RT_NULL) {
|
|
|
|
if (webclient_header_fields_add(
|
|
|
|
session, "User-Agent: RT-Thread HTTP Agent\r\n") < 0)
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strstr(header, "Accept:") == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
if (webclient_header_fields_add(session, "Accept: */*\r\n") < 0)
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* header data end */
|
2022-11-10 17:06:54 +08:00
|
|
|
web_snprintf(session->header->buffer + session->header->length,
|
|
|
|
session->header->size - session->header->length,
|
|
|
|
"\r\n");
|
2022-11-10 16:55:14 +08:00
|
|
|
session->header->length += 2;
|
|
|
|
|
|
|
|
/* check header size */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->header->length > session->header->size) {
|
|
|
|
LOG_E("send header failed, not enough header buffer size(%d)!",
|
|
|
|
session->header->size);
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_NOBUFFER;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
webclient_write(session, (unsigned char*)session->header->buffer,
|
|
|
|
session->header->length);
|
|
|
|
} else {
|
|
|
|
webclient_write(session, (unsigned char*)session->header->buffer,
|
|
|
|
session->header->length);
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get and echo request header data */
|
|
|
|
{
|
|
|
|
char *header_str, *header_ptr;
|
|
|
|
int header_line_len;
|
|
|
|
LOG_D("request header:");
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
for (header_str = session->header->buffer;
|
|
|
|
(header_ptr = strstr(header_str, "\r\n")) != RT_NULL;) {
|
2022-11-10 16:55:14 +08:00
|
|
|
header_line_len = header_ptr - header_str;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (header_line_len > 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_D("%.*s", header_line_len, header_str);
|
|
|
|
}
|
|
|
|
header_str = header_ptr + strlen("\r\n");
|
|
|
|
}
|
|
|
|
#ifdef WEBCLIENT_DEBUG
|
|
|
|
LOG_RAW("\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-11-22 21:05:05 +08:00
|
|
|
/* FIXME: 定制版发送响应头 */
|
2022-11-24 23:28:37 +08:00
|
|
|
static int webclient_send_header2(struct webclient_session* session,
|
2023-07-09 23:12:21 +08:00
|
|
|
int method) {
|
2022-11-22 21:05:05 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
char* header = RT_NULL;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
header = session->header->buffer;
|
|
|
|
|
|
|
|
if (strstr(header, "Host:") == RT_NULL) {
|
|
|
|
if (webclient_header_fields_add(session, "Host: %s\r\n",
|
|
|
|
session->host) < 0)
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(header, "User-Agent:") == RT_NULL) {
|
|
|
|
if (webclient_header_fields_add(
|
|
|
|
session, "User-Agent: PikaScript HTTP Agent\r\n") < 0)
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(header, "Accept:") == RT_NULL) {
|
|
|
|
if (webclient_header_fields_add(session, "Accept: */*\r\n") < 0)
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* header data end */
|
|
|
|
web_snprintf(session->header->buffer + session->header->length,
|
2023-07-09 23:12:21 +08:00
|
|
|
session->header->size - session->header->length, "\r\n");
|
2022-11-22 21:05:05 +08:00
|
|
|
session->header->length += 2;
|
|
|
|
|
|
|
|
/* check header size */
|
|
|
|
if (session->header->length > session->header->size) {
|
|
|
|
LOG_E("send header failed, not enough header buffer size(%d)!",
|
2023-07-09 23:12:21 +08:00
|
|
|
session->header->size);
|
2022-11-22 21:05:05 +08:00
|
|
|
rc = -WEBCLIENT_NOBUFFER;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
webclient_write(session, (unsigned char*)session->header->buffer,
|
|
|
|
session->header->length);
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:55:14 +08:00
|
|
|
/**
|
|
|
|
* resolve server response data.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
*
|
|
|
|
* @return <0: resolve response data failed
|
|
|
|
* =0: success
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_handle_response(struct webclient_session* session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
2022-11-10 17:06:54 +08:00
|
|
|
char* mime_buffer = RT_NULL;
|
|
|
|
char* mime_ptr = RT_NULL;
|
|
|
|
const char* transfer_encoding;
|
2022-11-10 16:55:14 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
/* clean header buffer and size */
|
|
|
|
web_memset(session->header->buffer, 0x00, session->header->size);
|
|
|
|
session->header->length = 0;
|
|
|
|
|
|
|
|
LOG_D("response header:");
|
|
|
|
/* We now need to read the header information */
|
2022-11-10 17:06:54 +08:00
|
|
|
while (1) {
|
2022-11-10 16:55:14 +08:00
|
|
|
mime_buffer = session->header->buffer + session->header->length;
|
|
|
|
|
|
|
|
/* read a line from the header information. */
|
2022-11-10 17:06:54 +08:00
|
|
|
rc = webclient_read_line(
|
|
|
|
session, mime_buffer,
|
|
|
|
session->header->size - session->header->length);
|
2022-11-10 16:55:14 +08:00
|
|
|
if (rc < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* End of headers is a blank line. exit. */
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((rc == 1) && (mime_buffer[0] == '\r')) {
|
2022-11-10 16:55:14 +08:00
|
|
|
mime_buffer[0] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set terminal charater */
|
|
|
|
mime_buffer[rc - 1] = '\0';
|
|
|
|
|
|
|
|
/* echo response header data */
|
|
|
|
LOG_D("%s", mime_buffer);
|
|
|
|
|
|
|
|
session->header->length += rc;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->header->length >= session->header->size) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("not enough header buffer size(%d)!", session->header->size);
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get HTTP status code */
|
|
|
|
mime_ptr = web_strdup(session->header->buffer);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (mime_ptr == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("no memory for get http status code buffer!");
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strstr(mime_ptr, "HTTP/1.")) {
|
|
|
|
char* ptr = mime_ptr;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
ptr += strlen("HTTP/1.x");
|
|
|
|
|
|
|
|
while (*ptr && (*ptr == ' ' || *ptr == '\t'))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/* Terminate string after status code */
|
2022-11-10 17:06:54 +08:00
|
|
|
for (i = 0; ((ptr[i] != ' ') && (ptr[i] != '\t')); i++)
|
|
|
|
;
|
2022-11-10 16:55:14 +08:00
|
|
|
ptr[i] = '\0';
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
session->resp_status = (int)strtol(ptr, RT_NULL, 10);
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get content length */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_header_fields_get(session, "Content-Length") != RT_NULL) {
|
|
|
|
session->content_length =
|
|
|
|
atoi(webclient_header_fields_get(session, "Content-Length"));
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
session->content_remainder =
|
|
|
|
session->content_length ? (size_t)session->content_length : 0xFFFFFFFF;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
transfer_encoding =
|
|
|
|
webclient_header_fields_get(session, "Transfer-Encoding");
|
|
|
|
if (transfer_encoding && strcmp(transfer_encoding, "chunked") == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rt_uint16_t len = session->header->size;
|
2022-11-10 17:06:54 +08:00
|
|
|
char* line = rt_malloc(len);
|
2022-11-10 16:55:14 +08:00
|
|
|
/* chunk mode, we should get the first chunk size */
|
|
|
|
webclient_read_line(session, line, len);
|
|
|
|
session->chunk_sz = strtol(line, RT_NULL, len);
|
|
|
|
session->chunk_offset = 0;
|
|
|
|
rt_free(line);
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (mime_ptr) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(mime_ptr);
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session->resp_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 21:05:05 +08:00
|
|
|
* FIXME: userful func
|
2022-11-10 16:55:14 +08:00
|
|
|
* create webclient session, set maximum header and response size
|
|
|
|
*
|
|
|
|
* @param header_sz maximum send header size
|
|
|
|
* @param resp_sz maximum response data size
|
|
|
|
*
|
|
|
|
* @return webclient session structure
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
struct webclient_session* webclient_session_create(size_t header_sz) {
|
|
|
|
struct webclient_session* session;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
/* create session */
|
2022-11-10 17:06:54 +08:00
|
|
|
session = (struct webclient_session*)web_calloc(
|
|
|
|
1, sizeof(struct webclient_session));
|
|
|
|
if (session == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("webclient create failed, no memory for webclient session!");
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the socket of session */
|
|
|
|
session->socket = -1;
|
|
|
|
session->content_length = -1;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
session->header = (struct webclient_header*)web_calloc(
|
|
|
|
1, sizeof(struct webclient_header));
|
|
|
|
if (session->header == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("webclient create failed, no memory for session header!");
|
|
|
|
web_free(session);
|
|
|
|
session = RT_NULL;
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->header->size = header_sz;
|
2022-11-10 17:06:54 +08:00
|
|
|
session->header->buffer = (char*)web_calloc(1, header_sz);
|
|
|
|
if (session->header->buffer == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("webclient create failed, no memory for session header buffer!");
|
|
|
|
web_free(session->header);
|
|
|
|
web_free(session);
|
|
|
|
session = RT_NULL;
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_clean(struct webclient_session* session);
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* send GET request to http server and get response header.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
* @param header GET request header
|
|
|
|
* = NULL: use default header data
|
|
|
|
* != NULL: use custom header data
|
|
|
|
*
|
|
|
|
* @return <0: send GET request failed
|
|
|
|
* >0: response http status code
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_get(struct webclient_session* session, const char* URI) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int resp_status = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
rc = webclient_connect(session, URI);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* connect to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = webclient_send_header(session, WEBCLIENT_GET);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* send header to webclient server failed. */
|
2022-11-10 17:06:54 +08:00
|
|
|
return rc;
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the response header of webclient server */
|
|
|
|
resp_status = webclient_handle_response(session);
|
|
|
|
|
|
|
|
LOG_D("get position handle response(%d).", resp_status);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (resp_status > 0) {
|
|
|
|
const char* location = webclient_header_fields_get(session, "Location");
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
/* relocation */
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((resp_status == 302 || resp_status == 301) && location) {
|
|
|
|
char* new_url;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
new_url = web_strdup(location);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (new_url == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean webclient session */
|
|
|
|
webclient_clean(session);
|
|
|
|
/* clean webclient session header */
|
|
|
|
session->header->length = 0;
|
|
|
|
web_memset(session->header->buffer, 0, session->header->size);
|
|
|
|
|
|
|
|
rc = webclient_get(session, new_url);
|
|
|
|
|
|
|
|
web_free(new_url);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp_status;
|
|
|
|
}
|
|
|
|
|
2022-11-24 23:28:37 +08:00
|
|
|
/**
|
|
|
|
* FIXME: better interface
|
|
|
|
* send GET request to http server and get response header.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
* @param header GET request header
|
|
|
|
* = NULL: use default header data
|
|
|
|
* != NULL: use custom header data
|
|
|
|
*
|
|
|
|
* @return <0: send GET request failed
|
|
|
|
* >0: response http status code
|
|
|
|
*/
|
|
|
|
int webclient_get2(struct webclient_session* session, const char* URI) {
|
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int resp_status = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
2022-12-04 00:37:45 +08:00
|
|
|
/* LOG_E("||url:%.1024s||", URI); */
|
2022-11-24 23:28:37 +08:00
|
|
|
rc = webclient_connect(session, URI);
|
|
|
|
if (rc != WEBCLIENT_OK) {
|
|
|
|
/* connect to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = webclient_send_header2(session, WEBCLIENT_GET);
|
|
|
|
if (rc != WEBCLIENT_OK) {
|
|
|
|
/* send header to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the response header of webclient server */
|
|
|
|
resp_status = webclient_handle_response(session);
|
|
|
|
|
|
|
|
LOG_D("get position handle response(%d).", resp_status);
|
|
|
|
|
|
|
|
if (resp_status > 0) {
|
|
|
|
const char* location = webclient_header_fields_get(session, "Location");
|
|
|
|
|
|
|
|
/* relocation */
|
|
|
|
if ((resp_status == 302 || resp_status == 301) && location) {
|
|
|
|
char* new_url;
|
|
|
|
|
|
|
|
new_url = web_strdup(location);
|
|
|
|
if (new_url == RT_NULL) {
|
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean webclient session */
|
|
|
|
webclient_clean(session);
|
|
|
|
/* clean webclient session header */
|
|
|
|
session->header->length = 0;
|
|
|
|
web_memset(session->header->buffer, 0, session->header->size);
|
|
|
|
|
|
|
|
rc = webclient_get(session, new_url);
|
|
|
|
|
|
|
|
web_free(new_url);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp_status;
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:55:14 +08:00
|
|
|
/**
|
|
|
|
* register a handle function for http breakpoint resume and shard download.
|
|
|
|
*
|
|
|
|
* @param function
|
|
|
|
*
|
|
|
|
* @return the pointer
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int* webclient_register_shard_position_function(
|
|
|
|
struct webclient_session* session,
|
|
|
|
int (*handle_function)(char* buffer, int size)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
session->handle_function = handle_function;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
return (int*)session->handle_function;
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* http breakpoint resume and shard download.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
* @param length the length of point
|
|
|
|
*
|
|
|
|
* @return <0: send GET request failed
|
|
|
|
* >0: response http status code
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_shard_head_function(struct webclient_session* session,
|
|
|
|
const char* URI,
|
|
|
|
int* length) {
|
2022-11-10 16:55:14 +08:00
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
|
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int resp_status = 0;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->socket == -1) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = webclient_connect(session, URI);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean header buffer and size */
|
|
|
|
web_memset(session->header->buffer, 0x00, session->header->size);
|
|
|
|
session->header->length = 0;
|
|
|
|
|
|
|
|
rc = webclient_send_header(session, WEBCLIENT_HEAD);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the response header of webclient server by HEAD request */
|
|
|
|
resp_status = webclient_handle_response(session);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (resp_status >= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
*length = webclient_content_length_get(session);
|
|
|
|
LOG_D("The length[%04d] of real data of URI.", *length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* http breakpoint resume and shard download.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
* @param start the position of you want to receive
|
|
|
|
* @param length the length of data length from "webclient_shard_head_function"
|
|
|
|
* @param mem_size the buffer size that you alloc
|
|
|
|
*
|
|
|
|
* @return <0: send GET request failed
|
|
|
|
* >0: response http status code
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_shard_position_function(struct webclient_session* session,
|
|
|
|
const char* URI,
|
|
|
|
int start,
|
|
|
|
int length,
|
|
|
|
int mem_size) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int result = RT_EOK;
|
|
|
|
int resp_status = 0;
|
|
|
|
size_t resp_len = 0;
|
2022-11-10 17:06:54 +08:00
|
|
|
char* buffer = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
int start_position, end_position = 0;
|
|
|
|
int total_len = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
RT_ASSERT(mem_size);
|
|
|
|
|
|
|
|
/* set the offset of "Range" and "total_len" */
|
|
|
|
end_position = start;
|
|
|
|
total_len = start + length;
|
|
|
|
|
|
|
|
/* clean header buffer and size */
|
|
|
|
web_memset(session->header->buffer, 0x00, session->header->size);
|
|
|
|
session->header->length = 0;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
for (start_position = end_position; total_len != end_position + 1;) {
|
2022-11-10 16:55:14 +08:00
|
|
|
RT_ASSERT(start_position <= total_len);
|
|
|
|
int data_len = 0;
|
|
|
|
|
|
|
|
end_position = start_position + mem_size - 1;
|
2022-11-10 17:06:54 +08:00
|
|
|
if (end_position >= total_len) {
|
2022-11-10 16:55:14 +08:00
|
|
|
end_position = total_len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* splice header and send header */
|
|
|
|
LOG_D("Range: [%04d -> %04d]", start_position, end_position);
|
2022-11-10 17:06:54 +08:00
|
|
|
webclient_header_fields_add(session, "Range: bytes=%d-%d\r\n",
|
|
|
|
start_position, end_position);
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = webclient_send_header(session, WEBCLIENT_GET);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle the response header of webclient server */
|
|
|
|
resp_status = webclient_handle_response(session);
|
|
|
|
LOG_D("get position handle response(%d).", resp_status);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (resp_status == 206) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* normal resp_status */
|
2022-11-10 17:06:54 +08:00
|
|
|
} else if (resp_status > 0) {
|
|
|
|
const char* location =
|
|
|
|
webclient_header_fields_get(session, "Location");
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
/* relocation */
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((resp_status == 302 || resp_status == 301) && location) {
|
|
|
|
char* new_url;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
new_url = web_strdup(location);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (new_url == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean webclient session */
|
|
|
|
webclient_clean(session);
|
|
|
|
/* clean webclient session header */
|
|
|
|
session->header->length = 0;
|
|
|
|
web_memset(session->header->buffer, 0, session->header->size);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
rc = webclient_shard_position_function(session, new_url, start,
|
|
|
|
length, mem_size);
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
web_free(new_url);
|
|
|
|
return rc;
|
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
|
|
|
if (resp_status == -WEBCLIENT_ERROR) {
|
|
|
|
if (session->socket == -WEBCLIENT_ERROR) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* clean webclient session */
|
|
|
|
webclient_clean(session);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_connect(session, URI) == WEBCLIENT_OK) {
|
|
|
|
LOG_D("webclient reconnect success, retry at [%06d]",
|
|
|
|
end_position);
|
2022-11-10 16:55:14 +08:00
|
|
|
end_position = start_position;
|
|
|
|
continue;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
|
|
|
LOG_E(
|
|
|
|
"webclient reconnect failed. Please retry by "
|
|
|
|
"yourself.");
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* receive the incoming data */
|
2022-11-10 17:06:54 +08:00
|
|
|
data_len = webclient_response(session, (void**)&buffer, &resp_len);
|
|
|
|
if (data_len > 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
start_position += data_len;
|
|
|
|
result = session->handle_function(buffer, data_len);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (result != RT_EOK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* clean webclient session */
|
|
|
|
webclient_clean(session);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->socket == -WEBCLIENT_ERROR) {
|
2022-11-10 16:55:14 +08:00
|
|
|
webclient_connect(session, URI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean header buffer and size */
|
|
|
|
web_memset(session->header->buffer, 0x00, session->header->size);
|
|
|
|
session->header->length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* send POST request to server and get response header data.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
* @param post_data data send to the server
|
|
|
|
* = NULL: just connect server and send header
|
|
|
|
* != NULL: send header and body data, resolve response data
|
|
|
|
* @param data_len the length of send data
|
|
|
|
*
|
|
|
|
* @return <0: send POST request failed
|
|
|
|
* =0: send POST header success
|
|
|
|
* >0: response http status code
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_post(struct webclient_session* session,
|
|
|
|
const char* URI,
|
|
|
|
const void* post_data,
|
|
|
|
size_t data_len) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int resp_status = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((post_data != RT_NULL) && (data_len == 0)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("input post data length failed");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = webclient_connect(session, URI);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* connect to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = webclient_send_header(session, WEBCLIENT_POST);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc != WEBCLIENT_OK) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* send header to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (post_data && (data_len > 0)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
webclient_write(session, post_data, data_len);
|
|
|
|
|
|
|
|
/* resolve response data, get http status code */
|
|
|
|
resp_status = webclient_handle_response(session);
|
|
|
|
LOG_D("post handle response(%d).", resp_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp_status;
|
|
|
|
}
|
|
|
|
|
2022-11-24 23:28:37 +08:00
|
|
|
/**
|
|
|
|
* FIXME: better interface
|
|
|
|
* send POST request to server and get response header data.
|
|
|
|
*
|
|
|
|
* @param session webclient session
|
|
|
|
* @param URI input server URI address
|
|
|
|
* @param post_data data send to the server
|
|
|
|
* = NULL: just connect server and send header
|
|
|
|
* != NULL: send header and body data, resolve response data
|
|
|
|
* @param data_len the length of send data
|
|
|
|
*
|
|
|
|
* @return <0: send POST request failed
|
|
|
|
* =0: send POST header success
|
|
|
|
* >0: response http status code
|
|
|
|
*/
|
|
|
|
int webclient_post2(struct webclient_session* session,
|
2023-07-09 23:12:21 +08:00
|
|
|
const char* URI,
|
|
|
|
const void* post_data,
|
|
|
|
size_t data_len) {
|
2022-11-24 23:28:37 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int resp_status = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
|
|
|
|
if ((post_data != RT_NULL) && (data_len == 0)) {
|
|
|
|
LOG_E("input post data length failed");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = webclient_connect(session, URI);
|
|
|
|
if (rc != WEBCLIENT_OK) {
|
|
|
|
/* connect to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = webclient_send_header2(session, WEBCLIENT_POST);
|
|
|
|
if (rc != WEBCLIENT_OK) {
|
|
|
|
/* send header to webclient server failed. */
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post_data && (data_len > 0)) {
|
|
|
|
webclient_write(session, post_data, data_len);
|
|
|
|
|
|
|
|
/* resolve response data, get http status code */
|
|
|
|
resp_status = webclient_handle_response(session);
|
|
|
|
LOG_D("post handle response(%d).", resp_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp_status;
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:55:14 +08:00
|
|
|
/**
|
|
|
|
* set receive and send data timeout.
|
|
|
|
*
|
|
|
|
* @param session http session
|
|
|
|
* @param millisecond timeout millisecond
|
|
|
|
*
|
|
|
|
* @return 0: set timeout success
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_set_timeout(struct webclient_session* session, int millisecond) {
|
2022-11-10 16:55:14 +08:00
|
|
|
struct timeval timeout;
|
|
|
|
int second = rt_tick_from_millisecond(millisecond) / 1000;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
timeout.tv_sec = second;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
/* set recv timeout option */
|
2022-11-12 13:55:15 +08:00
|
|
|
__platform_setsockopt(session->socket, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(void*)&timeout, sizeof(timeout));
|
|
|
|
__platform_setsockopt(session->socket, SOL_SOCKET, SO_SNDTIMEO,
|
|
|
|
(void*)&timeout, sizeof(timeout));
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_next_chunk(struct webclient_session* session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
char line[64];
|
|
|
|
int length;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
web_memset(line, 0x00, sizeof(line));
|
|
|
|
length = webclient_read_line(session, line, sizeof(line));
|
2022-11-10 17:06:54 +08:00
|
|
|
if (length > 0) {
|
|
|
|
if (strcmp(line, "\r") == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
length = webclient_read_line(session, line, sizeof(line));
|
2022-11-10 17:06:54 +08:00
|
|
|
if (length <= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->chunk_sz = strtol(line, RT_NULL, 16);
|
|
|
|
session->chunk_offset = 0;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->chunk_sz == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* end of chunks */
|
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
session->chunk_sz = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session->chunk_sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read data from http server.
|
|
|
|
*
|
|
|
|
* @param session http session
|
|
|
|
* @param buffer read buffer
|
|
|
|
* @param length the maximum of read buffer size
|
|
|
|
*
|
|
|
|
* @return <0: read data error
|
|
|
|
* =0: http server disconnect
|
|
|
|
* >0: successfully read data length
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_read(struct webclient_session* session,
|
|
|
|
void* buffer,
|
|
|
|
size_t length) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int bytes_read = 0;
|
|
|
|
int total_read = 0;
|
|
|
|
int left;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
/* get next chunk size is zero, client is already closed, return zero */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->chunk_sz < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->socket < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_DISCONNECT;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (length == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* which is transfered as chunk mode */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->chunk_sz) {
|
|
|
|
if ((int)length > (session->chunk_sz - session->chunk_offset)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
length = session->chunk_sz - session->chunk_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes_read = webclient_recv(session, buffer, length, 0);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (bytes_read <= 0) {
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* recv timeout */
|
|
|
|
return -WEBCLIENT_TIMEOUT;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
session->chunk_offset += bytes_read;
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->chunk_offset >= session->chunk_sz) {
|
2022-11-10 16:55:14 +08:00
|
|
|
webclient_next_chunk(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->content_length > 0) {
|
|
|
|
if (length > session->content_remainder) {
|
2022-11-10 16:55:14 +08:00
|
|
|
length = session->content_remainder;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (length == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read until: there is an error, we've read "size" bytes or the remote
|
|
|
|
* side has closed the connection.
|
|
|
|
*/
|
|
|
|
left = length;
|
2022-11-10 17:06:54 +08:00
|
|
|
do {
|
|
|
|
bytes_read = webclient_recv(
|
|
|
|
session, (void*)((char*)buffer + total_read), left, 0);
|
|
|
|
if (bytes_read <= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
#if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS)
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->is_tls && (bytes_read == MBEDTLS_ERR_SSL_WANT_READ ||
|
|
|
|
bytes_read == MBEDTLS_ERR_SSL_WANT_WRITE)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
LOG_D("receive data error(%d).", bytes_read);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (total_read) {
|
2022-11-10 16:55:14 +08:00
|
|
|
break;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* recv timeout */
|
|
|
|
LOG_E("receive data timeout.");
|
|
|
|
return -WEBCLIENT_TIMEOUT;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= bytes_read;
|
|
|
|
total_read += bytes_read;
|
2022-11-10 17:06:54 +08:00
|
|
|
} while (left);
|
2022-11-10 16:55:14 +08:00
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->content_length > 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
session->content_remainder -= total_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write data to http server.
|
|
|
|
*
|
|
|
|
* @param session http session
|
|
|
|
* @param buffer write buffer
|
|
|
|
* @param length write buffer size
|
|
|
|
*
|
|
|
|
* @return <0: write data error
|
|
|
|
* =0: http server disconnect
|
|
|
|
* >0: successfully write data length
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_write(struct webclient_session* session,
|
|
|
|
const void* buffer,
|
|
|
|
size_t length) {
|
2022-11-10 16:55:14 +08:00
|
|
|
int bytes_write = 0;
|
|
|
|
int total_write = 0;
|
|
|
|
int left = length;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->socket < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_DISCONNECT;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (length == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send all of data on the buffer. */
|
2022-11-10 17:06:54 +08:00
|
|
|
do {
|
|
|
|
bytes_write = webclient_send(
|
|
|
|
session, (void*)((char*)buffer + total_write), left, 0);
|
|
|
|
if (bytes_write <= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
#if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS)
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->is_tls &&
|
|
|
|
(bytes_write == MBEDTLS_ERR_SSL_WANT_READ ||
|
|
|
|
bytes_write == MBEDTLS_ERR_SSL_WANT_WRITE)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-10 17:06:54 +08:00
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* send timeout */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (total_write) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return total_write;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
/* TODO: whether return the TIMEOUT
|
|
|
|
* return -WEBCLIENT_TIMEOUT; */
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (total_write == 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_DISCONNECT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= bytes_write;
|
|
|
|
total_write += bytes_write;
|
2022-11-10 17:06:54 +08:00
|
|
|
} while (left);
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
return total_write;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close session socket, free host and request url */
|
2022-11-10 17:06:54 +08:00
|
|
|
static int webclient_clean(struct webclient_session* session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
#ifdef WEBCLIENT_USING_MBED_TLS
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->tls_session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
mbedtls_client_close(session->tls_session);
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
|
|
|
if (session->socket >= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->socket >= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
__platform_close(session->socket);
|
|
|
|
session->socket = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->host) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(session->host);
|
|
|
|
session->host = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->req_url) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(session->req_url);
|
|
|
|
session->req_url = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->content_length = -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* close a webclient client session.
|
|
|
|
*
|
|
|
|
* @param session http client session
|
|
|
|
*
|
|
|
|
* @return 0: close success
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_close(struct webclient_session* session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
RT_ASSERT(session);
|
|
|
|
|
|
|
|
webclient_clean(session);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->header && session->header->buffer) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(session->header->buffer);
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->header) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(session->header);
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(session);
|
|
|
|
session = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get wenclient request response data.
|
|
|
|
*
|
|
|
|
* @param session wenclient session
|
|
|
|
* @param response response buffer address
|
|
|
|
* @param resp_len response buffer length
|
|
|
|
*
|
|
|
|
* @return response data size
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_response(struct webclient_session* session,
|
|
|
|
void** response,
|
|
|
|
size_t* resp_len) {
|
|
|
|
unsigned char* buf_ptr;
|
|
|
|
unsigned char* response_buf = 0;
|
2022-11-10 16:55:14 +08:00
|
|
|
int length, total_read = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(session);
|
|
|
|
RT_ASSERT(response);
|
|
|
|
|
|
|
|
/* initialize response */
|
|
|
|
*response = RT_NULL;
|
|
|
|
|
|
|
|
/* not content length field kind */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session->content_length < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
size_t result_sz;
|
|
|
|
|
|
|
|
total_read = 0;
|
2022-11-10 17:06:54 +08:00
|
|
|
while (1) {
|
|
|
|
unsigned char* new_resp = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
result_sz = total_read + WEBCLIENT_RESPONSE_BUFSZ;
|
|
|
|
new_resp = web_realloc(response_buf, result_sz + 1);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (new_resp == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("no memory for realloc new response buffer!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
response_buf = new_resp;
|
2022-11-10 17:06:54 +08:00
|
|
|
buf_ptr = (unsigned char*)response_buf + total_read;
|
2022-11-10 16:55:14 +08:00
|
|
|
|
|
|
|
/* read result */
|
|
|
|
length = webclient_read(session, buf_ptr, result_sz - total_read);
|
|
|
|
if (length <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
total_read += length;
|
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
int result_sz;
|
|
|
|
|
|
|
|
result_sz = session->content_length;
|
|
|
|
response_buf = web_calloc(1, result_sz + 1);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (response_buf == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_NOMEM;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
buf_ptr = (unsigned char*)response_buf;
|
|
|
|
for (total_read = 0; total_read < result_sz;) {
|
2022-11-10 16:55:14 +08:00
|
|
|
length = webclient_read(session, buf_ptr, result_sz - total_read);
|
|
|
|
if (length <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
buf_ptr += length;
|
|
|
|
total_read += length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((total_read == 0) && (response_buf != 0)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
web_free(response_buf);
|
|
|
|
response_buf = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (response_buf) {
|
|
|
|
*response = (void*)response_buf;
|
2022-11-10 16:55:14 +08:00
|
|
|
*(response_buf + total_read) = '\0';
|
|
|
|
*resp_len = total_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add request(GET/POST) header data.
|
|
|
|
*
|
|
|
|
* @param request_header add request buffer address
|
|
|
|
* @param fmt fields format
|
|
|
|
*
|
|
|
|
* @return <=0: add header failed
|
|
|
|
* >0: add header data size
|
|
|
|
*/
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_request_header_add(char** request_header, const char* fmt, ...) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rt_int32_t length, header_length;
|
2022-11-10 17:06:54 +08:00
|
|
|
char* header;
|
2022-11-10 16:55:14 +08:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
RT_ASSERT(request_header);
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (*request_header == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
header = rt_calloc(1, WEBCLIENT_HEADER_BUFSZ);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (header == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("No memory for webclient request header add.");
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
*request_header = header;
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
header = *request_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
header_length = strlen(header);
|
2022-11-10 17:06:54 +08:00
|
|
|
length = web_vsnprintf(header + header_length,
|
|
|
|
WEBCLIENT_HEADER_BUFSZ - header_length, fmt, args);
|
|
|
|
if (length < 0) {
|
|
|
|
LOG_E("add request header data failed, return length(%d) error.",
|
|
|
|
length);
|
2023-07-09 23:12:21 +08:00
|
|
|
va_end(args);
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
/* check header size */
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strlen(header) >= WEBCLIENT_HEADER_BUFSZ) {
|
|
|
|
LOG_E("not enough request header data size(%d)!",
|
|
|
|
WEBCLIENT_HEADER_BUFSZ);
|
2022-11-10 16:55:14 +08:00
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* send request(GET/POST) to server and get response data.
|
|
|
|
*
|
|
|
|
* @param URI input server address
|
|
|
|
* @param header send header data
|
|
|
|
* = NULL: use default header data, must be GET request
|
|
|
|
* != NULL: user custom header data, GET or POST request
|
|
|
|
* @param post_data data sent to the server
|
|
|
|
* = NULL: it is GET request
|
|
|
|
* != NULL: it is POST request
|
|
|
|
* @param data_len send data length
|
|
|
|
* @param response response buffer address
|
|
|
|
* @param resp_len response buffer length
|
|
|
|
*
|
|
|
|
* @return <0: request failed
|
|
|
|
* >=0: response buffer size
|
|
|
|
*/
|
2022-11-10 17:06:54 +08:00
|
|
|
int webclient_request(const char* URI,
|
|
|
|
const char* header,
|
|
|
|
const void* post_data,
|
|
|
|
size_t data_len,
|
|
|
|
void** response,
|
|
|
|
size_t* resp_len) {
|
|
|
|
struct webclient_session* session = RT_NULL;
|
2022-11-10 16:55:14 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int totle_length = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(URI);
|
|
|
|
|
2022-11-22 21:05:05 +08:00
|
|
|
if (post_data == RT_NULL && response == RT_NULL) {
|
|
|
|
LOG_E("request get failed, get response data cannot be empty.");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((post_data != RT_NULL) && (data_len == 0)) {
|
|
|
|
LOG_E("input post data length failed");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((response != RT_NULL && resp_len == RT_NULL) ||
|
|
|
|
(response == RT_NULL && resp_len != RT_NULL)) {
|
|
|
|
LOG_E("input response data or length failed");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post_data == RT_NULL) {
|
|
|
|
/* send get request */
|
|
|
|
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
|
|
|
|
if (session == RT_NULL) {
|
|
|
|
rc = -WEBCLIENT_NOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header != RT_NULL) {
|
|
|
|
char *header_str, *header_ptr;
|
|
|
|
int header_line_length;
|
|
|
|
|
|
|
|
for (header_str = (char*)header;
|
|
|
|
(header_ptr = strstr(header_str, "\r\n")) != RT_NULL;) {
|
|
|
|
header_line_length = header_ptr + strlen("\r\n") - header_str;
|
|
|
|
webclient_header_fields_add(session, "%.*s", header_line_length,
|
|
|
|
header_str);
|
|
|
|
header_str += header_line_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (webclient_get(session, URI) != 200) {
|
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
totle_length = webclient_response(session, response, resp_len);
|
|
|
|
if (totle_length <= 0) {
|
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* send post request */
|
|
|
|
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
|
|
|
|
if (session == RT_NULL) {
|
|
|
|
rc = -WEBCLIENT_NOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header != RT_NULL) {
|
|
|
|
char *header_str, *header_ptr;
|
|
|
|
int header_line_length;
|
|
|
|
|
|
|
|
for (header_str = (char*)header;
|
|
|
|
(header_ptr = strstr(header_str, "\r\n")) != RT_NULL;) {
|
|
|
|
header_line_length = header_ptr + strlen("\r\n") - header_str;
|
|
|
|
webclient_header_fields_add(session, "%.*s", header_line_length,
|
|
|
|
header_str);
|
|
|
|
header_str += header_line_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(session->header->buffer, "Content-Length") == RT_NULL) {
|
|
|
|
webclient_header_fields_add(session, "Content-Length: %d\r\n",
|
|
|
|
strlen(post_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(session->header->buffer, "Content-Type") == RT_NULL) {
|
|
|
|
webclient_header_fields_add(
|
|
|
|
session, "Content-Type: application/octet-stream\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (webclient_post(session, URI, post_data, data_len) != 200) {
|
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
totle_length = webclient_response(session, response, resp_len);
|
|
|
|
if (totle_length <= 0) {
|
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
if (session) {
|
|
|
|
webclient_close(session);
|
|
|
|
session = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totle_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Better interface.
|
|
|
|
* send request(GET/POST) to server and get response data.
|
|
|
|
*
|
|
|
|
* @param URI input server address
|
|
|
|
* @param header send header data
|
|
|
|
* = NULL: use default header data, must be GET request
|
|
|
|
* != NULL: user custom header data, GET or POST request
|
|
|
|
* @param post_data data sent to the server
|
|
|
|
* = NULL: it is GET request
|
|
|
|
* != NULL: it is POST request
|
|
|
|
* @param data_len send data length
|
|
|
|
* @param response response buffer address
|
|
|
|
* @param resp_len response buffer length
|
|
|
|
*
|
|
|
|
* @return <0: request failed
|
|
|
|
* >=0: response buffer size
|
|
|
|
*/
|
2023-07-09 23:12:21 +08:00
|
|
|
int webclient_request2(struct webclient_session* session,
|
|
|
|
const char* URI,
|
|
|
|
const char* header,
|
|
|
|
const void* post_data,
|
|
|
|
size_t data_len,
|
|
|
|
double timeout,
|
|
|
|
void** response,
|
|
|
|
size_t* resp_len) {
|
2022-11-22 21:05:05 +08:00
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
int totle_length = 0;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (post_data == RT_NULL && response == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("request get failed, get response data cannot be empty.");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if ((post_data != RT_NULL) && (data_len == 0)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("input post data length failed");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((response != RT_NULL && resp_len == RT_NULL) ||
|
2022-11-10 17:06:54 +08:00
|
|
|
(response == RT_NULL && resp_len != RT_NULL)) {
|
2022-11-10 16:55:14 +08:00
|
|
|
LOG_E("input response data or length failed");
|
|
|
|
return -WEBCLIENT_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (post_data == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* send get request */
|
|
|
|
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_NOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (header != RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
char *header_str, *header_ptr;
|
|
|
|
int header_line_length;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
for (header_str = (char*)header;
|
|
|
|
(header_ptr = strstr(header_str, "\r\n")) != RT_NULL;) {
|
2022-11-10 16:55:14 +08:00
|
|
|
header_line_length = header_ptr + strlen("\r\n") - header_str;
|
2022-11-10 17:06:54 +08:00
|
|
|
webclient_header_fields_add(session, "%.*s", header_line_length,
|
|
|
|
header_str);
|
2022-11-10 16:55:14 +08:00
|
|
|
header_str += header_line_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_get(session, URI) != 200) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
totle_length = webclient_response(session, response, resp_len);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (totle_length <= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
2022-11-10 17:06:54 +08:00
|
|
|
} else {
|
2022-11-10 16:55:14 +08:00
|
|
|
/* send post request */
|
|
|
|
session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session == RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_NOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (header != RT_NULL) {
|
2022-11-10 16:55:14 +08:00
|
|
|
char *header_str, *header_ptr;
|
|
|
|
int header_line_length;
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
for (header_str = (char*)header;
|
|
|
|
(header_ptr = strstr(header_str, "\r\n")) != RT_NULL;) {
|
2022-11-10 16:55:14 +08:00
|
|
|
header_line_length = header_ptr + strlen("\r\n") - header_str;
|
2022-11-10 17:06:54 +08:00
|
|
|
webclient_header_fields_add(session, "%.*s", header_line_length,
|
|
|
|
header_str);
|
2022-11-10 16:55:14 +08:00
|
|
|
header_str += header_line_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strstr(session->header->buffer, "Content-Length") == RT_NULL) {
|
|
|
|
webclient_header_fields_add(session, "Content-Length: %d\r\n",
|
|
|
|
strlen(post_data));
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (strstr(session->header->buffer, "Content-Type") == RT_NULL) {
|
|
|
|
webclient_header_fields_add(
|
|
|
|
session, "Content-Type: application/octet-stream\r\n");
|
2022-11-10 16:55:14 +08:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (webclient_post(session, URI, post_data, data_len) != 200) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
totle_length = webclient_response(session, response, resp_len);
|
2022-11-10 17:06:54 +08:00
|
|
|
if (totle_length <= 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
rc = -WEBCLIENT_ERROR;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
2022-11-10 17:06:54 +08:00
|
|
|
if (session) {
|
2022-11-10 16:55:14 +08:00
|
|
|
webclient_close(session);
|
|
|
|
session = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:54 +08:00
|
|
|
if (rc < 0) {
|
2022-11-10 16:55:14 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totle_length;
|
|
|
|
}
|