curl.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /*
  10. * @介绍:curl是一个著名的URL命令行工具,支持ftp、telnet、smtp、http等多种协议,
  11. * 最常用的还是用作http请求,这个示例代码使用libhv的http客户端实现了类似功能。
  12. *
  13. */
  14. #include "http_client.h"
  15. #ifdef _MSC_VER
  16. #include "misc/win32_getopt.h"
  17. #else
  18. #include <getopt.h>
  19. #endif
  20. static int http_version = 1;
  21. static int grpc = 0;
  22. static bool verbose = false;
  23. static const char* url = NULL;
  24. static const char* method = NULL;
  25. static const char* headers = NULL;
  26. static const char* range = NULL;
  27. static const char* data = NULL;
  28. static const char* form = NULL;
  29. static int send_count = 1;
  30. static const char* options = "hVvX:H:r:d:F:n:";
  31. static const struct option long_options[] = {
  32. {"help", no_argument, NULL, 'h'},
  33. {"verion", no_argument, NULL, 'V'},
  34. {"verbose", no_argument, NULL, 'v'},
  35. {"method", required_argument, NULL, 'X'},
  36. {"header", required_argument, NULL, 'H'},
  37. {"range", required_argument, NULL, 'r'},
  38. {"data", required_argument, NULL, 'd'},
  39. {"form", required_argument, NULL, 'F'},
  40. {"http2", no_argument, &http_version, 2},
  41. {"grpc", no_argument, &grpc, 1},
  42. {"count", required_argument, NULL, 'n'},
  43. {NULL, 0, NULL, 0}
  44. };
  45. static const char* help = R"(Options:
  46. -h|--help Print this message.
  47. -V|--version Print version.
  48. -v|--verbose Show verbose infomation.
  49. -X|--method Set http method.
  50. -H|--header Add http headers, -H "Content-Type:application/json Accept:*/*"
  51. -r|--range Add http header Range: bytes=0-1023
  52. -d|--data Set http body.
  53. -F|--form Set http form, -F "name1=content;name2=@filename"
  54. -n|--count Send request count, used for test keep-alive
  55. --http2 Use http2
  56. --grpc Use grpc over http2
  57. Examples:
  58. curl -v localhost:8080
  59. curl -v localhost:8080 -X HEAD
  60. curl -v localhost:8080 -r 0-9
  61. curl -v localhost:8080/ping
  62. curl -v localhost:8080/query?page_no=1&page_size=10
  63. curl -v localhost:8080/echo -d 'hello,world!'
  64. curl -v localhost:8080/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  65. curl -v localhost:8080/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  66. curl -v localhost:8080/form -F 'file=@filename'
  67. )";
  68. void print_usage() {
  69. printf("Usage: curl [%s] url\n", options);
  70. }
  71. void print_version() {
  72. printf("curl version 1.0.0\n");
  73. }
  74. void print_help() {
  75. print_usage();
  76. puts(help);
  77. print_version();
  78. }
  79. int parse_cmdline(int argc, char* argv[]) {
  80. int opt;
  81. int opt_idx;
  82. while ((opt = getopt_long(argc, argv, options, long_options, &opt_idx)) != EOF) {
  83. switch(opt) {
  84. case 'h': print_help(); exit(0);
  85. case 'V': print_version(); exit(0);
  86. case 'v': verbose = true; break;
  87. case 'X': method = optarg; break;
  88. case 'H': headers = optarg; break;
  89. case 'r': range = optarg; break;
  90. case 'd': data = optarg; break;
  91. case 'F': form = optarg; break;
  92. case 'n': send_count = atoi(optarg); break;
  93. default: break;
  94. }
  95. }
  96. if (optind == argc) {
  97. printf("Missing url\n");
  98. print_usage();
  99. exit(-1);
  100. }
  101. url = argv[optind];
  102. return 0;
  103. }
  104. int main(int argc, char* argv[]) {
  105. if (argc < 2) {
  106. print_usage();
  107. return 0;
  108. }
  109. parse_cmdline(argc, argv);
  110. int ret = 0;
  111. // 解析命令行参数,填充到请求对象
  112. HttpRequest req;
  113. if (grpc) {
  114. http_version = 2;
  115. req.content_type = APPLICATION_GRPC;
  116. }
  117. if (http_version == 2) {
  118. req.http_major = 2;
  119. req.http_minor = 0;
  120. }
  121. req.url = url;
  122. if (method) {
  123. req.method = http_method_enum(method);
  124. }
  125. enum {
  126. s_key,
  127. s_value,
  128. } state = s_key;
  129. if (headers) {
  130. const char* p = headers;
  131. const char* key = p;
  132. const char* value = NULL;
  133. int key_len = 0;
  134. int value_len = 0;
  135. state = s_key;
  136. while (*p != '\0') {
  137. if (*p == ' ') {
  138. if (key_len && value_len) {
  139. req.headers[std::string(key,key_len)] = std::string(value,value_len);
  140. key_len = value_len = 0;
  141. }
  142. state = s_key;
  143. key = p+1;
  144. }
  145. else if (*p == ':') {
  146. state = s_value;
  147. value = p+1;
  148. }
  149. else {
  150. state == s_key ? ++key_len : ++value_len;
  151. }
  152. ++p;
  153. }
  154. if (key_len && value_len) {
  155. req.headers[std::string(key,key_len)] = std::string(value,value_len);
  156. key_len = value_len = 0;
  157. }
  158. }
  159. if (range) {
  160. req.headers["Range"] = std::string("bytes=").append(range);
  161. }
  162. if (data || form) {
  163. if (method == NULL) {
  164. req.method = HTTP_POST;
  165. }
  166. if (data) {
  167. req.body = data;
  168. }
  169. else if (form) {
  170. const char* p = form;
  171. const char* key = p;
  172. const char* value = NULL;
  173. int key_len = 0;
  174. int value_len = 0;
  175. state = s_key;
  176. while (*p != '\0') {
  177. if (*p == ' ') {
  178. if (key_len && value_len) {
  179. FormData data;
  180. if (*value == '@') {
  181. data.filename = std::string(value+1, value_len-1);
  182. }
  183. else {
  184. data.content = std::string(value, value_len);
  185. }
  186. req.form[std::string(key,key_len)] = data;
  187. key_len = value_len = 0;
  188. }
  189. state = s_key;
  190. key = p+1;
  191. }
  192. else if (*p == '=') {
  193. state = s_value;
  194. value = p+1;
  195. }
  196. else {
  197. state == s_key ? ++key_len : ++value_len;
  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. // 创建http客户端
  216. http_client_t* hc = http_client_new();
  217. send:
  218. // 发送请求,接收响应
  219. ret = http_client_send(hc, &req, &res);
  220. if (verbose) {
  221. printf("%s\n", req.Dump(true,true).c_str());
  222. }
  223. if (ret != 0) {
  224. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  225. }
  226. else {
  227. if (verbose) {
  228. printf("%s\n", res.Dump(true,true).c_str());
  229. }
  230. else {
  231. printf("%s\n", res.body.c_str());
  232. }
  233. }
  234. if (--send_count > 0) {
  235. printf("send again later...%d\n", send_count);
  236. #ifdef _WIN32
  237. Sleep(3*1000);
  238. #else
  239. sleep(3);
  240. #endif
  241. goto send;
  242. }
  243. // 销毁http客户端
  244. http_client_del(hc);
  245. return ret;
  246. }