curl.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #ifdef _MSC_VER
  5. #include "misc/win32_getopt.h"
  6. #else
  7. #include <getopt.h>
  8. #endif
  9. #include "http_client.h"
  10. static const char* options = "hVvX:H:d:";
  11. static const struct option long_options[] = {
  12. {"help", no_argument, NULL, 'h'},
  13. {"verion", no_argument, NULL, 'V'},
  14. {"verbose", no_argument, NULL, 'v'},
  15. {"method", required_argument, NULL, 'X'},
  16. {"header", required_argument, NULL, 'H'},
  17. {"data", required_argument, NULL, 'd'},
  18. {NULL, 0, NULL, 0}
  19. };
  20. static const char* help = R"(Options:
  21. -h|--help Print this message.
  22. -V|--version Print version.
  23. -v|--verbose Show verbose infomation.
  24. -X|--method Set http method.
  25. -H|--header Add http headers, format -H "Content-Type:application/json Accept:*/*"
  26. -d|--data Set http body.
  27. Examples:
  28. curl -v localhost:8086
  29. curl -v localhost:8086/v1/api/query?page_no=1&page_size=10
  30. curl -v -X POST localhost:8086/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  31. curl -v -X POST localhost:8086/v1/api/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  32. curl -v -X POST localhost:8086/v1/api/echo -H "Content-Type:text/plain" -d 'hello,world!'
  33. )";
  34. void print_usage() {
  35. printf("Usage: curl [%s] url\n", options);
  36. }
  37. void print_version() {
  38. printf("curl version 1.0.0\n");
  39. }
  40. void print_help() {
  41. print_usage();
  42. puts(help);
  43. print_version();
  44. }
  45. bool verbose = false;
  46. static const char* url = NULL;
  47. static const char* method = NULL;
  48. static const char* headers = NULL;
  49. static const char* data = NULL;
  50. int parse_cmdline(int argc, char* argv[]) {
  51. int opt;
  52. int opt_idx;
  53. while ((opt = getopt_long(argc, argv, options, long_options, &opt_idx)) != EOF) {
  54. switch(opt) {
  55. case 'h': print_help(); exit(0);
  56. case 'V': print_version(); exit(0);
  57. case 'v': verbose = true; break;
  58. case 'X': method = optarg; break;
  59. case 'H': headers = optarg; break;
  60. case 'd': data = optarg; break;
  61. default:
  62. print_usage();
  63. exit(-1);
  64. }
  65. }
  66. if (optind == argc) {
  67. printf("Missing url\n");
  68. print_usage();
  69. exit(-1);
  70. }
  71. url = argv[optind];
  72. return 0;
  73. }
  74. int main(int argc, char* argv[]) {
  75. if (argc < 2) {
  76. print_usage();
  77. return 0;
  78. }
  79. parse_cmdline(argc, argv);
  80. int ret = 0;
  81. HttpRequest req;
  82. req.url = url;
  83. if (method) {
  84. req.method = http_method_enum(method);
  85. }
  86. if (headers) {
  87. enum {
  88. s_key,
  89. s_value,
  90. } state = s_key;
  91. const char* p = headers;
  92. const char* key = p;
  93. const char* value = NULL;
  94. int key_len = 0;
  95. int value_len = 0;
  96. while (*p != '\0') {
  97. if (*p == ' ') {
  98. if (key_len && value_len) {
  99. req.headers[std::string(key,key_len)] = std::string(value,value_len);
  100. key_len = value_len = 0;
  101. }
  102. state = s_key;
  103. key = p+1;
  104. }
  105. else if (*p == ':') {
  106. state = s_value;
  107. value = p+1;
  108. }
  109. else {
  110. state == s_key ? ++key_len : ++value_len;
  111. }
  112. ++p;
  113. }
  114. if (key_len && value_len) {
  115. req.headers[std::string(key,key_len)] = std::string(value,value_len);
  116. key_len = value_len = 0;
  117. }
  118. }
  119. if (data) {
  120. if (method == NULL) {
  121. req.method = HTTP_POST;
  122. }
  123. req.body = data;
  124. }
  125. HttpResponse res;
  126. ret = http_client_send(&req, &res, 0);
  127. if (verbose) {
  128. printf("%s\n", req.dump(true,true).c_str());
  129. }
  130. if (ret != 0) {
  131. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  132. }
  133. else {
  134. if (verbose) {
  135. printf("%s\n", res.dump(true,true).c_str());
  136. }
  137. else {
  138. printf("%s\n", res.body.c_str());
  139. }
  140. }
  141. return ret;
  142. }