OptionHandlerImpl.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 "OptionHandlerImpl.h"
  36. #include <cassert>
  37. #include <cstdio>
  38. #include <utility>
  39. #include <algorithm>
  40. #include <numeric>
  41. #include <sstream>
  42. #include <iterator>
  43. #include <vector>
  44. #include "util.h"
  45. #include "DlAbortEx.h"
  46. #include "prefs.h"
  47. #include "Option.h"
  48. #include "fmt.h"
  49. #include "A2STR.h"
  50. #include "Request.h"
  51. #include "a2functional.h"
  52. #include "message.h"
  53. #include "File.h"
  54. #include "FileEntry.h"
  55. #include "a2io.h"
  56. #include "LogFactory.h"
  57. namespace aria2 {
  58. NullOptionHandler::~NullOptionHandler() {}
  59. bool NullOptionHandler::canHandle(const std::string& optName) { return true; }
  60. void NullOptionHandler::parse(Option& option, const std::string& arg) {}
  61. bool NullOptionHandler::hasTag(const std::string& tag) const { return false; }
  62. void NullOptionHandler::addTag(const std::string& tag) {}
  63. std::string NullOptionHandler::toTagString() const { return A2STR::NIL; }
  64. const std::string& NullOptionHandler::getName() const { return A2STR::NIL; }
  65. const std::string& NullOptionHandler::getDescription() const
  66. {
  67. return A2STR::NIL;
  68. }
  69. const std::string& NullOptionHandler::getDefaultValue() const
  70. {
  71. return A2STR::NIL;
  72. }
  73. std::string NullOptionHandler::createPossibleValuesString() const
  74. {
  75. return A2STR::NIL;
  76. }
  77. bool NullOptionHandler::isHidden() const
  78. {
  79. return true;
  80. }
  81. void NullOptionHandler::hide() {}
  82. OptionHandler::ARG_TYPE NullOptionHandler::getArgType() const
  83. {
  84. return OptionHandler::NO_ARG;
  85. }
  86. int NullOptionHandler::getOptionID() const
  87. {
  88. return id_;
  89. }
  90. void NullOptionHandler::setOptionID(int id)
  91. {
  92. id_ = id;
  93. }
  94. char NullOptionHandler::getShortName() const
  95. {
  96. return 0;
  97. }
  98. BooleanOptionHandler::BooleanOptionHandler
  99. (const std::string& optName,
  100. const std::string& description,
  101. const std::string& defaultValue,
  102. OptionHandler::ARG_TYPE argType,
  103. char shortName)
  104. : NameMatchOptionHandler(optName, description, defaultValue,
  105. argType, shortName)
  106. {}
  107. BooleanOptionHandler::~BooleanOptionHandler() {}
  108. void BooleanOptionHandler::parseArg(Option& option, const std::string& optarg)
  109. {
  110. if(optarg == "true" ||
  111. ((argType_ == OptionHandler::OPT_ARG ||
  112. argType_ == OptionHandler::NO_ARG)
  113. && optarg.empty())) {
  114. option.put(optName_, A2_V_TRUE);
  115. } else if(optarg == "false") {
  116. option.put(optName_, A2_V_FALSE);
  117. } else {
  118. std::string msg = optName_;
  119. strappend(msg, " ", _("must be either 'true' or 'false'."));
  120. throw DL_ABORT_EX(msg);
  121. }
  122. }
  123. std::string BooleanOptionHandler::createPossibleValuesString() const
  124. {
  125. return "true, false";
  126. }
  127. IntegerRangeOptionHandler::IntegerRangeOptionHandler
  128. (const std::string& optName,
  129. const std::string& description,
  130. const std::string& defaultValue,
  131. int32_t min, int32_t max,
  132. char shortName)
  133. : NameMatchOptionHandler(optName, description, defaultValue,
  134. OptionHandler::REQ_ARG, shortName),
  135. min_(min),
  136. max_(max)
  137. {}
  138. IntegerRangeOptionHandler::~IntegerRangeOptionHandler() {}
  139. void IntegerRangeOptionHandler::parseArg
  140. (Option& option, const std::string& optarg)
  141. {
  142. IntSequence seq = util::parseIntRange(optarg);
  143. while(seq.hasNext()) {
  144. int32_t v = seq.next();
  145. if(v < min_ || max_ < v) {
  146. std::string msg = optName_;
  147. strappend(msg, " ", _("must be between %s and %s."));
  148. throw DL_ABORT_EX
  149. (fmt(msg.c_str(), util::itos(min_).c_str(),
  150. util::itos(max_).c_str()));
  151. }
  152. option.put(optName_, optarg);
  153. }
  154. }
  155. std::string IntegerRangeOptionHandler::createPossibleValuesString() const
  156. {
  157. return util::itos(min_)+"-"+util::itos(max_);
  158. }
  159. NumberOptionHandler::NumberOptionHandler
  160. (const std::string& optName,
  161. const std::string& description,
  162. const std::string& defaultValue,
  163. int64_t min,
  164. int64_t max,
  165. char shortName)
  166. : NameMatchOptionHandler(optName, description, defaultValue,
  167. OptionHandler::REQ_ARG, shortName),
  168. min_(min),
  169. max_(max)
  170. {}
  171. NumberOptionHandler::~NumberOptionHandler() {}
  172. void NumberOptionHandler::parseArg(Option& option, const std::string& optarg)
  173. {
  174. int64_t num = util::parseLLInt(optarg);
  175. parseArg(option, num);
  176. }
  177. void NumberOptionHandler::parseArg(Option& option, int64_t number)
  178. {
  179. if((min_ == -1 || min_ <= number) && (max_ == -1 || number <= max_)) {
  180. option.put(optName_, util::itos(number));
  181. } else {
  182. std::string msg = optName_;
  183. msg += " ";
  184. if(min_ == -1 && max_ != -1) {
  185. msg += fmt(_("must be smaller than or equal to %s."),
  186. util::itos(max_).c_str());
  187. } else if(min_ != -1 && max_ != -1) {
  188. msg += fmt(_("must be between %s and %s."),
  189. util::itos(min_).c_str(),
  190. util::itos(max_).c_str());
  191. } else if(min_ != -1 && max_ == -1) {
  192. msg += fmt(_("must be greater than or equal to %s."),
  193. util::itos(min_).c_str());
  194. } else {
  195. msg += _("must be a number.");
  196. }
  197. throw DL_ABORT_EX(msg);
  198. }
  199. }
  200. std::string NumberOptionHandler::createPossibleValuesString() const
  201. {
  202. std::string values;
  203. if(min_ == -1) {
  204. values += "*";
  205. } else {
  206. values += util::itos(min_);
  207. }
  208. values += "-";
  209. if(max_ == -1) {
  210. values += "*";
  211. } else {
  212. values += util::itos(max_);
  213. }
  214. return values;
  215. }
  216. UnitNumberOptionHandler::UnitNumberOptionHandler
  217. (const std::string& optName,
  218. const std::string& description,
  219. const std::string& defaultValue,
  220. int64_t min,
  221. int64_t max,
  222. char shortName)
  223. : NumberOptionHandler(optName, description, defaultValue, min, max,
  224. shortName)
  225. {}
  226. UnitNumberOptionHandler::~UnitNumberOptionHandler() {}
  227. void UnitNumberOptionHandler::parseArg
  228. (Option& option, const std::string& optarg)
  229. {
  230. int64_t num = util::getRealSize(optarg);
  231. NumberOptionHandler::parseArg(option, num);
  232. }
  233. FloatNumberOptionHandler::FloatNumberOptionHandler
  234. (const std::string& optName,
  235. const std::string& description,
  236. const std::string& defaultValue,
  237. double min,
  238. double max,
  239. char shortName)
  240. : NameMatchOptionHandler(optName, description, defaultValue,
  241. OptionHandler::REQ_ARG, shortName),
  242. min_(min),
  243. max_(max)
  244. {}
  245. FloatNumberOptionHandler::~FloatNumberOptionHandler() {}
  246. void FloatNumberOptionHandler::parseArg
  247. (Option& option, const std::string& optarg)
  248. {
  249. double number = strtod(optarg.c_str(), 0);
  250. if((min_ < 0 || min_ <= number) && (max_ < 0 || number <= max_)) {
  251. option.put(optName_, optarg);
  252. } else {
  253. std::string msg = optName_;
  254. msg += " ";
  255. if(min_ < 0 && max_ >= 0) {
  256. msg += fmt(_("must be smaller than or equal to %.1f."), max_);
  257. } else if(min_ >= 0 && max_ >= 0) {
  258. msg += fmt(_("must be between %.1f and %.1f."), min_, max_);
  259. } else if(min_ >= 0 && max_ < 0) {
  260. msg += fmt(_("must be greater than or equal to %.1f."), min_);
  261. } else {
  262. msg += _("must be a number.");
  263. }
  264. throw DL_ABORT_EX(msg);
  265. }
  266. }
  267. std::string FloatNumberOptionHandler::createPossibleValuesString() const
  268. {
  269. std::string valuesString;
  270. if(min_ < 0) {
  271. valuesString += "*";
  272. } else {
  273. char buf[11];
  274. snprintf(buf, sizeof(buf), "%.1f", min_);
  275. valuesString += buf;
  276. }
  277. valuesString += "-";
  278. if(max_ < 0) {
  279. valuesString += "*";
  280. } else {
  281. char buf[11];
  282. snprintf(buf, sizeof(buf), "%.1f", max_);
  283. valuesString += buf;
  284. }
  285. return valuesString;
  286. }
  287. DefaultOptionHandler::DefaultOptionHandler
  288. (const std::string& optName,
  289. const std::string& description,
  290. const std::string& defaultValue,
  291. const std::string& possibleValuesString,
  292. OptionHandler::ARG_TYPE argType,
  293. char shortName)
  294. : NameMatchOptionHandler(optName, description, defaultValue, argType,
  295. shortName),
  296. possibleValuesString_(possibleValuesString)
  297. {}
  298. DefaultOptionHandler::~DefaultOptionHandler() {}
  299. void DefaultOptionHandler::parseArg(Option& option, const std::string& optarg)
  300. {
  301. option.put(optName_, optarg);
  302. }
  303. std::string DefaultOptionHandler::createPossibleValuesString() const
  304. {
  305. return possibleValuesString_;
  306. }
  307. CumulativeOptionHandler::CumulativeOptionHandler
  308. (const std::string& optName,
  309. const std::string& description,
  310. const std::string& defaultValue,
  311. const std::string& delim,
  312. const std::string& possibleValuesString,
  313. OptionHandler::ARG_TYPE argType,
  314. char shortName)
  315. : NameMatchOptionHandler(optName, description, defaultValue, argType,
  316. shortName),
  317. delim_(delim),
  318. possibleValuesString_(possibleValuesString)
  319. {}
  320. CumulativeOptionHandler::~CumulativeOptionHandler() {}
  321. void CumulativeOptionHandler::parseArg
  322. (Option& option, const std::string& optarg)
  323. {
  324. std::string value = option.get(optName_);
  325. strappend(value, optarg, delim_);
  326. option.put(optName_, value);
  327. }
  328. std::string CumulativeOptionHandler::createPossibleValuesString() const
  329. {
  330. return possibleValuesString_;
  331. }
  332. IndexOutOptionHandler::IndexOutOptionHandler
  333. (const std::string& optName,
  334. const std::string& description,
  335. char shortName)
  336. : NameMatchOptionHandler(optName, description, NO_DEFAULT_VALUE,
  337. OptionHandler::REQ_ARG, shortName)
  338. {}
  339. IndexOutOptionHandler::~IndexOutOptionHandler() {}
  340. void IndexOutOptionHandler::parseArg(Option& option, const std::string& optarg)
  341. {
  342. // See optarg is in the fomrat of "INDEX=PATH"
  343. util::parseIndexPath(optarg);
  344. std::string value = option.get(optName_);
  345. strappend(value, optarg, "\n");
  346. option.put(optName_, value);
  347. }
  348. std::string IndexOutOptionHandler::createPossibleValuesString() const
  349. {
  350. return "INDEX=PATH";
  351. }
  352. ParameterOptionHandler::ParameterOptionHandler
  353. (const std::string& optName,
  354. const std::string& description,
  355. const std::string& defaultValue,
  356. const std::vector<std::string>& validParamValues,
  357. char shortName)
  358. : NameMatchOptionHandler(optName, description, defaultValue,
  359. OptionHandler::REQ_ARG, shortName),
  360. validParamValues_(validParamValues)
  361. {}
  362. ParameterOptionHandler::ParameterOptionHandler
  363. (const std::string& optName,
  364. const std::string& description,
  365. const std::string& defaultValue,
  366. const std::string& validParamValue,
  367. char shortName)
  368. : NameMatchOptionHandler(optName, description, defaultValue,
  369. OptionHandler::REQ_ARG, shortName)
  370. {
  371. validParamValues_.push_back(validParamValue);
  372. }
  373. ParameterOptionHandler::ParameterOptionHandler
  374. (const std::string& optName,
  375. const std::string& description,
  376. const std::string& defaultValue,
  377. const std::string& validParamValue1,
  378. const std::string& validParamValue2,
  379. char shortName)
  380. : NameMatchOptionHandler(optName, description, defaultValue,
  381. OptionHandler::REQ_ARG, shortName)
  382. {
  383. validParamValues_.push_back(validParamValue1);
  384. validParamValues_.push_back(validParamValue2);
  385. }
  386. ParameterOptionHandler::ParameterOptionHandler
  387. (const std::string& optName,
  388. const std::string& description,
  389. const std::string& defaultValue,
  390. const std::string& validParamValue1,
  391. const std::string& validParamValue2,
  392. const std::string& validParamValue3,
  393. char shortName)
  394. : NameMatchOptionHandler(optName, description, defaultValue,
  395. OptionHandler::REQ_ARG, shortName)
  396. {
  397. validParamValues_.push_back(validParamValue1);
  398. validParamValues_.push_back(validParamValue2);
  399. validParamValues_.push_back(validParamValue3);
  400. }
  401. ParameterOptionHandler::~ParameterOptionHandler() {}
  402. void ParameterOptionHandler::parseArg(Option& option, const std::string& optarg)
  403. {
  404. std::vector<std::string>::const_iterator itr =
  405. std::find(validParamValues_.begin(), validParamValues_.end(), optarg);
  406. if(itr == validParamValues_.end()) {
  407. std::string msg = optName_;
  408. strappend(msg, " ", _("must be one of the following:"));
  409. if(validParamValues_.size() == 0) {
  410. msg += "''";
  411. } else {
  412. for(std::vector<std::string>::const_iterator itr =
  413. validParamValues_.begin(), eoi = validParamValues_.end();
  414. itr != eoi; ++itr) {
  415. strappend(msg, "'", *itr, "' ");
  416. }
  417. }
  418. throw DL_ABORT_EX(msg);
  419. } else {
  420. option.put(optName_, optarg);
  421. }
  422. }
  423. std::string ParameterOptionHandler::createPossibleValuesString() const
  424. {
  425. std::stringstream s;
  426. std::copy(validParamValues_.begin(), validParamValues_.end(),
  427. std::ostream_iterator<std::string>(s, ", "));
  428. return util::strip(s.str(), ", ");
  429. }
  430. HostPortOptionHandler::HostPortOptionHandler
  431. (const std::string& optName,
  432. const std::string& description,
  433. const std::string& defaultValue,
  434. const std::string& hostOptionName,
  435. const std::string& portOptionName,
  436. char shortName)
  437. : NameMatchOptionHandler(optName, description, defaultValue,
  438. OptionHandler::REQ_ARG, shortName),
  439. hostOptionName_(hostOptionName),
  440. portOptionName_(portOptionName)
  441. {}
  442. HostPortOptionHandler::~HostPortOptionHandler() {}
  443. void HostPortOptionHandler::parseArg(Option& option, const std::string& optarg)
  444. {
  445. std::string uri = "http://";
  446. uri += optarg;
  447. Request req;
  448. if(!req.setUri(uri)) {
  449. throw DL_ABORT_EX(_("Unrecognized format"));
  450. }
  451. option.put(optName_, optarg);
  452. setHostAndPort(option, req.getHost(), req.getPort());
  453. }
  454. void HostPortOptionHandler::setHostAndPort
  455. (Option& option, const std::string& hostname, uint16_t port)
  456. {
  457. option.put(hostOptionName_, hostname);
  458. option.put(portOptionName_, util::uitos(port));
  459. }
  460. std::string HostPortOptionHandler::createPossibleValuesString() const
  461. {
  462. return "HOST:PORT";
  463. }
  464. HttpProxyUserOptionHandler::HttpProxyUserOptionHandler
  465. (const std::string& optName,
  466. const std::string& description,
  467. const std::string& defaultValue,
  468. char shortName)
  469. : NameMatchOptionHandler(optName, description, defaultValue,
  470. OptionHandler::REQ_ARG, shortName)
  471. {}
  472. void HttpProxyUserOptionHandler::parseArg
  473. (Option& option, const std::string& optarg)
  474. {
  475. if(util::endsWith(optName_, "-user")) {
  476. const std::string proxyPref = optName_.substr(0, optName_.size()-5);
  477. const std::string& olduri = option.get(proxyPref);
  478. if(!olduri.empty()) {
  479. Request req;
  480. bool b = req.setUri(olduri);
  481. assert(b);
  482. std::string uri = "http://";
  483. if(!optarg.empty()) {
  484. uri += util::percentEncode(optarg);
  485. }
  486. if(req.hasPassword()) {
  487. uri += A2STR::COLON_C;
  488. uri += util::percentEncode(req.getPassword());
  489. }
  490. if(uri.size() > 7) {
  491. uri += "@";
  492. }
  493. strappend(uri, req.getHost(),A2STR::COLON_C,util::uitos(req.getPort()));
  494. option.put(proxyPref, uri);
  495. }
  496. }
  497. option.put(optName_, optarg);
  498. }
  499. std::string HttpProxyUserOptionHandler::createPossibleValuesString() const
  500. {
  501. return "";
  502. }
  503. HttpProxyPasswdOptionHandler::HttpProxyPasswdOptionHandler
  504. (const std::string& optName,
  505. const std::string& description,
  506. const std::string& defaultValue,
  507. char shortName)
  508. : NameMatchOptionHandler(optName, description, defaultValue,
  509. OptionHandler::REQ_ARG, shortName)
  510. {}
  511. void HttpProxyPasswdOptionHandler::parseArg
  512. (Option& option, const std::string& optarg)
  513. {
  514. if(util::endsWith(optName_, "-passwd")) {
  515. const std::string proxyPref = optName_.substr(0, optName_.size()-7);
  516. const std::string& olduri = option.get(proxyPref);
  517. if(!olduri.empty()) {
  518. Request req;
  519. bool b = req.setUri(olduri);
  520. assert(b);
  521. std::string uri = "http://";
  522. if(!req.getUsername().empty()) {
  523. uri += util::percentEncode(req.getUsername());
  524. }
  525. uri += A2STR::COLON_C;
  526. if(!optarg.empty()) {
  527. uri += util::percentEncode(optarg);
  528. }
  529. if(uri.size() > 7) {
  530. uri += "@";
  531. }
  532. strappend(uri, req.getHost(), A2STR::COLON_C,util::itos(req.getPort()));
  533. option.put(proxyPref, uri);
  534. }
  535. }
  536. option.put(optName_, optarg);
  537. }
  538. std::string HttpProxyPasswdOptionHandler::createPossibleValuesString() const
  539. {
  540. return "";
  541. }
  542. HttpProxyOptionHandler::HttpProxyOptionHandler
  543. (const std::string& optName,
  544. const std::string& description,
  545. const std::string& defaultValue,
  546. char shortName)
  547. : NameMatchOptionHandler(optName, description, defaultValue,
  548. OptionHandler::REQ_ARG, shortName),
  549. proxyUserPref_(optName_+"-user"),
  550. proxyPasswdPref_(optName_+"-passwd")
  551. {}
  552. HttpProxyOptionHandler::~HttpProxyOptionHandler() {}
  553. void HttpProxyOptionHandler::parseArg(Option& option, const std::string& optarg)
  554. {
  555. if(optarg.empty()) {
  556. option.put(optName_, optarg);
  557. } else {
  558. Request req;
  559. std::string uri;
  560. if(util::startsWith(optarg, "http://") ||
  561. util::startsWith(optarg, "https://") ||
  562. util::startsWith(optarg, "ftp://")) {
  563. uri = optarg;
  564. } else {
  565. uri = "http://";
  566. uri += optarg;
  567. }
  568. if(!req.setUri(uri)) {
  569. throw DL_ABORT_EX(_("unrecognized proxy format"));
  570. }
  571. uri = "http://";
  572. if(req.getUsername().empty()) {
  573. if(option.defined(proxyUserPref_)) {
  574. uri += util::percentEncode(option.get(proxyUserPref_));
  575. }
  576. } else {
  577. uri += util::percentEncode(req.getUsername());
  578. }
  579. if(!req.hasPassword()) {
  580. if(option.defined(proxyPasswdPref_)) {
  581. uri += A2STR::COLON_C;
  582. uri += util::percentEncode(option.get(proxyPasswdPref_));
  583. }
  584. } else {
  585. uri += A2STR::COLON_C;
  586. uri += util::percentEncode(req.getPassword());
  587. }
  588. if(uri.size() > 7) {
  589. uri += "@";
  590. }
  591. strappend(uri, req.getHost(), A2STR::COLON_C, util::uitos(req.getPort()));
  592. option.put(optName_, uri);
  593. }
  594. }
  595. std::string HttpProxyOptionHandler::createPossibleValuesString() const
  596. {
  597. return "[http://][USER:PASSWORD@]HOST[:PORT]";
  598. }
  599. LocalFilePathOptionHandler::LocalFilePathOptionHandler
  600. (const std::string& optName,
  601. const std::string& description,
  602. const std::string& defaultValue,
  603. bool acceptStdin,
  604. char shortName)
  605. : NameMatchOptionHandler(optName, description, defaultValue,
  606. OptionHandler::REQ_ARG, shortName),
  607. acceptStdin_(acceptStdin)
  608. {}
  609. void LocalFilePathOptionHandler::parseArg
  610. (Option& option, const std::string& optarg)
  611. {
  612. if(acceptStdin_ && optarg == "-") {
  613. option.put(optName_, DEV_STDIN);
  614. } else {
  615. File f(optarg);
  616. if(!f.exists() || f.isDir()) {
  617. throw DL_ABORT_EX(fmt(MSG_NOT_FILE, optarg.c_str()));
  618. }
  619. option.put(optName_, optarg);
  620. }
  621. }
  622. std::string LocalFilePathOptionHandler::createPossibleValuesString() const
  623. {
  624. if(acceptStdin_) {
  625. return PATH_TO_FILE_STDIN;
  626. } else {
  627. return PATH_TO_FILE;
  628. }
  629. }
  630. PrioritizePieceOptionHandler::PrioritizePieceOptionHandler
  631. (const std::string& optName,
  632. const std::string& description,
  633. const std::string& defaultValue,
  634. char shortName)
  635. : NameMatchOptionHandler(optName, description, defaultValue,
  636. OptionHandler::REQ_ARG, shortName)
  637. {}
  638. void PrioritizePieceOptionHandler::parseArg
  639. (Option& option, const std::string& optarg)
  640. {
  641. // Parse optarg against empty FileEntry list to detect syntax
  642. // error.
  643. std::vector<size_t> result;
  644. util::parsePrioritizePieceRange
  645. (result, optarg, std::vector<SharedHandle<FileEntry> >(), 1024);
  646. option.put(optName_, optarg);
  647. }
  648. std::string PrioritizePieceOptionHandler::createPossibleValuesString() const
  649. {
  650. return "head[=SIZE], tail[=SIZE]";
  651. }
  652. DeprecatedOptionHandler::DeprecatedOptionHandler
  653. (const SharedHandle<OptionHandler>& depOptHandler,
  654. const std::string& repOptName)
  655. : depOptHandler_(depOptHandler), repOptName_(repOptName)
  656. {}
  657. bool DeprecatedOptionHandler::canHandle(const std::string& optName)
  658. {
  659. return depOptHandler_->canHandle(optName);
  660. }
  661. void DeprecatedOptionHandler::parse(Option& option, const std::string& arg)
  662. {
  663. A2_LOG_WARN(fmt("--%s option is deprecated. Use --%s option instead.",
  664. depOptHandler_->getName().c_str(),
  665. repOptName_.c_str()));
  666. depOptHandler_->parse(option, arg);
  667. option.put(repOptName_, option.get(depOptHandler_->getName()));
  668. }
  669. std::string DeprecatedOptionHandler::createPossibleValuesString() const
  670. {
  671. return depOptHandler_->createPossibleValuesString();
  672. }
  673. bool DeprecatedOptionHandler::hasTag(const std::string& tag) const
  674. {
  675. return depOptHandler_->hasTag(tag);
  676. }
  677. void DeprecatedOptionHandler::addTag(const std::string& tag)
  678. {
  679. depOptHandler_->addTag(tag);
  680. }
  681. std::string DeprecatedOptionHandler::toTagString() const
  682. {
  683. return depOptHandler_->toTagString();
  684. }
  685. const std::string& DeprecatedOptionHandler::getName() const
  686. {
  687. return depOptHandler_->getName();
  688. }
  689. const std::string& DeprecatedOptionHandler::getDescription() const
  690. {
  691. return depOptHandler_->getDescription();
  692. }
  693. const std::string& DeprecatedOptionHandler::getDefaultValue() const
  694. {
  695. return depOptHandler_->getDefaultValue();
  696. }
  697. bool DeprecatedOptionHandler::isHidden() const
  698. {
  699. return depOptHandler_->isHidden();
  700. }
  701. void DeprecatedOptionHandler::hide()
  702. {
  703. depOptHandler_->hide();
  704. }
  705. OptionHandler::ARG_TYPE DeprecatedOptionHandler::getArgType() const
  706. {
  707. return depOptHandler_->getArgType();
  708. }
  709. char DeprecatedOptionHandler::getShortName() const
  710. {
  711. return depOptHandler_->getShortName();
  712. }
  713. int DeprecatedOptionHandler::getOptionID() const
  714. {
  715. return depOptHandler_->getOptionID();
  716. }
  717. void DeprecatedOptionHandler::setOptionID(int id)
  718. {
  719. depOptHandler_->setOptionID(id);
  720. }
  721. } // namespace aria2