example: update usage info
[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
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <dlfcn.h>
22 #include <getopt.h>
23
24 #include <libubox/blobmsg.h>
25
26 #include "uclient.h"
27
28 #ifdef __APPLE__
29 #define LIB_EXT "dylib"
30 #else
31 #define LIB_EXT "so"
32 #endif
33
34 static struct ustream_ssl_ctx *ssl_ctx;
35 static const struct ustream_ssl_ops *ssl_ops;
36 static int quiet = false;
37
38 static void example_header_done(struct uclient *cl)
39 {
40         struct blob_attr *cur;
41         int rem;
42
43         if (quiet)
44                 return;
45
46         printf("Headers (%d): \n", cl->status_code);
47         blobmsg_for_each_attr(cur, cl->meta, rem) {
48                 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
49         }
50
51         printf("Contents:\n");
52 }
53
54 static void example_read_data(struct uclient *cl)
55 {
56         char buf[256];
57         int len;
58
59         while (1) {
60                 len = uclient_read(cl, buf, sizeof(buf));
61                 if (!len)
62                         return;
63
64                 write(STDOUT_FILENO, buf, len);
65         }
66 }
67
68 static void msg_connecting(struct uclient *cl)
69 {
70         char addr[INET6_ADDRSTRLEN];
71         int port;
72
73         if (quiet)
74                 return;
75
76         uclient_get_addr(addr, &port, &cl->remote_addr);
77         fprintf(stderr, "Connecting to %s %s:%d\n", cl->url->host, addr, port);
78 }
79
80 static void example_request_sm(struct uclient *cl)
81 {
82         static int i = 0;
83
84         switch (i++) {
85         case 0:
86                 uclient_connect(cl);
87                 msg_connecting(cl);
88                 uclient_http_set_request_type(cl, "HEAD");
89                 uclient_request(cl);
90                 break;
91         case 1:
92                 uclient_connect(cl);
93                 uclient_http_set_request_type(cl, "GET");
94                 uclient_request(cl);
95                 break;
96         default:
97                 uloop_end();
98                 break;
99         };
100 }
101
102 static void example_eof(struct uclient *cl)
103 {
104         static int retries;
105
106         if (retries < 10 && uclient_http_redirect(cl)) {
107                 retries++;
108                 return;
109         }
110
111         retries = 0;
112         example_request_sm(cl);
113 }
114
115 static void example_error(struct uclient *cl, int code)
116 {
117         if (!quiet)
118                 fprintf(stderr, "Error %d!\n", code);
119         example_request_sm(cl);
120 }
121
122 static const struct uclient_cb cb = {
123         .header_done = example_header_done,
124         .data_read = example_read_data,
125         .data_eof = example_eof,
126         .error = example_error,
127 };
128
129 static int usage(const char *progname)
130 {
131         fprintf(stderr,
132                 "Usage: %s [options] <URL>\n"
133                 "Options:\n"
134                 "\n"
135                 "HTTPS options:\n"
136                 "       --ca-certificate=<cert>:        Load CA certificates from file <cert>\n"
137                 "       --no-check-certificate:         don't validate the server's certificate\n"
138                 "\n", progname);
139         return 1;
140 }
141
142
143 static void init_ustream_ssl(void)
144 {
145         void *dlh;
146
147         dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
148         if (!dlh)
149                 return;
150
151         ssl_ops = dlsym(dlh, "ustream_ssl_ops");
152         if (!ssl_ops)
153                 return;
154
155         ssl_ctx = ssl_ops->context_new(false);
156 }
157
158 static int no_ssl(const char *progname)
159 {
160         fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
161         return 1;
162 }
163
164 enum {
165         L_NO_CHECK_CERTIFICATE,
166         L_CA_CERTIFICATE,
167 };
168
169 static const struct option longopts[] = {
170         [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
171         [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
172         {}
173 };
174
175 int main(int argc, char **argv)
176 {
177         const char *progname = argv[0];
178         struct uclient *cl;
179         bool verify = true;
180         int ch;
181         int longopt_idx = 0;
182
183         init_ustream_ssl();
184
185         while ((ch = getopt_long(argc, argv, "q", longopts, &longopt_idx)) != -1) {
186                 switch(ch) {
187                 case 0:
188                         switch (longopt_idx) {
189                         case L_NO_CHECK_CERTIFICATE:
190                                 verify = false;
191                                 break;
192                         case L_CA_CERTIFICATE:
193                                 if (ssl_ctx)
194                                         ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
195                                 break;
196                         default:
197                                 return usage(progname);
198                         }
199                         break;
200                 case 'q':
201                         quiet = true;
202                         break;
203                 default:
204                         return usage(progname);
205                 }
206         }
207
208         argv += optind;
209         argc -= optind;
210
211         if (argc != 1)
212                 return usage(progname);
213
214         if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
215                 return no_ssl(progname);
216
217         uloop_init();
218
219         cl = uclient_new(argv[0], NULL, &cb);
220         if (!cl) {
221                 fprintf(stderr, "Failed to allocate uclient context\n");
222                 return 1;
223         }
224
225         if (ssl_ctx)
226                 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
227
228         example_request_sm(cl);
229         uloop_run();
230         uloop_done();
231
232         uclient_free(cl);
233
234         if (ssl_ctx)
235                 ssl_ops->context_free(ssl_ctx);
236
237         return 0;
238 }