example: only write output data on http 200/204
authorFelix Fietkau <nbd@openwrt.org>
Sun, 4 May 2014 18:16:50 +0000 (20:16 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 4 May 2014 18:16:50 +0000 (20:16 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
uclient-example.c

index 32fc314a5ceb011c41c28071f6805fb4899ab7d3..2874ec2fefb2abf89a7154bd13714dd9313b6305 100644 (file)
@@ -36,7 +36,7 @@ static struct ustream_ssl_ctx *ssl_ctx;
 static const struct ustream_ssl_ops *ssl_ops;
 static int quiet = false;
 static const char *output_file;
-static int output_fd;
+static int output_fd = -1;
 static int error_ret;
 
 static int open_output_file(const char *path, bool create)
@@ -83,26 +83,50 @@ static int open_output_file(const char *path, bool create)
        return open(filename, flags, 0644);
 }
 
+static void request_done(struct uclient *cl)
+{
+       uclient_disconnect(cl);
+       uloop_end();
+}
+
 static void example_header_done(struct uclient *cl)
 {
+       static int retries;
+
        struct blob_attr *cur;
        int rem;
 
-       if (quiet)
+       if (retries < 10 && uclient_http_redirect(cl)) {
+               if (!quiet)
+                       fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
+
+               retries++;
                return;
+       }
 
-       printf("Headers (%d): \n", cl->status_code);
-       blobmsg_for_each_attr(cur, cl->meta, rem) {
-               printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
+       retries = 0;
+       if (!quiet) {
+               fprintf(stderr, "Headers (%d): \n", cl->status_code);
+               blobmsg_for_each_attr(cur, cl->meta, rem) {
+                       fprintf(stderr, "%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
+               }
        }
 
-       output_fd = open_output_file(cl->url->location, true);
-       if (output_fd < 0) {
-               if (!quiet)
-                       perror("Cannot open output file");
-               error_ret = 3;
-               uclient_disconnect(cl);
-               uloop_end();
+       switch (cl->status_code) {
+       case 204:
+       case 200:
+               output_fd = open_output_file(cl->url->location, true);
+               if (output_fd < 0) {
+                       if (!quiet)
+                               perror("Cannot open output file");
+                       error_ret = 3;
+                       request_done(cl);
+               }
+               break;
+
+       default:
+               request_done(cl);
+               break;
        }
 }
 
@@ -111,7 +135,7 @@ static void example_read_data(struct uclient *cl)
        char buf[256];
        int len;
 
-       if (error_ret)
+       if (output_fd < 0)
                return;
 
        while (1) {
@@ -143,21 +167,8 @@ static void init_request(struct uclient *cl)
        uclient_request(cl);
 }
 
-static void request_done(struct uclient *cl)
-{
-       uloop_end();
-}
-
 static void example_eof(struct uclient *cl)
 {
-       static int retries;
-
-       if (retries < 10 && uclient_http_redirect(cl)) {
-               retries++;
-               return;
-       }
-
-       retries = 0;
        request_done(cl);
 }