3bf8d298c47b1aa9e1920d5ed9717b44af29224f
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.
24 #include <libubox/blobmsg.h>
29 #define LIB_EXT "dylib"
34 static struct ustream_ssl_ctx
*ssl_ctx
;
35 static const struct ustream_ssl_ops
*ssl_ops
;
36 static int quiet
= false;
38 static void example_header_done(struct uclient
*cl
)
40 struct blob_attr
*cur
;
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
));
51 printf("Contents:\n");
54 static void example_read_data(struct uclient
*cl
)
60 len
= uclient_read(cl
, buf
, sizeof(buf
));
64 write(STDOUT_FILENO
, buf
, len
);
68 static void msg_connecting(struct uclient
*cl
)
70 char addr
[INET6_ADDRSTRLEN
];
76 uclient_get_addr(addr
, &port
, &cl
->remote_addr
);
77 fprintf(stderr
, "Connecting to %s %s:%d\n", cl
->url
->host
, addr
, port
);
80 static void example_request_sm(struct uclient
*cl
)
88 uclient_http_set_request_type(cl
, "HEAD");
93 uclient_http_set_request_type(cl
, "GET");
102 static void example_eof(struct uclient
*cl
)
106 if (retries
< 10 && uclient_http_redirect(cl
)) {
112 example_request_sm(cl
);
115 static void example_error(struct uclient
*cl
, int code
)
118 fprintf(stderr
, "Error %d!\n", code
);
119 example_request_sm(cl
);
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
,
129 static int usage(const char *progname
)
132 "Usage: %s [options] <URL>\n"
136 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
137 " --no-check-certificate: don't validate the server's certificate\n"
143 static void init_ustream_ssl(void)
147 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
151 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
155 ssl_ctx
= ssl_ops
->context_new(false);
158 static int no_ssl(const char *progname
)
160 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
165 L_NO_CHECK_CERTIFICATE
,
169 static const struct option longopts
[] = {
170 [L_NO_CHECK_CERTIFICATE
] = { "no-check-certificate", no_argument
},
171 [L_CA_CERTIFICATE
] = { "ca-certificate", required_argument
},
175 int main(int argc
, char **argv
)
177 const char *progname
= argv
[0];
185 while ((ch
= getopt_long(argc
, argv
, "q", longopts
, &longopt_idx
)) != -1) {
188 switch (longopt_idx
) {
189 case L_NO_CHECK_CERTIFICATE
:
192 case L_CA_CERTIFICATE
:
194 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
197 return usage(progname
);
204 return usage(progname
);
212 return usage(progname
);
214 if (!strncmp(argv
[0], "https", 5) && !ssl_ctx
)
215 return no_ssl(progname
);
219 cl
= uclient_new(argv
[0], NULL
, &cb
);
221 fprintf(stderr
, "Failed to allocate uclient context\n");
226 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
228 example_request_sm(cl
);
235 ssl_ops
->context_free(ssl_ctx
);