FeatureConfig.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "FeatureConfig.h"
  36. #include <numeric>
  37. #include "array_fun.h"
  38. #include "util.h"
  39. #include "Request.h"
  40. namespace aria2 {
  41. SharedHandle<FeatureConfig> FeatureConfig::featureConfig_;
  42. const std::string FeatureConfig::FEATURE_HTTPS("HTTPS");
  43. const std::string FeatureConfig::FEATURE_BITTORRENT("BitTorrent");
  44. const std::string FeatureConfig::FEATURE_METALINK("Metalink");
  45. const std::string FeatureConfig::FEATURE_MESSAGE_DIGEST("Message Digest");
  46. const std::string FeatureConfig::FEATURE_ASYNC_DNS("Async DNS");
  47. const std::string FeatureConfig::FEATURE_XML_RPC("XML-RPC");
  48. const std::string FeatureConfig::FEATURE_GZIP("GZip");
  49. const std::string FeatureConfig::FEATURE_FIREFOX3_COOKIE("Firefox3 Cookie");
  50. #ifdef ENABLE_SSL
  51. # define HTTPS_ENABLED true
  52. #else // !ENABLE_SSL
  53. # define HTTPS_ENABLED false
  54. #endif // !ENABLE_SSL
  55. #ifdef ENABLE_BITTORRENT
  56. # define BITTORRENT_ENABLED true
  57. #else // !ENABLE_BITTORRENT
  58. # define BITTORRENT_ENABLED false
  59. #endif // !ENABLE_BITTORRENT
  60. #ifdef ENABLE_METALINK
  61. # define METALINK_ENABLED true
  62. #else // !ENABLE_METALINK
  63. # define METALINK_ENABLED false
  64. #endif // !ENABLE_METALINK
  65. #ifdef ENABLE_MESSAGE_DIGEST
  66. # define MESSAGE_DIGEST_ENABLED true
  67. #else // !ENABLE_MESSAGE_DIGEST
  68. # define MESSAGE_DIGEST_ENABLED false
  69. #endif // !ENABLE_MESSAGE_DIGEST
  70. #ifdef ENABLE_ASYNC_DNS
  71. # define ASYNC_DNS_ENABLED true
  72. #else // !ENABLE_ASYNC_DNS
  73. # define ASYNC_DNS_ENABLED false
  74. #endif // !ENABLE_ASYNC_DNS
  75. #ifdef ENABLE_XML_RPC
  76. # define XML_RPC_ENABLED true
  77. #else // !ENABLE_XML_RPC
  78. # define XML_RPC_ENABLED false
  79. #endif // !ENABLE_XML_RPC
  80. #ifdef HAVE_LIBZ
  81. # define GZIP_ENABLED true
  82. #else // !HAVE_LIBZ
  83. # define GZIP_ENABLED false
  84. #endif // !HAVE_LIBZ
  85. #ifdef HAVE_SQLITE3
  86. # define FIREFOX3_COOKIE_ENABLED true
  87. #else // !HAVE_SQLITE3
  88. # define FIREFOX3_COOKIE_ENABLED false
  89. #endif // !HAVE_SQLITE3
  90. FeatureConfig::FeatureConfig() {
  91. defaultPorts_.insert(PortMap::value_type(Request::PROTO_HTTP, 80));
  92. defaultPorts_.insert(PortMap::value_type(Request::PROTO_HTTPS, 443));
  93. defaultPorts_.insert(PortMap::value_type(Request::PROTO_FTP, 21));
  94. FeatureMap::value_type featureArray[] = {
  95. FeatureMap::value_type(FEATURE_HTTPS, HTTPS_ENABLED),
  96. FeatureMap::value_type(FEATURE_BITTORRENT, BITTORRENT_ENABLED),
  97. FeatureMap::value_type(FEATURE_METALINK, METALINK_ENABLED),
  98. FeatureMap::value_type(FEATURE_MESSAGE_DIGEST, MESSAGE_DIGEST_ENABLED),
  99. FeatureMap::value_type(FEATURE_ASYNC_DNS, ASYNC_DNS_ENABLED),
  100. FeatureMap::value_type(FEATURE_XML_RPC, XML_RPC_ENABLED),
  101. FeatureMap::value_type(FEATURE_GZIP, GZIP_ENABLED),
  102. FeatureMap::value_type(FEATURE_FIREFOX3_COOKIE, FIREFOX3_COOKIE_ENABLED),
  103. };
  104. features_.insert(vbegin(featureArray), vend(featureArray));
  105. }
  106. const SharedHandle<FeatureConfig>& FeatureConfig::getInstance()
  107. {
  108. if(featureConfig_.isNull()) {
  109. featureConfig_.reset(new FeatureConfig());
  110. }
  111. return featureConfig_;
  112. }
  113. uint16_t FeatureConfig::getDefaultPort(const std::string& protocol) const
  114. {
  115. PortMap::const_iterator itr = defaultPorts_.find(protocol);
  116. if(itr == defaultPorts_.end()) {
  117. return 0;
  118. } else {
  119. return itr->second;
  120. }
  121. }
  122. bool FeatureConfig::isSupported(const std::string& feature) const
  123. {
  124. FeatureMap::const_iterator itr = features_.find(feature);
  125. if(itr == features_.end()) {
  126. return false;
  127. } else {
  128. return itr->second;
  129. }
  130. }
  131. std::string FeatureConfig::featureSummary() const
  132. {
  133. std::string s;
  134. for(FeatureMap::const_iterator i = features_.begin(), eoi = features_.end();
  135. i != eoi; ++i) {
  136. if((*i).second) {
  137. s += (*i).first;
  138. s += ", ";
  139. }
  140. }
  141. return util::strip(s, ", ");
  142. }
  143. } // namespace aria2