a54aa889d202d5c6d8b9b32e843bf94da0009881
[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
22 #include <libubox/blobmsg.h>
23
24 #include "uclient.h"
25
26
27 static void example_header_done(struct uclient *cl)
28 {
29 struct blob_attr *cur;
30 char local[INET6_ADDRSTRLEN], remote[INET6_ADDRSTRLEN];
31 int local_port, remote_port;
32 int rem;
33
34 uclient_get_addr(local, &local_port, &cl->local_addr);
35 uclient_get_addr(remote, &remote_port, &cl->remote_addr);
36
37 fprintf(stderr, "Connected: %s:%d -> %s:%d\n",
38 local, local_port, remote, remote_port);
39
40 printf("Headers (%d): \n", cl->status_code);
41 blobmsg_for_each_attr(cur, cl->meta, rem) {
42 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
43 }
44
45 printf("Contents:\n");
46 }
47
48 static void example_read_data(struct uclient *cl)
49 {
50 char buf[256];
51 int len;
52
53 while (1) {
54 len = uclient_read(cl, buf, sizeof(buf));
55 if (!len)
56 return;
57
58 write(STDOUT_FILENO, buf, len);
59 }
60 }
61
62 static void example_request_sm(struct uclient *cl)
63 {
64 static int i = 0;
65
66 switch (i++) {
67 case 0:
68 uclient_connect(cl);
69 uclient_http_set_request_type(cl, "HEAD");
70 uclient_request(cl);
71 break;
72 case 1:
73 uclient_connect(cl);
74 uclient_http_set_request_type(cl, "GET");
75 uclient_request(cl);
76 break;
77 default:
78 uloop_end();
79 break;
80 };
81 }
82
83 static void example_eof(struct uclient *cl)
84 {
85 static int retries;
86
87 if (retries < 10 && uclient_http_redirect(cl)) {
88 retries++;
89 return;
90 }
91
92 retries = 0;
93 example_request_sm(cl);
94 }
95
96 static void example_error(struct uclient *cl, int code)
97 {
98 fprintf(stderr, "Error %d!\n", code);
99 example_request_sm(cl);
100 }
101
102 static const struct uclient_cb cb = {
103 .header_done = example_header_done,
104 .data_read = example_read_data,
105 .data_eof = example_eof,
106 .error = example_error,
107 };
108
109 static int usage(const char *progname)
110 {
111 fprintf(stderr,
112 "Usage: %s [options] <hostname> <port>\n"
113 "Options:\n"
114 " -c <cert>: Load CA certificates from file <cert>\n"
115 " -C: Skip certificate CN verification against hostname\n"
116 "\n", progname);
117 return 1;
118 }
119
120
121 int main(int argc, char **argv)
122 {
123 struct ustream_ssl_ctx *ctx;
124 const char *progname = argv[0];
125 struct uclient *cl;
126 bool verify = true;
127 int ch;
128
129 ctx = ustream_ssl_context_new(false);
130
131 while ((ch = getopt(argc, argv, "Cc:")) != -1) {
132 switch(ch) {
133 case 'c':
134 ustream_ssl_context_add_ca_crt_file(ctx, optarg);
135 break;
136 case 'C':
137 verify = false;
138 break;
139 default:
140 return usage(progname);
141 }
142 }
143
144 argv += optind;
145 argc -= optind;
146
147 if (argc != 1)
148 return usage(progname);
149
150 uloop_init();
151
152 cl = uclient_new(argv[0], NULL, &cb);
153 if (!cl) {
154 fprintf(stderr, "Failed to allocate uclient context\n");
155 return 1;
156 }
157
158 uclient_http_set_ssl_ctx(cl, ctx, verify);
159 example_request_sm(cl);
160 uloop_run();
161 uloop_done();
162
163 uclient_free(cl);
164 ustream_ssl_context_free(ctx);
165
166
167 return 0;
168 }