add support for passing in auth to uclient_set_url()
[project/uclient.git] / uclient-example.c
1 /*
2  * uclient - ustream based protocol client library
3  *
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <libubox/blobmsg.h>
19 #include <unistd.h>
20 #include <stdio.h>
21
22 #include "uclient.h"
23
24 static void example_header_done(struct uclient *cl)
25 {
26         struct blob_attr *cur;
27         int rem;
28
29         printf("Headers (%d): \n", cl->status_code);
30         blobmsg_for_each_attr(cur, cl->meta, rem) {
31                 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
32         }
33
34         printf("Contents:\n");
35 }
36
37 static void example_read_data(struct uclient *cl)
38 {
39         char buf[256];
40         int len;
41
42         while (1) {
43                 len = uclient_read(cl, buf, sizeof(buf));
44                 if (!len)
45                         return;
46
47                 write(STDOUT_FILENO, buf, len);
48         }
49 }
50
51 static void example_request_sm(struct uclient *cl)
52 {
53         static int i = 0;
54
55         switch (i++) {
56         case 0:
57                 uclient_connect(cl);
58                 uclient_http_set_request_type(cl, "HEAD");
59                 uclient_request(cl);
60                 break;
61         case 1:
62                 uclient_connect(cl);
63                 uclient_http_set_request_type(cl, "GET");
64                 uclient_request(cl);
65                 break;
66         default:
67                 uloop_end();
68                 break;
69         };
70 }
71
72 static void example_eof(struct uclient *cl)
73 {
74         static int retries;
75
76         if (retries < 10 && uclient_http_redirect(cl)) {
77                 retries++;
78                 return;
79         }
80
81         retries = 0;
82         example_request_sm(cl);
83 }
84
85 static void example_error(struct uclient *cl, int code)
86 {
87         fprintf(stderr, "Error %d!\n", code);
88         example_request_sm(cl);
89 }
90
91 static const struct uclient_cb cb = {
92         .header_done = example_header_done,
93         .data_read = example_read_data,
94         .data_eof = example_eof,
95         .error = example_error,
96 };
97
98 static int usage(const char *progname)
99 {
100         fprintf(stderr,
101                 "Usage: %s [options] <hostname> <port>\n"
102                 "Options:\n"
103                 "       -c <cert>:         Load CA certificates from file <cert>\n"
104                 "       -C:                Skip certificate CN verification against hostname\n"
105                 "\n", progname);
106         return 1;
107 }
108
109
110 int main(int argc, char **argv)
111 {
112         struct ustream_ssl_ctx *ctx;
113         const char *progname = argv[0];
114         struct uclient *cl;
115         bool verify = true;
116         int ch;
117
118         ctx = ustream_ssl_context_new(false);
119
120         while ((ch = getopt(argc, argv, "Cc:")) != -1) {
121                 switch(ch) {
122                 case 'c':
123                         ustream_ssl_context_add_ca_crt_file(ctx, optarg);
124                         break;
125                 case 'C':
126                         verify = false;
127                         break;
128                 default:
129                         return usage(progname);
130                 }
131         }
132
133         argv += optind;
134         argc -= optind;
135
136         if (argc != 1)
137                 return usage(progname);
138
139         uloop_init();
140
141         cl = uclient_new(argv[0], NULL, &cb);
142         if (!cl) {
143                 fprintf(stderr, "Failed to allocate uclient context\n");
144                 return 1;
145         }
146
147         uclient_http_set_ssl_ctx(cl, ctx, verify);
148         example_request_sm(cl);
149         uloop_run();
150         uloop_done();
151
152         uclient_free(cl);
153         ustream_ssl_context_free(ctx);
154
155
156         return 0;
157 }