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 const char *output_file
;
39 static int output_fd
= -1;
42 static int open_output_file(const char *path
, bool create
)
53 if (!strcmp(output_file
, "-"))
56 return open(output_file
, flags
, 0644);
59 /* Don't automatically overwrite files if the name is derived from the URL */
63 len
= strcspn(path
, ";&");
64 while (len
> 0 && path
[len
- 1] == '/')
67 for (str
= path
+ len
- 1; str
>= path
; str
--) {
76 filename
= alloca(len
+ 1);
77 strncpy(filename
, str
, len
);
80 filename
= "index.html";
83 return open(filename
, flags
, 0644);
86 static void request_done(struct uclient
*cl
)
92 uclient_disconnect(cl
);
96 static void example_header_done(struct uclient
*cl
)
100 struct blob_attr
*cur
;
103 if (retries
< 10 && uclient_http_redirect(cl
)) {
105 fprintf(stderr
, "Redirected to %s on %s\n", cl
->url
->location
, cl
->url
->host
);
113 fprintf(stderr
, "Headers (%d): \n", cl
->status_code
);
114 blobmsg_for_each_attr(cur
, cl
->meta
, rem
) {
115 fprintf(stderr
, "%s=%s\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
119 switch (cl
->status_code
) {
122 output_fd
= open_output_file(cl
->url
->location
, true);
125 perror("Cannot open output file");
138 static void example_read_data(struct uclient
*cl
)
147 len
= uclient_read(cl
, buf
, sizeof(buf
));
151 write(output_fd
, buf
, len
);
155 static void msg_connecting(struct uclient
*cl
)
157 char addr
[INET6_ADDRSTRLEN
];
163 uclient_get_addr(addr
, &port
, &cl
->remote_addr
);
164 fprintf(stderr
, "Connecting to %s %s:%d\n", cl
->url
->host
, addr
, port
);
167 static void init_request(struct uclient
*cl
)
171 uclient_http_set_request_type(cl
, "GET");
175 static void example_eof(struct uclient
*cl
)
180 static void example_error(struct uclient
*cl
, int code
)
183 fprintf(stderr
, "Error %d!\n", code
);
188 static const struct uclient_cb cb
= {
189 .header_done
= example_header_done
,
190 .data_read
= example_read_data
,
191 .data_eof
= example_eof
,
192 .error
= example_error
,
195 static int usage(const char *progname
)
198 "Usage: %s [options] <URL>\n"
200 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
203 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
204 " --no-check-certificate: don't validate the server's certificate\n"
210 static void init_ustream_ssl(void)
214 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
218 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
222 ssl_ctx
= ssl_ops
->context_new(false);
225 static int no_ssl(const char *progname
)
227 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
232 L_NO_CHECK_CERTIFICATE
,
236 static const struct option longopts
[] = {
237 [L_NO_CHECK_CERTIFICATE
] = { "no-check-certificate", no_argument
},
238 [L_CA_CERTIFICATE
] = { "ca-certificate", required_argument
},
242 int main(int argc
, char **argv
)
244 const char *progname
= argv
[0];
252 while ((ch
= getopt_long(argc
, argv
, "qO:", longopts
, &longopt_idx
)) != -1) {
255 switch (longopt_idx
) {
256 case L_NO_CHECK_CERTIFICATE
:
259 case L_CA_CERTIFICATE
:
261 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
264 return usage(progname
);
268 output_file
= optarg
;
274 return usage(progname
);
282 return usage(progname
);
284 if (!strncmp(argv
[0], "https", 5) && !ssl_ctx
)
285 return no_ssl(progname
);
289 cl
= uclient_new(argv
[0], NULL
, &cb
);
291 fprintf(stderr
, "Failed to allocate uclient context\n");
296 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
305 ssl_ops
->context_free(ssl_ctx
);