diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-12-27 18:39:47 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-12-27 18:40:08 +0900 |
| commit | b1132d6b10cd3791b9529d7031d723fd0e2d8215 (patch) | |
| tree | 9ce465b42bf09372118335c7c619bb2b41869eaf /src/FtpConnection.cc | |
| parent | 4abad2f64c9ff80144a61c5a9c2bea144852b883 (diff) | |
make clang-format using clang-format-3.6
Diffstat (limited to 'src/FtpConnection.cc')
| -rw-r--r-- | src/FtpConnection.cc | 335 |
1 files changed, 165 insertions, 170 deletions
diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index b92afe20..f41c5509 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -61,31 +61,30 @@ namespace aria2 { -FtpConnection::FtpConnection -(cuid_t cuid, - const std::shared_ptr<SocketCore>& socket, - const std::shared_ptr<Request>& req, - const std::shared_ptr<AuthConfig>& authConfig, - const Option* op) - : cuid_(cuid), - socket_(socket), - req_(req), - authConfig_(authConfig), - option_(op), - socketBuffer_(socket), - baseWorkingDir_("/") -{} +FtpConnection::FtpConnection(cuid_t cuid, + const std::shared_ptr<SocketCore>& socket, + const std::shared_ptr<Request>& req, + const std::shared_ptr<AuthConfig>& authConfig, + const Option* op) + : cuid_(cuid), + socket_(socket), + req_(req), + authConfig_(authConfig), + option_(op), + socketBuffer_(socket), + baseWorkingDir_("/") +{ +} FtpConnection::~FtpConnection() {} bool FtpConnection::sendUser() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "USER "; request += authConfig_->getUser(); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, "USER ********")); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, "USER ********")); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -94,12 +93,11 @@ bool FtpConnection::sendUser() bool FtpConnection::sendPass() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "PASS "; request += authConfig_->getPassword(); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, "PASS ********")); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, "PASS ********")); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -108,12 +106,11 @@ bool FtpConnection::sendPass() bool FtpConnection::sendType() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "TYPE "; request += (option_->get(PREF_FTP_TYPE) == V_ASCII ? 'A' : 'I'); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_,request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -122,10 +119,9 @@ bool FtpConnection::sendType() bool FtpConnection::sendPwd() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "PWD\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_,request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -134,12 +130,11 @@ bool FtpConnection::sendPwd() bool FtpConnection::sendCwd(const std::string& dir) { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "CWD "; request += util::percentDecode(dir.begin(), dir.end()); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_,request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -148,13 +143,12 @@ bool FtpConnection::sendCwd(const std::string& dir) bool FtpConnection::sendMdtm() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "MDTM "; request += - util::percentDecode(req_->getFile().begin(), req_->getFile().end()); + util::percentDecode(req_->getFile().begin(), req_->getFile().end()); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -163,13 +157,12 @@ bool FtpConnection::sendMdtm() bool FtpConnection::sendSize() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "SIZE "; request += - util::percentDecode(req_->getFile().begin(), req_->getFile().end()); + util::percentDecode(req_->getFile().begin(), req_->getFile().end()); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -178,10 +171,9 @@ bool FtpConnection::sendSize() bool FtpConnection::sendEpsv() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request("EPSV\r\n"); - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -190,10 +182,9 @@ bool FtpConnection::sendEpsv() bool FtpConnection::sendPasv() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request("PASV\r\n"); - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -212,17 +203,15 @@ std::shared_ptr<SocketCore> FtpConnection::createServerSocket() bool FtpConnection::sendEprt(const std::shared_ptr<SocketCore>& serverSocket) { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { sockaddr_union sockaddr; socklen_t len = sizeof(sockaddr); serverSocket->getAddrInfo(sockaddr, len); std::pair<std::string, uint16_t> addrinfo = - util::getNumericNameInfo(&sockaddr.sa, len); - std::string request = - fmt("EPRT |%d|%s|%u|\r\n", - sockaddr.storage.ss_family == AF_INET ? 1 : 2, - addrinfo.first.c_str(), - addrinfo.second); + util::getNumericNameInfo(&sockaddr.sa, len); + std::string request = fmt("EPRT |%d|%s|%u|\r\n", + sockaddr.storage.ss_family == AF_INET ? 1 : 2, + addrinfo.first.c_str(), addrinfo.second); A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } @@ -232,18 +221,17 @@ bool FtpConnection::sendEprt(const std::shared_ptr<SocketCore>& serverSocket) bool FtpConnection::sendPort(const std::shared_ptr<SocketCore>& serverSocket) { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::pair<std::string, uint16_t> addrinfo; socket_->getAddrInfo(addrinfo); int ipaddr[4]; - sscanf(addrinfo.first.c_str(), "%d.%d.%d.%d", - &ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]); + sscanf(addrinfo.first.c_str(), "%d.%d.%d.%d", &ipaddr[0], &ipaddr[1], + &ipaddr[2], &ipaddr[3]); serverSocket->getAddrInfo(addrinfo); - std::string request = fmt("PORT %d,%d,%d,%d,%d,%d\r\n", - ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3], - addrinfo.second/256, addrinfo.second%256); - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + std::string request = + fmt("PORT %d,%d,%d,%d,%d,%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], + ipaddr[3], addrinfo.second / 256, addrinfo.second % 256); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -252,13 +240,11 @@ bool FtpConnection::sendPort(const std::shared_ptr<SocketCore>& serverSocket) bool FtpConnection::sendRest(const std::shared_ptr<Segment>& segment) { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = - fmt("REST %" PRId64 "\r\n", - segment ? - segment->getPositionToWrite() : static_cast<int64_t>(0LL)); - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + fmt("REST %" PRId64 "\r\n", segment ? segment->getPositionToWrite() + : static_cast<int64_t>(0LL)); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -267,13 +253,12 @@ bool FtpConnection::sendRest(const std::shared_ptr<Segment>& segment) bool FtpConnection::sendRetr() { - if(socketBuffer_.sendBufferIsEmpty()) { + if (socketBuffer_.sendBufferIsEmpty()) { std::string request = "RETR "; request += - util::percentDecode(req_->getFile().begin(), req_->getFile().end()); + util::percentDecode(req_->getFile().begin(), req_->getFile().end()); request += "\r\n"; - A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, - cuid_, request.c_str())); + A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } socketBuffer_.send(); @@ -285,13 +270,14 @@ int FtpConnection::getStatus(const std::string& response) const int status; // When the response is not like "%d %*s", // we return 0. - if(response.find_first_not_of("0123456789") != 3 - || !(response.find(" ") == 3 || response.find("-") == 3)) { + if (response.find_first_not_of("0123456789") != 3 || + !(response.find(" ") == 3 || response.find("-") == 3)) { return 0; } - if(sscanf(response.c_str(), "%d %*s", &status) == 1) { + if (sscanf(response.c_str(), "%d %*s", &status) == 1) { return status; - } else { + } + else { return 0; } } @@ -300,33 +286,35 @@ int FtpConnection::getStatus(const std::string& response) const // The length includes \r\n. // If the whole response has not been received, then returns std::string::npos. std::string::size_type -FtpConnection::findEndOfResponse -(int status, const std::string& buf) const +FtpConnection::findEndOfResponse(int status, const std::string& buf) const { - if(buf.size() <= 4) { + if (buf.size() <= 4) { return std::string::npos; } // if 4th character of buf is '-', then multi line response is expected. - if(buf.at(3) == '-') { + if (buf.at(3) == '-') { // multi line response std::string::size_type p; p = buf.find(fmt("\r\n%d ", status)); - if(p == std::string::npos) { + if (p == std::string::npos) { return std::string::npos; } - p = buf.find("\r\n", p+6); - if(p == std::string::npos) { + p = buf.find("\r\n", p + 6); + if (p == std::string::npos) { return std::string::npos; - } else { - return p+2; } - } else { + else { + return p + 2; + } + } + else { // single line response std::string::size_type p = buf.find("\r\n"); - if(p == std::string::npos) { + if (p == std::string::npos) { return std::string::npos; - } else { - return p+2; + } + else { + return p + 2; } } } @@ -334,43 +322,42 @@ FtpConnection::findEndOfResponse bool FtpConnection::bulkReceiveResponse(std::pair<int, std::string>& response) { std::array<char, 1_k> buf; - while(1) { + while (1) { size_t size = buf.size(); socket_->readData(buf.data(), size); - if(size == 0) { - if(socket_->wantRead() || socket_->wantWrite()) { + if (size == 0) { + if (socket_->wantRead() || socket_->wantWrite()) { break; - } else { + } + else { throw DL_RETRY_EX(EX_GOT_EOF); } } - if(strbuf_.size()+size > MAX_RECV_BUFFER) { - throw DL_RETRY_EX - (fmt("Max FTP recv buffer reached. length=%lu", - static_cast<unsigned long>(strbuf_.size()+size))); + if (strbuf_.size() + size > MAX_RECV_BUFFER) { + throw DL_RETRY_EX(fmt("Max FTP recv buffer reached. length=%lu", + static_cast<unsigned long>(strbuf_.size() + size))); } strbuf_.append(std::begin(buf), std::begin(buf) + size); } int status; - if(strbuf_.size() >= 4) { + if (strbuf_.size() >= 4) { status = getStatus(strbuf_); - if(status == 0) { - throw DL_ABORT_EX2(EX_INVALID_RESPONSE, - error_code::FTP_PROTOCOL_ERROR); + if (status == 0) { + throw DL_ABORT_EX2(EX_INVALID_RESPONSE, error_code::FTP_PROTOCOL_ERROR); } - } else { + } + else { return false; } std::string::size_type length; - if((length = findEndOfResponse(status, strbuf_)) != std::string::npos) { + if ((length = findEndOfResponse(status, strbuf_)) != std::string::npos) { response.first = status; - response.second.assign(strbuf_.begin(), strbuf_.begin()+length); - A2_LOG_INFO(fmt(MSG_RECEIVE_RESPONSE, - cuid_, - response.second.c_str())); + response.second.assign(strbuf_.begin(), strbuf_.begin() + length); + A2_LOG_INFO(fmt(MSG_RECEIVE_RESPONSE, cuid_, response.second.c_str())); strbuf_.erase(0, length); return true; - } else { + } + else { // didn't receive response fully. return false; } @@ -379,57 +366,59 @@ bool FtpConnection::bulkReceiveResponse(std::pair<int, std::string>& response) int FtpConnection::receiveResponse() { std::pair<int, std::string> response; - if(bulkReceiveResponse(response)) { + if (bulkReceiveResponse(response)) { return response.first; - } else { + } + else { return 0; } } #ifdef __MINGW32__ -# define LONGLONG_PRINTF "%I64d" -# define ULONGLONG_PRINTF "%I64u" -# define LONGLONG_SCANF "%I64d" -# define ULONGLONG_SCANF "%I64u" +#define LONGLONG_PRINTF "%I64d" +#define ULONGLONG_PRINTF "%I64u" +#define LONGLONG_SCANF "%I64d" +#define ULONGLONG_SCANF "%I64u" #else -# define LONGLONG_PRINTF "%" PRId64 "" -# define ULONGLONG_PRINTF "%llu" -# define LONGLONG_SCANF "%Ld" +#define LONGLONG_PRINTF "%" PRId64 "" +#define ULONGLONG_PRINTF "%llu" +#define LONGLONG_SCANF "%Ld" // Mac OSX uses "%llu" for 64bits integer. -# define ULONGLONG_SCANF "%Lu" +#define ULONGLONG_SCANF "%Lu" #endif // __MINGW32__ int FtpConnection::receiveSizeResponse(int64_t& size) { std::pair<int, std::string> response; - if(bulkReceiveResponse(response)) { - if(response.first == 213) { + if (bulkReceiveResponse(response)) { + if (response.first == 213) { auto rp = util::divide(std::begin(response.second), - std::end(response.second), - ' '); - if(!util::parseLLIntNoThrow(size, std::string(rp.second.first, - rp.second.second)) || - size < 0) { + std::end(response.second), ' '); + if (!util::parseLLIntNoThrow( + size, std::string(rp.second.first, rp.second.second)) || + size < 0) { throw DL_ABORT_EX("Size must be positive integer"); } } return response.first; - } else { + } + else { return 0; } } namespace { -template<typename T> +template <typename T> bool getInteger(T* dest, const char* first, const char* last) { int res = 0; // We assume *dest won't overflow. - for(; first != last; ++first) { - if(util::isDigit(*first)) { + for (; first != last; ++first) { + if (util::isDigit(*first)) { res *= 10; - res += (*first)-'0'; - } else { + res += (*first) - '0'; + } + else { return false; } } @@ -442,33 +431,36 @@ int FtpConnection::receiveMdtmResponse(Time& time) { // MDTM command, specified in RFC3659. std::pair<int, std::string> response; - if(bulkReceiveResponse(response)) { - if(response.first == 213) { + if (bulkReceiveResponse(response)) { + if (response.first == 213) { char buf[15]; // YYYYMMDDhhmmss+\0, milli second part is dropped. sscanf(response.second.c_str(), "%*u %14s", buf); - if(strlen(buf) == 14) { + if (strlen(buf) == 14) { // We don't use Time::parse(buf,"%Y%m%d%H%M%S") here because Mac OS X // and included strptime doesn't parse data for this format. struct tm tm; memset(&tm, 0, sizeof(tm)); - if(getInteger(&tm.tm_sec, &buf[12], &buf[14]) && - getInteger(&tm.tm_min, &buf[10], &buf[12]) && - getInteger(&tm.tm_hour, &buf[8], &buf[10]) && - getInteger(&tm.tm_mday, &buf[6], &buf[8]) && - getInteger(&tm.tm_mon, &buf[4], &buf[6]) && - getInteger(&tm.tm_year, &buf[0], &buf[4])) { + if (getInteger(&tm.tm_sec, &buf[12], &buf[14]) && + getInteger(&tm.tm_min, &buf[10], &buf[12]) && + getInteger(&tm.tm_hour, &buf[8], &buf[10]) && + getInteger(&tm.tm_mday, &buf[6], &buf[8]) && + getInteger(&tm.tm_mon, &buf[4], &buf[6]) && + getInteger(&tm.tm_year, &buf[0], &buf[4])) { tm.tm_mon -= 1; tm.tm_year -= 1900; time = Time(timegm(&tm)); - } else { + } + else { time = Time::null(); } - } else { + } + else { time = Time::null(); } } return response.first; - } else { + } + else { return 0; } } @@ -476,58 +468,59 @@ int FtpConnection::receiveMdtmResponse(Time& time) int FtpConnection::receiveEpsvResponse(uint16_t& port) { std::pair<int, std::string> response; - if(bulkReceiveResponse(response)) { - if(response.first == 229) { + if (bulkReceiveResponse(response)) { + if (response.first == 229) { port = 0; std::string::size_type leftParen = response.second.find("("); std::string::size_type rightParen = response.second.find(")"); - if(leftParen == std::string::npos || rightParen == std::string::npos || - leftParen > rightParen) { + if (leftParen == std::string::npos || rightParen == std::string::npos || + leftParen > rightParen) { return response.first; } std::vector<Scip> rd; - util::splitIter(response.second.begin()+leftParen+1, - response.second.begin()+rightParen, + util::splitIter(response.second.begin() + leftParen + 1, + response.second.begin() + rightParen, std::back_inserter(rd), '|', true, true); uint32_t portTemp = 0; - if(rd.size() == 5 && - util::parseUIntNoThrow(portTemp, - std::string(rd[3].first, rd[3].second))) { - if(0 < portTemp && portTemp <= UINT16_MAX) { + if (rd.size() == 5 && + util::parseUIntNoThrow(portTemp, + std::string(rd[3].first, rd[3].second))) { + if (0 < portTemp && portTemp <= UINT16_MAX) { port = portTemp; } } } return response.first; - } else { + } + else { return 0; } } -int FtpConnection::receivePasvResponse -(std::pair<std::string, uint16_t>& dest) +int FtpConnection::receivePasvResponse(std::pair<std::string, uint16_t>& dest) { std::pair<int, std::string> response; - if(bulkReceiveResponse(response)) { - if(response.first == 227) { + if (bulkReceiveResponse(response)) { + if (response.first == 227) { // we assume the format of response is "227 Entering Passive // Mode (h1,h2,h3,h4,p1,p2)." int h1, h2, h3, h4, p1, p2; std::string::size_type p = response.second.find("("); - if(p >= 4) { - sscanf(response.second.c_str()+p, - "(%d,%d,%d,%d,%d,%d).", - &h1, &h2, &h3, &h4, &p1, &p2); + if (p >= 4) { + sscanf(response.second.c_str() + p, "(%d,%d,%d,%d,%d,%d).", &h1, &h2, + &h3, &h4, &p1, &p2); // ip address dest.first = fmt("%d.%d.%d.%d", h1, h2, h3, h4); // port number - dest.second = 256*p1+p2; - } else { + dest.second = 256 * p1 + p2; + } + else { throw DL_RETRY_EX(EX_INVALID_RESPONSE); } } return response.first; - } else { + } + else { return 0; } } @@ -535,21 +528,23 @@ int FtpConnection::receivePasvResponse int FtpConnection::receivePwdResponse(std::string& pwd) { std::pair<int, std::string> response; - if(bulkReceiveResponse(response)) { - if(response.first == 257) { + if (bulkReceiveResponse(response)) { + if (response.first == 257) { std::string::size_type first; std::string::size_type last; - if((first = response.second.find("\"")) != std::string::npos && - (last = response.second.find("\"", ++first)) != std::string::npos) { - pwd.assign(response.second.begin()+first, response.second.begin()+last); - } else { - throw DL_ABORT_EX2(EX_INVALID_RESPONSE, - error_code::FTP_PROTOCOL_ERROR); + if ((first = response.second.find("\"")) != std::string::npos && + (last = response.second.find("\"", ++first)) != std::string::npos) { + pwd.assign(response.second.begin() + first, + response.second.begin() + last); + } + else { + throw DL_ABORT_EX2(EX_INVALID_RESPONSE, error_code::FTP_PROTOCOL_ERROR); } } return response.first; - } else { + } + else { return 0; } } |
