mirror of https://github.com/aria2/aria2
Convert to autos and ranged loops
parent
d8f44ef4f6
commit
8526ceeb45
|
@ -263,8 +263,7 @@ std::string AdaptiveURISelector::getMaxDownloadSpeedUri
|
||||||
{
|
{
|
||||||
int max = -1;
|
int max = -1;
|
||||||
std::string uri = A2STR::NIL;
|
std::string uri = A2STR::NIL;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for(auto i = uris.begin(), eoi = uris.end(); i != eoi; ++i) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
|
||||||
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
||||||
if(!ss)
|
if(!ss)
|
||||||
continue;
|
continue;
|
||||||
|
@ -285,8 +284,7 @@ std::deque<std::string> AdaptiveURISelector::getUrisBySpeed
|
||||||
(const std::deque<std::string>& uris, int min) const
|
(const std::deque<std::string>& uris, int min) const
|
||||||
{
|
{
|
||||||
std::deque<std::string> bests;
|
std::deque<std::string> bests;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for(auto i = uris.begin(), eoi = uris.end(); i != eoi; ++i) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
|
||||||
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
||||||
if(!ss)
|
if(!ss)
|
||||||
continue;
|
continue;
|
||||||
|
@ -302,7 +300,7 @@ std::string AdaptiveURISelector::selectRandomUri
|
||||||
(const std::deque<std::string>& uris) const
|
(const std::deque<std::string>& uris) const
|
||||||
{
|
{
|
||||||
int pos = SimpleRandomizer::getInstance()->getRandomNumber(uris.size());
|
int pos = SimpleRandomizer::getInstance()->getRandomNumber(uris.size());
|
||||||
std::deque<std::string>::const_iterator i = uris.begin();
|
auto i = uris.begin();
|
||||||
i = i+pos;
|
i = i+pos;
|
||||||
return *i;
|
return *i;
|
||||||
}
|
}
|
||||||
|
@ -310,11 +308,10 @@ std::string AdaptiveURISelector::selectRandomUri
|
||||||
std::string AdaptiveURISelector::getFirstNotTestedUri
|
std::string AdaptiveURISelector::getFirstNotTestedUri
|
||||||
(const std::deque<std::string>& uris) const
|
(const std::deque<std::string>& uris) const
|
||||||
{
|
{
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for (const auto& i : uris) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
std::shared_ptr<ServerStat> ss = getServerStats(i);
|
||||||
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
|
||||||
if(!ss)
|
if(!ss)
|
||||||
return *i;
|
return i;
|
||||||
}
|
}
|
||||||
return A2STR::NIL;
|
return A2STR::NIL;
|
||||||
}
|
}
|
||||||
|
@ -324,9 +321,8 @@ std::string AdaptiveURISelector::getFirstToTestUri
|
||||||
{
|
{
|
||||||
int counter;
|
int counter;
|
||||||
int power;
|
int power;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for (const auto& u : uris) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
std::shared_ptr<ServerStat> ss = getServerStats(u);
|
||||||
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
|
||||||
if(!ss)
|
if(!ss)
|
||||||
continue;
|
continue;
|
||||||
counter = ss->getCounter();
|
counter = ss->getCounter();
|
||||||
|
@ -336,7 +332,7 @@ std::string AdaptiveURISelector::getFirstToTestUri
|
||||||
/* We test the mirror another time if it has not been
|
/* We test the mirror another time if it has not been
|
||||||
* tested since 2^counter days */
|
* tested since 2^counter days */
|
||||||
if(ss->getLastUpdated().difference() > power*24*60*60) {
|
if(ss->getLastUpdated().difference() > power*24*60*60) {
|
||||||
return *i;
|
return u;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return A2STR::NIL;
|
return A2STR::NIL;
|
||||||
|
@ -359,9 +355,8 @@ int AdaptiveURISelector::getNbTestedServers
|
||||||
(const std::deque<std::string>& uris) const
|
(const std::deque<std::string>& uris) const
|
||||||
{
|
{
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for(const auto& u : uris) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
std::shared_ptr<ServerStat> ss = getServerStats(u);
|
||||||
std::shared_ptr<ServerStat> ss = getServerStats(*i);
|
|
||||||
if(!ss)
|
if(!ss)
|
||||||
++counter;
|
++counter;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,12 +61,11 @@ AnnounceList::~AnnounceList() {}
|
||||||
void AnnounceList::reconfigure
|
void AnnounceList::reconfigure
|
||||||
(const std::vector<std::vector<std::string> >& announceList)
|
(const std::vector<std::vector<std::string> >& announceList)
|
||||||
{
|
{
|
||||||
for(std::vector<std::vector<std::string> >::const_iterator itr =
|
for (const auto& vec: announceList) {
|
||||||
announceList.begin(), eoi = announceList.end(); itr != eoi; ++itr) {
|
if(vec.empty()) {
|
||||||
if((*itr).empty()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::deque<std::string> urls((*itr).begin(), (*itr).end());
|
std::deque<std::string> urls(vec.begin(), vec.end());
|
||||||
std::shared_ptr<AnnounceTier> tier(new AnnounceTier(urls));
|
std::shared_ptr<AnnounceTier> tier(new AnnounceTier(urls));
|
||||||
tiers_.push_back(tier);
|
tiers_.push_back(tier);
|
||||||
}
|
}
|
||||||
|
@ -207,15 +206,13 @@ void AnnounceList::setCurrentTier
|
||||||
}
|
}
|
||||||
|
|
||||||
void AnnounceList::moveToStoppedAllowedTier() {
|
void AnnounceList::moveToStoppedAllowedTier() {
|
||||||
std::deque<std::shared_ptr<AnnounceTier> >::iterator itr =
|
auto itr = find_wrap_if(tiers_.begin(), tiers_.end(), currentTier_,
|
||||||
find_wrap_if(tiers_.begin(), tiers_.end(),
|
FindStoppedAllowedTier());
|
||||||
currentTier_,
|
|
||||||
FindStoppedAllowedTier());
|
|
||||||
setCurrentTier(itr);
|
setCurrentTier(itr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AnnounceList::moveToCompletedAllowedTier() {
|
void AnnounceList::moveToCompletedAllowedTier() {
|
||||||
std::deque<std::shared_ptr<AnnounceTier> >::iterator itr =
|
auto itr =
|
||||||
find_wrap_if(tiers_.begin(), tiers_.end(),
|
find_wrap_if(tiers_.begin(), tiers_.end(),
|
||||||
currentTier_,
|
currentTier_,
|
||||||
FindCompletedAllowedTier());
|
FindCompletedAllowedTier());
|
||||||
|
@ -223,9 +220,8 @@ void AnnounceList::moveToCompletedAllowedTier() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AnnounceList::shuffle() {
|
void AnnounceList::shuffle() {
|
||||||
for(std::deque<std::shared_ptr<AnnounceTier> >::const_iterator itr =
|
for (const auto& tier: tiers_) {
|
||||||
tiers_.begin(), eoi = tiers_.end(); itr != eoi; ++itr) {
|
auto& urls = tier->urls;
|
||||||
std::deque<std::string>& urls = (*itr)->urls;
|
|
||||||
std::random_shuffle(urls.begin(), urls.end(),
|
std::random_shuffle(urls.begin(), urls.end(),
|
||||||
*SimpleRandomizer::getInstance());
|
*SimpleRandomizer::getInstance());
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,7 +105,7 @@ namespace {
|
||||||
CFStringRef cerr = SecCopyErrorMessageString(err, nullptr);
|
CFStringRef cerr = SecCopyErrorMessageString(err, nullptr);
|
||||||
if (cerr) {
|
if (cerr) {
|
||||||
size_t len = CFStringGetLength(cerr) * 4;
|
size_t len = CFStringGetLength(cerr) * 4;
|
||||||
char *buf = new char[len];
|
auto buf = new char[len];
|
||||||
if (CFStringGetCString(cerr, buf, len, kCFStringEncodingUTF8)) {
|
if (CFStringGetCString(cerr, buf, len, kCFStringEncodingUTF8)) {
|
||||||
rv = buf;
|
rv = buf;
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,12 +151,11 @@ ares_addr_node* parseAsyncDNSServers(const std::string& serversOpt)
|
||||||
root.next = nullptr;
|
root.next = nullptr;
|
||||||
ares_addr_node* tail = &root;
|
ares_addr_node* tail = &root;
|
||||||
ares_addr_node* node = nullptr;
|
ares_addr_node* node = nullptr;
|
||||||
for(std::vector<std::string>::const_iterator i = servers.begin(),
|
for (const auto& s: servers) {
|
||||||
eoi = servers.end(); i != eoi; ++i) {
|
|
||||||
if(node == nullptr) {
|
if(node == nullptr) {
|
||||||
node = new ares_addr_node();
|
node = new ares_addr_node();
|
||||||
}
|
}
|
||||||
size_t len = net::getBinAddr(&node->addr, (*i).c_str());
|
size_t len = net::getBinAddr(&node->addr, s.c_str());
|
||||||
if(len != 0) {
|
if(len != 0) {
|
||||||
node->next = nullptr;
|
node->next = nullptr;
|
||||||
node->family = (len == 4 ? AF_INET : AF_INET6);
|
node->family = (len == 4 ? AF_INET : AF_INET6);
|
||||||
|
|
|
@ -108,7 +108,7 @@ unsigned char* BtBitfieldMessage::createMessage() {
|
||||||
* total: 5+bitfieldLength bytes
|
* total: 5+bitfieldLength bytes
|
||||||
*/
|
*/
|
||||||
const size_t msgLength = 5+bitfieldLength_;
|
const size_t msgLength = 5+bitfieldLength_;
|
||||||
unsigned char* msg = new unsigned char[msgLength];
|
auto msg = new unsigned char[msgLength];
|
||||||
bittorrent::createPeerMessageString(msg, msgLength, 1+bitfieldLength_, ID);
|
bittorrent::createPeerMessageString(msg, msgLength, 1+bitfieldLength_, ID);
|
||||||
memcpy(msg+5, bitfield_, bitfieldLength_);
|
memcpy(msg+5, bitfield_, bitfieldLength_);
|
||||||
return msg;
|
return msg;
|
||||||
|
|
|
@ -128,28 +128,23 @@ bool BtDependency::resolve()
|
||||||
} else {
|
} else {
|
||||||
std::vector<std::shared_ptr<FileEntry> > destFiles;
|
std::vector<std::shared_ptr<FileEntry> > destFiles;
|
||||||
destFiles.reserve(fileEntries.size());
|
destFiles.reserve(fileEntries.size());
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for(auto & e : fileEntries) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end(); i != eoi; ++i) {
|
e->setRequested(false);
|
||||||
(*i)->setRequested(false);
|
destFiles.push_back(e);
|
||||||
destFiles.push_back(*i);
|
|
||||||
}
|
}
|
||||||
std::sort(destFiles.begin(), destFiles.end(), EntryCmp());
|
std::sort(destFiles.begin(), destFiles.end(), EntryCmp());
|
||||||
// Copy file path in dependant_'s FileEntries to newly created
|
// Copy file path in dependant_'s FileEntries to newly created
|
||||||
// context's FileEntries to endorse the path structure of
|
// context's FileEntries to endorse the path structure of
|
||||||
// dependant_. URIs and singleHostMultiConnection are also copied.
|
// dependant_. URIs and singleHostMultiConnection are also copied.
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator s =
|
for(const auto& e: dependantFileEntries){
|
||||||
dependantFileEntries.begin(), eoi = dependantFileEntries.end();
|
const auto d = std::lower_bound(destFiles.begin(), destFiles.end(), e,
|
||||||
s != eoi; ++s){
|
EntryCmp());
|
||||||
std::vector<std::shared_ptr<FileEntry> >::const_iterator d =
|
|
||||||
std::lower_bound(destFiles.begin(), destFiles.end(), *s,
|
|
||||||
EntryCmp());
|
|
||||||
if(d == destFiles.end() ||
|
if(d == destFiles.end() ||
|
||||||
(*d)->getOriginalName() != (*s)->getOriginalName()) {
|
(*d)->getOriginalName() != e->getOriginalName()) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(fmt("No entry %s in torrent file",
|
(fmt("No entry %s in torrent file", e->getOriginalName().c_str()));
|
||||||
(*s)->getOriginalName().c_str()));
|
|
||||||
} else {
|
} else {
|
||||||
copyValues(*d, *s);
|
copyValues(*d, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@ unsigned char* BtExtendedMessage::createMessage()
|
||||||
*/
|
*/
|
||||||
std::string payload = extensionMessage_->getPayload();
|
std::string payload = extensionMessage_->getPayload();
|
||||||
msgLength_ = 6+payload.size();
|
msgLength_ = 6+payload.size();
|
||||||
unsigned char* msg = new unsigned char[msgLength_];
|
auto msg = new unsigned char[msgLength_];
|
||||||
bittorrent::createPeerMessageString(msg, msgLength_, 2+payload.size(), ID);
|
bittorrent::createPeerMessageString(msg, msgLength_, 2+payload.size(), ID);
|
||||||
*(msg+5) = extensionMessage_->getExtensionMessageID();
|
*(msg+5) = extensionMessage_->getExtensionMessageID();
|
||||||
memcpy(msg+6, payload.data(), payload.size());
|
memcpy(msg+6, payload.data(), payload.size());
|
||||||
|
|
|
@ -89,7 +89,7 @@ BtHandshakeMessage::create(const unsigned char* data, size_t dataLength)
|
||||||
|
|
||||||
unsigned char* BtHandshakeMessage::createMessage()
|
unsigned char* BtHandshakeMessage::createMessage()
|
||||||
{
|
{
|
||||||
unsigned char* msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
msg[0] = pstrlen_;
|
msg[0] = pstrlen_;
|
||||||
memcpy(msg+1, pstr_, PSTR_LENGTH);
|
memcpy(msg+1, pstr_, PSTR_LENGTH);
|
||||||
memcpy(msg+20, reserved_, RESERVED_LENGTH);
|
memcpy(msg+20, reserved_, RESERVED_LENGTH);
|
||||||
|
|
|
@ -45,7 +45,7 @@ unsigned char* BtKeepAliveMessage::createMessage()
|
||||||
* len --- 0, 4bytes
|
* len --- 0, 4bytes
|
||||||
* total: 4bytes
|
* total: 4bytes
|
||||||
*/
|
*/
|
||||||
unsigned char* msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
memset(msg, 0, MESSAGE_LENGTH);
|
memset(msg, 0, MESSAGE_LENGTH);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,9 +146,8 @@ void BtLeecherStateChoke::plannedOptimisticUnchoke
|
||||||
std::for_each(peerEntries.begin(), peerEntries.end(),
|
std::for_each(peerEntries.begin(), peerEntries.end(),
|
||||||
std::mem_fn(&PeerEntry::disableOptUnchoking));
|
std::mem_fn(&PeerEntry::disableOptUnchoking));
|
||||||
|
|
||||||
std::vector<PeerEntry>::iterator i =
|
auto i = std::partition(peerEntries.begin(), peerEntries.end(),
|
||||||
std::partition(peerEntries.begin(), peerEntries.end(),
|
PeerFilter(true, true));
|
||||||
PeerFilter(true, true));
|
|
||||||
if(i != peerEntries.begin()) {
|
if(i != peerEntries.begin()) {
|
||||||
std::random_shuffle(peerEntries.begin(), i,
|
std::random_shuffle(peerEntries.begin(), i,
|
||||||
*SimpleRandomizer::getInstance());
|
*SimpleRandomizer::getInstance());
|
||||||
|
@ -160,9 +159,8 @@ void BtLeecherStateChoke::plannedOptimisticUnchoke
|
||||||
|
|
||||||
void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
|
void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
|
||||||
{
|
{
|
||||||
std::vector<PeerEntry>::iterator rest =
|
auto rest = std::partition(peerEntries.begin(), peerEntries.end(),
|
||||||
std::partition(peerEntries.begin(), peerEntries.end(),
|
std::mem_fn(&PeerEntry::isRegularUnchoker));
|
||||||
std::mem_fn(&PeerEntry::isRegularUnchoker));
|
|
||||||
|
|
||||||
std::sort(peerEntries.begin(), rest);
|
std::sort(peerEntries.begin(), rest);
|
||||||
|
|
||||||
|
@ -170,29 +168,28 @@ void BtLeecherStateChoke::regularUnchoke(std::vector<PeerEntry>& peerEntries)
|
||||||
int count = 3;
|
int count = 3;
|
||||||
|
|
||||||
bool fastOptUnchoker = false;
|
bool fastOptUnchoker = false;
|
||||||
std::vector<PeerEntry>::iterator peerIter = peerEntries.begin();
|
auto peerIter = peerEntries.begin();
|
||||||
for(;peerIter != rest && count; ++peerIter, --count) {
|
for(;peerIter != rest && count; ++peerIter, --count) {
|
||||||
(*peerIter).disableChokingRequired();
|
peerIter->disableChokingRequired();
|
||||||
A2_LOG_INFO(fmt("RU: %s, dlspd=%d",
|
A2_LOG_INFO(fmt("RU: %s, dlspd=%d",
|
||||||
(*peerIter).getPeer()->getIPAddress().c_str(),
|
(*peerIter).getPeer()->getIPAddress().c_str(),
|
||||||
(*peerIter).getDownloadSpeed()));
|
(*peerIter).getDownloadSpeed()));
|
||||||
if((*peerIter).getPeer()->optUnchoking()) {
|
if(peerIter->getPeer()->optUnchoking()) {
|
||||||
fastOptUnchoker = true;
|
fastOptUnchoker = true;
|
||||||
(*peerIter).disableOptUnchoking();
|
peerIter->disableOptUnchoking();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(fastOptUnchoker) {
|
if(fastOptUnchoker) {
|
||||||
std::random_shuffle(peerIter, peerEntries.end(),
|
std::random_shuffle(peerIter, peerEntries.end(),
|
||||||
*SimpleRandomizer::getInstance());
|
*SimpleRandomizer::getInstance());
|
||||||
for(std::vector<PeerEntry>::iterator i = peerIter,
|
for (auto& p : peerEntries) {
|
||||||
eoi = peerEntries.end(); i != eoi; ++i) {
|
if(p.getPeer()->peerInterested()) {
|
||||||
if((*i).getPeer()->peerInterested()) {
|
p.enableOptUnchoking();
|
||||||
(*i).enableOptUnchoking();
|
A2_LOG_INFO(fmt("OU: %s", p.getPeer()->getIPAddress().c_str()));
|
||||||
A2_LOG_INFO(fmt("OU: %s", (*i).getPeer()->getIPAddress().c_str()));
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
(*i).disableChokingRequired();
|
p.disableChokingRequired();
|
||||||
A2_LOG_INFO(fmt("OU: %s", (*i).getPeer()->getIPAddress().c_str()));
|
A2_LOG_INFO(fmt("OU: %s", p.getPeer()->getIPAddress().c_str()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,7 +125,7 @@ void BtPieceMessage::doReceivedAction()
|
||||||
if(piece->getWrDiskCacheEntry()) {
|
if(piece->getWrDiskCacheEntry()) {
|
||||||
// Write Disk Cache enabled. Unfortunately, it incurs extra data
|
// Write Disk Cache enabled. Unfortunately, it incurs extra data
|
||||||
// copy.
|
// copy.
|
||||||
unsigned char* dataCopy = new unsigned char[blockLength_];
|
auto dataCopy = new unsigned char[blockLength_];
|
||||||
memcpy(dataCopy, data_+9, blockLength_);
|
memcpy(dataCopy, data_+9, blockLength_);
|
||||||
piece->updateWrCache(getPieceStorage()->getWrDiskCache(),
|
piece->updateWrCache(getPieceStorage()->getWrDiskCache(),
|
||||||
dataCopy, 0, blockLength_, blockLength_, offset);
|
dataCopy, 0, blockLength_, blockLength_, offset);
|
||||||
|
|
|
@ -106,7 +106,7 @@ unsigned char* BtPortMessage::createMessage()
|
||||||
* port --- port number, 2bytes
|
* port --- port number, 2bytes
|
||||||
* total: 7bytes
|
* total: 7bytes
|
||||||
*/
|
*/
|
||||||
unsigned char* msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 3, ID);
|
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 3, ID);
|
||||||
bittorrent::setShortIntParam(&msg[5], port_);
|
bittorrent::setShortIntParam(&msg[5], port_);
|
||||||
return msg;
|
return msg;
|
||||||
|
|
|
@ -124,8 +124,8 @@ void BtSeederStateChoke::unchoke
|
||||||
|
|
||||||
std::sort(peers.begin(), peers.end());
|
std::sort(peers.begin(), peers.end());
|
||||||
|
|
||||||
std::vector<PeerEntry>::iterator r = peers.begin();
|
auto r = peers.begin();
|
||||||
for(std::vector<PeerEntry>::iterator eoi = peers.end();
|
for(auto eoi = peers.end();
|
||||||
r != eoi && count; ++r, --count) {
|
r != eoi && count; ++r, --count) {
|
||||||
(*r).getPeer()->chokingRequired(false);
|
(*r).getPeer()->chokingRequired(false);
|
||||||
A2_LOG_INFO(fmt("RU: %s, ulspd=%d",
|
A2_LOG_INFO(fmt("RU: %s, ulspd=%d",
|
||||||
|
|
|
@ -229,10 +229,9 @@ void BtSetup::setup(std::vector<std::unique_ptr<Command>>& commands,
|
||||||
} else {
|
} else {
|
||||||
std::vector<std::pair<sockaddr_union, socklen_t> > ifAddrs;
|
std::vector<std::pair<sockaddr_union, socklen_t> > ifAddrs;
|
||||||
getInterfaceAddress(ifAddrs, lpdInterface, AF_INET, AI_NUMERICHOST);
|
getInterfaceAddress(ifAddrs, lpdInterface, AF_INET, AI_NUMERICHOST);
|
||||||
for(std::vector<std::pair<sockaddr_union, socklen_t> >::const_iterator
|
for (const auto& i : ifAddrs) {
|
||||||
i = ifAddrs.begin(), eoi = ifAddrs.end(); i != eoi; ++i) {
|
|
||||||
char host[NI_MAXHOST];
|
char host[NI_MAXHOST];
|
||||||
if(inetNtop(AF_INET, &(*i).first.in.sin_addr, host,
|
if(inetNtop(AF_INET, &i.first.in.sin_addr, host,
|
||||||
sizeof(host)) == 0 &&
|
sizeof(host)) == 0 &&
|
||||||
receiver->init(host)) {
|
receiver->init(host)) {
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
|
|
@ -142,7 +142,7 @@ void printProgressCompact(std::ostream& o, const DownloadEngine* e,
|
||||||
e->getRequestGroupMan()->getRequestGroups();
|
e->getRequestGroupMan()->getRequestGroups();
|
||||||
size_t cnt = 0;
|
size_t cnt = 0;
|
||||||
const size_t MAX_ITEM = 5;
|
const size_t MAX_ITEM = 5;
|
||||||
for(RequestGroupList::const_iterator i = groups.begin(),
|
for(auto i = groups.begin(),
|
||||||
eoi = groups.end(); i != eoi && cnt < MAX_ITEM; ++i, ++cnt) {
|
eoi = groups.end(); i != eoi && cnt < MAX_ITEM; ++i, ++cnt) {
|
||||||
const std::shared_ptr<RequestGroup>& rg = *i;
|
const std::shared_ptr<RequestGroup>& rg = *i;
|
||||||
TransferStat stat = rg->calculateStat();
|
TransferStat stat = rg->calculateStat();
|
||||||
|
|
|
@ -115,20 +115,19 @@ void showFiles
|
||||||
(const std::vector<std::string>& uris, const std::shared_ptr<Option>& op)
|
(const std::vector<std::string>& uris, const std::shared_ptr<Option>& op)
|
||||||
{
|
{
|
||||||
ProtocolDetector dt;
|
ProtocolDetector dt;
|
||||||
for(std::vector<std::string>::const_iterator i = uris.begin(),
|
for(const auto & uri : uris) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
|
||||||
printf(">>> ");
|
printf(">>> ");
|
||||||
printf(MSG_SHOW_FILES, (*i).c_str());
|
printf(MSG_SHOW_FILES, (uri).c_str());
|
||||||
printf("\n");
|
printf("\n");
|
||||||
try {
|
try {
|
||||||
#ifdef ENABLE_BITTORRENT
|
#ifdef ENABLE_BITTORRENT
|
||||||
if(dt.guessTorrentFile(*i)) {
|
if(dt.guessTorrentFile(uri)) {
|
||||||
showTorrentFile(*i);
|
showTorrentFile(uri);
|
||||||
} else
|
} else
|
||||||
#endif // ENABLE_BITTORRENT
|
#endif // ENABLE_BITTORRENT
|
||||||
#ifdef ENABLE_METALINK
|
#ifdef ENABLE_METALINK
|
||||||
if(dt.guessMetalinkFile(*i)) {
|
if(dt.guessMetalinkFile(uri)) {
|
||||||
showMetalinkFile(*i, op);
|
showMetalinkFile(uri, op);
|
||||||
} else
|
} else
|
||||||
#endif // ENABLE_METALINK
|
#endif // ENABLE_METALINK
|
||||||
{
|
{
|
||||||
|
@ -235,7 +234,7 @@ Context::Context(bool standalone,
|
||||||
// command-line. If they are left, because op is used as a template
|
// command-line. If they are left, because op is used as a template
|
||||||
// for new RequestGroup(such as created in RPC command), they causes
|
// for new RequestGroup(such as created in RPC command), they causes
|
||||||
// unintentional effect.
|
// unintentional effect.
|
||||||
for(std::shared_ptr<Option> i = op; i; i = i->getParent()) {
|
for(auto i = op; i; i = i->getParent()) {
|
||||||
i->remove(PREF_OUT);
|
i->remove(PREF_OUT);
|
||||||
i->remove(PREF_FORCE_SEQUENTIAL);
|
i->remove(PREF_FORCE_SEQUENTIAL);
|
||||||
i->remove(PREF_INPUT_FILE);
|
i->remove(PREF_INPUT_FILE);
|
||||||
|
|
|
@ -434,18 +434,17 @@ std::vector<const Cookie*> CookieStorage::criteriaFind
|
||||||
}
|
}
|
||||||
auto labels = splitDomainLabel(requestHost);
|
auto labels = splitDomainLabel(requestHost);
|
||||||
auto node = rootNode_.get();
|
auto node = rootNode_.get();
|
||||||
for(auto i = labels.rbegin(), eoi = labels.rend(); i != eoi; ++i) {
|
for (auto i = labels.rbegin(), eoi = labels.rend(); i != eoi; ++i) {
|
||||||
auto nextNode = node->findNext(*i);
|
auto nextNode = node->findNext(*i);
|
||||||
if(nextNode) {
|
if(!nextNode) {
|
||||||
nextNode->setLastAccessTime(now);
|
|
||||||
if(nextNode->getInLru()) {
|
|
||||||
updateLru(nextNode, now);
|
|
||||||
}
|
|
||||||
nextNode->findCookie(res, requestHost, requestPath, now, secure);
|
|
||||||
node = nextNode;
|
|
||||||
} else {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
nextNode->setLastAccessTime(now);
|
||||||
|
if(nextNode->getInLru()) {
|
||||||
|
updateLru(nextNode, now);
|
||||||
|
}
|
||||||
|
nextNode->findCookie(res, requestHost, requestPath, now, secure);
|
||||||
|
node = nextNode;
|
||||||
}
|
}
|
||||||
auto divs = std::vector<CookiePathDivider>{};
|
auto divs = std::vector<CookiePathDivider>{};
|
||||||
std::transform(std::begin(res), std::end(res), std::back_inserter(divs),
|
std::transform(std::begin(res), std::end(res), std::back_inserter(divs),
|
||||||
|
|
|
@ -97,11 +97,9 @@ void DHTAutoSaveCommand::save()
|
||||||
std::vector<std::shared_ptr<DHTNode> > nodes;
|
std::vector<std::shared_ptr<DHTNode> > nodes;
|
||||||
std::vector<std::shared_ptr<DHTBucket> > buckets;
|
std::vector<std::shared_ptr<DHTBucket> > buckets;
|
||||||
routingTable_->getBuckets(buckets);
|
routingTable_->getBuckets(buckets);
|
||||||
for(std::vector<std::shared_ptr<DHTBucket> >::const_iterator i = buckets.begin(),
|
for (const auto& b : buckets) {
|
||||||
eoi = buckets.end(); i != eoi; ++i) {
|
|
||||||
const std::shared_ptr<DHTBucket>& bucket = *i;
|
|
||||||
std::vector<std::shared_ptr<DHTNode> > goodNodes;
|
std::vector<std::shared_ptr<DHTNode> > goodNodes;
|
||||||
bucket->getGoodNodes(goodNodes);
|
b->getGoodNodes(goodNodes);
|
||||||
nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
|
nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ bool DHTBucket::isInRange(const unsigned char* nodeID,
|
||||||
bool DHTBucket::addNode(const std::shared_ptr<DHTNode>& node)
|
bool DHTBucket::addNode(const std::shared_ptr<DHTNode>& node)
|
||||||
{
|
{
|
||||||
notifyUpdate();
|
notifyUpdate();
|
||||||
std::deque<std::shared_ptr<DHTNode> >::iterator itr =
|
auto itr =
|
||||||
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
||||||
if(itr == nodes_.end()) {
|
if(itr == nodes_.end()) {
|
||||||
if(nodes_.size() < K) {
|
if(nodes_.size() < K) {
|
||||||
|
@ -144,7 +144,7 @@ void DHTBucket::cacheNode(const std::shared_ptr<DHTNode>& node)
|
||||||
void DHTBucket::dropNode(const std::shared_ptr<DHTNode>& node)
|
void DHTBucket::dropNode(const std::shared_ptr<DHTNode>& node)
|
||||||
{
|
{
|
||||||
if(!cachedNodes_.empty()) {
|
if(!cachedNodes_.empty()) {
|
||||||
std::deque<std::shared_ptr<DHTNode> >::iterator itr =
|
auto itr =
|
||||||
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
||||||
if(itr != nodes_.end()) {
|
if(itr != nodes_.end()) {
|
||||||
nodes_.erase(itr);
|
nodes_.erase(itr);
|
||||||
|
@ -156,7 +156,7 @@ void DHTBucket::dropNode(const std::shared_ptr<DHTNode>& node)
|
||||||
|
|
||||||
void DHTBucket::moveToHead(const std::shared_ptr<DHTNode>& node)
|
void DHTBucket::moveToHead(const std::shared_ptr<DHTNode>& node)
|
||||||
{
|
{
|
||||||
std::deque<std::shared_ptr<DHTNode> >::iterator itr =
|
auto itr =
|
||||||
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
||||||
if(itr != nodes_.end()) {
|
if(itr != nodes_.end()) {
|
||||||
nodes_.erase(itr);
|
nodes_.erase(itr);
|
||||||
|
@ -166,7 +166,7 @@ void DHTBucket::moveToHead(const std::shared_ptr<DHTNode>& node)
|
||||||
|
|
||||||
void DHTBucket::moveToTail(const std::shared_ptr<DHTNode>& node)
|
void DHTBucket::moveToTail(const std::shared_ptr<DHTNode>& node)
|
||||||
{
|
{
|
||||||
std::deque<std::shared_ptr<DHTNode> >::iterator itr =
|
auto itr =
|
||||||
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
||||||
if(itr != nodes_.end()) {
|
if(itr != nodes_.end()) {
|
||||||
nodes_.erase(itr);
|
nodes_.erase(itr);
|
||||||
|
@ -196,12 +196,11 @@ std::shared_ptr<DHTBucket> DHTBucket::split()
|
||||||
rMax, rMin, localNode_));
|
rMax, rMin, localNode_));
|
||||||
|
|
||||||
std::deque<std::shared_ptr<DHTNode> > lNodes;
|
std::deque<std::shared_ptr<DHTNode> > lNodes;
|
||||||
for(std::deque<std::shared_ptr<DHTNode> >::iterator i = nodes_.begin(),
|
for(auto & elem : nodes_) {
|
||||||
eoi = nodes_.end(); i != eoi; ++i) {
|
if(rBucket->isInRange(elem)) {
|
||||||
if(rBucket->isInRange(*i)) {
|
assert(rBucket->addNode(elem));
|
||||||
assert(rBucket->addNode(*i));
|
|
||||||
} else {
|
} else {
|
||||||
lNodes.push_back(*i);
|
lNodes.push_back(elem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nodes_ = lNodes;
|
nodes_ = lNodes;
|
||||||
|
@ -231,7 +230,7 @@ std::shared_ptr<DHTNode> DHTBucket::getNode(const unsigned char* nodeID, const s
|
||||||
std::shared_ptr<DHTNode> node(new DHTNode(nodeID));
|
std::shared_ptr<DHTNode> node(new DHTNode(nodeID));
|
||||||
node->setIPAddress(ipaddr);
|
node->setIPAddress(ipaddr);
|
||||||
node->setPort(port);
|
node->setPort(port);
|
||||||
std::deque<std::shared_ptr<DHTNode> >::const_iterator itr =
|
auto itr =
|
||||||
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
|
||||||
if(itr == nodes_.end() ||
|
if(itr == nodes_.end() ||
|
||||||
(*itr)->getIPAddress() != ipaddr || (*itr)->getPort() != port) {
|
(*itr)->getIPAddress() != ipaddr || (*itr)->getPort() != port) {
|
||||||
|
@ -275,7 +274,7 @@ bool DHTBucket::containsQuestionableNode() const
|
||||||
|
|
||||||
std::shared_ptr<DHTNode> DHTBucket::getLRUQuestionableNode() const
|
std::shared_ptr<DHTNode> DHTBucket::getLRUQuestionableNode() const
|
||||||
{
|
{
|
||||||
std::deque<std::shared_ptr<DHTNode> >::const_iterator i =
|
auto i =
|
||||||
std::find_if(nodes_.begin(), nodes_.end(), FindQuestionableNode());
|
std::find_if(nodes_.begin(), nodes_.end(), FindQuestionableNode());
|
||||||
if(i == nodes_.end()) {
|
if(i == nodes_.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
@ -56,23 +56,23 @@ void DHTBucketRefreshTask::startup()
|
||||||
{
|
{
|
||||||
std::vector<std::shared_ptr<DHTBucket> > buckets;
|
std::vector<std::shared_ptr<DHTBucket> > buckets;
|
||||||
getRoutingTable()->getBuckets(buckets);
|
getRoutingTable()->getBuckets(buckets);
|
||||||
for(std::vector<std::shared_ptr<DHTBucket> >::iterator i = buckets.begin(),
|
for (auto& b : buckets) {
|
||||||
eoi = buckets.end(); i != eoi; ++i) {
|
if(!forceRefresh_ && ! b->needsRefresh()) {
|
||||||
if(forceRefresh_ || (*i)->needsRefresh()) {
|
continue;
|
||||||
(*i)->notifyUpdate();
|
|
||||||
unsigned char targetID[DHT_ID_LENGTH];
|
|
||||||
(*i)->getRandomNodeID(targetID);
|
|
||||||
std::shared_ptr<DHTNodeLookupTask> task(new DHTNodeLookupTask(targetID));
|
|
||||||
task->setRoutingTable(getRoutingTable());
|
|
||||||
task->setMessageDispatcher(getMessageDispatcher());
|
|
||||||
task->setMessageFactory(getMessageFactory());
|
|
||||||
task->setTaskQueue(getTaskQueue());
|
|
||||||
task->setLocalNode(getLocalNode());
|
|
||||||
|
|
||||||
A2_LOG_INFO(fmt("Dispating bucket refresh. targetID=%s",
|
|
||||||
util::toHex(targetID, DHT_ID_LENGTH).c_str()));
|
|
||||||
getTaskQueue()->addPeriodicTask1(task);
|
|
||||||
}
|
}
|
||||||
|
b->notifyUpdate();
|
||||||
|
unsigned char targetID[DHT_ID_LENGTH];
|
||||||
|
b->getRandomNodeID(targetID);
|
||||||
|
std::shared_ptr<DHTNodeLookupTask> task(new DHTNodeLookupTask(targetID));
|
||||||
|
task->setRoutingTable(getRoutingTable());
|
||||||
|
task->setMessageDispatcher(getMessageDispatcher());
|
||||||
|
task->setMessageFactory(getMessageFactory());
|
||||||
|
task->setTaskQueue(getTaskQueue());
|
||||||
|
task->setLocalNode(getLocalNode());
|
||||||
|
|
||||||
|
A2_LOG_INFO(fmt("Dispating bucket refresh. targetID=%s",
|
||||||
|
util::toHex(targetID, DHT_ID_LENGTH).c_str()));
|
||||||
|
getTaskQueue()->addPeriodicTask1(task);
|
||||||
}
|
}
|
||||||
setFinished(true);
|
setFinished(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,9 +63,8 @@ bool DHTConnectionImpl::bind
|
||||||
}
|
}
|
||||||
std::random_shuffle(ports.begin(), ports.end(),
|
std::random_shuffle(ports.begin(), ports.end(),
|
||||||
*SimpleRandomizer::getInstance());
|
*SimpleRandomizer::getInstance());
|
||||||
for(std::vector<uint16_t>::const_iterator i = ports.begin(),
|
for (const auto& p : ports) {
|
||||||
eoi = ports.end(); i != eoi; ++i) {
|
port = p;
|
||||||
port = *i;
|
|
||||||
if(bind(port, addr)) {
|
if(bind(port, addr)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -428,8 +428,8 @@ DHTMessageFactoryImpl::createGetPeersReplyMessage
|
||||||
std::vector<std::shared_ptr<Peer>> peers;
|
std::vector<std::shared_ptr<Peer>> peers;
|
||||||
size_t clen = bittorrent::getCompactLength(family_);
|
size_t clen = bittorrent::getCompactLength(family_);
|
||||||
if(valuesList) {
|
if(valuesList) {
|
||||||
for(auto i = valuesList->begin(), eoi = valuesList->end(); i != eoi; ++i) {
|
for(auto & elem : *valuesList) {
|
||||||
const String* data = downcast<String>(*i);
|
const String* data = downcast<String>(elem);
|
||||||
if(data && data->s().size() == clen) {
|
if(data && data->s().size() == clen) {
|
||||||
auto addr = bittorrent::unpackcompact(data->uc(), family_);
|
auto addr = bittorrent::unpackcompact(data->uc(), family_);
|
||||||
if(addr.first.empty()) {
|
if(addr.first.empty()) {
|
||||||
|
|
|
@ -51,7 +51,7 @@ DHTPeerAnnounceEntry::~DHTPeerAnnounceEntry() {}
|
||||||
|
|
||||||
void DHTPeerAnnounceEntry::addPeerAddrEntry(const PeerAddrEntry& entry)
|
void DHTPeerAnnounceEntry::addPeerAddrEntry(const PeerAddrEntry& entry)
|
||||||
{
|
{
|
||||||
std::vector<PeerAddrEntry>::iterator i =
|
auto i =
|
||||||
std::find(peerAddrEntries_.begin(), peerAddrEntries_.end(), entry);
|
std::find(peerAddrEntries_.begin(), peerAddrEntries_.end(), entry);
|
||||||
if(i == peerAddrEntries_.end()) {
|
if(i == peerAddrEntries_.end()) {
|
||||||
peerAddrEntries_.push_back(entry);
|
peerAddrEntries_.push_back(entry);
|
||||||
|
@ -98,9 +98,8 @@ bool DHTPeerAnnounceEntry::empty() const
|
||||||
void DHTPeerAnnounceEntry::getPeers
|
void DHTPeerAnnounceEntry::getPeers
|
||||||
(std::vector<std::shared_ptr<Peer> >& peers) const
|
(std::vector<std::shared_ptr<Peer> >& peers) const
|
||||||
{
|
{
|
||||||
for(std::vector<PeerAddrEntry>::const_iterator i = peerAddrEntries_.begin(),
|
for (const auto& p: peerAddrEntries_) {
|
||||||
eoi = peerAddrEntries_.end(); i != eoi; ++i) {
|
std::shared_ptr<Peer> peer(new Peer(p.getIPAddress(), p.getPort()));
|
||||||
std::shared_ptr<Peer> peer(new Peer((*i).getIPAddress(), (*i).getPort()));
|
|
||||||
peers.push_back(peer);
|
peers.push_back(peer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,18 +139,15 @@ void DHTPeerAnnounceStorage::handleTimeout()
|
||||||
void DHTPeerAnnounceStorage::announcePeer()
|
void DHTPeerAnnounceStorage::announcePeer()
|
||||||
{
|
{
|
||||||
A2_LOG_DEBUG("Now announcing peer.");
|
A2_LOG_DEBUG("Now announcing peer.");
|
||||||
for(DHTPeerAnnounceEntrySet::iterator i =
|
for (auto& e: entries_) {
|
||||||
entries_.begin(), eoi = entries_.end(); i != eoi; ++i) {
|
if(e->getLastUpdated().difference(global::wallclock()) < DHT_PEER_ANNOUNCE_INTERVAL) {
|
||||||
if((*i)->getLastUpdated().
|
continue;
|
||||||
difference(global::wallclock()) >= DHT_PEER_ANNOUNCE_INTERVAL) {
|
|
||||||
(*i)->notifyUpdate();
|
|
||||||
std::shared_ptr<DHTTask> task =
|
|
||||||
taskFactory_->createPeerAnnounceTask((*i)->getInfoHash());
|
|
||||||
taskQueue_->addPeriodicTask2(task);
|
|
||||||
A2_LOG_DEBUG
|
|
||||||
(fmt("Added 1 peer announce: infoHash=%s",
|
|
||||||
util::toHex((*i)->getInfoHash(), DHT_ID_LENGTH).c_str()));
|
|
||||||
}
|
}
|
||||||
|
e->notifyUpdate();
|
||||||
|
auto task = taskFactory_->createPeerAnnounceTask(e->getInfoHash());
|
||||||
|
taskQueue_->addPeriodicTask2(task);
|
||||||
|
A2_LOG_DEBUG(fmt("Added 1 peer announce: infoHash=%s",
|
||||||
|
util::toHex(e->getInfoHash(), DHT_ID_LENGTH).c_str()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,8 +91,8 @@ bool DHTTokenTracker::validateToken(const std::string& token,
|
||||||
const unsigned char* infoHash,
|
const unsigned char* infoHash,
|
||||||
const std::string& ipaddr, uint16_t port) const
|
const std::string& ipaddr, uint16_t port) const
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 2; ++i) {
|
for(auto & elem : secret_) {
|
||||||
if(generateToken(infoHash, ipaddr, port, secret_[i]) == token) {
|
if(generateToken(infoHash, ipaddr, port, elem) == token) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,8 +91,7 @@ bool DNSCache::CacheEntry::add(const std::string& addr)
|
||||||
std::vector<DNSCache::AddrEntry>::iterator DNSCache::CacheEntry::find
|
std::vector<DNSCache::AddrEntry>::iterator DNSCache::CacheEntry::find
|
||||||
(const std::string& addr)
|
(const std::string& addr)
|
||||||
{
|
{
|
||||||
for(std::vector<AddrEntry>::iterator i = addrEntries_.begin(),
|
for(auto i = addrEntries_.begin(), eoi = addrEntries_.end(); i != eoi; ++i) {
|
||||||
eoi = addrEntries_.end(); i != eoi; ++i) {
|
|
||||||
if((*i).addr_ == addr) {
|
if((*i).addr_ == addr) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -103,8 +102,7 @@ std::vector<DNSCache::AddrEntry>::iterator DNSCache::CacheEntry::find
|
||||||
std::vector<DNSCache::AddrEntry>::const_iterator DNSCache::CacheEntry::find
|
std::vector<DNSCache::AddrEntry>::const_iterator DNSCache::CacheEntry::find
|
||||||
(const std::string& addr) const
|
(const std::string& addr) const
|
||||||
{
|
{
|
||||||
for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
|
for(auto i = addrEntries_.begin(), eoi = addrEntries_.end(); i != eoi; ++i) {
|
||||||
eoi = addrEntries_.end(); i != eoi; ++i) {
|
|
||||||
if((*i).addr_ == addr) {
|
if((*i).addr_ == addr) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -119,10 +117,9 @@ bool DNSCache::CacheEntry::contains(const std::string& addr) const
|
||||||
|
|
||||||
const std::string& DNSCache::CacheEntry::getGoodAddr() const
|
const std::string& DNSCache::CacheEntry::getGoodAddr() const
|
||||||
{
|
{
|
||||||
for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
|
for(auto & elem : addrEntries_) {
|
||||||
eoi = addrEntries_.end(); i != eoi; ++i) {
|
if(elem.good_) {
|
||||||
if((*i).good_) {
|
return (elem).addr_;
|
||||||
return (*i).addr_;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return A2STR::NIL;
|
return A2STR::NIL;
|
||||||
|
@ -130,9 +127,9 @@ const std::string& DNSCache::CacheEntry::getGoodAddr() const
|
||||||
|
|
||||||
void DNSCache::CacheEntry::markBad(const std::string& addr)
|
void DNSCache::CacheEntry::markBad(const std::string& addr)
|
||||||
{
|
{
|
||||||
std::vector<AddrEntry>::iterator i = find(addr);
|
auto i = find(addr);
|
||||||
if(i != addrEntries_.end()) {
|
if(i != addrEntries_.end()) {
|
||||||
(*i).good_ = false;
|
i->good_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,10 +83,9 @@ private:
|
||||||
template<typename OutputIterator>
|
template<typename OutputIterator>
|
||||||
void getAllGoodAddrs(OutputIterator out) const
|
void getAllGoodAddrs(OutputIterator out) const
|
||||||
{
|
{
|
||||||
for(std::vector<AddrEntry>::const_iterator i = addrEntries_.begin(),
|
for(auto & elem : addrEntries_) {
|
||||||
eoi = addrEntries_.end(); i != eoi; ++i) {
|
if(elem.good_) {
|
||||||
if((*i).good_) {
|
*out++ = elem.addr_;
|
||||||
*out++ = (*i).addr_;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -363,11 +363,8 @@ void DefaultBtAnnounce::processUDPTrackerResponse
|
||||||
incomplete_ = reply->leechers;
|
incomplete_ = reply->leechers;
|
||||||
A2_LOG_DEBUG(fmt("Incomplete:%d", reply->leechers));
|
A2_LOG_DEBUG(fmt("Incomplete:%d", reply->leechers));
|
||||||
if(!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
|
if(!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
|
||||||
for(std::vector<std::pair<std::string, uint16_t> >::iterator i =
|
for(auto & elem : reply->peers) {
|
||||||
reply->peers.begin(), eoi = reply->peers.end(); i != eoi;
|
peerStorage_->addPeer(std::make_shared<Peer>(elem.first, elem.second));
|
||||||
++i) {
|
|
||||||
peerStorage_->addPeer(std::shared_ptr<Peer>(new Peer((*i).first,
|
|
||||||
(*i).second)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,10 +150,9 @@ void DefaultBtRequestFactory::doChokedAction()
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtRequestFactory::removeAllTargetPiece() {
|
void DefaultBtRequestFactory::removeAllTargetPiece() {
|
||||||
for(std::deque<std::shared_ptr<Piece> >::iterator itr = pieces_.begin(),
|
for(auto & elem : pieces_) {
|
||||||
eoi = pieces_.end(); itr != eoi; ++itr) {
|
dispatcher_->doAbortOutstandingRequestAction(elem);
|
||||||
dispatcher_->doAbortOutstandingRequestAction(*itr);
|
pieceStorage_->cancelPiece(elem, cuid_);
|
||||||
pieceStorage_->cancelPiece(*itr, cuid_);
|
|
||||||
}
|
}
|
||||||
pieces_.clear();
|
pieces_.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,7 @@ void DefaultPeerStorage::addPeer(const std::vector<std::shared_ptr<Peer> >& peer
|
||||||
{
|
{
|
||||||
size_t added = 0;
|
size_t added = 0;
|
||||||
size_t addMax = std::min(maxPeerListSize_, MAX_PEER_LIST_UPDATE);
|
size_t addMax = std::min(maxPeerListSize_, MAX_PEER_LIST_UPDATE);
|
||||||
for(std::vector<std::shared_ptr<Peer> >::const_iterator itr = peers.begin(),
|
for(auto itr = peers.begin(),
|
||||||
eoi = peers.end(); itr != eoi && added < addMax; ++itr) {
|
eoi = peers.end(); itr != eoi && added < addMax; ++itr) {
|
||||||
const std::shared_ptr<Peer>& peer = *itr;
|
const std::shared_ptr<Peer>& peer = *itr;
|
||||||
if(isPeerAlreadyAdded(peer)) {
|
if(isPeerAlreadyAdded(peer)) {
|
||||||
|
@ -148,7 +148,7 @@ void DefaultPeerStorage::addDroppedPeer(const std::shared_ptr<Peer>& peer)
|
||||||
{
|
{
|
||||||
// Make sure that duplicated peers exist in droppedPeers_. If
|
// Make sure that duplicated peers exist in droppedPeers_. If
|
||||||
// exists, erase older one.
|
// exists, erase older one.
|
||||||
for(std::deque<std::shared_ptr<Peer> >::iterator i = droppedPeers_.begin(),
|
for(auto i = droppedPeers_.begin(),
|
||||||
eoi = droppedPeers_.end(); i != eoi; ++i) {
|
eoi = droppedPeers_.end(); i != eoi; ++i) {
|
||||||
if((*i)->getIPAddress() == peer->getIPAddress() &&
|
if((*i)->getIPAddress() == peer->getIPAddress() &&
|
||||||
(*i)->getPort() == peer->getPort()) {
|
(*i)->getPort() == peer->getPort()) {
|
||||||
|
@ -183,21 +183,23 @@ bool DefaultPeerStorage::isPeerAvailable() {
|
||||||
|
|
||||||
bool DefaultPeerStorage::isBadPeer(const std::string& ipaddr)
|
bool DefaultPeerStorage::isBadPeer(const std::string& ipaddr)
|
||||||
{
|
{
|
||||||
std::map<std::string, time_t>::iterator i = badPeers_.find(ipaddr);
|
auto i = badPeers_.find(ipaddr);
|
||||||
if(i == badPeers_.end()) {
|
if(i == badPeers_.end()) {
|
||||||
return false;
|
return false;
|
||||||
} else if(global::wallclock().getTime() >= (*i).second) {
|
}
|
||||||
|
|
||||||
|
if(global::wallclock().getTime() >= (*i).second) {
|
||||||
badPeers_.erase(i);
|
badPeers_.erase(i);
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultPeerStorage::addBadPeer(const std::string& ipaddr)
|
void DefaultPeerStorage::addBadPeer(const std::string& ipaddr)
|
||||||
{
|
{
|
||||||
if(lastBadPeerCleaned_.difference(global::wallclock()) >= 3600) {
|
if(lastBadPeerCleaned_.difference(global::wallclock()) >= 3600) {
|
||||||
for(std::map<std::string, time_t>::iterator i = badPeers_.begin(),
|
for(auto i = badPeers_.begin(),
|
||||||
eoi = badPeers_.end(); i != eoi;) {
|
eoi = badPeers_.end(); i != eoi;) {
|
||||||
if(global::wallclock().getTime() >= (*i).second) {
|
if(global::wallclock().getTime() >= (*i).second) {
|
||||||
A2_LOG_DEBUG(fmt("Purge %s from bad peer", (*i).first.c_str()));
|
A2_LOG_DEBUG(fmt("Purge %s from bad peer", (*i).first.c_str()));
|
||||||
|
|
|
@ -247,11 +247,10 @@ void unsetExcludedIndexes(BitfieldMan& bitfield,
|
||||||
void DefaultPieceStorage::createFastIndexBitfield
|
void DefaultPieceStorage::createFastIndexBitfield
|
||||||
(BitfieldMan& bitfield, const std::shared_ptr<Peer>& peer)
|
(BitfieldMan& bitfield, const std::shared_ptr<Peer>& peer)
|
||||||
{
|
{
|
||||||
for(std::set<size_t>::const_iterator itr =
|
const auto& is = peer->getPeerAllowedIndexSet();
|
||||||
peer->getPeerAllowedIndexSet().begin(),
|
for(const auto& i: is) {
|
||||||
eoi = peer->getPeerAllowedIndexSet().end(); itr != eoi; ++itr) {
|
if(!bitfieldMan_->isBitSet(i) && peer->hasPiece(i)) {
|
||||||
if(!bitfieldMan_->isBitSet(*itr) && peer->hasPiece(*itr)) {
|
bitfield.setBit(i);
|
||||||
bitfield.setBit(*itr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -567,9 +566,8 @@ int64_t DefaultPieceStorage::getFilteredCompletedLength()
|
||||||
int64_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
|
int64_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
|
||||||
{
|
{
|
||||||
int64_t len = 0;
|
int64_t len = 0;
|
||||||
for(UsedPieceSet::const_iterator i = usedPieces_.begin(),
|
for(auto & elem : usedPieces_) {
|
||||||
eoi = usedPieces_.end(); i != eoi; ++i) {
|
len += elem->getCompletedLength();
|
||||||
len += (*i)->getCompletedLength();
|
|
||||||
}
|
}
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
@ -577,10 +575,9 @@ int64_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
|
||||||
int64_t DefaultPieceStorage::getInFlightPieceFilteredCompletedLength() const
|
int64_t DefaultPieceStorage::getInFlightPieceFilteredCompletedLength() const
|
||||||
{
|
{
|
||||||
int64_t len = 0;
|
int64_t len = 0;
|
||||||
for(UsedPieceSet::const_iterator i = usedPieces_.begin(),
|
for(auto & elem : usedPieces_) {
|
||||||
eoi = usedPieces_.end(); i != eoi; ++i) {
|
if(bitfieldMan_->isFilterBitSet(elem->getIndex())) {
|
||||||
if(bitfieldMan_->isFilterBitSet((*i)->getIndex())) {
|
len += elem->getCompletedLength();
|
||||||
len += (*i)->getCompletedLength();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return len;
|
return len;
|
||||||
|
@ -592,10 +589,8 @@ void DefaultPieceStorage::setupFileFilter()
|
||||||
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
||||||
downloadContext_->getFileEntries();
|
downloadContext_->getFileEntries();
|
||||||
bool allSelected = true;
|
bool allSelected = true;
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for(auto & e : fileEntries) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end();
|
if(!e->isRequested()) {
|
||||||
i != eoi; ++i) {
|
|
||||||
if(!(*i)->isRequested()) {
|
|
||||||
allSelected = false;
|
allSelected = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -603,10 +598,9 @@ void DefaultPieceStorage::setupFileFilter()
|
||||||
if(allSelected) {
|
if(allSelected) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for(auto & e: fileEntries) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end(); i != eoi; ++i) {
|
if(e->isRequested()) {
|
||||||
if((*i)->isRequested()) {
|
bitfieldMan_->addFilter(e->getOffset(), e->getLength());
|
||||||
bitfieldMan_->addFilter((*i)->getOffset(), (*i)->getLength());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bitfieldMan_->enableFilter();
|
bitfieldMan_->enableFilter();
|
||||||
|
@ -697,12 +691,11 @@ void DefaultPieceStorage::flushWrDiskCacheEntry()
|
||||||
// UsedPieceSet is sorted by piece index. It means we can flush
|
// UsedPieceSet is sorted by piece index. It means we can flush
|
||||||
// cache by non-decreasing offset, which is good to reduce disk seek
|
// cache by non-decreasing offset, which is good to reduce disk seek
|
||||||
// unless the file is heavily fragmented.
|
// unless the file is heavily fragmented.
|
||||||
for(UsedPieceSet::const_iterator i = usedPieces_.begin(),
|
for(auto & piece : usedPieces_) {
|
||||||
eoi = usedPieces_.end(); i != eoi; ++i) {
|
auto ce = piece->getWrDiskCacheEntry();
|
||||||
WrDiskCacheEntry* ce = (*i)->getWrDiskCacheEntry();
|
|
||||||
if(ce) {
|
if(ce) {
|
||||||
(*i)->flushWrCache(wrDiskCache_);
|
piece->flushWrCache(wrDiskCache_);
|
||||||
(*i)->releaseWrCache(wrDiskCache_);
|
piece->releaseWrCache(wrDiskCache_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -753,8 +746,8 @@ public:
|
||||||
|
|
||||||
void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
|
void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
|
||||||
{
|
{
|
||||||
std::deque<HaveEntry>::iterator itr =
|
auto itr = std::find_if(haves_.begin(), haves_.end(),
|
||||||
std::find_if(haves_.begin(), haves_.end(), FindElapsedHave(elapsed));
|
FindElapsedHave(elapsed));
|
||||||
if(itr != haves_.end()) {
|
if(itr != haves_.end()) {
|
||||||
A2_LOG_DEBUG(fmt(MSG_REMOVED_HAVE_ENTRY,
|
A2_LOG_DEBUG(fmt(MSG_REMOVED_HAVE_ENTRY,
|
||||||
static_cast<unsigned long>(haves_.end()-itr)));
|
static_cast<unsigned long>(haves_.end()-itr)));
|
||||||
|
|
|
@ -101,9 +101,8 @@ DownloadContext::findFileEntryByOffset(int64_t offset) const
|
||||||
|
|
||||||
std::shared_ptr<FileEntry> obj(new FileEntry());
|
std::shared_ptr<FileEntry> obj(new FileEntry());
|
||||||
obj->setOffset(offset);
|
obj->setOffset(offset);
|
||||||
std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
auto i = std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
|
||||||
std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
|
DerefLess<std::shared_ptr<FileEntry> >());
|
||||||
DerefLess<std::shared_ptr<FileEntry> >());
|
|
||||||
if(i != fileEntries_.end() && (*i)->getOffset() == offset) {
|
if(i != fileEntries_.end() && (*i)->getOffset() == offset) {
|
||||||
return *i;
|
return *i;
|
||||||
} else {
|
} else {
|
||||||
|
@ -216,10 +215,9 @@ const std::string& DownloadContext::getBasePath() const
|
||||||
std::shared_ptr<FileEntry>
|
std::shared_ptr<FileEntry>
|
||||||
DownloadContext::getFirstRequestedFileEntry() const
|
DownloadContext::getFirstRequestedFileEntry() const
|
||||||
{
|
{
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for (auto& e : fileEntries_) {
|
||||||
fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
|
if(e->isRequested()) {
|
||||||
if((*i)->isRequested()) {
|
return e;
|
||||||
return *i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -228,9 +226,8 @@ DownloadContext::getFirstRequestedFileEntry() const
|
||||||
size_t DownloadContext::countRequestedFileEntry() const
|
size_t DownloadContext::countRequestedFileEntry() const
|
||||||
{
|
{
|
||||||
size_t numFiles = 0;
|
size_t numFiles = 0;
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for (const auto& e: fileEntries_) {
|
||||||
fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
|
if(e->isRequested()) {
|
||||||
if((*i)->isRequested()) {
|
|
||||||
++numFiles;
|
++numFiles;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -304,10 +304,9 @@ void DownloadEngine::poolSocket(const std::string& key,
|
||||||
std::multimap<std::string, SocketPoolEntry> newPool;
|
std::multimap<std::string, SocketPoolEntry> newPool;
|
||||||
A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry.");
|
A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry.");
|
||||||
lastSocketPoolScan_ = global::wallclock();
|
lastSocketPoolScan_ = global::wallclock();
|
||||||
for(std::multimap<std::string, SocketPoolEntry>::iterator i =
|
for(auto & elem : socketPool_) {
|
||||||
socketPool_.begin(), eoi = socketPool_.end(); i != eoi; ++i) {
|
if(!elem.second.isTimeout()) {
|
||||||
if(!(*i).second.isTimeout()) {
|
newPool.insert(elem);
|
||||||
newPool.insert(*i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
A2_LOG_DEBUG(fmt("%lu entries removed.",
|
A2_LOG_DEBUG(fmt("%lu entries removed.",
|
||||||
|
@ -425,8 +424,7 @@ DownloadEngine::findSocketPoolEntry(const std::string& key)
|
||||||
std::pair<std::multimap<std::string, SocketPoolEntry>::iterator,
|
std::pair<std::multimap<std::string, SocketPoolEntry>::iterator,
|
||||||
std::multimap<std::string, SocketPoolEntry>::iterator> range =
|
std::multimap<std::string, SocketPoolEntry>::iterator> range =
|
||||||
socketPool_.equal_range(key);
|
socketPool_.equal_range(key);
|
||||||
for(std::multimap<std::string, SocketPoolEntry>::iterator i =
|
for(auto i = range.first, eoi = range.second; i != eoi; ++i) {
|
||||||
range.first, eoi = range.second; i != eoi; ++i) {
|
|
||||||
const SocketPoolEntry& e = (*i).second;
|
const SocketPoolEntry& e = (*i).second;
|
||||||
// We assume that if socket is readable it means peer shutdowns
|
// We assume that if socket is readable it means peer shutdowns
|
||||||
// connection and the socket will receive EOF. So skip it.
|
// connection and the socket will receive EOF. So skip it.
|
||||||
|
@ -444,9 +442,8 @@ DownloadEngine::popPooledSocket
|
||||||
const std::string& proxyhost, uint16_t proxyport)
|
const std::string& proxyhost, uint16_t proxyport)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SocketCore> s;
|
std::shared_ptr<SocketCore> s;
|
||||||
std::multimap<std::string, SocketPoolEntry>::iterator i =
|
auto i = findSocketPoolEntry(createSockPoolKey(ipaddr, port, A2STR::NIL,
|
||||||
findSocketPoolEntry
|
proxyhost, proxyport));
|
||||||
(createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport));
|
|
||||||
if(i != socketPool_.end()) {
|
if(i != socketPool_.end()) {
|
||||||
s = (*i).second.getSocket();
|
s = (*i).second.getSocket();
|
||||||
socketPool_.erase(i);
|
socketPool_.erase(i);
|
||||||
|
@ -462,9 +459,8 @@ DownloadEngine::popPooledSocket
|
||||||
const std::string& proxyhost, uint16_t proxyport)
|
const std::string& proxyhost, uint16_t proxyport)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SocketCore> s;
|
std::shared_ptr<SocketCore> s;
|
||||||
std::multimap<std::string, SocketPoolEntry>::iterator i =
|
auto i = findSocketPoolEntry(createSockPoolKey(ipaddr, port, username,
|
||||||
findSocketPoolEntry
|
proxyhost, proxyport));
|
||||||
(createSockPoolKey(ipaddr, port, username, proxyhost, proxyport));
|
|
||||||
if(i != socketPool_.end()) {
|
if(i != socketPool_.end()) {
|
||||||
s = (*i).second.getSocket();
|
s = (*i).second.getSocket();
|
||||||
options = (*i).second.getOptions();
|
options = (*i).second.getOptions();
|
||||||
|
@ -478,9 +474,8 @@ DownloadEngine::popPooledSocket
|
||||||
(const std::vector<std::string>& ipaddrs, uint16_t port)
|
(const std::vector<std::string>& ipaddrs, uint16_t port)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SocketCore> s;
|
std::shared_ptr<SocketCore> s;
|
||||||
for(std::vector<std::string>::const_iterator i = ipaddrs.begin(),
|
for(const auto & ipaddr : ipaddrs) {
|
||||||
eoi = ipaddrs.end(); i != eoi; ++i) {
|
s = popPooledSocket(ipaddr, port, A2STR::NIL, 0);
|
||||||
s = popPooledSocket(*i, port, A2STR::NIL, 0);
|
|
||||||
if(s) {
|
if(s) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -495,9 +490,8 @@ DownloadEngine::popPooledSocket
|
||||||
const std::string& username)
|
const std::string& username)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SocketCore> s;
|
std::shared_ptr<SocketCore> s;
|
||||||
for(std::vector<std::string>::const_iterator i = ipaddrs.begin(),
|
for(const auto & ipaddr : ipaddrs) {
|
||||||
eoi = ipaddrs.end(); i != eoi; ++i) {
|
s = popPooledSocket(options, ipaddr, port, username, A2STR::NIL, 0);
|
||||||
s = popPooledSocket(options, *i, port, username, A2STR::NIL, 0);
|
|
||||||
if(s) {
|
if(s) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,11 +61,10 @@ std::string FeedbackURISelector::select
|
||||||
const std::vector<std::pair<size_t, std::string> >& usedHosts)
|
const std::vector<std::pair<size_t, std::string> >& usedHosts)
|
||||||
{
|
{
|
||||||
if(A2_LOG_DEBUG_ENABLED) {
|
if(A2_LOG_DEBUG_ENABLED) {
|
||||||
for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
|
for (const auto& h: usedHosts) {
|
||||||
usedHosts.begin(), eoi = usedHosts.end(); i != eoi; ++i) {
|
|
||||||
A2_LOG_DEBUG(fmt("UsedHost=%lu, %s",
|
A2_LOG_DEBUG(fmt("UsedHost=%lu, %s",
|
||||||
static_cast<unsigned long>((*i).first),
|
static_cast<unsigned long>(h.first),
|
||||||
(*i).second.c_str()));
|
h.second.c_str()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(fileEntry->getRemainingUris().empty()) {
|
if(fileEntry->getRemainingUris().empty()) {
|
||||||
|
@ -92,27 +91,24 @@ std::string FeedbackURISelector::selectRarer
|
||||||
{
|
{
|
||||||
// pair of host and URI
|
// pair of host and URI
|
||||||
std::vector<std::pair<std::string, std::string> > cands;
|
std::vector<std::pair<std::string, std::string> > cands;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for (const auto& u: uris) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
|
||||||
uri_split_result us;
|
uri_split_result us;
|
||||||
if(uri_split(&us, (*i).c_str()) == -1) {
|
if(uri_split(&us, u.c_str()) == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::string host = uri::getFieldString(us, USR_HOST, (*i).c_str());
|
auto host = uri::getFieldString(us, USR_HOST, u.c_str());
|
||||||
std::string protocol = uri::getFieldString(us, USR_SCHEME, (*i).c_str());
|
auto protocol = uri::getFieldString(us, USR_SCHEME, u.c_str());
|
||||||
std::shared_ptr<ServerStat> ss = serverStatMan_->find(host, protocol);
|
auto ss = serverStatMan_->find(host, protocol);
|
||||||
if(ss && ss->isError()) {
|
if(ss && ss->isError()) {
|
||||||
A2_LOG_DEBUG(fmt("Error not considered: %s", (*i).c_str()));
|
A2_LOG_DEBUG(fmt("Error not considered: %s", u.c_str()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cands.push_back(std::make_pair(host, *i));
|
cands.push_back(std::make_pair(host, u));
|
||||||
}
|
}
|
||||||
for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
|
for (const auto& h: usedHosts) {
|
||||||
usedHosts.begin(), eoi = usedHosts.end(); i != eoi; ++i) {
|
for (const auto& c: cands) {
|
||||||
for(std::vector<std::pair<std::string, std::string> >::const_iterator j =
|
if(h.second == c.first) {
|
||||||
cands.begin(), eoj = cands.end(); j != eoj; ++j) {
|
return c.second;
|
||||||
if((*i).second == (*j).first) {
|
|
||||||
return (*j).second;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,27 +126,29 @@ std::string FeedbackURISelector::selectFaster
|
||||||
const int SPEED_THRESHOLD = 20*1024;
|
const int SPEED_THRESHOLD = 20*1024;
|
||||||
std::vector<std::pair<std::shared_ptr<ServerStat>, std::string> > fastCands;
|
std::vector<std::pair<std::shared_ptr<ServerStat>, std::string> > fastCands;
|
||||||
std::vector<std::string> normCands;
|
std::vector<std::string> normCands;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for (const auto& u: uris) {
|
||||||
eoi = uris.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
|
if (fastCands.size() >= NUM_URI) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
uri_split_result us;
|
uri_split_result us;
|
||||||
if(uri_split(&us, (*i).c_str()) == -1) {
|
if(uri_split(&us, u.c_str()) == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::string host = uri::getFieldString(us, USR_HOST, (*i).c_str());
|
auto host = uri::getFieldString(us, USR_HOST, u.c_str());
|
||||||
if(findSecond(usedHosts.begin(), usedHosts.end(), host) !=
|
if(findSecond(usedHosts.begin(), usedHosts.end(), host) !=
|
||||||
usedHosts.end()) {
|
usedHosts.end()) {
|
||||||
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", u.c_str()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::string protocol = uri::getFieldString(us, USR_SCHEME, (*i).c_str());
|
auto protocol = uri::getFieldString(us, USR_SCHEME, u.c_str());
|
||||||
std::shared_ptr<ServerStat> ss = serverStatMan_->find(host, protocol);
|
auto ss = serverStatMan_->find(host, protocol);
|
||||||
if(!ss) {
|
if(!ss) {
|
||||||
normCands.push_back(*i);
|
normCands.push_back(u);
|
||||||
} else if(ss->isOK()) {
|
} else if(ss->isOK()) {
|
||||||
if(ss->getDownloadSpeed() > SPEED_THRESHOLD) {
|
if(ss->getDownloadSpeed() > SPEED_THRESHOLD) {
|
||||||
fastCands.push_back(std::make_pair(ss, *i));
|
fastCands.push_back(std::make_pair(ss, u));
|
||||||
} else {
|
} else {
|
||||||
normCands.push_back(*i);
|
normCands.push_back(u);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -382,9 +382,8 @@ public:
|
||||||
void FileEntry::extractURIResult
|
void FileEntry::extractURIResult
|
||||||
(std::deque<URIResult>& res, error_code::Value r)
|
(std::deque<URIResult>& res, error_code::Value r)
|
||||||
{
|
{
|
||||||
std::deque<URIResult>::iterator i =
|
auto i = std::stable_partition(uriResults_.begin(), uriResults_.end(),
|
||||||
std::stable_partition(uriResults_.begin(), uriResults_.end(),
|
FindURIResultByResult(r));
|
||||||
FindURIResultByResult(r));
|
|
||||||
std::copy(uriResults_.begin(), i, std::back_inserter(res));
|
std::copy(uriResults_.begin(), i, std::back_inserter(res));
|
||||||
uriResults_.erase(uriResults_.begin(), i);
|
uriResults_.erase(uriResults_.begin(), i);
|
||||||
}
|
}
|
||||||
|
@ -392,9 +391,8 @@ void FileEntry::extractURIResult
|
||||||
void FileEntry::reuseUri(const std::vector<std::string>& ignore)
|
void FileEntry::reuseUri(const std::vector<std::string>& ignore)
|
||||||
{
|
{
|
||||||
if(A2_LOG_DEBUG_ENABLED) {
|
if(A2_LOG_DEBUG_ENABLED) {
|
||||||
for(std::vector<std::string>::const_iterator i = ignore.begin(),
|
for (const auto& i: ignore) {
|
||||||
eoi = ignore.end(); i != eoi; ++i) {
|
A2_LOG_DEBUG(fmt("ignore host=%s", i.c_str()));
|
||||||
A2_LOG_DEBUG(fmt("ignore host=%s", (*i).c_str()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::deque<std::string> uris = spentUris_;
|
std::deque<std::string> uris = spentUris_;
|
||||||
|
@ -417,8 +415,8 @@ void FileEntry::reuseUri(const std::vector<std::string>& ignore)
|
||||||
std::set_difference(uris.begin(), uris.end(),
|
std::set_difference(uris.begin(), uris.end(),
|
||||||
errorUris.begin(), errorUris.end(),
|
errorUris.begin(), errorUris.end(),
|
||||||
std::back_inserter(reusableURIs));
|
std::back_inserter(reusableURIs));
|
||||||
std::vector<std::string>::iterator insertionPoint = reusableURIs.begin();
|
auto insertionPoint = reusableURIs.begin();
|
||||||
for(std::vector<std::string>::iterator i = reusableURIs.begin(),
|
for(auto i = reusableURIs.begin(),
|
||||||
eoi = reusableURIs.end(); i != eoi; ++i) {
|
eoi = reusableURIs.end(); i != eoi; ++i) {
|
||||||
uri_split_result us;
|
uri_split_result us;
|
||||||
if(uri_split(&us, (*i).c_str()) == 0 &&
|
if(uri_split(&us, (*i).c_str()) == 0 &&
|
||||||
|
@ -485,36 +483,32 @@ InputIterator findRequestByUri
|
||||||
|
|
||||||
bool FileEntry::removeUri(const std::string& uri)
|
bool FileEntry::removeUri(const std::string& uri)
|
||||||
{
|
{
|
||||||
std::deque<std::string>::iterator itr =
|
auto itr = std::find(spentUris_.begin(), spentUris_.end(), uri);
|
||||||
std::find(spentUris_.begin(), spentUris_.end(), uri);
|
|
||||||
if(itr == spentUris_.end()) {
|
if(itr == spentUris_.end()) {
|
||||||
itr = std::find(uris_.begin(), uris_.end(), uri);
|
itr = std::find(uris_.begin(), uris_.end(), uri);
|
||||||
if(itr == uris_.end()) {
|
if(itr == uris_.end()) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
uris_.erase(itr);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
} else {
|
uris_.erase(itr);
|
||||||
spentUris_.erase(itr);
|
|
||||||
std::shared_ptr<Request> req;
|
|
||||||
InFlightRequestSet::iterator riter =
|
|
||||||
findRequestByUri(inFlightRequests_.begin(), inFlightRequests_.end(), uri);
|
|
||||||
if(riter == inFlightRequests_.end()) {
|
|
||||||
RequestPool::iterator riter = findRequestByUri(requestPool_.begin(),
|
|
||||||
requestPool_.end(), uri);
|
|
||||||
if(riter == requestPool_.end()) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
req = *riter;
|
|
||||||
requestPool_.erase(riter);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
req = *riter;
|
|
||||||
}
|
|
||||||
req->requestRemoval();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
spentUris_.erase(itr);
|
||||||
|
std::shared_ptr<Request> req;
|
||||||
|
InFlightRequestSet::iterator riter =
|
||||||
|
findRequestByUri(inFlightRequests_.begin(), inFlightRequests_.end(), uri);
|
||||||
|
if(riter == inFlightRequests_.end()) {
|
||||||
|
RequestPool::iterator riter = findRequestByUri(requestPool_.begin(),
|
||||||
|
requestPool_.end(), uri);
|
||||||
|
if(riter == requestPool_.end()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
req = *riter;
|
||||||
|
requestPool_.erase(riter);
|
||||||
|
} else {
|
||||||
|
req = *riter;
|
||||||
|
}
|
||||||
|
req->requestRemoval();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FileEntry::getBasename() const
|
std::string FileEntry::getBasename() const
|
||||||
|
@ -547,13 +541,12 @@ bool FileEntry::addUri(const std::string& uri)
|
||||||
bool FileEntry::insertUri(const std::string& uri, size_t pos)
|
bool FileEntry::insertUri(const std::string& uri, size_t pos)
|
||||||
{
|
{
|
||||||
std::string peUri = util::percentEncodeMini(uri);
|
std::string peUri = util::percentEncodeMini(uri);
|
||||||
if(uri_split(nullptr, peUri.c_str()) == 0) {
|
if(uri_split(nullptr, peUri.c_str()) != 0) {
|
||||||
pos = std::min(pos, uris_.size());
|
|
||||||
uris_.insert(uris_.begin()+pos, peUri);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
pos = std::min(pos, uris_.size());
|
||||||
|
uris_.insert(uris_.begin()+pos, peUri);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileEntry::setPath(const std::string& path)
|
void FileEntry::setPath(const std::string& path)
|
||||||
|
@ -599,12 +592,13 @@ void writeFilePath
|
||||||
} else {
|
} else {
|
||||||
o << uris.front();
|
o << uris.front();
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(memory) {
|
||||||
|
o << "[MEMORY]" << File(entry->getPath()).getBasename();
|
||||||
} else {
|
} else {
|
||||||
if(memory) {
|
o << entry->getPath();
|
||||||
o << "[MEMORY]" << File(entry->getPath()).getBasename();
|
|
||||||
} else {
|
|
||||||
o << entry->getPath();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ int GroupId::expandUnique(a2_gid_t& n, const char* hex)
|
||||||
}
|
}
|
||||||
p <<= 64-i*4;
|
p <<= 64-i*4;
|
||||||
a2_gid_t mask = UINT64_MAX-((1LL << (64-i*4))-1);
|
a2_gid_t mask = UINT64_MAX-((1LL << (64-i*4))-1);
|
||||||
std::set<a2_gid_t>::const_iterator itr = set_.lower_bound(p);
|
auto itr = set_.lower_bound(p);
|
||||||
if(itr == set_.end()) {
|
if(itr == set_.end()) {
|
||||||
return ERR_NOT_FOUND;
|
return ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,14 +185,13 @@ HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
|
||||||
}
|
}
|
||||||
const Dict* extDict = downcast<Dict>(dict->get("m"));
|
const Dict* extDict = downcast<Dict>(dict->get("m"));
|
||||||
if(extDict) {
|
if(extDict) {
|
||||||
for(Dict::ValueType::const_iterator i = extDict->begin(),
|
for(auto & elem : *extDict) {
|
||||||
eoi = extDict->end(); i != eoi; ++i) {
|
const Integer* extId = downcast<Integer>(elem.second);
|
||||||
const Integer* extId = downcast<Integer>((*i).second);
|
|
||||||
if(extId) {
|
if(extId) {
|
||||||
int key = keyBtExtension((*i).first.c_str());
|
int key = keyBtExtension(elem.first.c_str());
|
||||||
if(key == ExtensionMessageRegistry::MAX_EXTENSION) {
|
if(key == ExtensionMessageRegistry::MAX_EXTENSION) {
|
||||||
A2_LOG_DEBUG(fmt("Unsupported BitTorrent extension %s=%" PRId64,
|
A2_LOG_DEBUG(fmt("Unsupported BitTorrent extension %s=%" PRId64,
|
||||||
(*i).first.c_str(), extId->i()));
|
elem.first.c_str(), extId->i()));
|
||||||
} else {
|
} else {
|
||||||
msg->setExtension(key, extId->i());
|
msg->setExtension(key, extId->i());
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,9 +57,8 @@ void HaveEraseCommand::process()
|
||||||
{
|
{
|
||||||
const RequestGroupList& groups =
|
const RequestGroupList& groups =
|
||||||
getDownloadEngine()->getRequestGroupMan()->getRequestGroups();
|
getDownloadEngine()->getRequestGroupMan()->getRequestGroups();
|
||||||
for(RequestGroupList::const_iterator i = groups.begin(),
|
for(auto & group : groups) {
|
||||||
eoi = groups.end(); i != eoi; ++i) {
|
const auto& ps = group->getPieceStorage();
|
||||||
const std::shared_ptr<PieceStorage>& ps = (*i)->getPieceStorage();
|
|
||||||
if(ps) {
|
if(ps) {
|
||||||
ps->removeAdvertisedPiece(5);
|
ps->removeAdvertisedPiece(5);
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ bool HttpHeader::defined(int hdKey) const
|
||||||
|
|
||||||
const std::string& HttpHeader::find(int hdKey) const
|
const std::string& HttpHeader::find(int hdKey) const
|
||||||
{
|
{
|
||||||
std::multimap<int, std::string>::const_iterator itr = table_.find(hdKey);
|
auto itr = table_.find(hdKey);
|
||||||
if(itr == table_.end()) {
|
if(itr == table_.end()) {
|
||||||
return A2STR::NIL;
|
return A2STR::NIL;
|
||||||
} else {
|
} else {
|
||||||
|
@ -71,9 +71,7 @@ const std::string& HttpHeader::find(int hdKey) const
|
||||||
std::vector<std::string> HttpHeader::findAll(int hdKey) const
|
std::vector<std::string> HttpHeader::findAll(int hdKey) const
|
||||||
{
|
{
|
||||||
std::vector<std::string> v;
|
std::vector<std::string> v;
|
||||||
std::pair<std::multimap<int, std::string>::const_iterator,
|
auto itrpair = table_.equal_range(hdKey);
|
||||||
std::multimap<int, std::string>::const_iterator> itrpair =
|
|
||||||
table_.equal_range(hdKey);
|
|
||||||
while(itrpair.first != itrpair.second) {
|
while(itrpair.first != itrpair.second) {
|
||||||
v.push_back((*itrpair.first).second);
|
v.push_back((*itrpair.first).second);
|
||||||
++itrpair.first;
|
++itrpair.first;
|
||||||
|
@ -90,7 +88,7 @@ HttpHeader::equalRange(int hdKey) const
|
||||||
|
|
||||||
Range HttpHeader::getRange() const
|
Range HttpHeader::getRange() const
|
||||||
{
|
{
|
||||||
const std::string& rangeStr = find(CONTENT_RANGE);
|
const auto& rangeStr = find(CONTENT_RANGE);
|
||||||
if(rangeStr.empty()) {
|
if(rangeStr.empty()) {
|
||||||
const std::string& clenStr = find(CONTENT_LENGTH);
|
const std::string& clenStr = find(CONTENT_LENGTH);
|
||||||
if(clenStr.empty()) {
|
if(clenStr.empty()) {
|
||||||
|
@ -113,8 +111,7 @@ Range HttpHeader::getRange() const
|
||||||
// we expect that rangeStr looks like 'bytes 100-199/100'
|
// we expect that rangeStr looks like 'bytes 100-199/100'
|
||||||
// but some server returns '100-199/100', omitting bytes-unit sepcifier
|
// but some server returns '100-199/100', omitting bytes-unit sepcifier
|
||||||
// 'bytes'.
|
// 'bytes'.
|
||||||
std::string::const_iterator byteRangeSpec =
|
auto byteRangeSpec = std::find(rangeStr.begin(), rangeStr.end(), ' ');
|
||||||
std::find(rangeStr.begin(), rangeStr.end(), ' ');
|
|
||||||
if(byteRangeSpec == rangeStr.end()) {
|
if(byteRangeSpec == rangeStr.end()) {
|
||||||
// we assume bytes-unit specifier omitted.
|
// we assume bytes-unit specifier omitted.
|
||||||
byteRangeSpec = rangeStr.begin();
|
byteRangeSpec = rangeStr.begin();
|
||||||
|
@ -124,8 +121,7 @@ Range HttpHeader::getRange() const
|
||||||
++byteRangeSpec;
|
++byteRangeSpec;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string::const_iterator slash =
|
auto slash = std::find(byteRangeSpec, rangeStr.end(), '/');
|
||||||
std::find(byteRangeSpec, rangeStr.end(), '/');
|
|
||||||
if(slash == rangeStr.end() || slash+1 == rangeStr.end() ||
|
if(slash == rangeStr.end() || slash+1 == rangeStr.end() ||
|
||||||
(byteRangeSpec+1 == slash && *byteRangeSpec == '*') ||
|
(byteRangeSpec+1 == slash && *byteRangeSpec == '*') ||
|
||||||
(slash+2 == rangeStr.end() && *(slash+1) == '*')) {
|
(slash+2 == rangeStr.end() && *(slash+1) == '*')) {
|
||||||
|
@ -134,7 +130,7 @@ Range HttpHeader::getRange() const
|
||||||
// not satisfiable) status.
|
// not satisfiable) status.
|
||||||
return Range();
|
return Range();
|
||||||
}
|
}
|
||||||
std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-');
|
auto minus = std::find(byteRangeSpec, slash, '-');
|
||||||
if(minus == slash) {
|
if(minus == slash) {
|
||||||
return Range();
|
return Range();
|
||||||
}
|
}
|
||||||
|
@ -218,17 +214,15 @@ bool HttpHeader::fieldContains(int hdKey, const char* value)
|
||||||
std::pair<std::multimap<int, std::string>::const_iterator,
|
std::pair<std::multimap<int, std::string>::const_iterator,
|
||||||
std::multimap<int, std::string>::const_iterator> range =
|
std::multimap<int, std::string>::const_iterator> range =
|
||||||
equalRange(hdKey);
|
equalRange(hdKey);
|
||||||
for(std::multimap<int, std::string>::const_iterator i = range.first;
|
for(auto i = range.first; i != range.second; ++i) {
|
||||||
i != range.second; ++i) {
|
|
||||||
std::vector<Scip> values;
|
std::vector<Scip> values;
|
||||||
util::splitIter((*i).second.begin(), (*i).second.end(),
|
util::splitIter((*i).second.begin(), (*i).second.end(),
|
||||||
std::back_inserter(values),
|
std::back_inserter(values),
|
||||||
',',
|
',',
|
||||||
true // doStrip
|
true // doStrip
|
||||||
);
|
);
|
||||||
for(std::vector<Scip>::const_iterator j = values.begin(),
|
for (const auto& v: values) {
|
||||||
eoj = values.end(); j != eoj; ++j) {
|
if(util::strieq(v.first, v.second, value)) {
|
||||||
if(util::strieq((*j).first, (*j).second, value)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,17 +94,16 @@ int64_t HttpRequest::getEndByte() const
|
||||||
{
|
{
|
||||||
if(!segment_ || !request_) {
|
if(!segment_ || !request_) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
|
||||||
if(request_->isPipeliningEnabled()) {
|
|
||||||
int64_t endByte =
|
|
||||||
fileEntry_->gtoloff(segment_->getPosition()+segment_->getLength()-1);
|
|
||||||
return std::min(endByte, fileEntry_->getLength()-1);
|
|
||||||
} else if(endOffsetOverride_ > 0) {
|
|
||||||
return endOffsetOverride_ - 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if(request_->isPipeliningEnabled()) {
|
||||||
|
int64_t endByte =
|
||||||
|
fileEntry_->gtoloff(segment_->getPosition()+segment_->getLength()-1);
|
||||||
|
return std::min(endByte, fileEntry_->getLength()-1);
|
||||||
|
}
|
||||||
|
if(endOffsetOverride_ > 0) {
|
||||||
|
return endOffsetOverride_ - 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Range HttpRequest::getRange() const
|
Range HttpRequest::getRange() const
|
||||||
|
@ -112,9 +111,8 @@ Range HttpRequest::getRange() const
|
||||||
// content-length is always 0
|
// content-length is always 0
|
||||||
if(!segment_) {
|
if(!segment_) {
|
||||||
return Range();
|
return Range();
|
||||||
} else {
|
|
||||||
return Range(getStartByte(), getEndByte(), fileEntry_->getLength());
|
|
||||||
}
|
}
|
||||||
|
return Range(getStartByte(), getEndByte(), fileEntry_->getLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpRequest::isRangeSatisfied(const Range& range) const
|
bool HttpRequest::isRangeSatisfied(const Range& range) const
|
||||||
|
@ -128,9 +126,8 @@ bool HttpRequest::isRangeSatisfied(const Range& range) const
|
||||||
((fileEntry_->getLength() == 0) ||
|
((fileEntry_->getLength() == 0) ||
|
||||||
(fileEntry_->getLength() == range.entityLength))) {
|
(fileEntry_->getLength() == range.entityLength))) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -444,7 +441,7 @@ bool HttpRequest::conditionalRequest() const
|
||||||
if(!ifModSinceHeader_.empty()) {
|
if(!ifModSinceHeader_.empty()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for(std::vector<std::string>::const_iterator i = headers_.begin(),
|
for(auto i = headers_.begin(),
|
||||||
eoi = headers_.end(); i != eoi; ++i) {
|
eoi = headers_.end(); i != eoi; ++i) {
|
||||||
if(util::istartsWith(*i, "if-modified-since") ||
|
if(util::istartsWith(*i, "if-modified-since") ||
|
||||||
util::istartsWith(*i, "if-none-match")) {
|
util::istartsWith(*i, "if-none-match")) {
|
||||||
|
|
|
@ -124,15 +124,13 @@ std::string HttpResponse::determinFilename() const
|
||||||
httpRequest_->getFile().end());
|
httpRequest_->getFile().end());
|
||||||
if(file.empty()) {
|
if(file.empty()) {
|
||||||
return "index.html";
|
return "index.html";
|
||||||
} else {
|
|
||||||
return file;
|
|
||||||
}
|
}
|
||||||
} else {
|
return file;
|
||||||
A2_LOG_INFO(fmt(MSG_CONTENT_DISPOSITION_DETECTED,
|
|
||||||
cuid_,
|
|
||||||
contentDisposition.c_str()));
|
|
||||||
return contentDisposition;
|
|
||||||
}
|
}
|
||||||
|
A2_LOG_INFO(fmt(MSG_CONTENT_DISPOSITION_DETECTED,
|
||||||
|
cuid_,
|
||||||
|
contentDisposition.c_str()));
|
||||||
|
return contentDisposition;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpResponse::retrieveCookie()
|
void HttpResponse::retrieveCookie()
|
||||||
|
@ -299,9 +297,8 @@ bool parseMetalinkHttpLink(MetalinkHttpEntry& result, const std::string& s)
|
||||||
std::string::const_iterator> p = util::stripIter(first+1, last);
|
std::string::const_iterator> p = util::stripIter(first+1, last);
|
||||||
if(p.first == p.second) {
|
if(p.first == p.second) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
result.uri.assign(p.first, p.second);
|
|
||||||
}
|
}
|
||||||
|
result.uri.assign(p.first, p.second);
|
||||||
last = std::find(last, s.end(), ';');
|
last = std::find(last, s.end(), ';');
|
||||||
if(last != s.end()) {
|
if(last != s.end()) {
|
||||||
++last;
|
++last;
|
||||||
|
@ -363,15 +360,13 @@ void HttpResponse::getMetalinKHttpEntries
|
||||||
if(option->defined(PREF_METALINK_LOCATION)) {
|
if(option->defined(PREF_METALINK_LOCATION)) {
|
||||||
const std::string& loc = option->get(PREF_METALINK_LOCATION);
|
const std::string& loc = option->get(PREF_METALINK_LOCATION);
|
||||||
util::split(loc.begin(), loc.end(), std::back_inserter(locs), ',', true);
|
util::split(loc.begin(), loc.end(), std::back_inserter(locs), ',', true);
|
||||||
for(std::vector<std::string>::iterator i = locs.begin(), eoi = locs.end();
|
for (auto& l: locs) {
|
||||||
i != eoi; ++i) {
|
util::lowercase(l);
|
||||||
util::lowercase(*i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(std::vector<MetalinkHttpEntry>::iterator i = result.begin(),
|
for (auto& r: result) {
|
||||||
eoi = result.end(); i != eoi; ++i) {
|
if(std::find(locs.begin(), locs.end(), r.geo) != locs.end()) {
|
||||||
if(std::find(locs.begin(), locs.end(), (*i).geo) != locs.end()) {
|
r.pri -= 999999;
|
||||||
(*i).pri -= 999999;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -409,10 +404,9 @@ void HttpResponse::getDigest(std::vector<Checksum>& result) const
|
||||||
}
|
}
|
||||||
std::sort(result.begin(), result.end(), HashTypeStronger());
|
std::sort(result.begin(), result.end(), HashTypeStronger());
|
||||||
std::vector<Checksum> temp;
|
std::vector<Checksum> temp;
|
||||||
for(std::vector<Checksum>::iterator i = result.begin(),
|
for(auto i = result.begin(), eoi = result.end(); i != eoi;) {
|
||||||
eoi = result.end(); i != eoi;) {
|
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
std::vector<Checksum>::iterator j = i+1;
|
auto j = i+1;
|
||||||
for(; j != eoi; ++j) {
|
for(; j != eoi; ++j) {
|
||||||
if((*i).getHashType() != (*j).getHashType()) {
|
if((*i).getHashType() != (*j).getHashType()) {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -194,10 +194,9 @@ bool HttpResponseCommand::executeInternal()
|
||||||
getDownloadContext()->setAcceptMetalink(false);
|
getDownloadContext()->setAcceptMetalink(false);
|
||||||
std::vector<MetalinkHttpEntry> entries;
|
std::vector<MetalinkHttpEntry> entries;
|
||||||
httpResponse->getMetalinKHttpEntries(entries, getOption());
|
httpResponse->getMetalinKHttpEntries(entries, getOption());
|
||||||
for(std::vector<MetalinkHttpEntry>::iterator i = entries.begin(),
|
for(const auto& e : entries) {
|
||||||
eoi = entries.end(); i != eoi; ++i) {
|
getFileEntry()->addUri(e.uri);
|
||||||
getFileEntry()->addUri((*i).uri);
|
A2_LOG_DEBUG(fmt("Adding URI=%s", e.uri.c_str()));
|
||||||
A2_LOG_DEBUG(fmt("Adding URI=%s", (*i).uri.c_str()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,16 +204,16 @@ bool HttpResponseCommand::executeInternal()
|
||||||
if(httpHeader->defined(HttpHeader::DIGEST)) {
|
if(httpHeader->defined(HttpHeader::DIGEST)) {
|
||||||
std::vector<Checksum> checksums;
|
std::vector<Checksum> checksums;
|
||||||
httpResponse->getDigest(checksums);
|
httpResponse->getDigest(checksums);
|
||||||
for(std::vector<Checksum>::iterator i = checksums.begin(),
|
for(const auto &checksum : checksums) {
|
||||||
eoi = checksums.end(); i != eoi; ++i) {
|
|
||||||
if(getDownloadContext()->getHashType().empty()) {
|
if(getDownloadContext()->getHashType().empty()) {
|
||||||
A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s",
|
A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s",
|
||||||
(*i).getHashType().c_str(),
|
checksum.getHashType().c_str(),
|
||||||
(*i).getDigest().c_str()));
|
checksum.getDigest().c_str()));
|
||||||
getDownloadContext()->setDigest((*i).getHashType(), (*i).getDigest());
|
getDownloadContext()->setDigest(checksum.getHashType(),
|
||||||
|
checksum.getDigest());
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if(checkChecksum(getDownloadContext(), *i)) {
|
if(checkChecksum(getDownloadContext(), checksum)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,9 +280,8 @@ bool HttpResponseCommand::executeInternal()
|
||||||
httpHeader->defined(HttpHeader::DIGEST)) {
|
httpHeader->defined(HttpHeader::DIGEST)) {
|
||||||
std::vector<Checksum> checksums;
|
std::vector<Checksum> checksums;
|
||||||
httpResponse->getDigest(checksums);
|
httpResponse->getDigest(checksums);
|
||||||
for(std::vector<Checksum>::iterator i = checksums.begin(),
|
for(const auto &checksum : checksums) {
|
||||||
eoi = checksums.end(); i != eoi; ++i) {
|
if(checkChecksum(getDownloadContext(), checksum)) {
|
||||||
if(checkChecksum(getDownloadContext(), *i)) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ unsigned char* IndexBtMessage::createMessage()
|
||||||
* piece index --- index, 4bytes
|
* piece index --- index, 4bytes
|
||||||
* total: 9bytes
|
* total: 9bytes
|
||||||
*/
|
*/
|
||||||
unsigned char* msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 5, getId());
|
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 5, getId());
|
||||||
bittorrent::setIntParam(&msg[5], index_);
|
bittorrent::setIntParam(&msg[5], index_);
|
||||||
return msg;
|
return msg;
|
||||||
|
|
|
@ -128,7 +128,7 @@ void LibuvEventPoll::poll(const struct timeval& tv)
|
||||||
|
|
||||||
// timeout == 0 will tick once
|
// timeout == 0 will tick once
|
||||||
if (timeout >= 0) {
|
if (timeout >= 0) {
|
||||||
uv_timer_t* timer = new uv_timer_t;
|
auto timer = new uv_timer_t;
|
||||||
uv_timer_init(loop_, timer);
|
uv_timer_init(loop_, timer);
|
||||||
uv_timer_start(timer, timer_callback, timeout, timeout);
|
uv_timer_start(timer, timer_callback, timeout, timeout);
|
||||||
|
|
||||||
|
@ -147,11 +147,10 @@ void LibuvEventPoll::poll(const struct timeval& tv)
|
||||||
// own timeout and ares may create new sockets or closes socket in
|
// own timeout and ares may create new sockets or closes socket in
|
||||||
// their API. So we call ares_process_fd for all ares_channel and
|
// their API. So we call ares_process_fd for all ares_channel and
|
||||||
// re-register their sockets.
|
// re-register their sockets.
|
||||||
for(KAsyncNameResolverEntrySet::iterator i = nameResolverEntries_.begin(),
|
for (auto& r: nameResolverEntries_) {
|
||||||
eoi = nameResolverEntries_.end(); i != eoi; ++i) {
|
r->processTimeout();
|
||||||
(*i)->processTimeout();
|
r->removeSocketEvents(this);
|
||||||
(*i)->removeSocketEvents(this);
|
r->addSocketEvents(this);
|
||||||
(*i)->addSocketEvents(this);
|
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
@ -219,8 +218,8 @@ void LibuvEventPoll::pollCallback(KPoll* poll, int status, int events)
|
||||||
bool LibuvEventPoll::addEvents(sock_t socket,
|
bool LibuvEventPoll::addEvents(sock_t socket,
|
||||||
const LibuvEventPoll::KEvent& event)
|
const LibuvEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
|
auto socketEntry = std::make_shared<KSocketEntry>(socket);
|
||||||
KSocketEntrySet::iterator i = socketEntries_.lower_bound(socketEntry);
|
auto i = socketEntries_.lower_bound(socketEntry);
|
||||||
|
|
||||||
if (i != socketEntries_.end() && **i == *socketEntry) {
|
if (i != socketEntries_.end() && **i == *socketEntry) {
|
||||||
event.addSelf(*i);
|
event.addSelf(*i);
|
||||||
|
@ -234,7 +233,7 @@ bool LibuvEventPoll::addEvents(sock_t socket,
|
||||||
|
|
||||||
socketEntries_.insert(i, socketEntry);
|
socketEntries_.insert(i, socketEntry);
|
||||||
event.addSelf(socketEntry);
|
event.addSelf(socketEntry);
|
||||||
KPoll* poll = new KPoll(this, socketEntry.get(), socket);
|
auto poll = new KPoll(this, socketEntry.get(), socket);
|
||||||
polls_[socket] = poll;
|
polls_[socket] = poll;
|
||||||
poll->start();
|
poll->start();
|
||||||
return true;
|
return true;
|
||||||
|
@ -302,10 +301,8 @@ bool LibuvEventPoll::deleteEvents(sock_t socket, Command* command,
|
||||||
bool LibuvEventPoll::addNameResolver(const std::shared_ptr<AsyncNameResolver>& resolver,
|
bool LibuvEventPoll::addNameResolver(const std::shared_ptr<AsyncNameResolver>& resolver,
|
||||||
Command* command)
|
Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KAsyncNameResolverEntry> entry(
|
auto entry = std::make_shared<KAsyncNameResolverEntry>(resolver, command);
|
||||||
new KAsyncNameResolverEntry(resolver, command));
|
auto itr = nameResolverEntries_.lower_bound(entry);
|
||||||
KAsyncNameResolverEntrySet::iterator itr =
|
|
||||||
nameResolverEntries_.lower_bound(entry);
|
|
||||||
if (itr != nameResolverEntries_.end() && *(*itr) == *entry) {
|
if (itr != nameResolverEntries_.end() && *(*itr) == *entry) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -317,9 +314,8 @@ bool LibuvEventPoll::addNameResolver(const std::shared_ptr<AsyncNameResolver>& r
|
||||||
bool LibuvEventPoll::deleteNameResolver(const std::shared_ptr<AsyncNameResolver>& resolver,
|
bool LibuvEventPoll::deleteNameResolver(const std::shared_ptr<AsyncNameResolver>& resolver,
|
||||||
Command* command)
|
Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KAsyncNameResolverEntry> entry(
|
auto entry = std::make_shared<KAsyncNameResolverEntry>(resolver, command);
|
||||||
new KAsyncNameResolverEntry(resolver, command));
|
auto itr = nameResolverEntries_.find(entry);
|
||||||
KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
|
|
||||||
if (itr == nameResolverEntries_.end()) {
|
if (itr == nameResolverEntries_.end()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,11 +107,10 @@ MSEHandshake::HANDSHAKE_TYPE MSEHandshake::identifyHandshakeType()
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - This is legacy BitTorrent handshake.",
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - This is legacy BitTorrent handshake.",
|
||||||
cuid_));
|
cuid_));
|
||||||
return HANDSHAKE_LEGACY;
|
return HANDSHAKE_LEGACY;
|
||||||
} else {
|
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - This may be encrypted BitTorrent handshake.",
|
|
||||||
cuid_));
|
|
||||||
return HANDSHAKE_ENCRYPTED;
|
|
||||||
}
|
}
|
||||||
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - This may be encrypted BitTorrent handshake.",
|
||||||
|
cuid_));
|
||||||
|
return HANDSHAKE_ENCRYPTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MSEHandshake::initEncryptionFacility(bool initiator)
|
void MSEHandshake::initEncryptionFacility(bool initiator)
|
||||||
|
@ -430,17 +429,15 @@ bool MSEHandshake::receiveReceiverHashAndPadCLength
|
||||||
// pointing to the position of HASH('req2', SKEY) xor HASH('req3', S)
|
// pointing to the position of HASH('req2', SKEY) xor HASH('req3', S)
|
||||||
unsigned char* rbufptr = rbuf_;
|
unsigned char* rbufptr = rbuf_;
|
||||||
std::shared_ptr<DownloadContext> downloadContext;
|
std::shared_ptr<DownloadContext> downloadContext;
|
||||||
for(std::vector<std::shared_ptr<DownloadContext> >::const_iterator i =
|
for(auto & ctx : downloadContexts) {
|
||||||
downloadContexts.begin(), eoi = downloadContexts.end();
|
|
||||||
i != eoi; ++i) {
|
|
||||||
unsigned char md[20];
|
unsigned char md[20];
|
||||||
const unsigned char* infohash = bittorrent::getInfoHash(*i);
|
const auto infohash = bittorrent::getInfoHash(ctx);
|
||||||
createReq23Hash(md, infohash);
|
createReq23Hash(md, infohash);
|
||||||
if(memcmp(md, rbufptr, sizeof(md)) == 0) {
|
if(memcmp(md, rbufptr, sizeof(md)) == 0) {
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - info hash found: %s",
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - info hash found: %s",
|
||||||
cuid_,
|
cuid_,
|
||||||
util::toHex(infohash, INFO_HASH_LENGTH).c_str()));
|
util::toHex(infohash, INFO_HASH_LENGTH).c_str()));
|
||||||
downloadContext = *i;
|
downloadContext = ctx;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,8 +76,7 @@ void FilesMetalinkParserState::beginElement
|
||||||
{
|
{
|
||||||
if(checkNsUri(nsUri) && strcmp(localname, "file") == 0) {
|
if(checkNsUri(nsUri) && strcmp(localname, "file") == 0) {
|
||||||
psm->setFileState();
|
psm->setFileState();
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "name", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "name", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr != attrs.end()) {
|
if(itr != attrs.end()) {
|
||||||
std::string name((*itr).value, (*itr).valueLength);
|
std::string name((*itr).value, (*itr).valueLength);
|
||||||
if(name.empty() || util::detectDirTraversal(name)) {
|
if(name.empty() || util::detectDirTraversal(name)) {
|
||||||
|
@ -113,7 +112,7 @@ void FileMetalinkParserState::beginElement
|
||||||
} else if(strcmp(localname, "resources") == 0) {
|
} else if(strcmp(localname, "resources") == 0) {
|
||||||
psm->setResourcesState();
|
psm->setResourcesState();
|
||||||
int maxConnections;
|
int maxConnections;
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr =
|
||||||
findAttr(attrs, "maxconnections", METALINK3_NAMESPACE_URI);
|
findAttr(attrs, "maxconnections", METALINK3_NAMESPACE_URI);
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
maxConnections = -1;
|
maxConnections = -1;
|
||||||
|
@ -198,8 +197,7 @@ void VerificationMetalinkParserState::beginElement
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
if(strcmp(localname, "hash") == 0) {
|
if(strcmp(localname, "hash") == 0) {
|
||||||
psm->setHashState();
|
psm->setHashState();
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
@ -210,8 +208,7 @@ void VerificationMetalinkParserState::beginElement
|
||||||
psm->setPiecesState();
|
psm->setPiecesState();
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "length", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "length", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
@ -223,8 +220,7 @@ void VerificationMetalinkParserState::beginElement
|
||||||
}
|
}
|
||||||
std::string type;
|
std::string type;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
@ -238,16 +234,14 @@ void VerificationMetalinkParserState::beginElement
|
||||||
#endif // ENABLE_MESSAGE_DIGEST
|
#endif // ENABLE_MESSAGE_DIGEST
|
||||||
if(strcmp(localname, "signature") == 0) {
|
if(strcmp(localname, "signature") == 0) {
|
||||||
psm->setSignatureState();
|
psm->setSignatureState();
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
psm->newSignatureTransaction();
|
psm->newSignatureTransaction();
|
||||||
psm->setTypeOfSignature
|
psm->setTypeOfSignature
|
||||||
(std::string((*itr).value, (*itr).valueLength));
|
(std::string((*itr).value, (*itr).valueLength));
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "file", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "file", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr != attrs.end()) {
|
if(itr != attrs.end()) {
|
||||||
std::string file((*itr).value, (*itr).valueLength);
|
std::string file((*itr).value, (*itr).valueLength);
|
||||||
if(!util::detectDirTraversal(file)) {
|
if(!util::detectDirTraversal(file)) {
|
||||||
|
@ -280,8 +274,7 @@ void PiecesMetalinkParserState::beginElement
|
||||||
{
|
{
|
||||||
if(checkNsUri(nsUri) && strcmp(localname, "hash") == 0) {
|
if(checkNsUri(nsUri) && strcmp(localname, "hash") == 0) {
|
||||||
psm->setPieceHashState();
|
psm->setPieceHashState();
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "piece", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "piece", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
psm->cancelChunkChecksumTransaction();
|
psm->cancelChunkChecksumTransaction();
|
||||||
} else {
|
} else {
|
||||||
|
@ -343,53 +336,44 @@ void ResourcesMetalinkParserState::beginElement
|
||||||
psm->setURLState();
|
psm->setURLState();
|
||||||
std::string type;
|
std::string type;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "type", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
type.assign((*itr).value, (*itr).valueLength);
|
|
||||||
}
|
}
|
||||||
|
type.assign((*itr).value, (*itr).valueLength);
|
||||||
}
|
}
|
||||||
std::string location;
|
std::string location;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "location", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "location", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr != attrs.end()) {
|
if(itr != attrs.end()) {
|
||||||
location.assign((*itr).value, (*itr).valueLength);
|
location.assign((*itr).value, (*itr).valueLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int preference;
|
int preference;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "preference", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "preference", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
preference = MetalinkResource::getLowestPriority();
|
preference = MetalinkResource::getLowestPriority();
|
||||||
} else {
|
}
|
||||||
if(util::parseIntNoThrow
|
else if(util::parseIntNoThrow(preference, std::string((*itr).value, (*itr).valueLength)) &&
|
||||||
(preference, std::string((*itr).value, (*itr).valueLength)) &&
|
preference >= 0) {
|
||||||
preference >= 0) {
|
// In Metalink3Spec, highest prefernce value is 100. We
|
||||||
// In Metalink3Spec, highest prefernce value is 100. We
|
// use Metalink4Spec priority unit system in which 1 is
|
||||||
// use Metalink4Spec priority unit system in which 1 is
|
// higest.
|
||||||
// higest.
|
preference = 101-preference;
|
||||||
preference = 101-preference;
|
}
|
||||||
} else {
|
else {
|
||||||
preference = MetalinkResource::getLowestPriority();
|
preference = MetalinkResource::getLowestPriority();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int maxConnections;
|
int maxConnections;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "maxconnections", METALINK3_NAMESPACE_URI);
|
||||||
findAttr(attrs, "maxconnections", METALINK3_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
maxConnections = -1;
|
maxConnections = -1;
|
||||||
} else {
|
} else if(!util::parseIntNoThrow(maxConnections, std::string((*itr).value, (*itr).valueLength)) ||
|
||||||
if(!util::parseIntNoThrow
|
maxConnections <= 0) {
|
||||||
(maxConnections, std::string((*itr).value, (*itr).valueLength)) ||
|
maxConnections = -1;
|
||||||
maxConnections <= 0) {
|
|
||||||
maxConnections = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
psm->newResourceTransaction();
|
psm->newResourceTransaction();
|
||||||
|
|
|
@ -60,24 +60,24 @@ void MetalinkMetalinkParserStateV4::beginElement
|
||||||
const char* nsUri,
|
const char* nsUri,
|
||||||
const std::vector<XmlAttr>& attrs)
|
const std::vector<XmlAttr>& attrs)
|
||||||
{
|
{
|
||||||
if(checkNsUri(nsUri) && strcmp(localname, "file") == 0) {
|
if(checkNsUri(nsUri) && strcmp(localname, "file") != 0) {
|
||||||
psm->setFileStateV4();
|
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
|
||||||
findAttr(attrs, "name", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
|
||||||
psm->logError("Missing file@name");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
std::string name((*itr).value, (*itr).valueLength);
|
|
||||||
if(util::detectDirTraversal(name)) {
|
|
||||||
psm->logError("Bad file@name");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
psm->newEntryTransaction();
|
|
||||||
psm->setFileNameOfEntry(name);
|
|
||||||
} else {
|
|
||||||
psm->setSkipTagState();
|
psm->setSkipTagState();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
psm->setFileStateV4();
|
||||||
|
auto itr = findAttr(attrs, "name", METALINK4_NAMESPACE_URI);
|
||||||
|
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
||||||
|
psm->logError("Missing file@name");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string name((*itr).value, (*itr).valueLength);
|
||||||
|
if(util::detectDirTraversal(name)) {
|
||||||
|
psm->logError("Bad file@name");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
psm->newEntryTransaction();
|
||||||
|
psm->setFileNameOfEntry(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileMetalinkParserStateV4::beginElement
|
void FileMetalinkParserStateV4::beginElement
|
||||||
|
@ -101,8 +101,7 @@ void FileMetalinkParserStateV4::beginElement
|
||||||
psm->setMetaurlStateV4();
|
psm->setMetaurlStateV4();
|
||||||
std::string name;
|
std::string name;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "name", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "name", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr != attrs.end()) {
|
if(itr != attrs.end()) {
|
||||||
name.assign((*itr).value, (*itr).valueLength);
|
name.assign((*itr).value, (*itr).valueLength);
|
||||||
if(name.empty() || util::detectDirTraversal(name)) {
|
if(name.empty() || util::detectDirTraversal(name)) {
|
||||||
|
@ -113,33 +112,27 @@ void FileMetalinkParserStateV4::beginElement
|
||||||
}
|
}
|
||||||
int priority;
|
int priority;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "priority", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "priority", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
priority = MetalinkResource::getLowestPriority();
|
priority = MetalinkResource::getLowestPriority();
|
||||||
} else {
|
} else if(util::parseIntNoThrow(priority, std::string((*itr).value, (*itr).valueLength))) {
|
||||||
if(util::parseIntNoThrow
|
if(priority < 1 || MetalinkResource::getLowestPriority() < priority) {
|
||||||
(priority, std::string((*itr).value, (*itr).valueLength))) {
|
psm->logError("metaurl@priority is out of range");
|
||||||
if(priority < 1 || MetalinkResource::getLowestPriority() < priority) {
|
|
||||||
psm->logError("metaurl@priority is out of range");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
psm->logError("Bad metaurl@priority");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
psm->logError("Bad metaurl@priority");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string mediatype;
|
std::string mediatype;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "mediatype", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "mediatype", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
||||||
psm->logError("Missing metaurl@mediatype");
|
psm->logError("Missing metaurl@mediatype");
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
mediatype.assign((*itr).value, (*itr).valueLength);
|
|
||||||
}
|
}
|
||||||
|
mediatype.assign((*itr).value, (*itr).valueLength);
|
||||||
}
|
}
|
||||||
psm->newMetaurlTransaction();
|
psm->newMetaurlTransaction();
|
||||||
psm->setPriorityOfMetaurl(priority);
|
psm->setPriorityOfMetaurl(priority);
|
||||||
|
@ -149,29 +142,24 @@ void FileMetalinkParserStateV4::beginElement
|
||||||
psm->setURLStateV4();
|
psm->setURLStateV4();
|
||||||
std::string location;
|
std::string location;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "location", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "location", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr != attrs.end()) {
|
if(itr != attrs.end()) {
|
||||||
location.assign((*itr).value, (*itr).valueLength);
|
location.assign((*itr).value, (*itr).valueLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int priority;
|
int priority;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "priority", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "priority", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end()) {
|
if(itr == attrs.end()) {
|
||||||
priority = MetalinkResource::getLowestPriority();
|
priority = MetalinkResource::getLowestPriority();
|
||||||
} else {
|
} else if(util::parseIntNoThrow(priority, std::string((*itr).value, (*itr).valueLength))) {
|
||||||
if(util::parseIntNoThrow
|
if(priority < 1 || MetalinkResource::getLowestPriority() < priority) {
|
||||||
(priority, std::string((*itr).value, (*itr).valueLength))) {
|
psm->logError("url@priority is out of range");
|
||||||
if(priority < 1 || MetalinkResource::getLowestPriority() < priority) {
|
|
||||||
psm->logError("url@priority is out of range");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
psm->logError("Bad url@priority");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
psm->logError("Bad url@priority");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
psm->newResourceTransaction();
|
psm->newResourceTransaction();
|
||||||
|
@ -181,40 +169,35 @@ void FileMetalinkParserStateV4::beginElement
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
else if(strcmp(localname, "hash") == 0) {
|
else if(strcmp(localname, "hash") == 0) {
|
||||||
psm->setHashStateV4();
|
psm->setHashStateV4();
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "type", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "type", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
||||||
psm->logError("Missing hash@type");
|
psm->logError("Missing hash@type");
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
psm->newChecksumTransaction();
|
|
||||||
psm->setTypeOfChecksum(std::string((*itr).value, (*itr).valueLength));
|
|
||||||
}
|
}
|
||||||
|
psm->newChecksumTransaction();
|
||||||
|
psm->setTypeOfChecksum(std::string((*itr).value, (*itr).valueLength));
|
||||||
} else if(strcmp(localname, "pieces") == 0) {
|
} else if(strcmp(localname, "pieces") == 0) {
|
||||||
psm->setPiecesStateV4();
|
psm->setPiecesStateV4();
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "length", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "length", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
||||||
psm->logError("Missing pieces@length");
|
psm->logError("Missing pieces@length");
|
||||||
return;
|
return;
|
||||||
} else if(!util::parseUIntNoThrow
|
}
|
||||||
(length, std::string((*itr).value, (*itr).valueLength))) {
|
if(!util::parseUIntNoThrow(length, std::string((*itr).value, (*itr).valueLength))) {
|
||||||
psm->logError("Bad pieces@length");
|
psm->logError("Bad pieces@length");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string type;
|
std::string type;
|
||||||
{
|
{
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "type", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "type", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
||||||
psm->logError("Missing pieces@type");
|
psm->logError("Missing pieces@type");
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
type.assign((*itr).value, (*itr).valueLength);
|
|
||||||
}
|
}
|
||||||
|
type.assign((*itr).value, (*itr).valueLength);
|
||||||
}
|
}
|
||||||
psm->newChunkChecksumTransactionV4();
|
psm->newChunkChecksumTransactionV4();
|
||||||
psm->setLengthOfChunkChecksumV4(length);
|
psm->setLengthOfChunkChecksumV4(length);
|
||||||
|
@ -223,8 +206,7 @@ void FileMetalinkParserStateV4::beginElement
|
||||||
#endif // ENABLE_MESSAGE_DIGEST
|
#endif // ENABLE_MESSAGE_DIGEST
|
||||||
else if(strcmp(localname, "signature") == 0) {
|
else if(strcmp(localname, "signature") == 0) {
|
||||||
psm->setSignatureStateV4();
|
psm->setSignatureStateV4();
|
||||||
std::vector<XmlAttr>::const_iterator itr =
|
auto itr = findAttr(attrs, "mediatype", METALINK4_NAMESPACE_URI);
|
||||||
findAttr(attrs, "mediatype", METALINK4_NAMESPACE_URI);
|
|
||||||
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
if(itr == attrs.end() || (*itr).valueLength == 0) {
|
||||||
psm->logError("Missing signature@mediatype");
|
psm->logError("Missing signature@mediatype");
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -169,21 +169,22 @@ void NumberOptionHandler::parseArg(Option& option, int64_t number) const
|
||||||
{
|
{
|
||||||
if((min_ == -1 || min_ <= number) && (max_ == -1 || number <= max_)) {
|
if((min_ == -1 || min_ <= number) && (max_ == -1 || number <= max_)) {
|
||||||
option.put(pref_, util::itos(number));
|
option.put(pref_, util::itos(number));
|
||||||
} else {
|
return;
|
||||||
std::string msg = pref_->k;
|
|
||||||
msg += " ";
|
|
||||||
if(min_ == -1 && max_ != -1) {
|
|
||||||
msg += fmt(_("must be smaller than or equal to %" PRId64 "."), max_);
|
|
||||||
} else if(min_ != -1 && max_ != -1) {
|
|
||||||
msg += fmt(_("must be between %" PRId64 " and %" PRId64 "."),
|
|
||||||
min_, max_);
|
|
||||||
} else if(min_ != -1 && max_ == -1) {
|
|
||||||
msg += fmt(_("must be greater than or equal to %" PRId64 "."), min_);
|
|
||||||
} else {
|
|
||||||
msg += _("must be a number.");
|
|
||||||
}
|
|
||||||
throw DL_ABORT_EX(msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string msg = pref_->k;
|
||||||
|
msg += " ";
|
||||||
|
if(min_ == -1 && max_ != -1) {
|
||||||
|
msg += fmt(_("must be smaller than or equal to %" PRId64 "."), max_);
|
||||||
|
} else if(min_ != -1 && max_ != -1) {
|
||||||
|
msg += fmt(_("must be between %" PRId64 " and %" PRId64 "."),
|
||||||
|
min_, max_);
|
||||||
|
} else if(min_ != -1 && max_ == -1) {
|
||||||
|
msg += fmt(_("must be greater than or equal to %" PRId64 "."), min_);
|
||||||
|
} else {
|
||||||
|
msg += _("must be a number.");
|
||||||
|
}
|
||||||
|
throw DL_ABORT_EX(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string NumberOptionHandler::createPossibleValuesString() const
|
std::string NumberOptionHandler::createPossibleValuesString() const
|
||||||
|
@ -405,20 +406,18 @@ ParameterOptionHandler::~ParameterOptionHandler() {}
|
||||||
void ParameterOptionHandler::parseArg(Option& option, const std::string& optarg)
|
void ParameterOptionHandler::parseArg(Option& option, const std::string& optarg)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
std::vector<std::string>::const_iterator itr =
|
auto itr = std::find(validParamValues_.begin(), validParamValues_.end(), optarg);
|
||||||
std::find(validParamValues_.begin(), validParamValues_.end(), optarg);
|
|
||||||
if(itr == validParamValues_.end()) {
|
if(itr == validParamValues_.end()) {
|
||||||
std::string msg = pref_->k;
|
std::string msg = pref_->k;
|
||||||
msg += " ";
|
msg += " ";
|
||||||
msg += _("must be one of the following:");
|
msg += _("must be one of the following:");
|
||||||
if(validParamValues_.size() == 0) {
|
if(validParamValues_.size() == 0) {
|
||||||
msg += "''";
|
msg += "''";
|
||||||
} else {
|
}
|
||||||
for(std::vector<std::string>::const_iterator itr =
|
else {
|
||||||
validParamValues_.begin(), eoi = validParamValues_.end();
|
for (const auto& p: validParamValues_) {
|
||||||
itr != eoi; ++itr) {
|
|
||||||
msg += "'";
|
msg += "'";
|
||||||
msg += *itr;
|
msg += p;
|
||||||
msg += "' ";
|
msg += "' ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -262,9 +262,8 @@ void OptionParser::parse(Option& option, const KeyVals& options) const
|
||||||
void OptionParser::setOptionHandlers
|
void OptionParser::setOptionHandlers
|
||||||
(const std::vector<OptionHandler*>& handlers)
|
(const std::vector<OptionHandler*>& handlers)
|
||||||
{
|
{
|
||||||
for(std::vector<OptionHandler*>::const_iterator i =
|
for (const auto& h: handlers) {
|
||||||
handlers.begin(), eoi = handlers.end(); i != eoi; ++i) {
|
addOptionHandler(h);
|
||||||
addOptionHandler(*i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,10 +279,9 @@ void OptionParser::addOptionHandler(OptionHandler* handler)
|
||||||
|
|
||||||
void OptionParser::parseDefaultValues(Option& option) const
|
void OptionParser::parseDefaultValues(Option& option) const
|
||||||
{
|
{
|
||||||
for(std::vector<OptionHandler*>::const_iterator i =
|
for (const auto& h: handlers_) {
|
||||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
if (h && !h->getDefaultValue().empty()) {
|
||||||
if(*i && !(*i)->getDefaultValue().empty()) {
|
h->parse(option, h->getDefaultValue());
|
||||||
(*i)->parse(option, (*i)->getDefaultValue());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -291,10 +289,9 @@ void OptionParser::parseDefaultValues(Option& option) const
|
||||||
std::vector<const OptionHandler*> OptionParser::findByTag(uint32_t tag) const
|
std::vector<const OptionHandler*> OptionParser::findByTag(uint32_t tag) const
|
||||||
{
|
{
|
||||||
std::vector<const OptionHandler*> result;
|
std::vector<const OptionHandler*> result;
|
||||||
for(std::vector<OptionHandler*>::const_iterator i =
|
for (const auto& h: handlers_) {
|
||||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
if(h && !h->isHidden() && h->hasTag(tag)) {
|
||||||
if(*i && !(*i)->isHidden() && (*i)->hasTag(tag)) {
|
result.push_back(h);
|
||||||
result.push_back(*i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -304,14 +301,13 @@ std::vector<const OptionHandler*>
|
||||||
OptionParser::findByNameSubstring(const std::string& substring) const
|
OptionParser::findByNameSubstring(const std::string& substring) const
|
||||||
{
|
{
|
||||||
std::vector<const OptionHandler*> result;
|
std::vector<const OptionHandler*> result;
|
||||||
for(std::vector<OptionHandler*>::const_iterator i =
|
for (const auto& h: handlers_) {
|
||||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
if(h && !h->isHidden()) {
|
||||||
if(*i && !(*i)->isHidden()) {
|
size_t nameLen = strlen(h->getName());
|
||||||
size_t nameLen = strlen((*i)->getName());
|
if(std::search(h->getName(), h->getName()+nameLen,
|
||||||
if(std::search((*i)->getName(), (*i)->getName()+nameLen,
|
|
||||||
substring.begin(), substring.end()) !=
|
substring.begin(), substring.end()) !=
|
||||||
(*i)->getName()+nameLen) {
|
h->getName()+nameLen) {
|
||||||
result.push_back(*i);
|
result.push_back(h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -321,10 +317,9 @@ OptionParser::findByNameSubstring(const std::string& substring) const
|
||||||
std::vector<const OptionHandler*> OptionParser::findAll() const
|
std::vector<const OptionHandler*> OptionParser::findAll() const
|
||||||
{
|
{
|
||||||
std::vector<const OptionHandler*> result;
|
std::vector<const OptionHandler*> result;
|
||||||
for(std::vector<OptionHandler*>::const_iterator i =
|
for (const auto& h: handlers_) {
|
||||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
if(h && !h->isHidden()) {
|
||||||
if(*i && !(*i)->isHidden()) {
|
result.push_back(h);
|
||||||
result.push_back(*i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -274,7 +274,7 @@ void PeerConnection::reserveBuffer(size_t minSize)
|
||||||
{
|
{
|
||||||
if(bufferCapacity_ < minSize) {
|
if(bufferCapacity_ < minSize) {
|
||||||
bufferCapacity_ = minSize;
|
bufferCapacity_ = minSize;
|
||||||
unsigned char *buf = new unsigned char[bufferCapacity_];
|
auto buf = new unsigned char[bufferCapacity_];
|
||||||
memcpy(buf, resbuf_, resbufLength_);
|
memcpy(buf, resbuf_, resbufLength_);
|
||||||
delete [] resbuf_;
|
delete [] resbuf_;
|
||||||
resbuf_ = buf;
|
resbuf_ = buf;
|
||||||
|
|
|
@ -362,7 +362,7 @@ void Piece::updateWrCache(WrDiskCache* diskCache, unsigned char* data,
|
||||||
}
|
}
|
||||||
assert(wrCache_);
|
assert(wrCache_);
|
||||||
A2_LOG_DEBUG(fmt("updateWrCache entry=%p", wrCache_));
|
A2_LOG_DEBUG(fmt("updateWrCache entry=%p", wrCache_));
|
||||||
WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
|
auto cell = new WrDiskCacheEntry::DataCell();
|
||||||
cell->goff = goff;
|
cell->goff = goff;
|
||||||
cell->data = data;
|
cell->data = data;
|
||||||
cell->offset = offset;
|
cell->offset = offset;
|
||||||
|
|
|
@ -168,7 +168,7 @@ bool PollEventPoll::addEvents
|
||||||
event.addSelf(socketEntry);
|
event.addSelf(socketEntry);
|
||||||
if(pollfdCapacity_ == pollfdNum_) {
|
if(pollfdCapacity_ == pollfdNum_) {
|
||||||
pollfdCapacity_ *= 2;
|
pollfdCapacity_ *= 2;
|
||||||
struct pollfd* newPollfds = new struct pollfd[pollfdCapacity_];
|
auto newPollfds = new struct pollfd[pollfdCapacity_];
|
||||||
memcpy(newPollfds, pollfds_, pollfdNum_*sizeof(struct pollfd));
|
memcpy(newPollfds, pollfds_, pollfdNum_*sizeof(struct pollfd));
|
||||||
delete [] pollfds_;
|
delete [] pollfds_;
|
||||||
pollfds_ = newPollfds;
|
pollfds_ = newPollfds;
|
||||||
|
|
|
@ -44,8 +44,8 @@ PriorityPieceSelector::PriorityPieceSelector
|
||||||
bool PriorityPieceSelector::select
|
bool PriorityPieceSelector::select
|
||||||
(size_t& index, const unsigned char* bitfield, size_t nbits) const
|
(size_t& index, const unsigned char* bitfield, size_t nbits) const
|
||||||
{
|
{
|
||||||
for(std::vector<size_t>::const_iterator i = prioritizedPieces_.begin(),
|
for(auto i = prioritizedPieces_.begin(), eoi = prioritizedPieces_.end();
|
||||||
eoi = prioritizedPieces_.end(); i != eoi; ++i) {
|
i != eoi; ++i) {
|
||||||
if(bitfield::test(bitfield, nbits, *i)) {
|
if(bitfield::test(bitfield, nbits, *i)) {
|
||||||
index = *i;
|
index = *i;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -57,7 +57,7 @@ unsigned char* RangeBtMessage::createMessage()
|
||||||
* length -- length, 4bytes
|
* length -- length, 4bytes
|
||||||
* total: 17bytes
|
* total: 17bytes
|
||||||
*/
|
*/
|
||||||
unsigned char* msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 13, getId());
|
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 13, getId());
|
||||||
bittorrent::setIntParam(&msg[5], index_);
|
bittorrent::setIntParam(&msg[5], index_);
|
||||||
bittorrent::setIntParam(&msg[9], begin_);
|
bittorrent::setIntParam(&msg[9], begin_);
|
||||||
|
|
|
@ -330,7 +330,7 @@ void RequestGroup::createInitialCommand
|
||||||
progressInfoFilePtr->setBtRuntime(btRuntime);
|
progressInfoFilePtr->setBtRuntime(btRuntime);
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultPeerStorage* peerStoragePtr(new DefaultPeerStorage());
|
auto peerStoragePtr(new DefaultPeerStorage());
|
||||||
peerStoragePtr->setBtRuntime(btRuntime);
|
peerStoragePtr->setBtRuntime(btRuntime);
|
||||||
peerStoragePtr->setPieceStorage(pieceStorage_);
|
peerStoragePtr->setPieceStorage(pieceStorage_);
|
||||||
peerStorage_ = peerStoragePtr;
|
peerStorage_ = peerStoragePtr;
|
||||||
|
@ -574,7 +574,7 @@ void RequestGroup::initPieceStorage()
|
||||||
#endif // ENABLE_BITTORRENT
|
#endif // ENABLE_BITTORRENT
|
||||||
)) {
|
)) {
|
||||||
#ifdef ENABLE_BITTORRENT
|
#ifdef ENABLE_BITTORRENT
|
||||||
DefaultPieceStorage* ps =
|
auto ps =
|
||||||
new DefaultPieceStorage(downloadContext_, option_.get());
|
new DefaultPieceStorage(downloadContext_, option_.get());
|
||||||
std::shared_ptr<PieceStorage> psHolder(ps);
|
std::shared_ptr<PieceStorage> psHolder(ps);
|
||||||
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
|
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
|
||||||
|
@ -616,7 +616,7 @@ void RequestGroup::initPieceStorage()
|
||||||
}
|
}
|
||||||
tempPieceStorage.swap(psHolder);
|
tempPieceStorage.swap(psHolder);
|
||||||
} else {
|
} else {
|
||||||
UnknownLengthPieceStorage* ps =
|
auto ps =
|
||||||
new UnknownLengthPieceStorage(downloadContext_);
|
new UnknownLengthPieceStorage(downloadContext_);
|
||||||
std::shared_ptr<PieceStorage> psHolder(ps);
|
std::shared_ptr<PieceStorage> psHolder(ps);
|
||||||
if(diskWriterFactory_) {
|
if(diskWriterFactory_) {
|
||||||
|
|
|
@ -279,17 +279,16 @@ private:
|
||||||
group->getSegmentMan()->getPeerStats().size() == 1;
|
group->getSegmentMan()->getPeerStats().size() == 1;
|
||||||
const std::vector<std::shared_ptr<PeerStat> >& peerStats =
|
const std::vector<std::shared_ptr<PeerStat> >& peerStats =
|
||||||
group->getSegmentMan()->getFastestPeerStats();
|
group->getSegmentMan()->getFastestPeerStats();
|
||||||
for(std::vector<std::shared_ptr<PeerStat> >::const_iterator i =
|
for(auto & stat : peerStats) {
|
||||||
peerStats.begin(), eoi = peerStats.end(); i != eoi; ++i) {
|
if(stat->getHostname().empty() || stat->getProtocol().empty()) {
|
||||||
if((*i)->getHostname().empty() || (*i)->getProtocol().empty()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int speed = (*i)->getAvgDownloadSpeed();
|
int speed = stat->getAvgDownloadSpeed();
|
||||||
if (speed == 0) continue;
|
if (speed == 0) continue;
|
||||||
|
|
||||||
std::shared_ptr<ServerStat> ss =
|
std::shared_ptr<ServerStat> ss =
|
||||||
e_->getRequestGroupMan()->getOrCreateServerStat((*i)->getHostname(),
|
e_->getRequestGroupMan()->getOrCreateServerStat(stat->getHostname(),
|
||||||
(*i)->getProtocol());
|
stat->getProtocol());
|
||||||
ss->increaseCounter();
|
ss->increaseCounter();
|
||||||
ss->updateDownloadSpeed(speed);
|
ss->updateDownloadSpeed(speed);
|
||||||
if(singleConnection) {
|
if(singleConnection) {
|
||||||
|
@ -361,14 +360,13 @@ public:
|
||||||
GroupId::toHex(group->getGID()).c_str()));
|
GroupId::toHex(group->getGID()).c_str()));
|
||||||
const std::vector<std::shared_ptr<FileEntry> >& files =
|
const std::vector<std::shared_ptr<FileEntry> >& files =
|
||||||
dctx->getFileEntries();
|
dctx->getFileEntries();
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for(auto & file : files) {
|
||||||
files.begin(), eoi = files.end(); i != eoi; ++i) {
|
if(!file->isRequested()) {
|
||||||
if(!(*i)->isRequested()) {
|
if(File(file->getPath()).remove()) {
|
||||||
if(File((*i)->getPath()).remove()) {
|
A2_LOG_INFO(fmt(MSG_FILE_REMOVED, file->getPath().c_str()));
|
||||||
A2_LOG_INFO(fmt(MSG_FILE_REMOVED, (*i)->getPath().c_str()));
|
|
||||||
} else {
|
} else {
|
||||||
A2_LOG_INFO(fmt(MSG_FILE_COULD_NOT_REMOVED,
|
A2_LOG_INFO(fmt(MSG_FILE_COULD_NOT_REMOVED,
|
||||||
(*i)->getPath().c_str()));
|
file->getPath().c_str()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -535,9 +533,8 @@ void RequestGroupMan::save()
|
||||||
|
|
||||||
void RequestGroupMan::closeFile()
|
void RequestGroupMan::closeFile()
|
||||||
{
|
{
|
||||||
for(RequestGroupList::iterator itr = requestGroups_.begin(),
|
for(auto & elem : requestGroups_) {
|
||||||
eoi = requestGroups_.end(); itr != eoi; ++itr) {
|
elem->closeFile();
|
||||||
(*itr)->closeFile();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -548,10 +545,8 @@ RequestGroupMan::DownloadStat RequestGroupMan::getDownloadStat() const
|
||||||
int inprogress = 0;
|
int inprogress = 0;
|
||||||
int removed = 0;
|
int removed = 0;
|
||||||
error_code::Value lastError = removedLastErrorResult_;
|
error_code::Value lastError = removedLastErrorResult_;
|
||||||
for(DownloadResultList::const_iterator itr =
|
for(auto & dr : downloadResults_) {
|
||||||
downloadResults_.begin(), eoi = downloadResults_.end(); itr != eoi;
|
|
||||||
++itr) {
|
|
||||||
const std::shared_ptr<DownloadResult>& dr = *itr;
|
|
||||||
if(dr->belongsTo != 0) {
|
if(dr->belongsTo != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -637,10 +632,8 @@ void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const
|
||||||
int err = 0;
|
int err = 0;
|
||||||
int inpr = 0;
|
int inpr = 0;
|
||||||
int rm = 0;
|
int rm = 0;
|
||||||
for(DownloadResultList::const_iterator itr =
|
for(auto & dr : downloadResults_) {
|
||||||
downloadResults_.begin(), eoi = downloadResults_.end(); itr != eoi;
|
|
||||||
++itr) {
|
|
||||||
const std::shared_ptr<DownloadResult>& dr = *itr;
|
|
||||||
if(dr->belongsTo != 0) {
|
if(dr->belongsTo != 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -719,9 +712,8 @@ void RequestGroupMan::formatDownloadResultFull
|
||||||
bool head = true;
|
bool head = true;
|
||||||
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
||||||
downloadResult->fileEntries;
|
downloadResult->fileEntries;
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for(auto & f: fileEntries) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end(); i != eoi; ++i) {
|
if(!f->isRequested()) {
|
||||||
if(!(*i)->isRequested()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::stringstream o;
|
std::stringstream o;
|
||||||
|
@ -731,14 +723,14 @@ void RequestGroupMan::formatDownloadResultFull
|
||||||
} else {
|
} else {
|
||||||
o << " | | |";
|
o << " | | |";
|
||||||
}
|
}
|
||||||
if((*i)->getLength() == 0 || downloadResult->bitfield.empty()) {
|
if(f->getLength() == 0 || downloadResult->bitfield.empty()) {
|
||||||
o << " -|";
|
o << " -|";
|
||||||
} else {
|
} else {
|
||||||
int64_t completedLength =
|
int64_t completedLength =
|
||||||
bt.getOffsetCompletedLength((*i)->getOffset(), (*i)->getLength());
|
bt.getOffsetCompletedLength(f->getOffset(), f->getLength());
|
||||||
o << std::setw(3) << 100*completedLength/(*i)->getLength() << "|";
|
o << std::setw(3) << 100*completedLength/f->getLength() << "|";
|
||||||
}
|
}
|
||||||
writeFilePath(o, *i, downloadResult->inMemoryDownload);
|
writeFilePath(o, f, downloadResult->inMemoryDownload);
|
||||||
o << "\n";
|
o << "\n";
|
||||||
out.write(o.str().c_str());
|
out.write(o.str().c_str());
|
||||||
}
|
}
|
||||||
|
@ -787,9 +779,7 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
for(RequestGroupList::const_iterator itr = requestGroups_.begin(),
|
for(auto & rg : requestGroups_) {
|
||||||
eoi = requestGroups_.end(); itr != eoi; ++itr) {
|
|
||||||
const std::shared_ptr<RequestGroup>& rg = *itr;
|
|
||||||
if(rg.get() != requestGroup) {
|
if(rg.get() != requestGroup) {
|
||||||
const std::vector<std::shared_ptr<FileEntry> >& entries =
|
const std::vector<std::shared_ptr<FileEntry> >& entries =
|
||||||
rg->getDownloadContext()->getFileEntries();
|
rg->getDownloadContext()->getFileEntries();
|
||||||
|
@ -807,17 +797,15 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons
|
||||||
|
|
||||||
void RequestGroupMan::halt()
|
void RequestGroupMan::halt()
|
||||||
{
|
{
|
||||||
for(RequestGroupList::const_iterator i = requestGroups_.begin(),
|
for(auto & elem : requestGroups_) {
|
||||||
eoi = requestGroups_.end(); i != eoi; ++i) {
|
elem->setHaltRequested(true);
|
||||||
(*i)->setHaltRequested(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RequestGroupMan::forceHalt()
|
void RequestGroupMan::forceHalt()
|
||||||
{
|
{
|
||||||
for(RequestGroupList::const_iterator i = requestGroups_.begin(),
|
for(auto & elem : requestGroups_) {
|
||||||
eoi = requestGroups_.end(); i != eoi; ++i) {
|
elem->setForceHaltRequested(true);
|
||||||
(*i)->setForceHaltRequested(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -102,9 +102,8 @@ void gatherOption
|
||||||
// header and index-out option can take array as value
|
// header and index-out option can take array as value
|
||||||
const List* oplist = downcast<List>((*first).second);
|
const List* oplist = downcast<List>((*first).second);
|
||||||
if(oplist) {
|
if(oplist) {
|
||||||
for(List::ValueType::const_iterator argiter = oplist->begin(),
|
for(auto & elem : *oplist) {
|
||||||
eoi = oplist->end(); argiter != eoi; ++argiter) {
|
const String* opval = downcast<String>(elem);
|
||||||
const String* opval = downcast<String>(*argiter);
|
|
||||||
if(opval) {
|
if(opval) {
|
||||||
handler->parse(*option, opval->s());
|
handler->parse(*option, opval->s());
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,9 +212,8 @@ template<typename OutputIterator>
|
||||||
void extractUris(OutputIterator out, const List* src)
|
void extractUris(OutputIterator out, const List* src)
|
||||||
{
|
{
|
||||||
if(src) {
|
if(src) {
|
||||||
for(List::ValueType::const_iterator i = src->begin(), eoi = src->end();
|
for(auto & elem : *src) {
|
||||||
i != eoi; ++i) {
|
const String* uri = downcast<String>(elem);
|
||||||
const String* uri = downcast<String>(*i);
|
|
||||||
if(uri) {
|
if(uri) {
|
||||||
out++ = uri->s();
|
out++ = uri->s();
|
||||||
}
|
}
|
||||||
|
@ -1272,27 +1271,24 @@ std::unique_ptr<ValueBase> ChangeUriRpcMethod::process
|
||||||
}
|
}
|
||||||
auto& s = files[index];
|
auto& s = files[index];
|
||||||
size_t delcount = 0;
|
size_t delcount = 0;
|
||||||
for(auto i = delUrisParam->begin(), eoi = delUrisParam->end();
|
for(auto & elem : *delUrisParam) {
|
||||||
i != eoi; ++i) {
|
const String* uri = downcast<String>(elem);
|
||||||
const String* uri = downcast<String>(*i);
|
|
||||||
if(uri && s->removeUri(uri->s())) {
|
if(uri && s->removeUri(uri->s())) {
|
||||||
++delcount;
|
++delcount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t addcount = 0;
|
size_t addcount = 0;
|
||||||
if(posGiven) {
|
if(posGiven) {
|
||||||
for(auto i = addUrisParam->begin(), eoi = addUrisParam->end();
|
for(auto & elem : *addUrisParam) {
|
||||||
i != eoi; ++i) {
|
const String* uri = downcast<String>(elem);
|
||||||
const String* uri = downcast<String>(*i);
|
|
||||||
if(uri && s->insertUri(uri->s(), pos)) {
|
if(uri && s->insertUri(uri->s(), pos)) {
|
||||||
++addcount;
|
++addcount;
|
||||||
++pos;
|
++pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(auto i = addUrisParam->begin(), eoi = addUrisParam->end();
|
for(auto & elem : *addUrisParam) {
|
||||||
i != eoi; ++i) {
|
const String* uri = downcast<String>(elem);
|
||||||
const String* uri = downcast<String>(*i);
|
|
||||||
if(uri && s->addUri(uri->s())) {
|
if(uri && s->addUri(uri->s())) {
|
||||||
++addcount;
|
++addcount;
|
||||||
}
|
}
|
||||||
|
@ -1354,8 +1350,8 @@ std::unique_ptr<ValueBase> SystemMulticallRpcMethod::process
|
||||||
{
|
{
|
||||||
const List* methodSpecs = checkRequiredParam<List>(req, 0);
|
const List* methodSpecs = checkRequiredParam<List>(req, 0);
|
||||||
auto list = List::g();
|
auto list = List::g();
|
||||||
for(auto i = methodSpecs->begin(), eoi = methodSpecs->end(); i != eoi; ++i) {
|
for(auto & methodSpec : *methodSpecs) {
|
||||||
Dict* methodDict = downcast<Dict>(*i);
|
Dict* methodDict = downcast<Dict>(methodSpec);
|
||||||
if(!methodDict) {
|
if(!methodDict) {
|
||||||
list->append(createErrorResponse
|
list->append(createErrorResponse
|
||||||
(DL_ABORT_EX("system.multicall expected struct."), req));
|
(DL_ABORT_EX("system.multicall expected struct."), req));
|
||||||
|
@ -1453,9 +1449,8 @@ void changeOption
|
||||||
if(option.defined(PREF_MAX_CONNECTION_PER_SERVER)) {
|
if(option.defined(PREF_MAX_CONNECTION_PER_SERVER)) {
|
||||||
int maxConn = grOption->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
|
int maxConn = grOption->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
|
||||||
const std::vector<std::shared_ptr<FileEntry> >& files = dctx->getFileEntries();
|
const std::vector<std::shared_ptr<FileEntry> >& files = dctx->getFileEntries();
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i = files.begin(),
|
for(auto & file : files) {
|
||||||
eoi = files.end(); i != eoi; ++i) {
|
(file)->setMaxConnectionPerServer(maxConn);
|
||||||
(*i)->setMaxConnectionPerServer(maxConn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(option.defined(PREF_DIR) || option.defined(PREF_OUT)) {
|
if(option.defined(PREF_DIR) || option.defined(PREF_OUT)) {
|
||||||
|
|
|
@ -116,9 +116,8 @@ void toStringList(OutputIterator out, const List* src)
|
||||||
if(!src) {
|
if(!src) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(List::ValueType::const_iterator i = src->begin(), eoi = src->end();
|
for(auto & elem : *src) {
|
||||||
i != eoi; ++i) {
|
const String* s = downcast<String>(elem);
|
||||||
const String* s = downcast<String>(*i);
|
|
||||||
if(s) {
|
if(s) {
|
||||||
*out++ = s->s();
|
*out++ = s->s();
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,8 +164,7 @@ std::shared_ptr<Segment> SegmentMan::checkoutSegment
|
||||||
segment->getWrittenLength()));
|
segment->getWrittenLength()));
|
||||||
|
|
||||||
if(piece->getLength() > 0) {
|
if(piece->getLength() > 0) {
|
||||||
std::map<size_t, int32_t>::iterator positr =
|
auto positr = segmentWrittenLengthMemo_.find(segment->getIndex());
|
||||||
segmentWrittenLengthMemo_.find(segment->getIndex());
|
|
||||||
if(positr != segmentWrittenLengthMemo_.end()) {
|
if(positr != segmentWrittenLengthMemo_.end()) {
|
||||||
const int32_t writtenLength = (*positr).second;
|
const int32_t writtenLength = (*positr).second;
|
||||||
A2_LOG_DEBUG(fmt("writtenLength(in memo)=%d, writtenLength=%d",
|
A2_LOG_DEBUG(fmt("writtenLength(in memo)=%d, writtenLength=%d",
|
||||||
|
@ -332,8 +331,7 @@ void SegmentMan::cancelSegment
|
||||||
|
|
||||||
void SegmentMan::cancelAllSegments()
|
void SegmentMan::cancelAllSegments()
|
||||||
{
|
{
|
||||||
for(std::deque<std::shared_ptr<SegmentEntry> >::iterator itr =
|
for(auto itr = usedSegmentEntries_.begin(), eoi = usedSegmentEntries_.end();
|
||||||
usedSegmentEntries_.begin(), eoi = usedSegmentEntries_.end();
|
|
||||||
itr != eoi; ++itr) {
|
itr != eoi; ++itr) {
|
||||||
cancelSegmentInternal((*itr)->cuid, (*itr)->segment);
|
cancelSegmentInternal((*itr)->cuid, (*itr)->segment);
|
||||||
}
|
}
|
||||||
|
@ -393,8 +391,7 @@ void SegmentMan::registerPeerStat(const std::shared_ptr<PeerStat>& peerStat)
|
||||||
|
|
||||||
std::shared_ptr<PeerStat> SegmentMan::getPeerStat(cuid_t cuid) const
|
std::shared_ptr<PeerStat> SegmentMan::getPeerStat(cuid_t cuid) const
|
||||||
{
|
{
|
||||||
for(std::vector<std::shared_ptr<PeerStat> >::const_iterator i =
|
for(auto i = peerStats_.begin(), eoi = peerStats_.end(); i != eoi; ++i) {
|
||||||
peerStats_.begin(), eoi = peerStats_.end(); i != eoi; ++i) {
|
|
||||||
if((*i)->getCuid() == cuid) {
|
if((*i)->getCuid() == cuid) {
|
||||||
return *i;
|
return *i;
|
||||||
}
|
}
|
||||||
|
@ -420,9 +417,8 @@ public:
|
||||||
|
|
||||||
void SegmentMan::updateFastestPeerStat(const std::shared_ptr<PeerStat>& peerStat)
|
void SegmentMan::updateFastestPeerStat(const std::shared_ptr<PeerStat>& peerStat)
|
||||||
{
|
{
|
||||||
std::vector<std::shared_ptr<PeerStat> >::iterator i =
|
auto i = std::find_if(fastestPeerStats_.begin(), fastestPeerStats_.end(),
|
||||||
std::find_if(fastestPeerStats_.begin(), fastestPeerStats_.end(),
|
PeerStatHostProtoEqual(peerStat));
|
||||||
PeerStatHostProtoEqual(peerStat));
|
|
||||||
if(i == fastestPeerStats_.end()) {
|
if(i == fastestPeerStats_.end()) {
|
||||||
fastestPeerStats_.push_back(peerStat);
|
fastestPeerStats_.push_back(peerStat);
|
||||||
} else if((*i)->getAvgDownloadSpeed() < peerStat->getAvgDownloadSpeed()) {
|
} else if((*i)->getAvgDownloadSpeed() < peerStat->getAvgDownloadSpeed()) {
|
||||||
|
|
|
@ -79,9 +79,7 @@ void SelectEventPoll::SocketEntry::addCommandEvent
|
||||||
(Command* command, int events)
|
(Command* command, int events)
|
||||||
{
|
{
|
||||||
CommandEvent cev(command, events);
|
CommandEvent cev(command, events);
|
||||||
std::deque<CommandEvent>::iterator i = std::find(commandEvents_.begin(),
|
auto i = std::find(commandEvents_.begin(), commandEvents_.end(), cev);
|
||||||
commandEvents_.end(),
|
|
||||||
cev);
|
|
||||||
if(i == commandEvents_.end()) {
|
if(i == commandEvents_.end()) {
|
||||||
commandEvents_.push_back(cev);
|
commandEvents_.push_back(cev);
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,9 +90,7 @@ void SelectEventPoll::SocketEntry::removeCommandEvent
|
||||||
(Command* command, int events)
|
(Command* command, int events)
|
||||||
{
|
{
|
||||||
CommandEvent cev(command, events);
|
CommandEvent cev(command, events);
|
||||||
std::deque<CommandEvent>::iterator i = std::find(commandEvents_.begin(),
|
auto i = std::find(commandEvents_.begin(), commandEvents_.end(), cev);
|
||||||
commandEvents_.end(),
|
|
||||||
cev);
|
|
||||||
if(i == commandEvents_.end()) {
|
if(i == commandEvents_.end()) {
|
||||||
// not found
|
// not found
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -80,7 +80,7 @@ ssize_t SinkStreamFilter::transform
|
||||||
if(alen < wlen) {
|
if(alen < wlen) {
|
||||||
size_t len = wlen - alen;
|
size_t len = wlen - alen;
|
||||||
size_t capacity = std::max(len, static_cast<size_t>(4096));
|
size_t capacity = std::max(len, static_cast<size_t>(4096));
|
||||||
unsigned char* dataCopy = new unsigned char[capacity];
|
auto dataCopy = new unsigned char[capacity];
|
||||||
memcpy(dataCopy, inbuf + alen, len);
|
memcpy(dataCopy, inbuf + alen, len);
|
||||||
piece->updateWrCache(wrDiskCache_, dataCopy, 0, len, capacity,
|
piece->updateWrCache(wrDiskCache_, dataCopy, 0, len, capacity,
|
||||||
segment->getPositionToWrite() + alen);
|
segment->getPositionToWrite() + alen);
|
||||||
|
|
|
@ -1194,8 +1194,7 @@ bool verifyHostname(const std::string& hostname,
|
||||||
if(addrLen == 0) {
|
if(addrLen == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for(std::vector<std::string>::const_iterator i = ipAddrs.begin(),
|
for(auto i = ipAddrs.begin(), eoi = ipAddrs.end(); i != eoi; ++i) {
|
||||||
eoi = ipAddrs.end(); i != eoi; ++i) {
|
|
||||||
if(addrLen == (*i).size() &&
|
if(addrLen == (*i).size() &&
|
||||||
memcmp(binAddr, (*i).c_str(), addrLen) == 0) {
|
memcmp(binAddr, (*i).c_str(), addrLen) == 0) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -1205,8 +1204,7 @@ bool verifyHostname(const std::string& hostname,
|
||||||
if(dnsNames.empty()) {
|
if(dnsNames.empty()) {
|
||||||
return util::tlsHostnameMatch(commonName, hostname);
|
return util::tlsHostnameMatch(commonName, hostname);
|
||||||
}
|
}
|
||||||
for(std::vector<std::string>::const_iterator i = dnsNames.begin(),
|
for(auto i = dnsNames.begin(), eoi = dnsNames.end(); i != eoi; ++i) {
|
||||||
eoi = dnsNames.end(); i != eoi; ++i) {
|
|
||||||
if(util::tlsHostnameMatch(*i, hostname)) {
|
if(util::tlsHostnameMatch(*i, hostname)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,13 +73,10 @@ void StreamFileAllocationEntry::prepareForNextAction
|
||||||
getRequestGroup()->getDownloadContext();
|
getRequestGroup()->getDownloadContext();
|
||||||
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
||||||
dctx->getFileEntries();
|
dctx->getFileEntries();
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
for(auto & f : fileEntries) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end(); i != eoi; ++i) {
|
const auto& reqs = f->getInFlightRequests();
|
||||||
const FileEntry::InFlightRequestSet& reqs =
|
for(auto & req : reqs) {
|
||||||
(*i)->getInFlightRequests();
|
const std::shared_ptr<PeerStat>& peerStat = req->getPeerStat();
|
||||||
for(FileEntry::InFlightRequestSet::iterator j =
|
|
||||||
reqs.begin(), eoj = reqs.end(); j != eoj; ++j) {
|
|
||||||
const std::shared_ptr<PeerStat>& peerStat = (*j)->getPeerStat();
|
|
||||||
if(peerStat) {
|
if(peerStat) {
|
||||||
peerStat->downloadStart();
|
peerStat->downloadStart();
|
||||||
}
|
}
|
||||||
|
|
|
@ -452,8 +452,8 @@ std::shared_ptr<UDPTrackerRequest> UDPTrackerClient::findInflightRequest
|
||||||
bool remove)
|
bool remove)
|
||||||
{
|
{
|
||||||
std::shared_ptr<UDPTrackerRequest> res;
|
std::shared_ptr<UDPTrackerRequest> res;
|
||||||
for(std::deque<std::shared_ptr<UDPTrackerRequest> >::iterator i =
|
for(auto i = inflightRequests_.begin(), eoi = inflightRequests_.end();
|
||||||
inflightRequests_.begin(), eoi = inflightRequests_.end(); i != eoi;
|
i != eoi;
|
||||||
++i) {
|
++i) {
|
||||||
if((*i)->remoteAddr == remoteAddr && (*i)->remotePort == remotePort &&
|
if((*i)->remoteAddr == remoteAddr && (*i)->remotePort == remotePort &&
|
||||||
(*i)->transactionId == transactionId) {
|
(*i)->transactionId == transactionId) {
|
||||||
|
@ -470,9 +470,7 @@ std::shared_ptr<UDPTrackerRequest> UDPTrackerClient::findInflightRequest
|
||||||
UDPTrackerConnection* UDPTrackerClient::getConnectionId
|
UDPTrackerConnection* UDPTrackerClient::getConnectionId
|
||||||
(const std::string& remoteAddr, uint16_t remotePort, const Timer& now)
|
(const std::string& remoteAddr, uint16_t remotePort, const Timer& now)
|
||||||
{
|
{
|
||||||
std::map<std::pair<std::string, uint16_t>,
|
auto i = connectionIdCache_.find(std::make_pair(remoteAddr, remotePort));
|
||||||
UDPTrackerConnection>::iterator i =
|
|
||||||
connectionIdCache_.find(std::make_pair(remoteAddr, remotePort));
|
|
||||||
if(i == connectionIdCache_.end()) {
|
if(i == connectionIdCache_.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,9 +57,8 @@ bool UTMetadataRequestTracker::tracks(size_t index)
|
||||||
|
|
||||||
void UTMetadataRequestTracker::remove(size_t index)
|
void UTMetadataRequestTracker::remove(size_t index)
|
||||||
{
|
{
|
||||||
std::vector<RequestEntry>::iterator i =
|
auto i = std::find(trackedRequests_.begin(), trackedRequests_.end(),
|
||||||
std::find(trackedRequests_.begin(), trackedRequests_.end(),
|
RequestEntry(index));
|
||||||
RequestEntry(index));
|
|
||||||
if(i != trackedRequests_.end()) {
|
if(i != trackedRequests_.end()) {
|
||||||
trackedRequests_.erase(i);
|
trackedRequests_.erase(i);
|
||||||
}
|
}
|
||||||
|
@ -109,8 +108,7 @@ size_t UTMetadataRequestTracker::avail() const
|
||||||
std::vector<size_t> UTMetadataRequestTracker::getAllTrackedIndex() const
|
std::vector<size_t> UTMetadataRequestTracker::getAllTrackedIndex() const
|
||||||
{
|
{
|
||||||
std::vector<size_t> indexes;
|
std::vector<size_t> indexes;
|
||||||
for(std::vector<RequestEntry>::const_iterator i = trackedRequests_.begin(),
|
for(auto i = trackedRequests_.begin(), eoi = trackedRequests_.end(); i != eoi; ++i) {
|
||||||
eoi = trackedRequests_.end(); i != eoi; ++i) {
|
|
||||||
indexes.push_back((*i).index_);
|
indexes.push_back((*i).index_);
|
||||||
}
|
}
|
||||||
return indexes;
|
return indexes;
|
||||||
|
|
|
@ -57,7 +57,7 @@ UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
|
||||||
|
|
||||||
void UnknownLengthPieceStorage::initStorage()
|
void UnknownLengthPieceStorage::initStorage()
|
||||||
{
|
{
|
||||||
DirectDiskAdaptor* directDiskAdaptor(new DirectDiskAdaptor());
|
auto directDiskAdaptor(new DirectDiskAdaptor());
|
||||||
directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
|
directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
|
||||||
directDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
|
directDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
|
||||||
downloadContext_->getFileEntries().end());
|
downloadContext_->getFileEntries().end());
|
||||||
|
|
|
@ -43,25 +43,25 @@
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
ValueValueBaseStructParserState* valueState =
|
auto valueState =
|
||||||
new ValueValueBaseStructParserState();
|
new ValueValueBaseStructParserState();
|
||||||
DictValueBaseStructParserState* dictState =
|
auto dictState =
|
||||||
new DictValueBaseStructParserState();
|
new DictValueBaseStructParserState();
|
||||||
DictKeyValueBaseStructParserState* dictKeyState =
|
auto dictKeyState =
|
||||||
new DictKeyValueBaseStructParserState();
|
new DictKeyValueBaseStructParserState();
|
||||||
DictDataValueBaseStructParserState* dictDataState =
|
auto dictDataState =
|
||||||
new DictDataValueBaseStructParserState();
|
new DictDataValueBaseStructParserState();
|
||||||
ArrayValueBaseStructParserState* arrayState =
|
auto arrayState =
|
||||||
new ArrayValueBaseStructParserState();
|
new ArrayValueBaseStructParserState();
|
||||||
ArrayDataValueBaseStructParserState* arrayDataState =
|
auto arrayDataState =
|
||||||
new ArrayDataValueBaseStructParserState();
|
new ArrayDataValueBaseStructParserState();
|
||||||
StringValueBaseStructParserState* stringState =
|
auto stringState =
|
||||||
new StringValueBaseStructParserState();
|
new StringValueBaseStructParserState();
|
||||||
NumberValueBaseStructParserState* numberState =
|
auto numberState =
|
||||||
new NumberValueBaseStructParserState();
|
new NumberValueBaseStructParserState();
|
||||||
BoolValueBaseStructParserState* boolState =
|
auto boolState =
|
||||||
new BoolValueBaseStructParserState();
|
new BoolValueBaseStructParserState();
|
||||||
NullValueBaseStructParserState* nullState =
|
auto nullState =
|
||||||
new NullValueBaseStructParserState();
|
new NullValueBaseStructParserState();
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
|
@ -41,53 +41,37 @@ namespace aria2 {
|
||||||
namespace rpc {
|
namespace rpc {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
InitialXmlRpcRequestParserState*
|
auto initialState = new InitialXmlRpcRequestParserState();
|
||||||
initialState = new InitialXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
UnknownElementXmlRpcRequestParserState*
|
auto unknownElementState = new UnknownElementXmlRpcRequestParserState();
|
||||||
unknownElementState = new UnknownElementXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
MethodCallXmlRpcRequestParserState*
|
auto methodCallState = new MethodCallXmlRpcRequestParserState();
|
||||||
methodCallState = new MethodCallXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
MethodNameXmlRpcRequestParserState*
|
auto methodNameState = new MethodNameXmlRpcRequestParserState();
|
||||||
methodNameState = new MethodNameXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
ParamsXmlRpcRequestParserState*
|
auto paramsState = new ParamsXmlRpcRequestParserState();
|
||||||
paramsState = new ParamsXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
ParamXmlRpcRequestParserState*
|
auto paramState = new ParamXmlRpcRequestParserState();
|
||||||
paramState = new ParamXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
ValueXmlRpcRequestParserState*
|
auto valueState = new ValueXmlRpcRequestParserState();
|
||||||
valueState = new ValueXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
IntXmlRpcRequestParserState*
|
auto intState = new IntXmlRpcRequestParserState();
|
||||||
intState = new IntXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
StringXmlRpcRequestParserState*
|
auto stringState = new StringXmlRpcRequestParserState();
|
||||||
stringState = new StringXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
Base64XmlRpcRequestParserState*
|
auto base64State = new Base64XmlRpcRequestParserState();
|
||||||
base64State = new Base64XmlRpcRequestParserState();
|
|
||||||
|
|
||||||
StructXmlRpcRequestParserState*
|
auto structState = new StructXmlRpcRequestParserState();
|
||||||
structState = new StructXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
MemberXmlRpcRequestParserState*
|
auto memberState = new MemberXmlRpcRequestParserState();
|
||||||
memberState = new MemberXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
NameXmlRpcRequestParserState*
|
auto nameState = new NameXmlRpcRequestParserState();
|
||||||
nameState = new NameXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
ArrayXmlRpcRequestParserState*
|
auto arrayState = new ArrayXmlRpcRequestParserState();
|
||||||
arrayState = new ArrayXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
DataXmlRpcRequestParserState*
|
auto dataState = new DataXmlRpcRequestParserState();
|
||||||
dataState = new DataXmlRpcRequestParserState();
|
|
||||||
|
|
||||||
ArrayValueXmlRpcRequestParserState*
|
auto arrayValueState = new ArrayValueXmlRpcRequestParserState();
|
||||||
arrayValueState = new ArrayValueXmlRpcRequestParserState();
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
XmlRpcRequestParserStateMachine::XmlRpcRequestParserStateMachine():
|
XmlRpcRequestParserStateMachine::XmlRpcRequestParserStateMachine():
|
||||||
|
|
|
@ -48,7 +48,7 @@ unsigned char* ZeroBtMessage::createMessage()
|
||||||
* id --- ?, 1byte
|
* id --- ?, 1byte
|
||||||
* total: 5bytes
|
* total: 5bytes
|
||||||
*/
|
*/
|
||||||
unsigned char* msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 1, getId());
|
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 1, getId());
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,9 +154,8 @@ void extractUrlList
|
||||||
|
|
||||||
virtual void visit(const List& v) CXX11_OVERRIDE
|
virtual void visit(const List& v) CXX11_OVERRIDE
|
||||||
{
|
{
|
||||||
for(List::ValueType::const_iterator itr = v.begin(), eoi = v.end();
|
for(auto & elem : v) {
|
||||||
itr != eoi; ++itr) {
|
const String* uri = downcast<String>(elem);
|
||||||
const String* uri = downcast<String>(*itr);
|
|
||||||
if(uri) {
|
if(uri) {
|
||||||
std::string utf8Uri = util::encodeNonUtf8(uri->s());
|
std::string utf8Uri = util::encodeNonUtf8(uri->s());
|
||||||
uris_.push_back(utf8Uri);
|
uris_.push_back(utf8Uri);
|
||||||
|
@ -235,9 +234,8 @@ void extractFileEntries
|
||||||
int64_t offset = 0;
|
int64_t offset = 0;
|
||||||
// multi-file mode
|
// multi-file mode
|
||||||
torrent->mode = BT_FILE_MODE_MULTI;
|
torrent->mode = BT_FILE_MODE_MULTI;
|
||||||
for(List::ValueType::const_iterator itr = filesList->begin(),
|
for(auto & f : *filesList) {
|
||||||
eoi = filesList->end(); itr != eoi; ++itr) {
|
const Dict* fileDict = downcast<Dict>(f);
|
||||||
const Dict* fileDict = downcast<Dict>(*itr);
|
|
||||||
if(!fileDict) {
|
if(!fileDict) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -267,11 +265,10 @@ void extractFileEntries
|
||||||
|
|
||||||
std::vector<std::string> pathelem(pathList->size()+1);
|
std::vector<std::string> pathelem(pathList->size()+1);
|
||||||
pathelem[0] = utf8Name;
|
pathelem[0] = utf8Name;
|
||||||
std::vector<std::string>::iterator pathelemOutItr = pathelem.begin();
|
auto pathelemOutItr = pathelem.begin();
|
||||||
++pathelemOutItr;
|
++pathelemOutItr;
|
||||||
for(List::ValueType::const_iterator itr = pathList->begin(),
|
for(auto & p : *pathList) {
|
||||||
eoi = pathList->end(); itr != eoi; ++itr) {
|
const String* elem = downcast<String>(p);
|
||||||
const String* elem = downcast<String>(*itr);
|
|
||||||
if(elem) {
|
if(elem) {
|
||||||
(*pathelemOutItr++) = elem->s();
|
(*pathelemOutItr++) = elem->s();
|
||||||
} else {
|
} else {
|
||||||
|
@ -315,12 +312,11 @@ void extractFileEntries
|
||||||
// For each uri in urlList, if it ends with '/', then
|
// For each uri in urlList, if it ends with '/', then
|
||||||
// concatenate name to it. Specification just says so.
|
// concatenate name to it. Specification just says so.
|
||||||
std::vector<std::string> uris;
|
std::vector<std::string> uris;
|
||||||
for(std::vector<std::string>::const_iterator i = urlList.begin(),
|
for(auto & elem : urlList) {
|
||||||
eoi = urlList.end(); i != eoi; ++i) {
|
if(!elem.empty() && elem[elem.size()-1] == '/') {
|
||||||
if(!(*i).empty() && (*i)[(*i).size()-1] == '/') {
|
uris.push_back(elem+util::percentEncode(utf8Name));
|
||||||
uris.push_back((*i)+util::percentEncode(utf8Name));
|
|
||||||
} else {
|
} else {
|
||||||
uris.push_back(*i);
|
uris.push_back(elem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::shared_ptr<FileEntry> fileEntry
|
std::shared_ptr<FileEntry> fileEntry
|
||||||
|
@ -344,16 +340,14 @@ void extractAnnounce(TorrentAttribute* torrent, const Dict* rootDict)
|
||||||
{
|
{
|
||||||
const List* announceList = downcast<List>(rootDict->get(C_ANNOUNCE_LIST));
|
const List* announceList = downcast<List>(rootDict->get(C_ANNOUNCE_LIST));
|
||||||
if(announceList) {
|
if(announceList) {
|
||||||
for(List::ValueType::const_iterator tierIter = announceList->begin(),
|
for(auto & elem : *announceList) {
|
||||||
eoi = announceList->end(); tierIter != eoi; ++tierIter) {
|
const List* tier = downcast<List>(elem);
|
||||||
const List* tier = downcast<List>(*tierIter);
|
|
||||||
if(!tier) {
|
if(!tier) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::vector<std::string> ntier;
|
std::vector<std::string> ntier;
|
||||||
for(List::ValueType::const_iterator uriIter = tier->begin(),
|
for(auto & t : *tier) {
|
||||||
eoi2 = tier->end(); uriIter != eoi2; ++uriIter) {
|
const String* uri = downcast<String>(t);
|
||||||
const String* uri = downcast<String>(*uriIter);
|
|
||||||
if(uri) {
|
if(uri) {
|
||||||
ntier.push_back(util::encodeNonUtf8(util::strip(uri->s())));
|
ntier.push_back(util::encodeNonUtf8(util::strip(uri->s())));
|
||||||
}
|
}
|
||||||
|
@ -378,9 +372,8 @@ void extractNodes(TorrentAttribute* torrent, const ValueBase* nodesListSrc)
|
||||||
{
|
{
|
||||||
const List* nodesList = downcast<List>(nodesListSrc);
|
const List* nodesList = downcast<List>(nodesListSrc);
|
||||||
if(nodesList) {
|
if(nodesList) {
|
||||||
for(List::ValueType::const_iterator i = nodesList->begin(),
|
for(auto & elem : *nodesList) {
|
||||||
eoi = nodesList->end(); i != eoi; ++i) {
|
const List* addrPairList = downcast<List>(elem);
|
||||||
const List* addrPairList = downcast<List>(*i);
|
|
||||||
if(!addrPairList || addrPairList->size() != 2) {
|
if(!addrPairList || addrPairList->size() != 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -942,9 +935,9 @@ std::unique_ptr<TorrentAttribute> parseMagnet(const std::string& magnet)
|
||||||
}
|
}
|
||||||
const List* trs = downcast<List>(r->get("tr"));
|
const List* trs = downcast<List>(r->get("tr"));
|
||||||
if(trs) {
|
if(trs) {
|
||||||
for(auto i = trs->begin(), eoi = trs->end(); i != eoi; ++i) {
|
for(auto & tr : *trs) {
|
||||||
std::vector<std::string> tier;
|
std::vector<std::string> tier;
|
||||||
tier.push_back(util::encodeNonUtf8(downcast<String>(*i)->s()));
|
tier.push_back(util::encodeNonUtf8(downcast<String>(tr)->s()));
|
||||||
attrs->announceList.push_back(tier);
|
attrs->announceList.push_back(tier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -973,10 +966,9 @@ std::string metadata2Torrent
|
||||||
std::string torrent = "d";
|
std::string torrent = "d";
|
||||||
|
|
||||||
List announceList;
|
List announceList;
|
||||||
for(auto tierIter = attrs->announceList.begin(),
|
for(auto & elem : attrs->announceList) {
|
||||||
eoi = attrs->announceList.end(); tierIter != eoi; ++tierIter) {
|
|
||||||
auto tier = List::g();
|
auto tier = List::g();
|
||||||
for(auto& uri : *tierIter) {
|
for(auto& uri : elem) {
|
||||||
tier->append(uri);
|
tier->append(uri);
|
||||||
}
|
}
|
||||||
if(!tier->empty()) {
|
if(!tier->empty()) {
|
||||||
|
@ -1006,11 +998,9 @@ std::string torrent2Magnet(const TorrentAttribute* attrs)
|
||||||
uri += "&dn=";
|
uri += "&dn=";
|
||||||
uri += util::percentEncode(attrs->name);
|
uri += util::percentEncode(attrs->name);
|
||||||
}
|
}
|
||||||
for(std::vector<std::vector<std::string> >::const_iterator tierIter =
|
for(auto & elem : attrs->announceList) {
|
||||||
attrs->announceList.begin(),
|
for(auto uriIter = elem.begin(),
|
||||||
eoi = attrs->announceList.end(); tierIter != eoi; ++tierIter) {
|
eoi2 = elem.end(); uriIter != eoi2; ++uriIter) {
|
||||||
for(std::vector<std::string>::const_iterator uriIter = (*tierIter).begin(),
|
|
||||||
eoi2 = (*tierIter).end(); uriIter != eoi2; ++uriIter) {
|
|
||||||
uri += "&tr=";
|
uri += "&tr=";
|
||||||
uri += util::percentEncode(*uriIter);
|
uri += util::percentEncode(*uriIter);
|
||||||
}
|
}
|
||||||
|
@ -1036,9 +1026,8 @@ void removeAnnounceUri
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(std::find(uris.begin(), uris.end(), "*") == uris.end()) {
|
if(std::find(uris.begin(), uris.end(), "*") == uris.end()) {
|
||||||
for(std::vector<std::vector<std::string> >::iterator i =
|
for(auto i = attrs->announceList.begin(); i != attrs->announceList.end();) {
|
||||||
attrs->announceList.begin(); i != attrs->announceList.end();) {
|
for(auto j = (*i).begin(); j != (*i).end();) {
|
||||||
for(std::vector<std::string>::iterator j =(*i).begin();j != (*i).end();) {
|
|
||||||
if(std::find(uris.begin(), uris.end(), *j) == uris.end()) {
|
if(std::find(uris.begin(), uris.end(), *j) == uris.end()) {
|
||||||
++j;
|
++j;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1059,10 +1048,9 @@ void removeAnnounceUri
|
||||||
void addAnnounceUri
|
void addAnnounceUri
|
||||||
(TorrentAttribute* attrs, const std::vector<std::string>& uris)
|
(TorrentAttribute* attrs, const std::vector<std::string>& uris)
|
||||||
{
|
{
|
||||||
for(std::vector<std::string>::const_iterator i = uris.begin(),
|
for(auto & uri : uris) {
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
|
||||||
std::vector<std::string> tier;
|
std::vector<std::string> tier;
|
||||||
tier.push_back(*i);
|
tier.push_back(uri);
|
||||||
attrs->announceList.push_back(tier);
|
attrs->announceList.push_back(tier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -302,9 +302,8 @@ void extractPeer(const ValueBase* peerData, int family, OutputIterator dest)
|
||||||
|
|
||||||
virtual void visit(const List& peerData) CXX11_OVERRIDE
|
virtual void visit(const List& peerData) CXX11_OVERRIDE
|
||||||
{
|
{
|
||||||
for(List::ValueType::const_iterator itr = peerData.begin(),
|
for(auto & elem : peerData) {
|
||||||
eoi = peerData.end(); itr != eoi; ++itr) {
|
const Dict* peerDict = downcast<Dict>(elem);
|
||||||
const Dict* peerDict = downcast<Dict>(*itr);
|
|
||||||
if(!peerDict) {
|
if(!peerDict) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -354,9 +353,8 @@ void print(Output& o, const std::shared_ptr<DownloadContext>& dctx)
|
||||||
for(std::vector<std::vector<std::string> >::const_iterator tierIter =
|
for(std::vector<std::vector<std::string> >::const_iterator tierIter =
|
||||||
torrentAttrs->announceList.begin(),
|
torrentAttrs->announceList.begin(),
|
||||||
eoi = torrentAttrs->announceList.end(); tierIter != eoi; ++tierIter) {
|
eoi = torrentAttrs->announceList.end(); tierIter != eoi; ++tierIter) {
|
||||||
for(std::vector<std::string>::const_iterator i = (*tierIter).begin(),
|
for(auto & elem : *tierIter) {
|
||||||
eoi2 = (*tierIter).end(); i != eoi2; ++i) {
|
o.printf(" %s", elem.c_str());
|
||||||
o.printf(" %s", (*i).c_str());
|
|
||||||
}
|
}
|
||||||
o.write("\n");
|
o.write("\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,8 +168,8 @@ void showCandidates
|
||||||
global::cerr()->printf("\n");
|
global::cerr()->printf("\n");
|
||||||
global::cerr()->printf(_("Did you mean:"));
|
global::cerr()->printf(_("Did you mean:"));
|
||||||
global::cerr()->printf("\n");
|
global::cerr()->printf("\n");
|
||||||
for(std::vector<std::pair<int, const Pref*> >::iterator i = cands.begin(),
|
for(auto i = cands.begin(), eoi = cands.end();
|
||||||
eoi = cands.end(); i != eoi && (*i).first <= threshold; ++i) {
|
i != eoi && (*i).first <= threshold; ++i) {
|
||||||
global::cerr()->printf("\t--%s\n", (*i).second->k);
|
global::cerr()->printf("\t--%s\n", (*i).second->k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,9 +221,8 @@ void expand
|
||||||
while(first != last) {
|
while(first != last) {
|
||||||
InputIterator i = first;
|
InputIterator i = first;
|
||||||
for(; i != last && *i != '{' && *i != '['; ++i);
|
for(; i != last && *i != '{' && *i != '['; ++i);
|
||||||
for(std::vector<std::string>::iterator j = res.begin(), eoj = res.end();
|
for(auto & re : res) {
|
||||||
j != eoj; ++j) {
|
re.append(first, i);
|
||||||
(*j).append(first, i);
|
|
||||||
}
|
}
|
||||||
first = i;
|
first = i;
|
||||||
if(first == last) {
|
if(first == last) {
|
||||||
|
|
|
@ -65,7 +65,7 @@ public:
|
||||||
Pref* makePref(const char* key)
|
Pref* makePref(const char* key)
|
||||||
{
|
{
|
||||||
size_t id = nextId();
|
size_t id = nextId();
|
||||||
Pref* pref = new Pref(key, id);
|
auto pref = new Pref(key, id);
|
||||||
i2p_.push_back(pref);
|
i2p_.push_back(pref);
|
||||||
k2p_[key] = pref;
|
k2p_[key] = pref;
|
||||||
return pref;
|
return pref;
|
||||||
|
@ -81,7 +81,7 @@ public:
|
||||||
}
|
}
|
||||||
const Pref* k2p(const std::string& k) const
|
const Pref* k2p(const std::string& k) const
|
||||||
{
|
{
|
||||||
std::map<std::string, const Pref*>::const_iterator i = k2p_.find(k);
|
auto i = k2p_.find(k);
|
||||||
if(i == k2p_.end()) {
|
if(i == k2p_.end()) {
|
||||||
return i2p_[0];
|
return i2p_[0];
|
||||||
} else {
|
} else {
|
||||||
|
@ -96,7 +96,7 @@ private:
|
||||||
|
|
||||||
PrefFactory* getPrefFactory()
|
PrefFactory* getPrefFactory()
|
||||||
{
|
{
|
||||||
static PrefFactory* pf = new PrefFactory();
|
static auto pf = new PrefFactory();
|
||||||
return pf;
|
return pf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -663,8 +663,7 @@ void computeHeadPieces
|
||||||
if(head == 0) {
|
if(head == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator fi =
|
for(auto fi = fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
|
|
||||||
if((*fi)->getLength() == 0) {
|
if((*fi)->getLength() == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -688,8 +687,7 @@ void computeTailPieces
|
||||||
if(tail == 0) {
|
if(tail == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator fi =
|
for(auto fi = fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
|
||||||
fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
|
|
||||||
if((*fi)->getLength() == 0) {
|
if((*fi)->getLength() == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ namespace global {
|
||||||
|
|
||||||
Timer& wallclock()
|
Timer& wallclock()
|
||||||
{
|
{
|
||||||
static Timer* t = new Timer();
|
static auto t = new Timer();
|
||||||
return *t;
|
return *t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue