|
|
|
@ -30,8 +30,13 @@
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#else
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <lwip/netdb.h>
|
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
|
#endif
|
|
|
|
|
#endif /* RT_USING_SAL */
|
|
|
|
|
|
|
|
|
|
#define DBG_ENABLE
|
|
|
|
@ -42,7 +47,9 @@
|
|
|
|
|
#define DBG_LEVEL DBG_INFO
|
|
|
|
|
#endif /* WEBCLIENT_DEBUG */
|
|
|
|
|
#define DBG_COLOR
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* default receive or send timeout */
|
|
|
|
|
#define WEBCLIENT_DEFAULT_TIMEO 6
|
|
|
|
@ -123,8 +130,26 @@ static int webclient_read_line(struct webclient_session *session, char *buffer,
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (rc <= 0)
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
|
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
|
|
|
|
|
#else
|
|
|
|
|
int err = errno;
|
|
|
|
|
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
else if (rc == 0)
|
|
|
|
|
{
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ch == '\n' && last_ch == '\r')
|
|
|
|
|
break;
|
|
|
|
@ -377,7 +402,7 @@ static int webclient_open_tls(struct webclient_session *session, const char *URI
|
|
|
|
|
* @return <0: connect failed or other error
|
|
|
|
|
* =0: connect success
|
|
|
|
|
*/
|
|
|
|
|
static int webclient_connect(struct webclient_session *session, const char *URI)
|
|
|
|
|
int webclient_connect(struct webclient_session *session, const char *URI)
|
|
|
|
|
{
|
|
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
|
int socket_handle;
|
|
|
|
@ -629,7 +654,7 @@ int webclient_content_length_get(struct webclient_session *session)
|
|
|
|
|
return session->content_length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int webclient_send_header(struct webclient_session *session, int method)
|
|
|
|
|
int webclient_send_header(struct webclient_session *session, int method)
|
|
|
|
|
{
|
|
|
|
|
int rc = WEBCLIENT_OK;
|
|
|
|
|
char *header = RT_NULL;
|
|
|
|
@ -1327,7 +1352,13 @@ int webclient_read(struct webclient_session *session, void *buffer, size_t lengt
|
|
|
|
|
bytes_read = webclient_recv(session, buffer, length, 0);
|
|
|
|
|
if (bytes_read <= 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN)
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
|
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
|
|
|
|
|
#else
|
|
|
|
|
int err = errno;
|
|
|
|
|
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
/* recv timeout */
|
|
|
|
|
return -WEBCLIENT_TIMEOUT;
|
|
|
|
@ -1382,19 +1413,25 @@ int webclient_read(struct webclient_session *session, void *buffer, size_t lengt
|
|
|
|
|
#endif
|
|
|
|
|
LOG_D("receive data error(%d).", bytes_read);
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
|
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
|
|
|
|
|
#else
|
|
|
|
|
int err = errno;
|
|
|
|
|
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
/* recv timeout */
|
|
|
|
|
LOG_E("receive data timeout.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (total_read)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN)
|
|
|
|
|
{
|
|
|
|
|
/* recv timeout */
|
|
|
|
|
LOG_E("receive data timeout.");
|
|
|
|
|
return -WEBCLIENT_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
closesocket(session->socket);
|
|
|
|
|
session->socket = -1;
|
|
|
|
@ -1458,7 +1495,13 @@ int webclient_write(struct webclient_session *session, const void *buffer, size_
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN)
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
|
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
|
|
|
|
|
#else
|
|
|
|
|
int err = errno;
|
|
|
|
|
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
/* send timeout */
|
|
|
|
|
if (total_write)
|
|
|
|
|