FtpNegotiationCommand.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. #include "FtpNegotiationCommand.h"
  36. #include <stdint.h>
  37. #include <cassert>
  38. #include <utility>
  39. #include <map>
  40. #include "Request.h"
  41. #include "DownloadEngine.h"
  42. #include "FtpConnection.h"
  43. #include "RequestGroup.h"
  44. #include "PieceStorage.h"
  45. #include "FtpDownloadCommand.h"
  46. #include "FileEntry.h"
  47. #include "DlAbortEx.h"
  48. #include "message.h"
  49. #include "prefs.h"
  50. #include "util.h"
  51. #include "Option.h"
  52. #include "Logger.h"
  53. #include "Segment.h"
  54. #include "DownloadContext.h"
  55. #include "DefaultBtProgressInfoFile.h"
  56. #include "RequestGroupMan.h"
  57. #include "DownloadFailureException.h"
  58. #include "Socket.h"
  59. #include "StringFormat.h"
  60. #include "DiskAdaptor.h"
  61. #include "SegmentMan.h"
  62. #include "AuthConfigFactory.h"
  63. #include "AuthConfig.h"
  64. #include "a2functional.h"
  65. #include "URISelector.h"
  66. #include "HttpConnection.h"
  67. #include "HttpHeader.h"
  68. #include "HttpRequest.h"
  69. #include "HttpResponse.h"
  70. #include "DlRetryEx.h"
  71. #include "CookieStorage.h"
  72. #include "ServerStatMan.h"
  73. #include "FileAllocationEntry.h"
  74. #include "CheckIntegrityEntry.h"
  75. namespace aria2 {
  76. FtpNegotiationCommand::FtpNegotiationCommand
  77. (cuid_t cuid,
  78. const SharedHandle<Request>& req,
  79. const SharedHandle<FileEntry>& fileEntry,
  80. RequestGroup* requestGroup,
  81. DownloadEngine* e,
  82. const SocketHandle& socket,
  83. Seq seq,
  84. const std::string& baseWorkingDir):
  85. AbstractCommand(cuid, req, fileEntry, requestGroup, e, socket), sequence_(seq),
  86. ftp_(new FtpConnection(cuid, socket, req,
  87. e->getAuthConfigFactory()->createAuthConfig
  88. (req, requestGroup->getOption().get()),
  89. getOption().get()))
  90. {
  91. ftp_->setBaseWorkingDir(baseWorkingDir);
  92. if(seq == SEQ_RECV_GREETING) {
  93. setTimeout(getOption()->getAsInt(PREF_CONNECT_TIMEOUT));
  94. }
  95. disableReadCheckSocket();
  96. setWriteCheckSocket(getSocket());
  97. }
  98. FtpNegotiationCommand::~FtpNegotiationCommand() {}
  99. bool FtpNegotiationCommand::executeInternal() {
  100. while(processSequence(getSegments().front()));
  101. if(sequence_ == SEQ_RETRY) {
  102. return prepareForRetry(0);
  103. } else if(sequence_ == SEQ_NEGOTIATION_COMPLETED) {
  104. FtpDownloadCommand* command =
  105. new FtpDownloadCommand
  106. (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), ftp_,
  107. getDownloadEngine(), dataSocket_, getSocket());
  108. command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
  109. command->setLowestDownloadSpeedLimit
  110. (getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
  111. if(getFileEntry()->isUniqueProtocol()) {
  112. getFileEntry()->removeURIWhoseHostnameIs(getRequest()->getHost());
  113. }
  114. getRequestGroup()->getURISelector()->tuneDownloadCommand
  115. (getFileEntry()->getRemainingUris(), command);
  116. getDownloadEngine()->addCommand(command);
  117. return true;
  118. } else if(sequence_ == SEQ_HEAD_OK ||
  119. sequence_ == SEQ_DOWNLOAD_ALREADY_COMPLETED) {
  120. return true;
  121. } else if(sequence_ == SEQ_FILE_PREPARATION) {
  122. if(getOption()->getAsBool(PREF_FTP_PASV)) {
  123. sequence_ = SEQ_SEND_PASV;
  124. } else {
  125. sequence_ = SEQ_PREPARE_SERVER_SOCKET;
  126. }
  127. return false;
  128. } else if(sequence_ == SEQ_EXIT) {
  129. return true;
  130. } else {
  131. getDownloadEngine()->addCommand(this);
  132. return false;
  133. }
  134. }
  135. bool FtpNegotiationCommand::recvGreeting() {
  136. if(!checkIfConnectionEstablished
  137. (getSocket(), connectedHostname_, connectedAddr_, connectedPort_)) {
  138. sequence_ = SEQ_EXIT;
  139. return false;
  140. }
  141. setTimeout(getRequestGroup()->getTimeout());
  142. //socket->setBlockingMode();
  143. disableWriteCheckSocket();
  144. setReadCheckSocket(getSocket());
  145. unsigned int status = ftp_->receiveResponse();
  146. if(status == 0) {
  147. return false;
  148. }
  149. if(status != 220) {
  150. throw DL_ABORT_EX(EX_CONNECTION_FAILED);
  151. }
  152. sequence_ = SEQ_SEND_USER;
  153. return true;
  154. }
  155. bool FtpNegotiationCommand::sendUser() {
  156. if(ftp_->sendUser()) {
  157. disableWriteCheckSocket();
  158. sequence_ = SEQ_RECV_USER;
  159. } else {
  160. setWriteCheckSocket(getSocket());
  161. }
  162. return false;
  163. }
  164. bool FtpNegotiationCommand::recvUser() {
  165. unsigned int status = ftp_->receiveResponse();
  166. switch(status) {
  167. case 0:
  168. return false;
  169. case 230:
  170. sequence_ = SEQ_SEND_TYPE;
  171. break;
  172. case 331:
  173. sequence_ = SEQ_SEND_PASS;
  174. break;
  175. default:
  176. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  177. }
  178. return true;
  179. }
  180. bool FtpNegotiationCommand::sendPass() {
  181. if(ftp_->sendPass()) {
  182. disableWriteCheckSocket();
  183. sequence_ = SEQ_RECV_PASS;
  184. } else {
  185. setWriteCheckSocket(getSocket());
  186. }
  187. return false;
  188. }
  189. bool FtpNegotiationCommand::recvPass() {
  190. unsigned int status = ftp_->receiveResponse();
  191. if(status == 0) {
  192. return false;
  193. }
  194. if(status != 230) {
  195. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  196. }
  197. sequence_ = SEQ_SEND_TYPE;
  198. return true;
  199. }
  200. bool FtpNegotiationCommand::sendType() {
  201. if(ftp_->sendType()) {
  202. disableWriteCheckSocket();
  203. sequence_ = SEQ_RECV_TYPE;
  204. } else {
  205. setWriteCheckSocket(getSocket());
  206. }
  207. return false;
  208. }
  209. bool FtpNegotiationCommand::recvType() {
  210. unsigned int status = ftp_->receiveResponse();
  211. if(status == 0) {
  212. return false;
  213. }
  214. if(status != 200) {
  215. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  216. }
  217. sequence_ = SEQ_SEND_PWD;
  218. return true;
  219. }
  220. bool FtpNegotiationCommand::sendPwd()
  221. {
  222. if(ftp_->sendPwd()) {
  223. disableWriteCheckSocket();
  224. sequence_ = SEQ_RECV_PWD;
  225. } else {
  226. setWriteCheckSocket(getSocket());
  227. }
  228. return false;
  229. }
  230. bool FtpNegotiationCommand::recvPwd()
  231. {
  232. std::string pwd;
  233. unsigned int status = ftp_->receivePwdResponse(pwd);
  234. if(status == 0) {
  235. return false;
  236. }
  237. if(status != 257) {
  238. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  239. }
  240. ftp_->setBaseWorkingDir(pwd);
  241. if(getLogger()->info()) {
  242. getLogger()->info("CUID#%s - base working directory is '%s'",
  243. util::itos(getCuid()).c_str(), pwd.c_str());
  244. }
  245. sequence_ = SEQ_SEND_CWD_PREP;
  246. return true;
  247. }
  248. bool FtpNegotiationCommand::sendCwdPrep()
  249. {
  250. // Calling setReadCheckSocket() is needed when the socket is reused,
  251. setReadCheckSocket(getSocket());
  252. util::split(getRequest()->getDir(), std::back_inserter(cwdDirs_),
  253. A2STR::SLASH_C);
  254. cwdDirs_.push_front(ftp_->getBaseWorkingDir());
  255. sequence_ = SEQ_SEND_CWD;
  256. return true;
  257. }
  258. bool FtpNegotiationCommand::sendCwd()
  259. {
  260. if(ftp_->sendCwd(cwdDirs_.front())) {
  261. disableWriteCheckSocket();
  262. sequence_ = SEQ_RECV_CWD;
  263. } else {
  264. setWriteCheckSocket(getSocket());
  265. }
  266. return false;
  267. }
  268. bool FtpNegotiationCommand::recvCwd()
  269. {
  270. unsigned int status = ftp_->receiveResponse();
  271. if(status == 0) {
  272. return false;
  273. }
  274. if(status != 250) {
  275. poolConnection();
  276. getRequestGroup()->increaseAndValidateFileNotFoundCount();
  277. if (status == 550)
  278. throw DL_ABORT_EX2(MSG_RESOURCE_NOT_FOUND,
  279. downloadresultcode::RESOURCE_NOT_FOUND);
  280. else
  281. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  282. }
  283. cwdDirs_.pop_front();
  284. if(cwdDirs_.empty()) {
  285. if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
  286. sequence_ = SEQ_SEND_MDTM;
  287. } else {
  288. sequence_ = SEQ_SEND_SIZE;
  289. }
  290. } else {
  291. sequence_ = SEQ_SEND_CWD;
  292. }
  293. return true;
  294. }
  295. bool FtpNegotiationCommand::sendMdtm()
  296. {
  297. if(ftp_->sendMdtm()) {
  298. disableWriteCheckSocket();
  299. sequence_ = SEQ_RECV_MDTM;
  300. } else {
  301. setWriteCheckSocket(getSocket());
  302. }
  303. return false;
  304. }
  305. bool FtpNegotiationCommand::recvMdtm()
  306. {
  307. Time lastModifiedTime = Time::null();
  308. unsigned int status = ftp_->receiveMdtmResponse(lastModifiedTime);
  309. if(status == 0) {
  310. return false;
  311. }
  312. if(status == 213) {
  313. if(lastModifiedTime.good()) {
  314. getRequestGroup()->updateLastModifiedTime(lastModifiedTime);
  315. time_t t = lastModifiedTime.getTime();
  316. struct tm* tms = gmtime(&t); // returned struct is statically allocated.
  317. if(tms) {
  318. if(getLogger()->debug()) {
  319. getLogger()->debug("MDTM result was parsed as: %s GMT", asctime(tms));
  320. }
  321. } else {
  322. if(getLogger()->debug()) {
  323. getLogger()->debug("gmtime() failed for MDTM result.");
  324. }
  325. }
  326. } else {
  327. if(getLogger()->debug()) {
  328. getLogger()->debug("MDTM response was returned, but it seems not to be"
  329. " a time value as in specified in RFC3659.");
  330. }
  331. }
  332. } else {
  333. if(getLogger()->info()) {
  334. getLogger()->info("CUID#%s - MDTM command failed.",
  335. util::itos(getCuid()).c_str());
  336. }
  337. }
  338. sequence_ = SEQ_SEND_SIZE;
  339. return true;
  340. }
  341. bool FtpNegotiationCommand::sendSize() {
  342. if(ftp_->sendSize()) {
  343. disableWriteCheckSocket();
  344. sequence_ = SEQ_RECV_SIZE;
  345. } else {
  346. setWriteCheckSocket(getSocket());
  347. }
  348. return false;
  349. }
  350. bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength)
  351. {
  352. getFileEntry()->setLength(totalLength);
  353. if(getFileEntry()->getPath().empty()) {
  354. getFileEntry()->setPath
  355. (util::applyDir
  356. (getDownloadContext()->getDir(),
  357. util::fixTaintedBasename
  358. (util::percentDecode(getRequest()->getFile()))));
  359. }
  360. getRequestGroup()->preDownloadProcessing();
  361. if(getDownloadEngine()->getRequestGroupMan()->
  362. isSameFileBeingDownloaded(getRequestGroup())) {
  363. throw DOWNLOAD_FAILURE_EXCEPTION
  364. (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
  365. getRequestGroup()->getFirstFilePath().c_str()).str());
  366. }
  367. if(totalLength == 0) {
  368. if(getOption()->getAsBool(PREF_FTP_PASV)) {
  369. sequence_ = SEQ_SEND_PASV;
  370. } else {
  371. sequence_ = SEQ_PREPARE_SERVER_SOCKET;
  372. }
  373. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  374. getRequestGroup()->initPieceStorage();
  375. onDryRunFileFound();
  376. return false;
  377. }
  378. if(getRequestGroup()->downloadFinishedByFileLength()) {
  379. getRequestGroup()->initPieceStorage();
  380. getPieceStorage()->markAllPiecesDone();
  381. sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
  382. getLogger()->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
  383. util::itos(getRequestGroup()->getGID()).c_str(),
  384. getRequestGroup()->getFirstFilePath().c_str());
  385. poolConnection();
  386. return false;
  387. }
  388. getRequestGroup()->shouldCancelDownloadForSafety();
  389. getRequestGroup()->initPieceStorage();
  390. getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
  391. if(getDownloadContext()->knowsTotalLength()) {
  392. sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
  393. poolConnection();
  394. return false;
  395. }
  396. // We have to make sure that command that has Request object must
  397. // have segment after PieceStorage is initialized. See
  398. // AbstractCommand::execute()
  399. getSegmentMan()->getSegment(getCuid(), 0);
  400. return true;
  401. } else {
  402. getRequestGroup()->adjustFilename
  403. (SharedHandle<BtProgressInfoFile>
  404. (new DefaultBtProgressInfoFile
  405. (getDownloadContext(),
  406. SharedHandle<PieceStorage>(),
  407. getOption().get())));
  408. getRequestGroup()->initPieceStorage();
  409. if(getOption()->getAsBool(PREF_DRY_RUN)) {
  410. onDryRunFileFound();
  411. return false;
  412. }
  413. BtProgressInfoFileHandle infoFile
  414. (new DefaultBtProgressInfoFile(getDownloadContext(),
  415. getPieceStorage(),
  416. getOption().get()));
  417. if(!infoFile->exists() &&
  418. getRequestGroup()->downloadFinishedByFileLength()) {
  419. getPieceStorage()->markAllPiecesDone();
  420. sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
  421. getLogger()->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
  422. util::itos(getRequestGroup()->getGID()).c_str(),
  423. getRequestGroup()->getFirstFilePath().c_str());
  424. poolConnection();
  425. return false;
  426. }
  427. getRequestGroup()->loadAndOpenFile(infoFile);
  428. // We have to make sure that command that has Request object must
  429. // have segment after PieceStorage is initialized. See
  430. // AbstractCommand::execute()
  431. getSegmentMan()->getSegment(getCuid(), 0);
  432. prepareForNextAction(this);
  433. disableReadCheckSocket();
  434. }
  435. return false;
  436. }
  437. bool FtpNegotiationCommand::recvSize() {
  438. uint64_t size = 0;
  439. unsigned int status = ftp_->receiveSizeResponse(size);
  440. if(status == 0) {
  441. return false;
  442. }
  443. if(status == 213) {
  444. if(size > INT64_MAX) {
  445. throw DL_ABORT_EX
  446. (StringFormat(EX_TOO_LARGE_FILE,
  447. util::uitos(size, true).c_str()).str());
  448. }
  449. if(getPieceStorage().isNull()) {
  450. sequence_ = SEQ_FILE_PREPARATION;
  451. return onFileSizeDetermined(size);
  452. } else {
  453. getRequestGroup()->validateTotalLength(getFileEntry()->getLength(), size);
  454. }
  455. } else {
  456. if(getLogger()->info()) {
  457. getLogger()->info("CUID#%s - The remote FTP Server doesn't recognize SIZE"
  458. " command. Continue.", util::itos(getCuid()).c_str());
  459. }
  460. // Even if one of the other servers waiting in the queue supports SIZE
  461. // command, resuming and segmented downloading are disabled when the first
  462. // contacted FTP server doesn't support it.
  463. if(getPieceStorage().isNull()) {
  464. getDownloadContext()->markTotalLengthIsUnknown();
  465. return onFileSizeDetermined(0);
  466. }
  467. // TODO Skipping RequestGroup::validateTotalLength(0) here will allow
  468. // wrong file to be downloaded if user-specified URL is wrong.
  469. }
  470. if(getOption()->getAsBool(PREF_FTP_PASV)) {
  471. sequence_ = SEQ_SEND_PASV;
  472. } else {
  473. sequence_ = SEQ_PREPARE_SERVER_SOCKET;
  474. }
  475. return true;
  476. }
  477. void FtpNegotiationCommand::afterFileAllocation()
  478. {
  479. setReadCheckSocket(getSocket());
  480. }
  481. bool FtpNegotiationCommand::prepareServerSocket()
  482. {
  483. serverSocket_ = ftp_->createServerSocket();
  484. sequence_ = SEQ_SEND_PORT;
  485. return true;
  486. }
  487. bool FtpNegotiationCommand::sendPort() {
  488. afterFileAllocation();
  489. if(ftp_->sendPort(serverSocket_)) {
  490. disableWriteCheckSocket();
  491. sequence_ = SEQ_RECV_PORT;
  492. } else {
  493. setWriteCheckSocket(getSocket());
  494. }
  495. return false;
  496. }
  497. bool FtpNegotiationCommand::recvPort() {
  498. unsigned int status = ftp_->receiveResponse();
  499. if(status == 0) {
  500. return false;
  501. }
  502. if(status != 200) {
  503. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  504. }
  505. sequence_ = SEQ_SEND_REST;
  506. return true;
  507. }
  508. bool FtpNegotiationCommand::sendPasv() {
  509. afterFileAllocation();
  510. if(ftp_->sendPasv()) {
  511. disableWriteCheckSocket();
  512. sequence_ = SEQ_RECV_PASV;
  513. } else {
  514. setWriteCheckSocket(getSocket());
  515. }
  516. return false;
  517. }
  518. bool FtpNegotiationCommand::recvPasv() {
  519. std::pair<std::string, uint16_t> dest;
  520. unsigned int status = ftp_->receivePasvResponse(dest);
  521. if(status == 0) {
  522. return false;
  523. }
  524. if(status != 227) {
  525. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  526. }
  527. // TODO Should we check to see that dest.first is not in noProxy list?
  528. if(isProxyDefined()) {
  529. dataConnAddr_ = dest;
  530. sequence_ = SEQ_RESOLVE_PROXY;
  531. return true;
  532. } else {
  533. // make a data connection to the server.
  534. if(getLogger()->info()) {
  535. getLogger()->info(MSG_CONNECTING_TO_SERVER, util::itos(getCuid()).c_str(),
  536. dest.first.c_str(),
  537. dest.second);
  538. }
  539. dataSocket_.reset(new SocketCore());
  540. dataSocket_->establishConnection(dest.first, dest.second);
  541. disableReadCheckSocket();
  542. setWriteCheckSocket(dataSocket_);
  543. sequence_ = SEQ_SEND_REST_PASV;
  544. return false;
  545. }
  546. }
  547. bool FtpNegotiationCommand::resolveProxy()
  548. {
  549. SharedHandle<Request> proxyReq = createProxyRequest();
  550. std::vector<std::string> addrs;
  551. proxyAddr_ = resolveHostname
  552. (addrs, proxyReq->getHost(), proxyReq->getPort());
  553. if(proxyAddr_.empty()) {
  554. return false;
  555. }
  556. if(getLogger()->info()) {
  557. getLogger()->info(MSG_CONNECTING_TO_SERVER, util::itos(getCuid()).c_str(),
  558. proxyAddr_.c_str(), proxyReq->getPort());
  559. }
  560. dataSocket_.reset(new SocketCore());
  561. dataSocket_->establishConnection(proxyAddr_, proxyReq->getPort());
  562. disableReadCheckSocket();
  563. setWriteCheckSocket(dataSocket_);
  564. http_.reset(new HttpConnection(getCuid(), dataSocket_));
  565. sequence_ = SEQ_SEND_TUNNEL_REQUEST;
  566. return false;
  567. }
  568. bool FtpNegotiationCommand::sendTunnelRequest()
  569. {
  570. if(http_->sendBufferIsEmpty()) {
  571. if(dataSocket_->isReadable(0)) {
  572. std::string error = getSocket()->getSocketError();
  573. if(!error.empty()) {
  574. SharedHandle<Request> proxyReq = createProxyRequest();
  575. getDownloadEngine()->markBadIPAddress(proxyReq->getHost(),
  576. proxyAddr_,proxyReq->getPort());
  577. std::string nextProxyAddr = getDownloadEngine()->findCachedIPAddress
  578. (proxyReq->getHost(), proxyReq->getPort());
  579. if(nextProxyAddr.empty()) {
  580. getDownloadEngine()->removeCachedIPAddress(proxyReq->getHost(),
  581. proxyReq->getPort());
  582. throw DL_RETRY_EX
  583. (StringFormat(MSG_ESTABLISHING_CONNECTION_FAILED,
  584. error.c_str()).str());
  585. } else {
  586. if(getLogger()->info()) {
  587. getLogger()->info(MSG_CONNECT_FAILED_AND_RETRY,
  588. util::itos(getCuid()).c_str(),
  589. proxyAddr_.c_str(), proxyReq->getPort());
  590. }
  591. proxyAddr_ = nextProxyAddr;
  592. if(getLogger()->info()) {
  593. getLogger()->info(MSG_CONNECTING_TO_SERVER,
  594. util::itos(getCuid()).c_str(),
  595. proxyAddr_.c_str(), proxyReq->getPort());
  596. }
  597. dataSocket_->establishConnection(proxyAddr_, proxyReq->getPort());
  598. return false;
  599. }
  600. }
  601. }
  602. SharedHandle<HttpRequest> httpRequest(new HttpRequest());
  603. httpRequest->setUserAgent(getOption()->get(PREF_USER_AGENT));
  604. SharedHandle<Request> req(new Request());
  605. // Construct fake URI in order to use HttpRequest
  606. req->setUri(strconcat("ftp://", dataConnAddr_.first,
  607. A2STR::COLON_C, util::uitos(dataConnAddr_.second)));
  608. httpRequest->setRequest(req);
  609. httpRequest->setProxyRequest(createProxyRequest());
  610. http_->sendProxyRequest(httpRequest);
  611. } else {
  612. http_->sendPendingData();
  613. }
  614. if(http_->sendBufferIsEmpty()) {
  615. disableWriteCheckSocket();
  616. setReadCheckSocket(dataSocket_);
  617. sequence_ = SEQ_RECV_TUNNEL_RESPONSE;
  618. return false;
  619. } else {
  620. setWriteCheckSocket(dataSocket_);
  621. return false;
  622. }
  623. }
  624. bool FtpNegotiationCommand::recvTunnelResponse()
  625. {
  626. SharedHandle<HttpResponse> httpResponse = http_->receiveResponse();
  627. if(httpResponse.isNull()) {
  628. return false;
  629. }
  630. if(httpResponse->getResponseStatus() != HttpHeader::S200) {
  631. throw DL_RETRY_EX(EX_PROXY_CONNECTION_FAILED);
  632. }
  633. sequence_ = SEQ_SEND_REST_PASV;
  634. return true;
  635. }
  636. bool FtpNegotiationCommand::sendRestPasv(const SharedHandle<Segment>& segment) {
  637. //dataSocket_->setBlockingMode();
  638. setReadCheckSocket(getSocket());
  639. disableWriteCheckSocket();
  640. return sendRest(segment);
  641. }
  642. bool FtpNegotiationCommand::sendRest(const SharedHandle<Segment>& segment) {
  643. if(ftp_->sendRest(segment)) {
  644. disableWriteCheckSocket();
  645. sequence_ = SEQ_RECV_REST;
  646. } else {
  647. setWriteCheckSocket(getSocket());
  648. }
  649. return false;
  650. }
  651. bool FtpNegotiationCommand::recvRest(const SharedHandle<Segment>& segment) {
  652. unsigned int status = ftp_->receiveResponse();
  653. if(status == 0) {
  654. return false;
  655. }
  656. // If we recieve negative response and requested file position is not 0,
  657. // then throw exception here.
  658. if(status != 350) {
  659. if(!segment.isNull() && segment->getPositionToWrite() != 0) {
  660. throw DL_ABORT_EX2("FTP server doesn't support resuming.",
  661. downloadresultcode::CANNOT_RESUME);
  662. }
  663. }
  664. sequence_ = SEQ_SEND_RETR;
  665. return true;
  666. }
  667. bool FtpNegotiationCommand::sendRetr() {
  668. if(ftp_->sendRetr()) {
  669. disableWriteCheckSocket();
  670. sequence_ = SEQ_RECV_RETR;
  671. } else {
  672. setWriteCheckSocket(getSocket());
  673. }
  674. return false;
  675. }
  676. bool FtpNegotiationCommand::recvRetr() {
  677. unsigned int status = ftp_->receiveResponse();
  678. if(status == 0) {
  679. return false;
  680. }
  681. if(status != 150 && status != 125) {
  682. getRequestGroup()->increaseAndValidateFileNotFoundCount();
  683. if (status == 550)
  684. throw DL_ABORT_EX2(MSG_RESOURCE_NOT_FOUND,
  685. downloadresultcode::RESOURCE_NOT_FOUND);
  686. else
  687. throw DL_ABORT_EX(StringFormat(EX_BAD_STATUS, status).str());
  688. }
  689. if(getOption()->getAsBool(PREF_FTP_PASV)) {
  690. sequence_ = SEQ_NEGOTIATION_COMPLETED;
  691. return false;
  692. } else {
  693. disableReadCheckSocket();
  694. setReadCheckSocket(serverSocket_);
  695. sequence_ = SEQ_WAIT_CONNECTION;
  696. return false;
  697. }
  698. }
  699. bool FtpNegotiationCommand::waitConnection()
  700. {
  701. disableReadCheckSocket();
  702. setReadCheckSocket(getSocket());
  703. dataSocket_.reset(serverSocket_->acceptConnection());
  704. dataSocket_->setNonBlockingMode();
  705. sequence_ = SEQ_NEGOTIATION_COMPLETED;
  706. return false;
  707. }
  708. bool FtpNegotiationCommand::processSequence
  709. (const SharedHandle<Segment>& segment) {
  710. bool doNextSequence = true;
  711. switch(sequence_) {
  712. case SEQ_RECV_GREETING:
  713. return recvGreeting();
  714. case SEQ_SEND_USER:
  715. return sendUser();
  716. case SEQ_RECV_USER:
  717. return recvUser();
  718. case SEQ_SEND_PASS:
  719. return sendPass();
  720. case SEQ_RECV_PASS:
  721. return recvPass();
  722. case SEQ_SEND_TYPE:
  723. return sendType();
  724. case SEQ_RECV_TYPE:
  725. return recvType();
  726. case SEQ_SEND_PWD:
  727. return sendPwd();
  728. case SEQ_RECV_PWD:
  729. return recvPwd();
  730. case SEQ_SEND_CWD_PREP:
  731. return sendCwdPrep();
  732. case SEQ_SEND_CWD:
  733. return sendCwd();
  734. case SEQ_RECV_CWD:
  735. return recvCwd();
  736. case SEQ_SEND_MDTM:
  737. return sendMdtm();
  738. case SEQ_RECV_MDTM:
  739. return recvMdtm();
  740. case SEQ_SEND_SIZE:
  741. return sendSize();
  742. case SEQ_RECV_SIZE:
  743. return recvSize();
  744. case SEQ_PREPARE_SERVER_SOCKET:
  745. return prepareServerSocket();
  746. case SEQ_SEND_PORT:
  747. return sendPort();
  748. case SEQ_RECV_PORT:
  749. return recvPort();
  750. case SEQ_SEND_PASV:
  751. return sendPasv();
  752. case SEQ_RECV_PASV:
  753. return recvPasv();
  754. case SEQ_RESOLVE_PROXY:
  755. return resolveProxy();
  756. case SEQ_SEND_TUNNEL_REQUEST:
  757. return sendTunnelRequest();
  758. case SEQ_RECV_TUNNEL_RESPONSE:
  759. return recvTunnelResponse();
  760. case SEQ_SEND_REST_PASV:
  761. return sendRestPasv(segment);
  762. case SEQ_SEND_REST:
  763. return sendRest(segment);
  764. case SEQ_RECV_REST:
  765. return recvRest(segment);
  766. case SEQ_SEND_RETR:
  767. return sendRetr();
  768. case SEQ_RECV_RETR:
  769. return recvRetr();
  770. case SEQ_WAIT_CONNECTION:
  771. return waitConnection();
  772. default:
  773. abort();
  774. }
  775. return doNextSequence;
  776. }
  777. void FtpNegotiationCommand::poolConnection() const
  778. {
  779. if(getOption()->getAsBool(PREF_FTP_REUSE_CONNECTION)) {
  780. std::map<std::string, std::string> options;
  781. options["baseWorkingDir"] = ftp_->getBaseWorkingDir();
  782. getDownloadEngine()->poolSocket(getRequest(), ftp_->getUser(),
  783. createProxyRequest(), getSocket(), options);
  784. }
  785. }
  786. void FtpNegotiationCommand::onDryRunFileFound()
  787. {
  788. getPieceStorage()->markAllPiecesDone();
  789. poolConnection();
  790. sequence_ = SEQ_HEAD_OK;
  791. }
  792. } // namespace aria2