UrlRequestInfo.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "UrlRequestInfo.h"
  36. #include "TorrentRequestInfo.h"
  37. #include "MetalinkRequestInfo.h"
  38. #include "prefs.h"
  39. #include "DownloadEngineFactory.h"
  40. extern RequestInfo* requestInfo;
  41. extern void setSignalHander(int signal, void (*handler)(int), int flags);
  42. void UrlRequestInfo::adjustRequestSize(Requests& requests,
  43. Requests& reserved,
  44. int maxConnections) const
  45. {
  46. if(maxConnections > 0 && (int)requests.size() > maxConnections) {
  47. copy(requests.begin()+maxConnections, requests.end(),
  48. back_inserter(reserved));
  49. //insert_iterator<Requests>(reserved, reserved.end()));
  50. requests.erase(requests.begin()+maxConnections, requests.end());
  51. }
  52. }
  53. RequestInfo* UrlRequestInfo::createNextRequestInfo() const
  54. {
  55. #ifdef ENABLE_BITTORRENT
  56. if(op->getAsBool(PREF_FOLLOW_TORRENT) &&
  57. Util::endsWith(fileInfo.filename, ".torrent")) {
  58. return new TorrentRequestInfo(fileInfo.filename, op);
  59. } else
  60. #endif // ENABLE_BITTORRENT
  61. #ifdef ENABLE_METALINK
  62. if(op->getAsBool(PREF_FOLLOW_METALINK) &&
  63. Util::endsWith(fileInfo.filename, ".metalink")) {
  64. return new MetalinkRequestInfo(fileInfo.filename, op);
  65. } else
  66. #endif // ENABLE_METALINK
  67. {
  68. return 0;
  69. }
  70. }
  71. void handler(int signal) {
  72. printf(_("\nstopping application...\n"));
  73. fflush(stdout);
  74. requestInfo->getDownloadEngine()->segmentMan->save();
  75. requestInfo->getDownloadEngine()->segmentMan->diskWriter->closeFile();
  76. delete requestInfo->getDownloadEngine();
  77. printf(_("done\n"));
  78. exit(EXIT_SUCCESS);
  79. }
  80. class CreateRequest {
  81. private:
  82. Requests* requestsPtr;
  83. string referer;
  84. int split;
  85. public:
  86. CreateRequest(Requests* requestsPtr,
  87. const string& referer,
  88. int split)
  89. :requestsPtr(requestsPtr),
  90. referer(referer),
  91. split(split) {}
  92. void operator()(const string& url) {
  93. for(int s = 1; s <= split; s++) {
  94. Request* req = new Request();
  95. req->setReferer(referer);
  96. if(req->setUrl(url)) {
  97. requestsPtr->push_back(req);
  98. } else {
  99. fprintf(stderr, _("Unrecognized URL or unsupported protocol: %s\n"),
  100. req->getUrl().c_str());
  101. delete req;
  102. }
  103. }
  104. }
  105. };
  106. RequestInfo* UrlRequestInfo::execute() {
  107. Requests requests;
  108. Requests reserved;
  109. for_each(urls.begin(), urls.end(),
  110. CreateRequest(&requests,
  111. op->get(PREF_REFERER),
  112. op->getAsInt(PREF_SPLIT)));
  113. adjustRequestSize(requests, reserved, maxConnections);
  114. e = DownloadEngineFactory::newConsoleEngine(op, requests, reserved);
  115. setSignalHander(SIGINT, handler, 0);
  116. setSignalHander(SIGTERM, handler, 0);
  117. RequestInfo* next = 0;
  118. try {
  119. e->run();
  120. if(e->segmentMan->finished()) {
  121. printDownloadCompeleteMessage(e->segmentMan->getFilePath());
  122. fileInfo.filename = e->segmentMan->getFilePath();
  123. fileInfo.length = e->segmentMan->totalSize;
  124. fileInfo.checksum = checksum;
  125. next = createNextRequestInfo();
  126. } else {
  127. e->segmentMan->save();
  128. e->segmentMan->diskWriter->closeFile();
  129. printDownloadAbortMessage();
  130. }
  131. } catch(Exception *e) {
  132. logger->error("Exception caught", e);
  133. delete e;
  134. fail = true;
  135. }
  136. for_each(requests.begin(), requests.end(), Deleter());
  137. for_each(reserved.begin(), reserved.end(), Deleter());
  138. setSignalHander(SIGINT, SIG_DFL, 0);
  139. setSignalHander(SIGTERM, SIG_DFL, 0);
  140. delete e;
  141. e = 0;
  142. return next;
  143. }