X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuclient.git;a=blobdiff_plain;f=uclient-example.c;h=9bfd241ad4a9ebb37e2dedf83fd330226878bddf;hp=07b330dc308edded71541938d1aecab5bf0d75fd;hb=25e44fc1e666fb333b3c53bcda90e44b0b74bf19;hpb=32f153a8dba2ef26ec14dd2b4dff917fbda90f83 diff --git a/uclient-example.c b/uclient-example.c index 07b330d..9bfd241 100644 --- a/uclient-example.c +++ b/uclient-example.c @@ -9,7 +9,7 @@ static void example_header_done(struct uclient *cl) struct blob_attr *cur; int rem; - printf("Headers: \n"); + 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)); } @@ -47,7 +47,6 @@ static void example_request_sm(struct uclient *cl) uclient_request(cl); break; default: - uclient_free(cl); uloop_end(); break; }; @@ -55,6 +54,20 @@ static void example_request_sm(struct uclient *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); +} + +static void example_error(struct uclient *cl, int code) +{ + fprintf(stderr, "Error %d!\n", code); example_request_sm(cl); } @@ -62,26 +75,66 @@ static const struct uclient_cb cb = { .header_done = example_header_done, .data_read = example_read_data, .data_eof = example_eof, + .error = example_error, }; +static int usage(const char *progname) +{ + fprintf(stderr, + "Usage: %s [options] \n" + "Options:\n" + " -c : Load CA certificates from file \n" + " -C: Skip certificate CN verification against hostname\n" + "\n", progname); + return 1; +} + + int main(int argc, char **argv) { + struct ustream_ssl_ctx *ctx; + const char *progname = argv[0]; struct uclient *cl; + bool verify = true; + int ch; - if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return 1; + ctx = ustream_ssl_context_new(false); + + while ((ch = getopt(argc, argv, "Cc:")) != -1) { + switch(ch) { + case 'c': + ustream_ssl_context_add_ca_crt_file(ctx, optarg); + break; + case 'C': + verify = false; + break; + default: + return usage(progname); + } } + argv += optind; + argc -= optind; + + if (argc != 1) + return usage(progname); + uloop_init(); - cl = uclient_new(argv[1], &cb); + + cl = uclient_new(argv[0], &cb); if (!cl) { fprintf(stderr, "Failed to allocate uclient context\n"); return 1; } + + uclient_http_set_ssl_ctx(cl, ctx, verify); example_request_sm(cl); uloop_run(); uloop_done(); + uclient_free(cl); + ustream_ssl_context_free(ctx); + + return 0; }