FeatureConfig.cc 4.8 KB

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