X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=uclient-example.c;h=7c54ce3429efc5fd20bb8b64f38ec1c7b250f584;hb=f9d9e776e6378bfe24737d6c4a7573f3842c7a9b;hp=2e88b28b871e673b689dc60938b51645ff370fcd;hpb=4a3a8cb176aa932ed4bc88354fad76d91764c65f;p=project%2Fuclient.git diff --git a/uclient-example.c b/uclient-example.c index 2e88b28..7c54ce3 100644 --- a/uclient-example.c +++ b/uclient-example.c @@ -15,23 +15,95 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include + #include #include +#include +#include +#include + +#include #include "uclient.h" +#ifdef __APPLE__ +#define LIB_EXT "dylib" +#else +#define LIB_EXT "so" +#endif + +static struct ustream_ssl_ctx *ssl_ctx; +static const struct ustream_ssl_ops *ssl_ops; +static int quiet = false; +static const char *output_file; +static int output_fd; +static int error_ret; + +static int open_output_file(const char *path, bool create) +{ + const char *str; + char *filename; + int len; + int flags = O_WRONLY; + + if (create) + flags |= O_CREAT; + + if (output_file) { + if (!strcmp(output_file, "-")) + return STDOUT_FILENO; + + return open(output_file, flags, 0644); + } + + /* Don't automatically overwrite files if the name is derived from the URL */ + if (create) + flags |= O_EXCL; + + len = strcspn(path, ";&"); + while (len > 0 && path[len - 1] == '/') + len--; + + for (str = path + len - 1; str >= path; str--) { + if (*str == '/') + break; + } + + str++; + len -= str - path; + + if (len > 0) { + filename = alloca(len + 1); + strncpy(filename, str, len); + filename[len] = 0; + } else { + filename = "index.html"; + } + + return open(filename, flags, 0644); +} + static void example_header_done(struct uclient *cl) { struct blob_attr *cur; int rem; + if (quiet) + return; + printf("Headers (%d): \n", cl->status_code); blobmsg_for_each_attr(cur, cl->meta, rem) { printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur)); } - printf("Contents:\n"); + output_fd = open_output_file(cl->url->location, true); + if (output_fd < 0) { + if (!quiet) { + perror("Cannot open output file"); + error_ret = 3; + uloop_end(); + } + } } static void example_read_data(struct uclient *cl) @@ -39,34 +111,41 @@ static void example_read_data(struct uclient *cl) char buf[256]; int len; + if (error_ret) + return; + while (1) { len = uclient_read(cl, buf, sizeof(buf)); if (!len) return; - write(STDOUT_FILENO, buf, len); + write(output_fd, buf, len); } } -static void example_request_sm(struct uclient *cl) +static void msg_connecting(struct uclient *cl) { - static int i = 0; - - switch (i++) { - case 0: - uclient_connect(cl); - uclient_http_set_request_type(cl, "HEAD"); - uclient_request(cl); - break; - case 1: - uclient_connect(cl); - uclient_http_set_request_type(cl, "GET"); - uclient_request(cl); - break; - default: - uloop_end(); - break; - }; + char addr[INET6_ADDRSTRLEN]; + int port; + + if (quiet) + return; + + uclient_get_addr(addr, &port, &cl->remote_addr); + fprintf(stderr, "Connecting to %s %s:%d\n", cl->url->host, addr, port); +} + +static void init_request(struct uclient *cl) +{ + uclient_connect(cl); + msg_connecting(cl); + uclient_http_set_request_type(cl, "GET"); + uclient_request(cl); +} + +static void request_done(struct uclient *cl) +{ + uloop_end(); } static void example_eof(struct uclient *cl) @@ -79,13 +158,15 @@ static void example_eof(struct uclient *cl) } retries = 0; - example_request_sm(cl); + request_done(cl); } static void example_error(struct uclient *cl, int code) { - fprintf(stderr, "Error %d!\n", code); - example_request_sm(cl); + if (!quiet) + fprintf(stderr, "Error %d!\n", code); + + request_done(cl); } static const struct uclient_cb cb = { @@ -98,32 +179,79 @@ static const struct uclient_cb cb = { static int usage(const char *progname) { fprintf(stderr, - "Usage: %s [options] \n" + "Usage: %s [options] \n" "Options:\n" - " -c : Load CA certificates from file \n" - " -C: Skip certificate CN verification against hostname\n" + "\n" + "HTTPS options:\n" + " --ca-certificate=: Load CA certificates from file \n" + " --no-check-certificate: don't validate the server's certificate\n" "\n", progname); return 1; } +static void init_ustream_ssl(void) +{ + void *dlh; + + dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL); + if (!dlh) + return; + + ssl_ops = dlsym(dlh, "ustream_ssl_ops"); + if (!ssl_ops) + return; + + ssl_ctx = ssl_ops->context_new(false); +} + +static int no_ssl(const char *progname) +{ + fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname); + return 1; +} + +enum { + L_NO_CHECK_CERTIFICATE, + L_CA_CERTIFICATE, +}; + +static const struct option longopts[] = { + [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument }, + [L_CA_CERTIFICATE] = { "ca-certificate", required_argument }, + {} +}; + int main(int argc, char **argv) { - struct ustream_ssl_ctx *ctx; const char *progname = argv[0]; struct uclient *cl; bool verify = true; int ch; + int longopt_idx = 0; - ctx = ustream_ssl_context_new(false); + init_ustream_ssl(); - while ((ch = getopt(argc, argv, "Cc:")) != -1) { + while ((ch = getopt_long(argc, argv, "qO:", longopts, &longopt_idx)) != -1) { switch(ch) { - case 'c': - ustream_ssl_context_add_ca_crt_file(ctx, optarg); + case 0: + switch (longopt_idx) { + case L_NO_CHECK_CERTIFICATE: + verify = false; + break; + case L_CA_CERTIFICATE: + if (ssl_ctx) + ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg); + break; + default: + return usage(progname); + } break; - case 'C': - verify = false; + case 'O': + output_file = optarg; + break; + case 'q': + quiet = true; break; default: return usage(progname); @@ -136,6 +264,9 @@ int main(int argc, char **argv) if (argc != 1) return usage(progname); + if (!strncmp(argv[0], "https", 5) && !ssl_ctx) + return no_ssl(progname); + uloop_init(); cl = uclient_new(argv[0], NULL, &cb); @@ -144,14 +275,17 @@ int main(int argc, char **argv) return 1; } - uclient_http_set_ssl_ctx(cl, ctx, verify); - example_request_sm(cl); + if (ssl_ctx) + uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify); + + init_request(cl); uloop_run(); uloop_done(); uclient_free(cl); - ustream_ssl_context_free(ctx); + if (ssl_ctx) + ssl_ops->context_free(ssl_ctx); - return 0; + return error_ret; }