#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>
+#include <getopt.h>
#include <libubox/blobmsg.h>
static struct ustream_ssl_ctx *ssl_ctx;
static const struct ustream_ssl_ops *ssl_ops;
+static int quiet = false;
static void example_header_done(struct uclient *cl)
{
struct blob_attr *cur;
- char local[INET6_ADDRSTRLEN], remote[INET6_ADDRSTRLEN];
- int local_port, remote_port;
int rem;
- uclient_get_addr(local, &local_port, &cl->local_addr);
- uclient_get_addr(remote, &remote_port, &cl->remote_addr);
-
- fprintf(stderr, "Connected: %s:%d -> %s:%d\n",
- local, local_port, remote, remote_port);
+ if (quiet)
+ return;
printf("Headers (%d): \n", cl->status_code);
blobmsg_for_each_attr(cur, cl->meta, rem) {
}
}
-static void example_request_sm(struct uclient *cl)
+static void msg_connecting(struct uclient *cl)
+{
+ 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)
{
- 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;
- };
+ 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)
}
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 = {
static int usage(const char *progname)
{
fprintf(stderr,
- "Usage: %s [options] <hostname> <port>\n"
+ "Usage: %s [options] <URL>\n"
"Options:\n"
- " -c <cert>: Load CA certificates from file <cert>\n"
- " -C: Skip certificate CN verification against hostname\n"
+ "\n"
+ "HTTPS options:\n"
+ " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
+ " --no-check-certificate: don't validate the server's certificate\n"
"\n", progname);
return 1;
}
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 uclient *cl;
bool verify = true;
int ch;
+ int longopt_idx = 0;
init_ustream_ssl();
- while ((ch = getopt(argc, argv, "Cc:")) != -1) {
+ while ((ch = getopt_long(argc, argv, "q", longopts, &longopt_idx)) != -1) {
switch(ch) {
- case 'c':
- if (ssl_ctx)
- ssl_ops->context_add_ca_crt_file(ssl_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 'q':
+ quiet = true;
break;
default:
return usage(progname);
if (ssl_ctx)
uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
- example_request_sm(cl);
+ init_request(cl);
uloop_run();
uloop_done();