wget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * @build: make examples
  3. * @server bin/httpd -s restart -d
  4. * @client bin/wget http://127.0.0.1:8080/
  5. */
  6. #include "http_client.h"
  7. #include "htime.h"
  8. using namespace hv;
  9. typedef std::function<void(size_t received_bytes, size_t total_bytes)> wget_progress_cb;
  10. static int wget(const char* url, const char* filepath, wget_progress_cb progress_cb = NULL) {
  11. int ret = 0;
  12. HttpClient cli;
  13. HttpRequest req;
  14. HttpResponse resp;
  15. // HEAD
  16. req.method = HTTP_HEAD;
  17. req.url = url;
  18. ret = cli.send(&req, &resp);
  19. if (ret != 0) {
  20. fprintf(stderr, "request error: %d\n", ret);
  21. return ret;
  22. }
  23. printd("%s", resp.Dump(true, false).c_str());
  24. if (resp.status_code == HTTP_STATUS_NOT_FOUND) {
  25. fprintf(stderr, "404 Not Found\n");
  26. return 404;
  27. }
  28. // use Range?
  29. bool use_range = false;
  30. int range_bytes = 1 << 20; // 1M
  31. long from = 0, to = 0;
  32. std::string accept_ranges = resp.GetHeader("Accept-Ranges");
  33. size_t content_length = hv::from_string<size_t>(resp.GetHeader("Content-Length"));
  34. // use Range if server accept_ranges and content_length > 1M
  35. if (resp.status_code == 200 &&
  36. accept_ranges == "bytes" &&
  37. content_length > range_bytes) {
  38. use_range = true;
  39. }
  40. // open file
  41. HFile file;
  42. ret = file.open(filepath, "wb");
  43. if (ret != 0) {
  44. fprintf(stderr, "Failed to open file %s\n", filepath);
  45. return ret;
  46. }
  47. printf("Save file to %s ...\n", filepath);
  48. // GET
  49. req.method = HTTP_GET;
  50. req.timeout = 3600; // 1h
  51. if (!use_range) {
  52. size_t received_bytes = 0;
  53. req.http_cb = [&file, &content_length, &received_bytes, &progress_cb]
  54. (HttpMessage* resp, http_parser_state state, const char* data, size_t size) {
  55. if (state == HP_HEADERS_COMPLETE) {
  56. content_length = hv::from_string<size_t>(resp->GetHeader("Content-Length"));
  57. printd("%s", resp->Dump(true, false).c_str());
  58. } else if (state == HP_BODY) {
  59. if (data && size) {
  60. file.write(data, size);
  61. received_bytes += size;
  62. if (progress_cb) {
  63. progress_cb(received_bytes, content_length);
  64. }
  65. }
  66. }
  67. };
  68. ret = cli.send(&req, &resp);
  69. if (ret != 0) {
  70. fprintf(stderr, "request error: %d\n", ret);
  71. goto error;
  72. }
  73. return 0;
  74. }
  75. // Range: bytes=from-to
  76. while (from < content_length) {
  77. to = from + range_bytes - 1;
  78. if (to >= content_length) to = content_length - 1;
  79. req.SetRange(from, to);
  80. printd("%s", req.Dump(true, false).c_str());
  81. ret = cli.send(&req, &resp);
  82. if (ret != 0) {
  83. fprintf(stderr, "request error: %d\n", ret);
  84. goto error;
  85. }
  86. printd("%s", resp.Dump(true, false).c_str());
  87. file.write(resp.body.data(), resp.body.size());
  88. from = to + 1;
  89. if (progress_cb) {
  90. progress_cb(from, content_length);
  91. }
  92. }
  93. return 0;
  94. error:
  95. file.close();
  96. remove(filepath);
  97. return ret;
  98. }
  99. int main(int argc, char** argv) {
  100. if (argc < 2) {
  101. printf("Usage: %s url [filepath]\n", argv[0]);
  102. return -10;
  103. }
  104. const char* url = argv[1];
  105. const char* filepath = "index.html";
  106. if (argc > 2) {
  107. filepath = argv[2];
  108. } else {
  109. const char* path = strrchr(url, '/');
  110. if (path && path[1]) {
  111. filepath = path + 1;
  112. }
  113. }
  114. unsigned int start_time = gettick_ms();
  115. int last_progress = 0;
  116. wget(url, filepath, [&last_progress](size_t received_bytes, size_t total_bytes) {
  117. // print progress
  118. if (total_bytes == 0) {
  119. printf("\rprogress: %lu/? = ?", (unsigned long)received_bytes);
  120. } else {
  121. int cur_progress = received_bytes * 100 / total_bytes;
  122. if (cur_progress > last_progress) {
  123. printf("\rprogress: %lu/%lu = %d%%", (unsigned long)received_bytes, (unsigned long)total_bytes, (int)cur_progress);
  124. last_progress = cur_progress;
  125. }
  126. }
  127. fflush(stdout);
  128. });
  129. unsigned int end_time = gettick_ms();
  130. printf("\ncost time %u ms\n", end_time - start_time);
  131. return 0;
  132. }