wget.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * @build: make examples
  3. * @server bin/httpd -s restart -d
  4. * @client bin/wget 127.0.0.1:8080/
  5. */
  6. #include "requests.h"
  7. static int wget(const char* url, const char* filepath) {
  8. HFile file;
  9. if (file.open(filepath, "wb") != 0) {
  10. fprintf(stderr, "Failed to open file %s\n", filepath);
  11. return -20;
  12. }
  13. printf("Save file to %s ...\n", filepath);
  14. // HEAD
  15. auto resp = requests::head(url);
  16. if (resp == NULL) {
  17. fprintf(stderr, "request failed!\n");
  18. return -1;
  19. }
  20. printd("%s", resp->Dump(true, false).c_str());
  21. bool use_range = false;
  22. int range_bytes = 1 << 20; // 1M
  23. std::string accept_ranges = resp->GetHeader("Accept-Ranges");
  24. size_t content_length = hv::from_string<size_t>(resp->GetHeader("Content-Length"));
  25. // use Range if server accept_ranges and content_length > 1M
  26. if (resp->status_code == 200 &&
  27. accept_ranges == "bytes" &&
  28. content_length > range_bytes) {
  29. use_range = true;
  30. }
  31. // GET
  32. if (!use_range) {
  33. resp = requests::get(url);
  34. if (resp == NULL) {
  35. fprintf(stderr, "request failed!\n");
  36. return -1;
  37. }
  38. printd("%s", resp->Dump(true, false).c_str());
  39. file.write(resp->body.data(), resp->body.size());
  40. printf("progress: %ld/%ld = 100%%\n", (long)resp->body.size(), (long)resp->body.size());
  41. return 0;
  42. }
  43. // Range: bytes=from-to
  44. long from = 0, to = 0;
  45. int last_progress = 0;
  46. http_client_t* cli = http_client_new();
  47. HttpRequestPtr req(new HttpRequest);
  48. req->method = HTTP_GET;
  49. req->url = url;
  50. while (from < content_length) {
  51. to = from + range_bytes - 1;
  52. if (to >= content_length) to = content_length - 1;
  53. req->SetRange(from, to);
  54. printd("%s", req->Dump(true, false).c_str());
  55. int ret = http_client_send(cli, req.get(), resp.get());
  56. if (ret != 0) {
  57. fprintf(stderr, "request failed!\n");
  58. return -1;
  59. }
  60. printd("%s", resp->Dump(true, false).c_str());
  61. file.write(resp->body.data(), resp->body.size());
  62. from = to + 1;
  63. // print progress
  64. int cur_progress = from * 100 / content_length;
  65. if (cur_progress > last_progress) {
  66. printf("progress: %ld/%ld = %d%%\n", (long)from, (long)content_length, (int)cur_progress);
  67. last_progress = cur_progress;
  68. }
  69. }
  70. http_client_del(cli);
  71. return 0;
  72. }
  73. int main(int argc, char** argv) {
  74. if (argc < 2) {
  75. printf("Usage: %s url [filepath]\n", argv[0]);
  76. return -10;
  77. }
  78. const char* url = argv[1];
  79. const char* filepath = "index.html";
  80. if (argc > 2) {
  81. filepath = argv[2];
  82. } else {
  83. const char* path = strrchr(url, '/');
  84. if (path && path[1]) {
  85. filepath = path + 1;
  86. }
  87. }
  88. wget(url, filepath);
  89. return 0;
  90. }