diff mbox

[OpenWrt-Devel,uclient] http: wait for socket to be ready before using it

Message ID 1421322620-7419-1-git-send-email-zajec5@gmail.com
State Rejected
Headers show

Commit Message

Rafał Miłecki Jan. 15, 2015, 11:50 a.m. UTC
This is required as we use USOCK_NONBLOCK.

Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
---
 uclient-http.c | 5 +++++
 uclient.h      | 2 ++
 2 files changed, 7 insertions(+)

Comments

Felix Fietkau Jan. 15, 2015, 12:17 p.m. UTC | #1
On 2015-01-15 12:50, Rafał Miłecki wrote:
> This is required as we use USOCK_NONBLOCK.
Are you sure? Theoretically, ustream should already handle it properly.
The first write fails if the socket is not connected yet, so ustream
will buffer the data and send it out again once epoll tells it that the
fd is writable.

- Felix
Rafał Miłecki Jan. 15, 2015, 12:25 p.m. UTC | #2
On 15 January 2015 at 13:17, Felix Fietkau <nbd@openwrt.org> wrote:
> On 2015-01-15 12:50, Rafał Miłecki wrote:
>> This is required as we use USOCK_NONBLOCK.
> Are you sure? Theoretically, ustream should already handle it properly.
> The first write fails if the socket is not connected yet, so ustream
> will buffer the data and send it out again once epoll tells it that the
> fd is writable.

Maybe we were just thinking about different issues? See two of mine below:

1) time ./uclient-fetch http://192.168.0.1/
Downloading 'http://192.168.0.1/'
Connecting to Unknown:32533
HTTP error 401
real    0m0.008s

(this Unknown:32533 should be IP + port)

2) time ./uclient-fetch http://192.168.1.15/
Downloading 'http://192.168.1.15/'
Connecting to Unknown:32656
real    2m7.191s

(so it finally silently quits, but it takes 2 minutes, more than one
could want to wait)
diff mbox

Patch

diff --git a/uclient-http.c b/uclient-http.c
index c25e52f..82bef8a 100644
--- a/uclient-http.c
+++ b/uclient-http.c
@@ -108,6 +108,7 @@  static int uclient_do_connect(struct uclient_http *uh, const char *port)
 {
 	socklen_t sl;
 	int fd;
+	int err;
 
 	if (uh->uc.url->port)
 		port = uh->uc.url->port;
@@ -116,6 +117,10 @@  static int uclient_do_connect(struct uclient_http *uh, const char *port)
 	if (fd < 0)
 		return -1;
 
+	err = usock_wait_ready(fd, UCLIENT_DEFAULT_CONNECTION_TIMEOUT_MS);
+	if (err)
+		return err;
+
 	ustream_fd_init(&uh->ufd, fd);
 
 	memset(&uh->uc.local_addr, 0, sizeof(uh->uc.local_addr));
diff --git a/uclient.h b/uclient.h
index d5a0d5b..d90b00a 100644
--- a/uclient.h
+++ b/uclient.h
@@ -24,6 +24,8 @@ 
 #include <libubox/ustream.h>
 #include <libubox/ustream-ssl.h>
 
+#define UCLIENT_DEFAULT_CONNECTION_TIMEOUT_MS		30000
+
 struct uclient_cb;
 struct uclient_backend;