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.
25 #include <libubox/blobmsg.h>
30 #define LIB_EXT "dylib"
35 static struct ustream_ssl_ctx *ssl_ctx;
36 static const struct ustream_ssl_ops *ssl_ops;
37 static int quiet = false;
38 static bool verify = true;
39 static const char *output_file;
40 static int output_fd = -1;
43 static int open_output_file(const char *path, bool create)
54 if (!strcmp(output_file, "-"))
57 return open(output_file, flags, 0644);
60 /* Don't automatically overwrite files if the name is derived from the URL */
64 len = strcspn(path, ";&");
65 while (len > 0 && path[len - 1] == '/')
68 for (str = path + len - 1; str >= path; str--) {
77 filename = alloca(len + 1);
78 strncpy(filename, str, len);
81 filename = "index.html";
84 return open(filename, flags, 0644);
87 static void request_done(struct uclient *cl)
89 if (output_fd >= 0 && !output_file) {
93 uclient_disconnect(cl);
97 static void example_header_done(struct uclient *cl)
101 struct blob_attr *cur;
104 if (retries < 10 && uclient_http_redirect(cl)) {
106 fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
114 fprintf(stderr, "Headers (%d): \n", cl->status_code);
115 blobmsg_for_each_attr(cur, cl->meta, rem) {
116 fprintf(stderr, "%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
120 switch (cl->status_code) {
123 output_fd = open_output_file(cl->url->location, true);
126 perror("Cannot open output file");
139 static void example_read_data(struct uclient *cl)
148 len = uclient_read(cl, buf, sizeof(buf));
152 write(output_fd, buf, len);
156 static void msg_connecting(struct uclient *cl)
158 char addr[INET6_ADDRSTRLEN];
164 uclient_get_addr(addr, &port, &cl->remote_addr);
165 fprintf(stderr, "Connecting to %s %s:%d\n", cl->url->host, addr, port);
168 static void init_request(struct uclient *cl)
172 uclient_http_set_request_type(cl, "GET");
176 static void example_eof(struct uclient *cl)
181 static void handle_uclient_error(struct uclient *cl, int code)
183 const char *type = "Unknown error";
187 case UCLIENT_ERROR_CONNECT:
188 type = "Connection failed";
191 case UCLIENT_ERROR_SSL_INVALID_CERT:
192 type = "Invalid SSL certificate";
196 case UCLIENT_ERROR_SSL_CN_MISMATCH:
197 type = "Server hostname does not match SSL certificate";
207 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
215 static const struct uclient_cb cb = {
216 .header_done = example_header_done,
217 .data_read = example_read_data,
218 .data_eof = example_eof,
219 .error = handle_uclient_error,
222 static int usage(const char *progname)
225 "Usage: %s [options] <URL>\n"
227 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
230 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
231 " --no-check-certificate: don't validate the server's certificate\n"
237 static void init_ustream_ssl(void)
241 dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
245 ssl_ops = dlsym(dlh, "ustream_ssl_ops");
249 ssl_ctx = ssl_ops->context_new(false);
252 static int no_ssl(const char *progname)
254 fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
259 L_NO_CHECK_CERTIFICATE,
263 static const struct option longopts[] = {
264 [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
265 [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
269 int main(int argc, char **argv)
271 const char *progname = argv[0];
278 while ((ch = getopt_long(argc, argv, "qO:", longopts, &longopt_idx)) != -1) {
281 switch (longopt_idx) {
282 case L_NO_CHECK_CERTIFICATE:
285 case L_CA_CERTIFICATE:
287 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
290 return usage(progname);
294 output_file = optarg;
300 return usage(progname);
308 return usage(progname);
310 if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
311 return no_ssl(progname);
315 cl = uclient_new(argv[0], NULL, &cb);
317 fprintf(stderr, "Failed to allocate uclient context\n");
322 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
330 if (output_fd >= 0 && output_fd != STDOUT_FILENO)
334 ssl_ops->context_free(ssl_ctx);