OptionHandlerImpl.cc 23 KB

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