curl.cpp 5.0 KB

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