curl.cpp 7.0 KB

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