9bfd241ad4a9ebb37e2dedf83fd330226878bddf
1 #include <libubox/blobmsg.h>
7 static void example_header_done(struct uclient
*cl
)
12 printf("Headers (%d): \n", cl
->status_code
);
13 blobmsg_for_each_attr(cur
, cl
->meta
, rem
) {
14 printf("%s=%s\n", blobmsg_name(cur
), (char *) blobmsg_data(cur
));
17 printf("Contents:\n");
20 static void example_read_data(struct uclient
*cl
)
26 len
= uclient_read(cl
, buf
, sizeof(buf
));
30 write(STDOUT_FILENO
, buf
, len
);
34 static void example_request_sm(struct uclient
*cl
)
41 uclient_http_set_request_type(cl
, "HEAD");
46 uclient_http_set_request_type(cl
, "GET");
55 static void example_eof(struct uclient
*cl
)
59 if (retries
< 10 && uclient_http_redirect(cl
)) {
65 example_request_sm(cl
);
68 static void example_error(struct uclient
*cl
, int code
)
70 fprintf(stderr
, "Error %d!\n", code
);
71 example_request_sm(cl
);
74 static const struct uclient_cb cb
= {
75 .header_done
= example_header_done
,
76 .data_read
= example_read_data
,
77 .data_eof
= example_eof
,
78 .error
= example_error
,
81 static int usage(const char *progname
)
84 "Usage: %s [options] <hostname> <port>\n"
86 " -c <cert>: Load CA certificates from file <cert>\n"
87 " -C: Skip certificate CN verification against hostname\n"
93 int main(int argc
, char **argv
)
95 struct ustream_ssl_ctx
*ctx
;
96 const char *progname
= argv
[0];
101 ctx
= ustream_ssl_context_new(false);
103 while ((ch
= getopt(argc
, argv
, "Cc:")) != -1) {
106 ustream_ssl_context_add_ca_crt_file(ctx
, optarg
);
112 return usage(progname
);
120 return usage(progname
);
124 cl
= uclient_new(argv
[0], &cb
);
126 fprintf(stderr
, "Failed to allocate uclient context\n");
130 uclient_http_set_ssl_ctx(cl
, ctx
, verify
);
131 example_request_sm(cl
);
136 ustream_ssl_context_free(ctx
);