curl.cpp 4.5 KB

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