type = "Connection failed";
error_ret = 4;
break;
+ case UCLIENT_ERROR_TIMEDOUT:
+ type = "Connection timed out";
+ error_ret = 4;
+ break;
case UCLIENT_ERROR_SSL_INVALID_CERT:
type = "Invalid SSL certificate";
ignore = !verify;
if (uh->eof)
return;
- if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
- uc->cb->data_read(uc);
+ if (uh->state == HTTP_STATE_RECV_DATA) {
+ /* Now it's uclient user turn to read some data */
+ uloop_timeout_cancel(&uc->connection_timeout);
+
+ if (uc->cb->data_read)
+ uc->cb->data_read(uc);
+ }
}
static void __uclient_notify_write(struct uclient_http *uh)
uclient_notify_eof(uh);
+ /* Now that we consumed something and if this isn't EOF, start timer again */
+ if (!uh->uc.eof && !cl->connection_timeout.pending)
+ uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
+
return len;
}
return NULL;
}
+static void uclient_connection_timeout(struct uloop_timeout *timeout)
+{
+ struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
+
+ if (cl->backend->disconnect)
+ cl->backend->disconnect(cl);
+
+ uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
+}
+
struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
{
struct uclient *cl;
cl->backend = url->backend;
cl->cb = cb;
cl->url = url;
+ cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
+ cl->connection_timeout.cb = uclient_connection_timeout;
return cl;
}
return 0;
}
+int uclient_set_timeout(struct uclient *cl, int msecs)
+{
+ if (msecs <= 0)
+ return -EINVAL;
+
+ cl->timeout_msecs = msecs;
+
+ return 0;
+}
+
int uclient_connect(struct uclient *cl)
{
return cl->backend->connect(cl);
int uclient_request(struct uclient *cl)
{
+ int err;
+
if (!cl->backend->request)
return -1;
- return cl->backend->request(cl);
+ err = cl->backend->request(cl);
+ if (err)
+ return err;
+
+ uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
+
+ return 0;
}
int uclient_read(struct uclient *cl, char *buf, int len)
void uclient_disconnect(struct uclient *cl)
{
+ uloop_timeout_cancel(&cl->connection_timeout);
+
if (!cl->backend->disconnect)
return;
if (cl->error_code)
return;
+ uloop_timeout_cancel(&cl->connection_timeout);
cl->error_code = code;
uclient_backend_change_state(cl);
}
if (cl->eof || cl->error_code)
return;
+ uloop_timeout_cancel(&cl->connection_timeout);
cl->eof = true;
uclient_backend_change_state(cl);
}
#include <libubox/ustream.h>
#include <libubox/ustream-ssl.h>
+#define UCLIENT_DEFAULT_TIMEOUT_MS 30000
+
struct uclient_cb;
struct uclient_backend;
enum uclient_error_code {
UCLIENT_ERROR_UNKNOWN,
UCLIENT_ERROR_CONNECT,
+ UCLIENT_ERROR_TIMEDOUT,
UCLIENT_ERROR_SSL_INVALID_CERT,
UCLIENT_ERROR_SSL_CN_MISMATCH,
UCLIENT_ERROR_MISSING_SSL_CONTEXT,
union uclient_addr local_addr, remote_addr;
struct uclient_url *url;
+ int timeout_msecs;
void *priv;
bool eof;
int status_code;
struct blob_attr *meta;
+ struct uloop_timeout connection_timeout;
struct uloop_timeout timeout;
};
void uclient_free(struct uclient *cl);
int uclient_set_url(struct uclient *cl, const char *url, const char *auth);
+
+/**
+ * Sets connection timeout.
+ *
+ * Provided timeout value will be used for:
+ * 1) Receiving HTTP response
+ * 2) Receiving data
+ *
+ * In case of timeout uclient will use error callback with
+ * UCLIENT_ERROR_TIMEDOUT code.
+ *
+ * @param msecs timeout in milliseconds
+ */
+int uclient_set_timeout(struct uclient *cl, int msecs);
+
int uclient_connect(struct uclient *cl);
void uclient_disconnect(struct uclient *cl);