FtpNegotiationCommand.cc 23 KB

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