curl.cpp 6.8 KB

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