diff --git a/src/Checksum.cc b/src/Checksum.cc index 05653304..0aaf6ac9 100644 --- a/src/Checksum.cc +++ b/src/Checksum.cc @@ -33,6 +33,7 @@ */ /* copyright --> */ #include "Checksum.h" +#include "MessageDigest.h" namespace aria2 { @@ -62,4 +63,24 @@ void Checksum::setAlgo(const std::string& algo) algo_ = algo; } +void Checksum::swap(Checksum& other) +{ + using std::swap; + if(this != &other) { + swap(algo_, other.algo_); + swap(messageDigest_, other.messageDigest_); + } +} + +void swap(Checksum& a, Checksum& b) +{ + a.swap(b); +} + +bool HashTypeStronger::operator() + (const Checksum& lhs, const Checksum& rhs) const +{ + return MessageDigest::isStronger(lhs.getAlgo(), rhs.getAlgo()); +} + } // namespace aria2 diff --git a/src/Checksum.h b/src/Checksum.h index a800111e..ea198733 100644 --- a/src/Checksum.h +++ b/src/Checksum.h @@ -64,6 +64,15 @@ public: { return algo_; } + + void swap(Checksum& other); +}; + +void swap(Checksum& a, Checksum& b); + +class HashTypeStronger { +public: + bool operator()(const Checksum& lhs, const Checksum& rhs) const; }; } // namespace aria2 diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index 17313732..da637798 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -306,12 +306,11 @@ bool parseMetalinkHttpLink(MetalinkHttpEntry& result, const std::string& s) if(last == s.end()) { return false; } - std::string uri(first+1, last); - uri::UriStruct us; - if(uri::parse(us, uri)) { - result.uri = uri; - } else { + std::string uri = util::stripIter(first+1, last); + if(uri.empty()) { return false; + } else { + result.uri = uri; } last = std::find(last, s.end(), ';'); if(last != s.end()) { @@ -388,9 +387,9 @@ void HttpResponse::getMetalinKHttpEntries #ifdef ENABLE_MESSAGE_DIGEST // Digest header field is defined by // http://tools.ietf.org/html/rfc3230. -SharedHandle HttpResponse::getDigest() const +void HttpResponse::getDigest(std::vector& result) const { - SharedHandle res; + using std::swap; std::pair::const_iterator, std::multimap::const_iterator> p = httpHeader_->getIterator(HttpHeader::DIGEST); @@ -413,15 +412,29 @@ SharedHandle HttpResponse::getDigest() const if(!MessageDigest::isValidHash(hashType, hexDigest)) { continue; } - if(!res) { - res.reset(new Checksum(hashType, hexDigest)); - } else if(MessageDigest::isStronger(hashType, res->getAlgo())) { - res->setAlgo(hashType); - res->setMessageDigest(hexDigest); - } + result.push_back(Checksum(hashType, hexDigest)); } } - return res; + std::sort(result.begin(), result.end(), HashTypeStronger()); + std::vector temp; + for(std::vector::iterator i = result.begin(), + eoi = result.end(); i != eoi;) { + bool ok = true; + std::vector::iterator j = i+1; + for(; j != eoi; ++j) { + if((*i).getAlgo() != (*j).getAlgo()) { + break; + } + if((*i).getMessageDigest() != (*j).getMessageDigest()) { + ok = false; + } + } + if(ok) { + temp.push_back(*i); + } + i = j; + } + swap(temp, result); } #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 247e7d96..2a28916c 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -136,11 +136,11 @@ public: (std::vector& result, const SharedHandle