OptionHandlerImpl.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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::pair<std::string, std::string> proxy = util::split(optarg, ":");
  462. int32_t port = util::parseInt(proxy.second);
  463. if(proxy.first.empty() || proxy.second.empty() ||
  464. port <= 0 || 65535 < port) {
  465. throw DL_ABORT_EX(_("unrecognized proxy format"));
  466. }
  467. option.put(_optName, optarg);
  468. setHostAndPort(option, proxy.first, port);
  469. }
  470. void setHostAndPort(Option& option, const std::string& hostname, uint16_t port)
  471. {
  472. option.put(_hostOptionName, hostname);
  473. option.put(_portOptionName, util::uitos(port));
  474. }
  475. virtual std::string createPossibleValuesString() const
  476. {
  477. return "HOST:PORT";
  478. }
  479. };
  480. class HttpProxyUserOptionHandler:public NameMatchOptionHandler {
  481. public:
  482. HttpProxyUserOptionHandler(const std::string& optName,
  483. const std::string& description,
  484. const std::string& defaultValue,
  485. char shortName = 0):
  486. NameMatchOptionHandler(optName, description, defaultValue,
  487. OptionHandler::REQ_ARG, shortName)
  488. {}
  489. virtual void parseArg(Option& option, const std::string& optarg)
  490. {
  491. if(util::endsWith(_optName, "-user")) {
  492. const std::string proxyPref = _optName.substr(0, _optName.size()-5);
  493. const std::string& olduri = option.get(proxyPref);
  494. if(!olduri.empty()) {
  495. Request req;
  496. bool b = req.setUri(olduri);
  497. assert(b);
  498. std::string uri = "http://";
  499. if(!optarg.empty()) {
  500. uri += util::percentEncode(optarg);
  501. }
  502. if(req.hasPassword()) {
  503. uri += A2STR::COLON_C;
  504. uri += util::percentEncode(req.getPassword());
  505. }
  506. if(uri.size() > 7) {
  507. uri += "@";
  508. }
  509. strappend(uri, req.getHost(),A2STR::COLON_C,util::uitos(req.getPort()));
  510. option.put(proxyPref, uri);
  511. }
  512. }
  513. option.put(_optName, optarg);
  514. }
  515. virtual std::string createPossibleValuesString() const
  516. {
  517. return "";
  518. }
  519. };
  520. class HttpProxyPasswdOptionHandler:public NameMatchOptionHandler {
  521. public:
  522. HttpProxyPasswdOptionHandler(const std::string& optName,
  523. const std::string& description,
  524. const std::string& defaultValue,
  525. char shortName = 0):
  526. NameMatchOptionHandler(optName, description, defaultValue,
  527. OptionHandler::REQ_ARG, shortName)
  528. {}
  529. virtual void parseArg(Option& option, const std::string& optarg)
  530. {
  531. if(util::endsWith(_optName, "-passwd")) {
  532. const std::string proxyPref = _optName.substr(0, _optName.size()-7);
  533. const std::string& olduri = option.get(proxyPref);
  534. if(!olduri.empty()) {
  535. Request req;
  536. bool b = req.setUri(olduri);
  537. assert(b);
  538. std::string uri = "http://";
  539. if(!req.getUsername().empty()) {
  540. uri += util::percentEncode(req.getUsername());
  541. }
  542. uri += A2STR::COLON_C;
  543. if(!optarg.empty()) {
  544. uri += util::percentEncode(optarg);
  545. }
  546. if(uri.size() > 7) {
  547. uri += "@";
  548. }
  549. strappend(uri, req.getHost(), A2STR::COLON_C,util::itos(req.getPort()));
  550. option.put(proxyPref, uri);
  551. }
  552. }
  553. option.put(_optName, optarg);
  554. }
  555. virtual std::string createPossibleValuesString() const
  556. {
  557. return "";
  558. }
  559. };
  560. class HttpProxyOptionHandler : public NameMatchOptionHandler {
  561. private:
  562. std::string _proxyUserPref;
  563. std::string _proxyPasswdPref;
  564. public:
  565. HttpProxyOptionHandler(const std::string& optName,
  566. const std::string& description,
  567. const std::string& defaultValue,
  568. char shortName = 0)
  569. :
  570. NameMatchOptionHandler(optName, description, defaultValue,
  571. OptionHandler::REQ_ARG, shortName),
  572. _proxyUserPref(_optName+"-user"),
  573. _proxyPasswdPref(_optName+"-passwd")
  574. {}
  575. virtual ~HttpProxyOptionHandler() {}
  576. virtual void parseArg(Option& option, const std::string& optarg)
  577. {
  578. if(optarg.empty()) {
  579. option.put(_optName, optarg);
  580. } else {
  581. Request req;
  582. std::string uri;
  583. if(util::startsWith(optarg, "http://")) {
  584. uri = optarg;
  585. } else {
  586. uri = "http://"+optarg;
  587. }
  588. if(!req.setUri(uri)) {
  589. throw DL_ABORT_EX(_("unrecognized proxy format"));
  590. }
  591. uri = "http://";
  592. if(req.getUsername().empty()) {
  593. if(option.defined(_proxyUserPref)) {
  594. uri += util::percentEncode(option.get(_proxyUserPref));
  595. }
  596. } else {
  597. uri += util::percentEncode(req.getUsername());
  598. }
  599. if(!req.hasPassword()) {
  600. if(option.defined(_proxyPasswdPref)) {
  601. uri += A2STR::COLON_C;
  602. uri += util::percentEncode(option.get(_proxyPasswdPref));
  603. }
  604. } else {
  605. uri += A2STR::COLON_C;
  606. uri += util::percentEncode(req.getPassword());
  607. }
  608. if(uri.size() > 7) {
  609. uri += "@";
  610. }
  611. strappend(uri, req.getHost(), A2STR::COLON_C, util::uitos(req.getPort()));
  612. option.put(_optName, uri);
  613. }
  614. }
  615. virtual std::string createPossibleValuesString() const
  616. {
  617. return "[http://][USER:PASSWORD@]HOST[:PORT]";
  618. }
  619. };
  620. class LocalFilePathOptionHandler : public NameMatchOptionHandler {
  621. private:
  622. bool _acceptStdin;
  623. public:
  624. LocalFilePathOptionHandler
  625. (const std::string& optName,
  626. const std::string& description = NO_DESCRIPTION,
  627. const std::string& defaultValue = NO_DEFAULT_VALUE,
  628. bool acceptStdin = false,
  629. char shortName = 0):
  630. NameMatchOptionHandler(optName, description, defaultValue,
  631. OptionHandler::REQ_ARG,
  632. shortName),
  633. _acceptStdin(acceptStdin) {}
  634. virtual void parseArg(Option& option, const std::string& optarg)
  635. {
  636. if(_acceptStdin && optarg == "-") {
  637. option.put(_optName, DEV_STDIN);
  638. } else {
  639. File f(optarg);
  640. if(!f.exists() || f.isDir()) {
  641. throw DL_ABORT_EX
  642. (StringFormat(MSG_NOT_FILE, optarg.c_str()).str());
  643. }
  644. option.put(_optName, optarg);
  645. }
  646. }
  647. virtual std::string createPossibleValuesString() const
  648. {
  649. return "/path/to/file";
  650. }
  651. };
  652. class PrioritizePieceOptionHandler:public NameMatchOptionHandler {
  653. public:
  654. PrioritizePieceOptionHandler
  655. (const std::string& optName,
  656. const std::string& description = NO_DESCRIPTION,
  657. const std::string& defaultValue = NO_DEFAULT_VALUE,
  658. char shortName = 0):
  659. NameMatchOptionHandler(optName, description, defaultValue,
  660. OptionHandler::REQ_ARG, shortName) {}
  661. virtual void parseArg(Option& option, const std::string& optarg)
  662. {
  663. // Parse optarg against empty FileEntry list to detect syntax
  664. // error.
  665. std::vector<size_t> result;
  666. util::parsePrioritizePieceRange
  667. (result, optarg, std::vector<SharedHandle<FileEntry> >(), 1024);
  668. option.put(_optName, optarg);
  669. }
  670. virtual std::string createPossibleValuesString() const
  671. {
  672. return "head[=SIZE],tail[=SIZE]";
  673. }
  674. };
  675. } // namespace aria2
  676. #endif // _D_OPTION_HANDLER_IMPL_H_