curl.cpp 4.3 KB

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