Jelajahi Sumber

2010-03-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Inspect all xt in magnet to find urn:btih.
	* src/bittorrent_helper.cc
	* test/BittorrentHelperTest.cc
Tatsuhiro Tsujikawa 15 tahun lalu
induk
melakukan
7bbc5769a5
3 mengubah file dengan 33 tambahan dan 17 penghapusan
  1. 6 0
      ChangeLog
  2. 21 17
      src/bittorrent_helper.cc
  3. 6 0
      test/BittorrentHelperTest.cc

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+2010-03-12  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Inspect all xt in magnet to find urn:btih.
+	* src/bittorrent_helper.cc
+	* test/BittorrentHelperTest.cc
+
 2010-03-12  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Use global option for DHTSetup.

+ 21 - 17
src/bittorrent_helper.cc

@@ -863,25 +863,29 @@ BDE parseMagnet(const std::string& magnet)
   if(!r.containsKey("xt")) {
     throw DL_ABORT_EX("Missing xt parameter in Magnet URI.");
   }
+  std::string infoHash;
   const BDE& xts = r["xt"];
-  if(xts.size() == 0 || !util::startsWith(xts[0].s(), "urn:btih:")) {
-    throw DL_ABORT_EX("Bad BitTorrent Magnet URI.");
-  }
-  std::string infoHash = xts[0].s().substr(9);
-  if(infoHash.size() == 32) {
-    std::string rawhash = base32::decode(infoHash);
-    if(rawhash.size() != 20) {
-      throw DL_ABORT_EX("Invalid BitTorrent Info Hash.");
-    }
-    infoHash = rawhash;
-  } else if(infoHash.size() == 40) {
-    std::string rawhash = util::fromHex(infoHash);
-    if(rawhash.empty()) {
-      throw DL_ABORT_EX("Invalid BitTorrent Info Hash.");
+  for(BDE::List::const_iterator xtiter = xts.listBegin(),
+        eoi = xts.listEnd(); xtiter != eoi && infoHash.empty(); ++xtiter) {
+    if(util::startsWith((*xtiter).s(), "urn:btih:")) {
+      std::string xtarg = (*xtiter).s().substr(9);
+      size_t size = xtarg.size();
+      if(size == 32) {
+        std::string rawhash = base32::decode(xtarg);
+        if(rawhash.size() == 20) {
+          infoHash.swap(rawhash);
+        }
+      } else if(size == 40) {
+        std::string rawhash = util::fromHex(xtarg);
+        if(!rawhash.empty()) {
+          infoHash.swap(rawhash);
+        }
+      }
     }
-    infoHash = rawhash;
-  } else {
-    throw DL_ABORT_EX("Invalid BitTorrent Info Hash.");
+  }
+  if(infoHash.empty()) {
+    throw DL_ABORT_EX("Bad BitTorrent Magnet URI. "
+                      "No valid BitTorrent Info Hash found.");
   }
   BDE announceList = BDE::list();
   if(r.containsKey("tr")) {

+ 6 - 0
test/BittorrentHelperTest.cc

@@ -729,6 +729,12 @@ void BittorrentHelperTest::testParseMagnet()
     (std::string("[METADATA]248d0a1cd08284299de78d5c1ed359bb46717d8c"),
      attrs[bittorrent::NAME].s());
   CPPUNIT_ASSERT(attrs[bittorrent::ANNOUNCE_LIST].size() == 0);
+
+  magnet = "magnet:?xt=urn:sha1:7899bdb90a026c746f3cbc10839dd9b2a2a3e985&"
+    "xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c";
+  attrs = bittorrent::parseMagnet(magnet);
+  CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
+                       util::toHex(attrs[bittorrent::INFO_HASH].s()));
 }
 
 void BittorrentHelperTest::testParseMagnet_base32()