9bfd241ad4a9ebb37e2dedf83fd330226878bddf
[project/uclient.git] / uclient-example.c
1 #include <libubox/blobmsg.h>
2 #include <unistd.h>
3 #include <stdio.h>
4
5 #include "uclient.h"
6
7 static void example_header_done(struct uclient *cl)
8 {
9         struct blob_attr *cur;
10         int rem;
11
12         printf("Headers (%d): \n", cl->status_code);
13         blobmsg_for_each_attr(cur, cl->meta, rem) {
14                 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
15         }
16
17         printf("Contents:\n");
18 }
19
20 static void example_read_data(struct uclient *cl)
21 {
22         char buf[256];
23         int len;
24
25         while (1) {
26                 len = uclient_read(cl, buf, sizeof(buf));
27                 if (!len)
28                         return;
29
30                 write(STDOUT_FILENO, buf, len);
31         }
32 }
33
34 static void example_request_sm(struct uclient *cl)
35 {
36         static int i = 0;
37
38         switch (i++) {
39         case 0:
40                 uclient_connect(cl);
41                 uclient_http_set_request_type(cl, "HEAD");
42                 uclient_request(cl);
43                 break;
44         case 1:
45                 uclient_connect(cl);
46                 uclient_http_set_request_type(cl, "GET");
47                 uclient_request(cl);
48                 break;
49         default:
50                 uloop_end();
51                 break;
52         };
53 }
54
55 static void example_eof(struct uclient *cl)
56 {
57         static int retries;
58
59         if (retries < 10 && uclient_http_redirect(cl)) {
60                 retries++;
61                 return;
62         }
63
64         retries = 0;
65         example_request_sm(cl);
66 }
67
68 static void example_error(struct uclient *cl, int code)
69 {
70         fprintf(stderr, "Error %d!\n", code);
71         example_request_sm(cl);
72 }
73
74 static const struct uclient_cb cb = {
75         .header_done = example_header_done,
76         .data_read = example_read_data,
77         .data_eof = example_eof,
78         .error = example_error,
79 };
80
81 static int usage(const char *progname)
82 {
83         fprintf(stderr,
84                 "Usage: %s [options] <hostname> <port>\n"
85                 "Options:\n"
86                 "       -c <cert>:         Load CA certificates from file <cert>\n"
87                 "       -C:                Skip certificate CN verification against hostname\n"
88                 "\n", progname);
89         return 1;
90 }
91
92
93 int main(int argc, char **argv)
94 {
95         struct ustream_ssl_ctx *ctx;
96         const char *progname = argv[0];
97         struct uclient *cl;
98         bool verify = true;
99         int ch;
100
101         ctx = ustream_ssl_context_new(false);
102
103         while ((ch = getopt(argc, argv, "Cc:")) != -1) {
104                 switch(ch) {
105                 case 'c':
106                         ustream_ssl_context_add_ca_crt_file(ctx, optarg);
107                         break;
108                 case 'C':
109                         verify = false;
110                         break;
111                 default:
112                         return usage(progname);
113                 }
114         }
115
116         argv += optind;
117         argc -= optind;
118
119         if (argc != 1)
120                 return usage(progname);
121
122         uloop_init();
123
124         cl = uclient_new(argv[0], &cb);
125         if (!cl) {
126                 fprintf(stderr, "Failed to allocate uclient context\n");
127                 return 1;
128         }
129
130         uclient_http_set_ssl_ctx(cl, ctx, verify);
131         example_request_sm(cl);
132         uloop_run();
133         uloop_done();
134
135         uclient_free(cl);
136         ustream_ssl_context_free(ctx);
137
138
139         return 0;
140 }