example: only write output data on http 200/204
[project/uclient.git] / uclient-example.c
index dc5ef91..2874ec2 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <dlfcn.h>
 #include <getopt.h>
+#include <fcntl.h>
 
 #include <libubox/blobmsg.h>
 
 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 = -1;
+static int error_ret;
+
+static int open_output_file(const char *path, bool create)
+{
+       const char *str;
+       char *filename;
+       int len;
+       int flags = O_WRONLY;
+
+       if (create)
+               flags |= O_CREAT;
+
+       if (output_file) {
+               if (!strcmp(output_file, "-"))
+                       return STDOUT_FILENO;
+
+               return open(output_file, flags, 0644);
+       }
+
+       /* Don't automatically overwrite files if the name is derived from the URL */
+       if (create)
+               flags |= O_EXCL;
+
+       len = strcspn(path, ";&");
+       while (len > 0 && path[len - 1] == '/')
+               len--;
+
+       for (str = path + len - 1; str >= path; str--) {
+               if (*str == '/')
+                       break;
+       }
+
+       str++;
+       len -= str - path;
+
+       if (len > 0) {
+               filename = alloca(len + 1);
+               strncpy(filename, str, len);
+               filename[len] = 0;
+       } else {
+               filename = "index.html";
+       }
+
+       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));
+               }
        }
 
-       printf("Contents:\n");
+       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;
+       }
 }
 
 static void example_read_data(struct uclient *cl)
@@ -56,12 +135,15 @@ static void example_read_data(struct uclient *cl)
        char buf[256];
        int len;
 
+       if (output_fd < 0)
+               return;
+
        while (1) {
                len = uclient_read(cl, buf, sizeof(buf));
                if (!len)
                        return;
 
-               write(STDOUT_FILENO, buf, len);
+               write(output_fd, buf, len);
        }
 }
 
@@ -77,46 +159,25 @@ static void msg_connecting(struct uclient *cl)
        fprintf(stderr, "Connecting to %s %s:%d\n", cl->url->host, addr, port);
 }
 
-static void example_request_sm(struct uclient *cl)
+static void init_request(struct uclient *cl)
 {
-       static int i = 0;
-
-       switch (i++) {
-       case 0:
-               uclient_connect(cl);
-               msg_connecting(cl);
-               uclient_http_set_request_type(cl, "HEAD");
-               uclient_request(cl);
-               break;
-       case 1:
-               uclient_connect(cl);
-               uclient_http_set_request_type(cl, "GET");
-               uclient_request(cl);
-               break;
-       default:
-               uloop_end();
-               break;
-       };
+       uclient_connect(cl);
+       msg_connecting(cl);
+       uclient_http_set_request_type(cl, "GET");
+       uclient_request(cl);
 }
 
 static void example_eof(struct uclient *cl)
 {
-       static int retries;
-
-       if (retries < 10 && uclient_http_redirect(cl)) {
-               retries++;
-               return;
-       }
-
-       retries = 0;
-       example_request_sm(cl);
+       request_done(cl);
 }
 
 static void example_error(struct uclient *cl, int code)
 {
        if (!quiet)
                fprintf(stderr, "Error %d!\n", code);
-       example_request_sm(cl);
+
+       request_done(cl);
 }
 
 static const struct uclient_cb cb = {
@@ -129,10 +190,13 @@ static const struct uclient_cb cb = {
 static int usage(const char *progname)
 {
        fprintf(stderr,
-               "Usage: %s [options] <hostname> <port>\n"
+               "Usage: %s [options] <URL>\n"
                "Options:\n"
-               "       -c <cert>:         Load CA certificates from file <cert>\n"
-               "       -C:                Skip certificate CN verification against hostname\n"
+               "       -O <file>:                      Redirect output to file (use \"-\" for stdout)\n"
+               "\n"
+               "HTTPS options:\n"
+               "       --ca-certificate=<cert>:        Load CA certificates from file <cert>\n"
+               "       --no-check-certificate:         don't validate the server's certificate\n"
                "\n", progname);
        return 1;
 }
@@ -180,7 +244,7 @@ int main(int argc, char **argv)
 
        init_ustream_ssl();
 
-       while ((ch = getopt_long(argc, argv, "q", longopts, &longopt_idx)) != -1) {
+       while ((ch = getopt_long(argc, argv, "qO:", longopts, &longopt_idx)) != -1) {
                switch(ch) {
                case 0:
                        switch (longopt_idx) {
@@ -195,6 +259,9 @@ int main(int argc, char **argv)
                                return usage(progname);
                        }
                        break;
+               case 'O':
+                       output_file = optarg;
+                       break;
                case 'q':
                        quiet = true;
                        break;
@@ -223,7 +290,7 @@ int main(int argc, char **argv)
        if (ssl_ctx)
                uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
 
-       example_request_sm(cl);
+       init_request(cl);
        uloop_run();
        uloop_done();
 
@@ -232,5 +299,5 @@ int main(int argc, char **argv)
        if (ssl_ctx)
                ssl_ops->context_free(ssl_ctx);
 
-       return 0;
+       return error_ret;
 }