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>
28 #include "uclient-utils.h"
31 #define LIB_EXT "dylib"
36 static struct ustream_ssl_ctx
*ssl_ctx
;
37 static const struct ustream_ssl_ops
*ssl_ops
;
38 static int quiet
= false;
39 static bool verify
= true;
40 static const char *output_file
;
41 static int output_fd
= -1;
44 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 filename
= uclient_get_url_filename(path
, "index.html");
65 ret
= open(filename
, flags
, 0644);
71 static void request_done(struct uclient
*cl
)
73 if (output_fd
>= 0 && !output_file
) {
77 uclient_disconnect(cl
);
81 static void header_done_cb(struct uclient
*cl
)
85 if (retries
< 10 && uclient_http_redirect(cl
)) {
87 fprintf(stderr
, "Redirected to %s on %s\n", cl
->url
->location
, cl
->url
->host
);
94 switch (cl
->status_code
) {
97 output_fd
= open_output_file(cl
->url
->location
, true);
100 perror("Cannot open output file");
108 fprintf(stderr
, "HTTP error %d\n", cl
->status_code
);
115 static void read_data_cb(struct uclient
*cl
)
124 len
= uclient_read(cl
, buf
, sizeof(buf
));
128 write(output_fd
, buf
, len
);
132 static void msg_connecting(struct uclient
*cl
)
134 char addr
[INET6_ADDRSTRLEN
];
140 uclient_get_addr(addr
, &port
, &cl
->remote_addr
);
141 fprintf(stderr
, "Connecting to %s %s:%d\n", cl
->url
->host
, addr
, port
);
144 static void init_request(struct uclient
*cl
)
148 uclient_http_set_request_type(cl
, "GET");
152 static void eof_cb(struct uclient
*cl
)
156 fprintf(stderr
, "Connection reset prematurely\n");
162 static void handle_uclient_error(struct uclient
*cl
, int code
)
164 const char *type
= "Unknown error";
168 case UCLIENT_ERROR_CONNECT
:
169 type
= "Connection failed";
172 case UCLIENT_ERROR_SSL_INVALID_CERT
:
173 type
= "Invalid SSL certificate";
177 case UCLIENT_ERROR_SSL_CN_MISMATCH
:
178 type
= "Server hostname does not match SSL certificate";
188 fprintf(stderr
, "Connection error: %s%s\n", type
, ignore
? " (ignored)" : "");
196 static const struct uclient_cb cb
= {
197 .header_done
= header_done_cb
,
198 .data_read
= read_data_cb
,
200 .error
= handle_uclient_error
,
203 static int usage(const char *progname
)
206 "Usage: %s [options] <URL>\n"
208 " -q: Turn off status messages\n"
209 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
212 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
213 " --no-check-certificate: don't validate the server's certificate\n"
219 static void init_ustream_ssl(void)
223 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
227 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
231 ssl_ctx
= ssl_ops
->context_new(false);
234 static int no_ssl(const char *progname
)
236 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
241 L_NO_CHECK_CERTIFICATE
,
245 static const struct option longopts
[] = {
246 [L_NO_CHECK_CERTIFICATE
] = { "no-check-certificate", no_argument
},
247 [L_CA_CERTIFICATE
] = { "ca-certificate", required_argument
},
251 int main(int argc
, char **argv
)
253 const char *progname
= argv
[0];
260 while ((ch
= getopt_long(argc
, argv
, "qO:", longopts
, &longopt_idx
)) != -1) {
263 switch (longopt_idx
) {
264 case L_NO_CHECK_CERTIFICATE
:
267 case L_CA_CERTIFICATE
:
269 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
272 return usage(progname
);
276 output_file
= optarg
;
282 return usage(progname
);
290 return usage(progname
);
292 if (!strncmp(argv
[0], "https", 5) && !ssl_ctx
)
293 return no_ssl(progname
);
297 cl
= uclient_new(argv
[0], NULL
, &cb
);
299 fprintf(stderr
, "Failed to allocate uclient context\n");
304 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
312 if (output_fd
>= 0 && output_fd
!= STDOUT_FILENO
)
316 ssl_ops
->context_free(ssl_ctx
);