FeatureConfig.cc 4.2 KB

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