UrlRequestInfo.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #include "RecoverableException.h"
  41. #include "FatalException.h"
  42. #include "message.h"
  43. #include "RequestFactory.h"
  44. #include "GlowFileAllocator.h"
  45. #include "File.h"
  46. #include "DefaultDiskWriter.h"
  47. #include "DlAbortEx.h"
  48. #include "DNSCache.h"
  49. std::ostream& operator<<(std::ostream& o, const HeadResult& hr) {
  50. o << "filename = " << hr.filename << ", " << "totalLength = " << hr.totalLength;
  51. return o;
  52. }
  53. extern volatile sig_atomic_t haltRequested;
  54. void UrlRequestInfo::adjustRequestSize(Requests& requests,
  55. Requests& reserved,
  56. int maxConnections) const
  57. {
  58. if(maxConnections > 0 && (int)requests.size() > maxConnections) {
  59. copy(requests.begin()+maxConnections, requests.end(),
  60. back_inserter(reserved));
  61. //insert_iterator<Requests>(reserved, reserved.end()));
  62. requests.erase(requests.begin()+maxConnections, requests.end());
  63. }
  64. }
  65. RequestInfo* UrlRequestInfo::createNextRequestInfo() const
  66. {
  67. #ifdef ENABLE_BITTORRENT
  68. if(op->getAsBool(PREF_FOLLOW_TORRENT) &&
  69. Util::endsWith(fileInfo.filename, ".torrent")) {
  70. return new TorrentRequestInfo(fileInfo.filename, op);
  71. } else
  72. #endif // ENABLE_BITTORRENT
  73. #ifdef ENABLE_METALINK
  74. if(op->getAsBool(PREF_FOLLOW_METALINK) &&
  75. Util::endsWith(fileInfo.filename, ".metalink")) {
  76. return new MetalinkRequestInfo(fileInfo.filename, op);
  77. } else
  78. #endif // ENABLE_METALINK
  79. {
  80. return 0;
  81. }
  82. }
  83. void handler(int signal) {
  84. haltRequested = true;
  85. }
  86. /*
  87. class CreateRequest {
  88. private:
  89. Requests* requestsPtr;
  90. string referer;
  91. int split;
  92. string method;
  93. public:
  94. CreateRequest(Requests* requestsPtr,
  95. const string& referer,
  96. int split,
  97. const string& method = Request::METHOD_GET)
  98. :requestsPtr(requestsPtr),
  99. referer(referer),
  100. split(split),
  101. method(method)
  102. {}
  103. void operator()(const string& url) {
  104. for(int s = 1; s <= split; s++) {
  105. RequestHandle req = RequestFactorySingletonHolder::instance()->createRequest();
  106. req->setReferer(referer);
  107. req->setMethod(method);
  108. if(req->setUrl(url)) {
  109. requestsPtr->push_back(req);
  110. } else {
  111. fprintf(stderr, _("Unrecognized URL or unsupported protocol: %s\n"),
  112. req->getUrl().c_str());
  113. }
  114. }
  115. }
  116. };
  117. */
  118. void UrlRequestInfo::printUrls(const Strings& urls) const {
  119. for(Strings::const_iterator itr = urls.begin(); itr != urls.end(); itr++) {
  120. logger->info("Adding URL: %s", itr->c_str());
  121. }
  122. }
  123. /*
  124. HeadResultHandle UrlRequestInfo::getHeadResult() {
  125. Requests requests;
  126. for_each(urls.begin(), urls.end(),
  127. CreateRequest(&requests,
  128. op->get(PREF_REFERER),
  129. 1,
  130. Request::METHOD_HEAD));
  131. if(requests.size() == 0) {
  132. return 0;
  133. }
  134. Requests reserved(requests.begin()+1, requests.end());
  135. requests.erase(requests.begin()+1, requests.end());
  136. SharedHandle<ConsoleDownloadEngine> e(DownloadEngineFactory::newConsoleEngine(op, requests, reserved));
  137. HeadResultHandle hr = 0;
  138. e->run();
  139. hr = new HeadResult();
  140. hr->filename = e->segmentMan->filename;
  141. hr->totalLength = e->segmentMan->totalSize;
  142. return hr;
  143. }
  144. */
  145. RequestInfos UrlRequestInfo::execute() {
  146. {
  147. DNSCacheHandle dnsCache = new SimpleDNSCache();
  148. DNSCacheSingletonHolder::instance(dnsCache);
  149. }
  150. RequestInfo* next = 0;
  151. try {
  152. RequestGroups requestGroups;
  153. Strings urls;
  154. urls.push_back("http://localhost/~tujikawa/linux-2.6.6.tar.bz2");
  155. RequestGroupHandle rg1 = new RequestGroup(urls, op);
  156. Strings urls2;
  157. urls2.push_back("http://localhost/~tujikawa/linux-2.6.19.1.tar.bz2");
  158. RequestGroupHandle rg2 = new RequestGroup(urls2, op);
  159. requestGroups.push_back(rg1);
  160. requestGroups.push_back(rg2);
  161. SharedHandle<ConsoleDownloadEngine> e(DownloadEngineFactory::newConsoleEngine(op, requestGroups));
  162. Strings reservedUrls1;
  163. reservedUrls1.push_back("http://localhost/~tujikawa/linux-2.6.1.tar.bz2");
  164. RequestGroupHandle rrg1 = new RequestGroup(reservedUrls1, op);
  165. e->_requestGroupMan->addReservedGroup(rrg1);
  166. e->fillCommand();
  167. // The number of simultaneous download is specified by PREF_MAX_CONCURRENT_DOWNLOADS.
  168. // The remaining urls are queued into FillRequestGroupCommand.
  169. // It observes the number of simultaneous downloads and if it is under
  170. // the limit, it adds RequestGroup object from its queue to DownloadEngine.
  171. // This is done every 1 second. At the same time, it removes finished/error
  172. // RequestGroup from DownloadEngine.
  173. Util::setGlobalSignalHandler(SIGINT, handler, 0);
  174. Util::setGlobalSignalHandler(SIGTERM, handler, 0);
  175. e->run();
  176. if(e->_requestGroupMan->downloadFinished()) {
  177. next = createNextRequestInfo();
  178. } else {
  179. e->_requestGroupMan->save();
  180. e->_requestGroupMan->closeFile();
  181. printDownloadAbortMessage();
  182. }
  183. } catch(RecoverableException *ex) {
  184. logger->error("Exception caught", ex);
  185. delete ex;
  186. fail = true;
  187. }
  188. RequestInfos nextReqInfos;
  189. if(next) {
  190. nextReqInfos.push_front(next);
  191. }
  192. Util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
  193. Util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
  194. return nextReqInfos;
  195. }