1
0

curl.cpp 7.3 KB

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