2 * uclient - ustream based protocol client library
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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.
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.
18 #include <libubox/blobmsg.h>
24 static void example_header_done(struct uclient *cl)
26 struct blob_attr *cur;
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));
34 printf("Contents:\n");
37 static void example_read_data(struct uclient *cl)
43 len = uclient_read(cl, buf, sizeof(buf));
47 write(STDOUT_FILENO, buf, len);
51 static void example_request_sm(struct uclient *cl)
58 uclient_http_set_request_type(cl, "HEAD");
63 uclient_http_set_request_type(cl, "GET");
72 static void example_eof(struct uclient *cl)
76 if (retries < 10 && uclient_http_redirect(cl)) {
82 example_request_sm(cl);
85 static void example_error(struct uclient *cl, int code)
87 fprintf(stderr, "Error %d!\n", code);
88 example_request_sm(cl);
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,
98 static int usage(const char *progname)
101 "Usage: %s [options] <hostname> <port>\n"
103 " -c <cert>: Load CA certificates from file <cert>\n"
104 " -C: Skip certificate CN verification against hostname\n"
110 int main(int argc, char **argv)
112 struct ustream_ssl_ctx *ctx;
113 const char *progname = argv[0];
118 ctx = ustream_ssl_context_new(false);
120 while ((ch = getopt(argc, argv, "Cc:")) != -1) {
123 ustream_ssl_context_add_ca_crt_file(ctx, optarg);
129 return usage(progname);
137 return usage(progname);
141 cl = uclient_new(argv[0], NULL, &cb);
143 fprintf(stderr, "Failed to allocate uclient context\n");
147 uclient_http_set_ssl_ctx(cl, ctx, verify);
148 example_request_sm(cl);
153 ustream_ssl_context_free(ctx);