uclient-example.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * uclient - ustream based protocol client library
  3. *
  4. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  5. *
  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.
  9. *
  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.
  17. */
  18. #include <libubox/blobmsg.h>
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include "uclient.h"
  22. static void example_header_done(struct uclient *cl)
  23. {
  24. struct blob_attr *cur;
  25. int rem;
  26. printf("Headers (%d): \n", cl->status_code);
  27. blobmsg_for_each_attr(cur, cl->meta, rem) {
  28. printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
  29. }
  30. printf("Contents:\n");
  31. }
  32. static void example_read_data(struct uclient *cl)
  33. {
  34. char buf[256];
  35. int len;
  36. while (1) {
  37. len = uclient_read(cl, buf, sizeof(buf));
  38. if (!len)
  39. return;
  40. write(STDOUT_FILENO, buf, len);
  41. }
  42. }
  43. static void example_request_sm(struct uclient *cl)
  44. {
  45. static int i = 0;
  46. switch (i++) {
  47. case 0:
  48. uclient_connect(cl);
  49. uclient_http_set_request_type(cl, "HEAD");
  50. uclient_request(cl);
  51. break;
  52. case 1:
  53. uclient_connect(cl);
  54. uclient_http_set_request_type(cl, "GET");
  55. uclient_request(cl);
  56. break;
  57. default:
  58. uloop_end();
  59. break;
  60. };
  61. }
  62. static void example_eof(struct uclient *cl)
  63. {
  64. static int retries;
  65. if (retries < 10 && uclient_http_redirect(cl)) {
  66. retries++;
  67. return;
  68. }
  69. retries = 0;
  70. example_request_sm(cl);
  71. }
  72. static void example_error(struct uclient *cl, int code)
  73. {
  74. fprintf(stderr, "Error %d!\n", code);
  75. example_request_sm(cl);
  76. }
  77. static const struct uclient_cb cb = {
  78. .header_done = example_header_done,
  79. .data_read = example_read_data,
  80. .data_eof = example_eof,
  81. .error = example_error,
  82. };
  83. static int usage(const char *progname)
  84. {
  85. fprintf(stderr,
  86. "Usage: %s [options] <hostname> <port>\n"
  87. "Options:\n"
  88. " -c <cert>: Load CA certificates from file <cert>\n"
  89. " -C: Skip certificate CN verification against hostname\n"
  90. "\n", progname);
  91. return 1;
  92. }
  93. int main(int argc, char **argv)
  94. {
  95. struct ustream_ssl_ctx *ctx;
  96. const char *progname = argv[0];
  97. struct uclient *cl;
  98. bool verify = true;
  99. int ch;
  100. ctx = ustream_ssl_context_new(false);
  101. while ((ch = getopt(argc, argv, "Cc:")) != -1) {
  102. switch(ch) {
  103. case 'c':
  104. ustream_ssl_context_add_ca_crt_file(ctx, optarg);
  105. break;
  106. case 'C':
  107. verify = false;
  108. break;
  109. default:
  110. return usage(progname);
  111. }
  112. }
  113. argv += optind;
  114. argc -= optind;
  115. if (argc != 1)
  116. return usage(progname);
  117. uloop_init();
  118. cl = uclient_new(argv[0], NULL, &cb);
  119. if (!cl) {
  120. fprintf(stderr, "Failed to allocate uclient context\n");
  121. return 1;
  122. }
  123. uclient_http_set_ssl_ctx(cl, ctx, verify);
  124. example_request_sm(cl);
  125. uloop_run();
  126. uloop_done();
  127. uclient_free(cl);
  128. ustream_ssl_context_free(ctx);
  129. return 0;
  130. }