OptionHandlerImpl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #ifndef _D_OPTION_HANDLER_IMPL_H_
  36. #define _D_OPTION_HANDLER_IMPL_H_
  37. #include "OptionHandler.h"
  38. #include <cstdio>
  39. #include <utility>
  40. #include <algorithm>
  41. #include <numeric>
  42. #include <sstream>
  43. #include <iterator>
  44. #include "NameMatchOptionHandler.h"
  45. #include "Util.h"
  46. #include "FatalException.h"
  47. #include "prefs.h"
  48. #include "Option.h"
  49. #include "StringFormat.h"
  50. #include "A2STR.h"
  51. #include "Request.h"
  52. namespace aria2 {
  53. class NullOptionHandler : public OptionHandler {
  54. public:
  55. virtual ~NullOptionHandler() {}
  56. virtual bool canHandle(const std::string& optName) { return true; }
  57. virtual void parse(Option* option, const std::string& arg) {}
  58. virtual bool hasTag(const std::string& tag) const { return false; }
  59. virtual void addTag(const std::string& tag) {}
  60. virtual std::string toTagString() const { return A2STR::NIL; }
  61. virtual const std::string& getName() const { return A2STR::NIL; }
  62. virtual const std::string& getDescription() const { return A2STR::NIL; }
  63. virtual const std::string& getDefaultValue() const { return A2STR::NIL; }
  64. virtual std::string createPossibleValuesString() const { return A2STR::NIL; }
  65. virtual bool isHidden() const { return true; }
  66. };
  67. class BooleanOptionHandler : public NameMatchOptionHandler {
  68. public:
  69. BooleanOptionHandler(const std::string& optName,
  70. const std::string& description = NO_DESCRIPTION,
  71. const std::string& defaultValue = NO_DEFAULT_VALUE):
  72. NameMatchOptionHandler(optName, description, defaultValue) {}
  73. virtual ~BooleanOptionHandler() {}
  74. virtual void parseArg(Option* option, const std::string& optarg)
  75. {
  76. if(optarg == "true") {
  77. option->put(_optName, V_TRUE);
  78. } else if(optarg == "false") {
  79. option->put(_optName, V_FALSE);
  80. } else {
  81. std::string msg = _optName+" "+_("must be either 'true' or 'false'.");
  82. throw FatalException(msg);
  83. }
  84. }
  85. virtual std::string createPossibleValuesString() const
  86. {
  87. return "true,false";
  88. }
  89. };
  90. class IntegerRangeOptionHandler : public NameMatchOptionHandler {
  91. private:
  92. int32_t _min;
  93. int32_t _max;
  94. public:
  95. IntegerRangeOptionHandler(const std::string& optName,
  96. const std::string& description,
  97. const std::string& defaultValue,
  98. int32_t min, int32_t max):
  99. NameMatchOptionHandler(optName, description, defaultValue),
  100. _min(min), _max(max) {}
  101. virtual ~IntegerRangeOptionHandler() {}
  102. virtual void parseArg(Option* option, const std::string& optarg)
  103. {
  104. IntSequence seq = Util::parseIntRange(optarg);
  105. while(seq.hasNext()) {
  106. int32_t v = seq.next();
  107. if(v < _min || _max < v) {
  108. std::string msg = _optName+" "+_("must be between %s and %s.");
  109. throw FatalException
  110. (StringFormat(msg.c_str(), Util::itos(_min).c_str(),
  111. Util::itos(_max).c_str()).str());
  112. }
  113. option->put(_optName, optarg);
  114. }
  115. }
  116. virtual std::string createPossibleValuesString() const
  117. {
  118. return Util::itos(_min)+"-"+Util::itos(_max);
  119. }
  120. };
  121. class NumberOptionHandler : public NameMatchOptionHandler {
  122. private:
  123. int64_t _min;
  124. int64_t _max;
  125. public:
  126. NumberOptionHandler(const std::string& optName,
  127. const std::string& description = NO_DESCRIPTION,
  128. const std::string& defaultValue = NO_DEFAULT_VALUE,
  129. int64_t min = -1,
  130. int64_t max = -1,
  131. bool hidden = false):
  132. NameMatchOptionHandler(optName, description, defaultValue, hidden),
  133. _min(min), _max(max) {}
  134. virtual ~NumberOptionHandler() {}
  135. virtual void parseArg(Option* option, const std::string& optarg)
  136. {
  137. int64_t num = Util::parseLLInt(optarg);
  138. parseArg(option, num);
  139. }
  140. void parseArg(Option* option, int64_t number)
  141. {
  142. if((_min == -1 || _min <= number) && (_max == -1 || number <= _max)) {
  143. option->put(_optName, Util::itos(number));
  144. } else {
  145. std::string msg = _optName+" ";
  146. if(_min == -1 && _max != -1) {
  147. msg += StringFormat(_("must be smaller than or equal to %s."),
  148. Util::itos(_max).c_str()).str();
  149. } else if(_min != -1 && _max != -1) {
  150. msg += StringFormat(_("must be between %s and %s."),
  151. Util::itos(_min).c_str(), Util::itos(_max).c_str()).str();
  152. } else if(_min != -1 && _max == -1) {
  153. msg += StringFormat(_("must be greater than or equal to %s."),
  154. Util::itos(_min).c_str()).str();
  155. } else {
  156. msg += _("must be a number.");
  157. }
  158. throw FatalException(msg);
  159. }
  160. }
  161. virtual std::string createPossibleValuesString() const
  162. {
  163. return (_min == -1 ? "*" : Util::itos(_min))+"-"+
  164. (_max == -1 ? "*" : Util::itos(_max));
  165. }
  166. };
  167. class UnitNumberOptionHandler : public NumberOptionHandler {
  168. public:
  169. UnitNumberOptionHandler(const std::string& optName,
  170. const std::string& description = NO_DESCRIPTION,
  171. const std::string& defaultValue = NO_DEFAULT_VALUE,
  172. int64_t min = -1,
  173. int64_t max = -1,
  174. bool hidden = false):
  175. NumberOptionHandler(optName, description, defaultValue, min, max, hidden) {}
  176. virtual ~UnitNumberOptionHandler() {}
  177. virtual void parseArg(Option* option, const std::string& optarg)
  178. {
  179. int64_t num = Util::getRealSize(optarg);
  180. NumberOptionHandler::parseArg(option, num);
  181. }
  182. };
  183. class FloatNumberOptionHandler : public NameMatchOptionHandler {
  184. private:
  185. double _min;
  186. double _max;
  187. public:
  188. FloatNumberOptionHandler(const std::string& optName,
  189. const std::string& description = NO_DESCRIPTION,
  190. const std::string& defaultValue = NO_DEFAULT_VALUE,
  191. double min = -1, double max = -1):
  192. NameMatchOptionHandler(optName, description, defaultValue),
  193. _min(min), _max(max) {}
  194. virtual ~FloatNumberOptionHandler() {}
  195. virtual void parseArg(Option* option, const std::string& optarg)
  196. {
  197. double number = strtod(optarg.c_str(), 0);
  198. if((_min < 0 || _min <= number) && (_max < 0 || number <= _max)) {
  199. option->put(_optName, optarg);
  200. } else {
  201. std::string msg = _optName+" ";
  202. if(_min < 0 && _max >= 0) {
  203. msg += StringFormat(_("must be smaller than or equal to %.1f."),
  204. _max).str();
  205. } else if(_min >= 0 && _max >= 0) {
  206. msg += StringFormat(_("must be between %.1f and %.1f."),
  207. _min, _max).str();
  208. } else if(_min >= 0 && _max < 0) {
  209. msg += StringFormat(_("must be greater than or equal to %.1f."),
  210. _min).str();
  211. } else {
  212. msg += _("must be a number.");
  213. }
  214. throw FatalException(msg);
  215. }
  216. }
  217. virtual std::string createPossibleValuesString() const
  218. {
  219. std::string valuesString;
  220. if(_min < 0) {
  221. valuesString += "*";
  222. } else {
  223. char buf[11];
  224. snprintf(buf, sizeof(buf), "%.1f", _min);
  225. valuesString += buf;
  226. }
  227. valuesString += "-";
  228. if(_max < 0) {
  229. valuesString += "*";
  230. } else {
  231. char buf[11];
  232. snprintf(buf, sizeof(buf), "%.1f", _max);
  233. valuesString += buf;
  234. }
  235. return valuesString;
  236. }
  237. };
  238. class DefaultOptionHandler : public NameMatchOptionHandler {
  239. private:
  240. std::string _possibleValuesString;
  241. public:
  242. DefaultOptionHandler(const std::string& optName,
  243. const std::string& description = NO_DESCRIPTION,
  244. const std::string& defaultValue = NO_DEFAULT_VALUE,
  245. const std::string& possibleValuesString = A2STR::NIL,
  246. bool hidden = false):
  247. NameMatchOptionHandler(optName, description, defaultValue, hidden),
  248. _possibleValuesString(possibleValuesString) {}
  249. virtual ~DefaultOptionHandler() {}
  250. virtual void parseArg(Option* option, const std::string& optarg)
  251. {
  252. option->put(_optName, optarg);
  253. }
  254. virtual std::string createPossibleValuesString() const
  255. {
  256. return _possibleValuesString;
  257. }
  258. };
  259. class CumulativeOptionHandler : public NameMatchOptionHandler {
  260. private:
  261. std::string _delim;
  262. std::string _possibleValuesString;
  263. public:
  264. CumulativeOptionHandler(const std::string& optName,
  265. const std::string& description,
  266. const std::string& defaultValue,
  267. const std::string& delim,
  268. const std::string& possibleValuesString = A2STR::NIL):
  269. NameMatchOptionHandler(optName, description, defaultValue),
  270. _delim(delim),
  271. _possibleValuesString(possibleValuesString) {}
  272. virtual ~CumulativeOptionHandler() {}
  273. virtual void parseArg(Option* option, const std::string& optarg)
  274. {
  275. std::string value = option->get(_optName);
  276. value += optarg+_delim;
  277. option->put(_optName, value);
  278. }
  279. virtual std::string createPossibleValuesString() const
  280. {
  281. return _possibleValuesString;
  282. }
  283. };
  284. class ParameterOptionHandler : public NameMatchOptionHandler {
  285. private:
  286. std::deque<std::string> _validParamValues;
  287. public:
  288. ParameterOptionHandler(const std::string& optName,
  289. const std::string& description,
  290. const std::string& defaultValue,
  291. const std::deque<std::string>& validParamValues):
  292. NameMatchOptionHandler(optName, description, defaultValue),
  293. _validParamValues(validParamValues) {}
  294. ParameterOptionHandler(const std::string& optName,
  295. const std::string& description,
  296. const std::string& defaultValue,
  297. const std::string& validParamValue):
  298. NameMatchOptionHandler(optName, description, defaultValue)
  299. {
  300. _validParamValues.push_back(validParamValue);
  301. }
  302. ParameterOptionHandler(const std::string& optName,
  303. const std::string& description,
  304. const std::string& defaultValue,
  305. const std::string& validParamValue1,
  306. const std::string& validParamValue2):
  307. NameMatchOptionHandler(optName, description, defaultValue)
  308. {
  309. _validParamValues.push_back(validParamValue1);
  310. _validParamValues.push_back(validParamValue2);
  311. }
  312. ParameterOptionHandler(const std::string& optName,
  313. const std::string& description,
  314. const std::string& defaultValue,
  315. const std::string& validParamValue1,
  316. const std::string& validParamValue2,
  317. const std::string& validParamValue3):
  318. NameMatchOptionHandler(optName, description, defaultValue)
  319. {
  320. _validParamValues.push_back(validParamValue1);
  321. _validParamValues.push_back(validParamValue2);
  322. _validParamValues.push_back(validParamValue3);
  323. }
  324. virtual ~ParameterOptionHandler() {}
  325. virtual void parseArg(Option* option, const std::string& optarg)
  326. {
  327. std::deque<std::string>::const_iterator itr =
  328. std::find(_validParamValues.begin(), _validParamValues.end(), optarg);
  329. if(itr == _validParamValues.end()) {
  330. std::string msg = _optName+" "+_("must be one of the following:");
  331. if(_validParamValues.size() == 0) {
  332. msg += "''";
  333. } else {
  334. for(std::deque<std::string>::const_iterator itr = _validParamValues.begin();
  335. itr != _validParamValues.end(); ++itr) {
  336. msg += "'"+*itr+"' ";
  337. }
  338. }
  339. throw FatalException(msg);
  340. } else {
  341. option->put(_optName, optarg);
  342. }
  343. }
  344. virtual std::string createPossibleValuesString() const
  345. {
  346. std::stringstream s;
  347. std::copy(_validParamValues.begin(), _validParamValues.end(),
  348. std::ostream_iterator<std::string>(s, ","));
  349. return Util::trim(s.str(), ", ");
  350. }
  351. };
  352. class HostPortOptionHandler : public NameMatchOptionHandler {
  353. private:
  354. std::string _hostOptionName;
  355. std::string _portOptionName;
  356. public:
  357. HostPortOptionHandler(const std::string& optName,
  358. const std::string& description,
  359. const std::string& defaultValue,
  360. const std::string& hostOptionName,
  361. const std::string& portOptionName):
  362. NameMatchOptionHandler(optName, description, defaultValue),
  363. _hostOptionName(hostOptionName),
  364. _portOptionName(portOptionName) {}
  365. virtual ~HostPortOptionHandler() {}
  366. virtual void parseArg(Option* option, const std::string& optarg)
  367. {
  368. std::pair<std::string, std::string> proxy = Util::split(optarg, ":");
  369. int32_t port = Util::parseInt(proxy.second);
  370. if(proxy.first.empty() || proxy.second.empty() ||
  371. port <= 0 || 65535 < port) {
  372. throw FatalException(_("unrecognized proxy format"));
  373. }
  374. option->put(_optName, optarg);
  375. setHostAndPort(option, proxy.first, port);
  376. }
  377. void setHostAndPort(Option* option, const std::string& hostname, uint16_t port)
  378. {
  379. option->put(_hostOptionName, hostname);
  380. option->put(_portOptionName, Util::uitos(port));
  381. }
  382. virtual std::string createPossibleValuesString() const
  383. {
  384. return "HOST:PORT";
  385. }
  386. };
  387. class HttpProxyOptionHandler : public NameMatchOptionHandler {
  388. public:
  389. HttpProxyOptionHandler(const std::string& optName,
  390. const std::string& description,
  391. const std::string& defaultValue)
  392. :
  393. NameMatchOptionHandler(optName, description, defaultValue)
  394. {}
  395. virtual ~HttpProxyOptionHandler() {}
  396. virtual void parseArg(Option* option, const std::string& optarg)
  397. {
  398. Request req;
  399. std::string url;
  400. if(Util::startsWith(optarg, "http://")) {
  401. url = optarg;
  402. } else {
  403. url = "http://"+optarg;
  404. }
  405. if(req.setUrl(url)) {
  406. option->put(_optName, url);
  407. } else {
  408. throw FatalException(_("unrecognized proxy format"));
  409. }
  410. }
  411. virtual std::string createPossibleValuesString() const
  412. {
  413. return "[http://][USER:PASSWORD@]HOST[:PORT]";
  414. }
  415. };
  416. } // namespace aria2
  417. #endif // _D_OPTION_HANDLER_IMPL_H_