FeatureConfig.cc 4.6 KB

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