curl.cpp 6.8 KB

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