1
0

curl.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 GET httpbin.org/get
  54. curl -v POST httpbin.org/post user=admin pswd=123456
  55. curl -v PUT httpbin.org/put user=admin pswd=123456
  56. curl -v localhost:8080
  57. curl -v localhost:8080 -r 0-9
  58. curl -v localhost:8080/ping
  59. curl -v localhost:8080/query?page_no=1\&page_size=10
  60. curl -v localhost:8080/echo hello,world!
  61. curl -v localhost:8080/kv user=admin\&pswd=123456
  62. curl -v localhost:8080/json user=admin pswd=123456
  63. curl -v localhost:8080/form -F file=@filename
  64. )";
  65. static void print_usage() {
  66. printf("Usage: curl [%s] [METHOD] url [header_field:header_value] [body_key=body_value]\n", options);
  67. }
  68. static void print_version() {
  69. printf("curl version 1.0.0\n");
  70. }
  71. static void print_help() {
  72. print_usage();
  73. puts(help);
  74. print_version();
  75. }
  76. static bool is_upper_string(const char* str) {
  77. const char* p = str;
  78. while (*p >= 'A' && *p <= 'Z') ++p;
  79. return *p == '\0';
  80. }
  81. int parse_cmdline(int argc, char* argv[]) {
  82. int opt;
  83. int opt_idx;
  84. while ((opt = getopt_long(argc, argv, options, long_options, &opt_idx)) != EOF) {
  85. switch(opt) {
  86. case 'h': print_help(); exit(0);
  87. case 'V': print_version(); exit(0);
  88. case 'v': verbose = true; break;
  89. case 'X': method = optarg; break;
  90. case 'H': headers = optarg; break;
  91. case 'r': range = optarg; break;
  92. case 'd': data = optarg; break;
  93. case 'F': form = optarg; break;
  94. case 'n': send_count = atoi(optarg); break;
  95. default: break;
  96. }
  97. }
  98. if (optind == argc) {
  99. printf("Missing url\n");
  100. print_usage();
  101. exit(-1);
  102. }
  103. if (is_upper_string(argv[optind])) {
  104. method = argv[optind++];
  105. }
  106. url = argv[optind++];
  107. return 0;
  108. }
  109. int main(int argc, char* argv[]) {
  110. if (argc < 2) {
  111. print_usage();
  112. return 0;
  113. }
  114. parse_cmdline(argc, argv);
  115. int ret = 0;
  116. HttpRequest req;
  117. if (grpc) {
  118. http_version = 2;
  119. req.content_type = APPLICATION_GRPC;
  120. }
  121. if (http_version == 2) {
  122. req.http_major = 2;
  123. req.http_minor = 0;
  124. }
  125. req.url = url;
  126. if (method) {
  127. req.method = http_method_enum(method);
  128. }
  129. enum {
  130. s_key,
  131. s_value,
  132. } state = s_key;
  133. if (headers) {
  134. const char* p = headers;
  135. const char* key = p;
  136. const char* value = NULL;
  137. int key_len = 0;
  138. int value_len = 0;
  139. state = s_key;
  140. while (*p != '\0') {
  141. if (*p == ' ') {
  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. state = s_key;
  146. }
  147. }
  148. else if (*p == ':') {
  149. state = s_value;
  150. }
  151. else {
  152. if (state == s_key) {
  153. if (++key_len == 1) key = p;
  154. }
  155. else {
  156. if (++value_len == 1) value = p;
  157. }
  158. }
  159. ++p;
  160. }
  161. if (key_len && value_len) {
  162. req.headers[std::string(key,key_len)] = std::string(value,value_len);
  163. key_len = value_len = 0;
  164. }
  165. }
  166. if (range) {
  167. req.headers["Range"] = std::string("bytes=").append(range);
  168. }
  169. if (data || form) {
  170. if (method == NULL) {
  171. req.method = HTTP_POST;
  172. }
  173. if (data) {
  174. req.body = data;
  175. }
  176. else if (form) {
  177. const char* p = form;
  178. const char* key = p;
  179. const char* value = NULL;
  180. int key_len = 0;
  181. int value_len = 0;
  182. state = s_key;
  183. while (*p != '\0') {
  184. if (*p == ' ') {
  185. if (key_len && value_len) {
  186. FormData data;
  187. if (*value == '@') {
  188. data.filename = std::string(value+1, value_len-1);
  189. }
  190. else {
  191. data.content = std::string(value, value_len);
  192. }
  193. req.form[std::string(key,key_len)] = data;
  194. key_len = value_len = 0;
  195. state = s_key;
  196. }
  197. }
  198. else if (*p == '=') {
  199. state = s_value;
  200. }
  201. else {
  202. if (state == s_key) {
  203. if (++key_len == 1) key = p;
  204. }
  205. else {
  206. if (++value_len == 1) value = p;
  207. }
  208. }
  209. ++p;
  210. }
  211. if (key_len && value_len) {
  212. // printf("key=%.*s value=%.*s\n", key_len, key, value_len, value);
  213. FormData data;
  214. if (*value == '@') {
  215. data.filename = std::string(value+1, value_len-1);
  216. }
  217. else {
  218. data.content = std::string(value, value_len);
  219. }
  220. req.form[std::string(key,key_len)] = data;
  221. }
  222. }
  223. }
  224. for (int d = optind; d < argc; ++d) {
  225. const char* arg = argv[d];
  226. const char* pos = NULL;
  227. if ((pos = strchr(arg, ':')) != NULL) {
  228. // header_field:header_value
  229. *(char*)pos = '\0';
  230. req.headers[arg] = pos + 1;
  231. } else {
  232. if (method == NULL) {
  233. req.method = HTTP_POST;
  234. }
  235. if ((pos = strchr(arg, '&')) != NULL) {
  236. if (req.ContentType() == CONTENT_TYPE_NONE) {
  237. req.content_type = X_WWW_FORM_URLENCODED;
  238. }
  239. req.body = arg;
  240. }
  241. else if ((pos = strchr(arg, '=')) != NULL) {
  242. // body_key=body_value
  243. if (req.ContentType() == CONTENT_TYPE_NONE) {
  244. req.content_type = APPLICATION_JSON;
  245. }
  246. *(char*)pos = '\0';
  247. req.Set(arg, pos + 1);
  248. }
  249. else {
  250. if (req.ContentType() == CONTENT_TYPE_NONE) {
  251. req.content_type = TEXT_PLAIN;
  252. }
  253. req.body = arg;
  254. }
  255. }
  256. }
  257. HttpResponse res;
  258. /*
  259. res.head_cb = [](const http_headers& headers){
  260. for (auto& header : headers) {
  261. printf("%s: %s\r\n", header.first.c_str(), header.second.c_str());
  262. }
  263. printf("\r\n");
  264. };
  265. res.body_cb = [](const char* data, size_t size){
  266. printf("%.*s", (int)size, data);
  267. };
  268. */
  269. res.chunked_cb = [](const char* data, size_t size){
  270. printf("%.*s", (int)size, data);
  271. };
  272. http_client_t* cli = http_client_new();
  273. send:
  274. if (verbose) {
  275. printf("%s\n", req.Dump(true,true).c_str());
  276. }
  277. ret = http_client_send(cli, &req, &res);
  278. if (ret != 0) {
  279. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  280. } else {
  281. if (verbose) {
  282. printf("%s\n", res.Dump(true,true).c_str());
  283. }
  284. else {
  285. printf("%s\n", res.body.c_str());
  286. }
  287. }
  288. if (--send_count > 0) {
  289. printf("send again later...%d\n", send_count);
  290. #ifdef _WIN32
  291. Sleep(3*1000);
  292. #else
  293. sleep(3);
  294. #endif
  295. goto send;
  296. }
  297. http_client_del(cli);
  298. return ret;
  299. }