Selaa lähdekoodia

2009-11-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added BitTorrent magnet link support to ProtocolDetector.
	* src/ProtocolDetector.cc
	* src/ProtocolDetector.h
	* test/ProtocolDetectorTest.cc
Tatsuhiro Tsujikawa 16 vuotta sitten
vanhempi
commit
c73d235ab8
4 muutettua tiedostoa jossa 33 lisäystä ja 2 poistoa
  1. 7 0
      ChangeLog
  2. 11 2
      src/ProtocolDetector.cc
  3. 4 0
      src/ProtocolDetector.h
  4. 11 0
      test/ProtocolDetectorTest.cc

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+2009-11-22  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Added BitTorrent magnet link support to ProtocolDetector.
+	* src/ProtocolDetector.cc
+	* src/ProtocolDetector.h
+	* test/ProtocolDetectorTest.cc
+
 2009-11-22  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Don't inject validator into BtMessage in metadataGetMode because

+ 11 - 2
src/ProtocolDetector.cc

@@ -33,12 +33,15 @@
  */
 /* copyright --> */
 #include "ProtocolDetector.h"
-#include "Request.h"
-#include "File.h"
+
 #include <cstring>
 #include <fstream>
 #include <iomanip>
 
+#include "Request.h"
+#include "File.h"
+#include "util.h"
+
 namespace aria2 {
 
 ProtocolDetector::ProtocolDetector() {}
@@ -65,6 +68,12 @@ bool ProtocolDetector::guessTorrentFile(const std::string& uri) const
   }
 }
 
+bool ProtocolDetector::guessTorrentMagnet(const std::string& uri) const
+{
+  return util::startsWith(uri, "magnet:?") &&
+    uri.find("xt=urn:btih:") != std::string::npos;
+}
+
 bool ProtocolDetector::guessMetalinkFile(const std::string& uri) const
 {
   if(!File(uri).isFile()) {

+ 4 - 0
src/ProtocolDetector.h

@@ -53,6 +53,10 @@ public:
   // metainfo file, otherwise returns false.
   bool guessTorrentFile(const std::string& uri) const;
 
+  // Returns true if ProtocolDetector thinks uri is BitTorrent Magnet link.
+  // magnet:?xt=urn:btih:<info-hash>...
+  bool guessTorrentMagnet(const std::string& uri) const;
+
   // Returns true if ProtocolDetector thinks uri is a path of Metalink XML
   // file, otherwise return false.
   bool guessMetalinkFile(const std::string& uri) const;

+ 11 - 0
test/ProtocolDetectorTest.cc

@@ -11,6 +11,7 @@ class ProtocolDetectorTest:public CppUnit::TestFixture {
   CPPUNIT_TEST_SUITE(ProtocolDetectorTest);
   CPPUNIT_TEST(testIsStreamProtocol);
   CPPUNIT_TEST(testGuessTorrentFile);
+  CPPUNIT_TEST(testGuessTorrentMagnet);
   CPPUNIT_TEST(testGuessMetalinkFile);
   CPPUNIT_TEST_SUITE_END();
 public:
@@ -20,6 +21,7 @@ public:
 
   void testIsStreamProtocol();
   void testGuessTorrentFile();
+  void testGuessTorrentMagnet();
   void testGuessMetalinkFile();
 };
 
@@ -42,6 +44,15 @@ void ProtocolDetectorTest::testGuessTorrentFile()
   CPPUNIT_ASSERT(!detector.guessTorrentFile("test.xml"));
 }
 
+void ProtocolDetectorTest::testGuessTorrentMagnet()
+{
+  ProtocolDetector detector;
+  CPPUNIT_ASSERT(detector.guessTorrentMagnet("magnet:?xt=urn:btih:abcdef"));
+  CPPUNIT_ASSERT(detector.guessTorrentMagnet
+		 ("magnet:?dn=name&xt=urn:btih:abcdef"));
+  CPPUNIT_ASSERT(!detector.guessTorrentMagnet("magnet:?"));
+}
+
 void ProtocolDetectorTest::testGuessMetalinkFile()
 {
   ProtocolDetector detector;