curl.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. state = s_key;
  136. }
  137. }
  138. else if (*p == ':') {
  139. state = s_value;
  140. }
  141. else {
  142. if (state == s_key) {
  143. if (++key_len == 1) key = p;
  144. }
  145. else {
  146. if (++value_len == 1) value = p;
  147. }
  148. }
  149. ++p;
  150. }
  151. if (key_len && value_len) {
  152. req.headers[std::string(key,key_len)] = std::string(value,value_len);
  153. key_len = value_len = 0;
  154. }
  155. }
  156. if (range) {
  157. req.headers["Range"] = std::string("bytes=").append(range);
  158. }
  159. if (data || form) {
  160. if (method == NULL) {
  161. req.method = HTTP_POST;
  162. }
  163. if (data) {
  164. req.body = data;
  165. }
  166. else if (form) {
  167. const char* p = form;
  168. const char* key = p;
  169. const char* value = NULL;
  170. int key_len = 0;
  171. int value_len = 0;
  172. state = s_key;
  173. while (*p != '\0') {
  174. if (*p == ' ') {
  175. if (key_len && value_len) {
  176. FormData data;
  177. if (*value == '@') {
  178. data.filename = std::string(value+1, value_len-1);
  179. }
  180. else {
  181. data.content = std::string(value, value_len);
  182. }
  183. req.form[std::string(key,key_len)] = data;
  184. key_len = value_len = 0;
  185. state = s_key;
  186. }
  187. }
  188. else if (*p == '=') {
  189. state = s_value;
  190. }
  191. else {
  192. if (state == s_key) {
  193. if (++key_len == 1) key = p;
  194. }
  195. else {
  196. if (++value_len == 1) value = p;
  197. }
  198. }
  199. ++p;
  200. }
  201. if (key_len && value_len) {
  202. // printf("key=%.*s value=%.*s\n", key_len, key, value_len, value);
  203. FormData data;
  204. if (*value == '@') {
  205. data.filename = std::string(value+1, value_len-1);
  206. }
  207. else {
  208. data.content = std::string(value, value_len);
  209. }
  210. req.form[std::string(key,key_len)] = data;
  211. }
  212. }
  213. }
  214. HttpResponse res;
  215. /*
  216. res.body_cb = [](const char* data, size_t size){
  217. printf("%.*s", (int)size, data);
  218. };
  219. */
  220. res.chunked_cb = [](const char* data, size_t size){
  221. printf("%.*s", (int)size, data);
  222. };
  223. http_client_t* hc = http_client_new();
  224. send:
  225. if (verbose) {
  226. printf("%s\n", req.Dump(true,true).c_str());
  227. }
  228. ret = http_client_send(hc, &req, &res);
  229. if (ret != 0) {
  230. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  231. } else {
  232. if (verbose) {
  233. printf("%s\n", res.Dump(true,true).c_str());
  234. }
  235. else {
  236. printf("%s\n", res.body.c_str());
  237. }
  238. }
  239. if (--send_count > 0) {
  240. printf("send again later...%d\n", send_count);
  241. #ifdef _WIN32
  242. Sleep(3*1000);
  243. #else
  244. sleep(3);
  245. #endif
  246. goto send;
  247. }
  248. http_client_del(hc);
  249. return ret;
  250. }