http: add proper error handling to uclient_http_redirect()
authorFelix Fietkau <nbd@openwrt.org>
Sat, 23 Jan 2016 00:23:00 +0000 (01:23 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sat, 23 Jan 2016 00:23:00 +0000 (01:23 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
uclient-fetch.c
uclient-http.c
uclient.h

index 223d364..d2ffde3 100644 (file)
@@ -165,12 +165,22 @@ static void header_done_cb(struct uclient *cl)
        uint64_t resume_offset = 0, resume_end, resume_size;
        static int retries;
 
        uint64_t resume_offset = 0, resume_end, resume_size;
        static int retries;
 
-       if (retries < 10 && uclient_http_redirect(cl)) {
-               if (!quiet)
-                       fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
+       if (retries < 10) {
+               int ret = uclient_http_redirect(cl);
+               if (ret < 0) {
+                       if (!quiet)
+                               fprintf(stderr, "Failed to redirect to %s on %s\n", cl->url->location, cl->url->host);
+                       error_ret = 8;
+                       request_done(cl);
+                       return;
+               }
+               if (ret > 0) {
+                       if (!quiet)
+                               fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
 
 
-               retries++;
-               return;
+                       retries++;
+                       return;
+               }
        }
 
        if (cl->status_code == 204 && cur_resume) {
        }
 
        if (cl->status_code == 204 && cur_resume) {
index 195fa1c..6fc30da 100644 (file)
@@ -1063,7 +1063,7 @@ uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
        return len;
 }
 
        return len;
 }
 
-bool uclient_http_redirect(struct uclient *cl)
+int uclient_http_redirect(struct uclient *cl)
 {
        struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
        struct blobmsg_policy location = {
 {
        struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
        struct blobmsg_policy location = {
@@ -1095,7 +1095,9 @@ bool uclient_http_redirect(struct uclient *cl)
 
        free(cl->url);
        cl->url = url;
 
        free(cl->url);
        cl->url = url;
-       uclient_http_connect(cl);
+       if (uclient_http_connect(cl))
+               return -1;
+
        uclient_http_request_done(cl);
 
        return true;
        uclient_http_request_done(cl);
 
        return true;
index 282b9f3..504fc35 100644 (file)
--- a/uclient.h
+++ b/uclient.h
@@ -121,7 +121,7 @@ extern const struct uclient_backend uclient_backend_http;
 int uclient_http_reset_headers(struct uclient *cl);
 int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
 int uclient_http_set_request_type(struct uclient *cl, const char *type);
 int uclient_http_reset_headers(struct uclient *cl);
 int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
 int uclient_http_set_request_type(struct uclient *cl, const char *type);
-bool uclient_http_redirect(struct uclient *cl);
+int uclient_http_redirect(struct uclient *cl);
 
 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
                             struct ustream_ssl_ctx *ctx, bool require_validation);
 
 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
                             struct ustream_ssl_ctx *ctx, bool require_validation);