Add implementation of timeouts for the apps/netutils/webclient. From Brennan Ashton

This commit is contained in:
Gregory Nutt 2014-09-29 17:10:49 -06:00
parent dc1b219b79
commit ff8338b5c2
2 changed files with 32 additions and 12 deletions

View File

@ -12,7 +12,11 @@ config NETUTILS_WEBCLIENT
if NETUTILS_WEBCLIENT if NETUTILS_WEBCLIENT
config NSH_WGET_USERAGENT config NSH_WGET_USERAGENT
string "wget Usert Agent" string "wget User Agent"
default "NuttX/6.xx.x (; http://www.nuttx.org/)" default "NuttX/6.xx.x (; http://www.nuttx.org/)"
config WEBCLIENT_TIMEOUT
int "Request and receive timeouts"
default 10
endif endif

View File

@ -102,7 +102,9 @@
* Definitions * Definitions
****************************************************************************/ ****************************************************************************/
#define WEBCLIENT_TIMEOUT 100 #ifndef CONFIG_WEBCLIENT_TIMEOUT
# define CONFIG_WEBCLIENT_TIMEOUT 10
#endif
#define WEBCLIENT_STATE_STATUSLINE 0 #define WEBCLIENT_STATE_STATUSLINE 0
#define WEBCLIENT_STATE_HEADERS 1 #define WEBCLIENT_STATE_HEADERS 1
@ -335,7 +337,8 @@ static inline int wget_parseheaders(struct wget_s *ws)
if (dest != NULL) if (dest != NULL)
{ {
*dest = 0; *dest = 0;
} }
strncpy(ws->mimetype, ws->line + strlen(g_httpcontenttype), sizeof(ws->mimetype)); strncpy(ws->mimetype, ws->line + strlen(g_httpcontenttype), sizeof(ws->mimetype));
} }
else else
@ -364,6 +367,7 @@ static inline int wget_parseheaders(struct wget_s *ws)
{ {
ndx++; ndx++;
} }
offset++; offset++;
} }
@ -408,6 +412,7 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
{ {
struct sockaddr_in server; struct sockaddr_in server;
struct wget_s ws; struct wget_s ws;
struct timeval tv;
bool redirected; bool redirected;
char *dest,post_size[8]; char *dest,post_size[8];
int sockfd; int sockfd;
@ -424,11 +429,11 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
/* Parse the hostname (with optional port number) and filename from the URL */ /* Parse the hostname (with optional port number) and filename from the URL */
ret = netlib_parsehttpurl(url, &ws.port, ret = netlib_parsehttpurl(url, &ws.port,
ws.hostname, CONFIG_WEBCLIENT_MAXHOSTNAME, ws.hostname, CONFIG_WEBCLIENT_MAXHOSTNAME,
ws.filename, CONFIG_WEBCLIENT_MAXFILENAME); ws.filename, CONFIG_WEBCLIENT_MAXFILENAME);
if (ret != 0) if (ret != 0)
{ {
ndbg("Malformed HTTP URL: %s\n", url); ndbg("ERROR: Malformed HTTP URL: %s\n", url);
set_errno(-ret); set_errno(-ret);
return ERROR; return ERROR;
} }
@ -456,11 +461,21 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
{ {
/* socket failed. It will set the errno appropriately */ /* socket failed. It will set the errno appropriately */
ndbg("socket failed: %d\n", errno); ndbg("ERROR: socket failed: %d\n", errno);
return ERROR; return ERROR;
} }
/* Get the server adddress from the host name */ /* Set send and receive timeout values */
tv.tv_sec = CONFIG_WEBCLIENT_TIMEOUT;
tv.tv_usec = 0;
(void)setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (FAR const void *)&tv,
sizeof(struct timeval));
(void)setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (FAR const void *)&tv,
sizeof(struct timeval));
/* Get the server address from the host name */
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons(ws.port); server.sin_port = htons(ws.port);
@ -469,7 +484,7 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
{ {
/* Could not resolve host (or malformed IP address) */ /* Could not resolve host (or malformed IP address) */
ndbg("Failed to resolve hostname\n"); ndbg("ERROR: Failed to resolve hostname\n");
ret = -EHOSTUNREACH; ret = -EHOSTUNREACH;
goto errout_with_errno; goto errout_with_errno;
} }
@ -482,7 +497,7 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
ret = connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)); ret = connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
if (ret < 0) if (ret < 0)
{ {
ndbg("connect failed: %d\n", errno); ndbg("ERROR: connect failed: %d\n", errno);
goto errout; goto errout;
} }
@ -537,7 +552,7 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
ret = send(sockfd, buffer, len, 0); ret = send(sockfd, buffer, len, 0);
if (ret < 0) if (ret < 0)
{ {
ndbg("send failed: %d\n", errno); ndbg("ERROR: send failed: %d\n", errno);
goto errout; goto errout;
} }
@ -553,7 +568,7 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
ws.datend = recv(sockfd, ws.buffer, ws.buflen, 0); ws.datend = recv(sockfd, ws.buffer, ws.buflen, 0);
if (ws.datend < 0) if (ws.datend < 0)
{ {
ndbg("recv failed: %d\n", errno); ndbg("ERROR: recv failed: %d\n", errno);
ret = ws.datend; ret = ws.datend;
goto errout_with_errno; goto errout_with_errno;
} }
@ -607,6 +622,7 @@ static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
} }
} }
while (redirected); while (redirected);
return OK; return OK;
errout_with_errno: errout_with_errno: