OptionHandlerImpl.h 24 KB

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