1
0

wget.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, bool use_range = true) {
  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. int range_bytes = 1 << 20; // 1M
  30. long from = 0, to = 0;
  31. size_t content_length = hv::from_string<size_t>(resp.GetHeader("Content-Length"));
  32. if (use_range) {
  33. use_range = false;
  34. std::string accept_ranges = resp.GetHeader("Accept-Ranges");
  35. // use Range if server accept_ranges and content_length > 1M
  36. if (resp.status_code == 200 &&
  37. accept_ranges == "bytes" &&
  38. content_length > range_bytes) {
  39. use_range = true;
  40. }
  41. }
  42. // open file
  43. std::string filepath_download(filepath);
  44. filepath_download += ".download";
  45. HFile file;
  46. if (use_range) {
  47. ret = file.open(filepath_download.c_str(), "ab");
  48. from = file.size();
  49. } else {
  50. ret = file.open(filepath_download.c_str(), "wb");
  51. }
  52. if (ret != 0) {
  53. fprintf(stderr, "Failed to open file %s\n", filepath_download.c_str());
  54. return ret;
  55. }
  56. printf("Save file to %s ...\n", filepath);
  57. // GET
  58. req.method = HTTP_GET;
  59. req.timeout = 3600; // 1h
  60. if (!use_range) {
  61. size_t received_bytes = 0;
  62. req.http_cb = [&file, &content_length, &received_bytes, &progress_cb]
  63. (HttpMessage* resp, http_parser_state state, const char* data, size_t size) {
  64. if (state == HP_HEADERS_COMPLETE) {
  65. content_length = hv::from_string<size_t>(resp->GetHeader("Content-Length"));
  66. printd("%s", resp->Dump(true, false).c_str());
  67. } else if (state == HP_BODY) {
  68. if (data && size) {
  69. file.write(data, size);
  70. received_bytes += size;
  71. if (progress_cb) {
  72. progress_cb(received_bytes, content_length);
  73. }
  74. }
  75. }
  76. };
  77. ret = cli.send(&req, &resp);
  78. if (ret != 0) {
  79. fprintf(stderr, "request error: %d\n", ret);
  80. goto error;
  81. }
  82. goto success;
  83. }
  84. // Range: bytes=from-to
  85. while (from < content_length) {
  86. to = from + range_bytes - 1;
  87. if (to >= content_length) to = content_length - 1;
  88. req.SetRange(from, to);
  89. printd("%s", req.Dump(true, false).c_str());
  90. ret = cli.send(&req, &resp);
  91. if (ret != 0) {
  92. fprintf(stderr, "request error: %d\n", ret);
  93. goto error;
  94. }
  95. printd("%s", resp.Dump(true, false).c_str());
  96. file.write(resp.body.data(), resp.body.size());
  97. from = to + 1;
  98. if (progress_cb) {
  99. progress_cb(from, content_length);
  100. }
  101. }
  102. success:
  103. file.close();
  104. ret = rename(filepath_download.c_str(), filepath);
  105. if (ret != 0) {
  106. fprintf(stderr, "mv %s => %s failed: %s:%d\n", filepath_download.c_str(), filepath, strerror(ret), ret);
  107. }
  108. return ret;
  109. error:
  110. file.close();
  111. // remove(filepath_download.c_str());
  112. return ret;
  113. }
  114. int main(int argc, char** argv) {
  115. if (argc < 2) {
  116. printf("Usage: %s [--use_range] url [filepath]\n", argv[0]);
  117. return -10;
  118. }
  119. int idx = 1;
  120. bool use_range = false;
  121. if (strcmp(argv[idx], "--use_range") == 0) {
  122. use_range = true;
  123. ++idx;
  124. }
  125. const char* url = argv[idx++];
  126. const char* filepath = "index.html";
  127. if (argv[idx]) {
  128. filepath = argv[idx];
  129. } else {
  130. const char* path = strrchr(url, '/');
  131. if (path && path[1]) {
  132. filepath = path + 1;
  133. }
  134. }
  135. unsigned int start_time = gettick_ms();
  136. int last_progress = 0;
  137. wget(url, filepath, [&last_progress](size_t received_bytes, size_t total_bytes) {
  138. // print progress
  139. if (total_bytes == 0) {
  140. printf("\rprogress: %lu/? = ?", (unsigned long)received_bytes);
  141. } else {
  142. int cur_progress = received_bytes * 100 / total_bytes;
  143. if (cur_progress > last_progress) {
  144. printf("\rprogress: %lu/%lu = %d%%", (unsigned long)received_bytes, (unsigned long)total_bytes, (int)cur_progress);
  145. last_progress = cur_progress;
  146. }
  147. }
  148. fflush(stdout);
  149. }, use_range);
  150. unsigned int end_time = gettick_ms();
  151. unsigned int cost_time = end_time - start_time;
  152. printf("\ncost time %u ms\n", cost_time);
  153. // 1B/ms = 1KB/s = 8Kbps
  154. printf("download rate = %lu KB/s\n", (unsigned long)hv_filesize(filepath) / cost_time);
  155. return 0;
  156. }