diff --git a/ChangeLog b/ChangeLog index e15ce2bb..93c5d744 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-11-22 Tatsuhiro Tsujikawa + + Added BitTorrent magnet link support to ProtocolDetector. + * src/ProtocolDetector.cc + * src/ProtocolDetector.h + * test/ProtocolDetectorTest.cc + 2009-11-22 Tatsuhiro Tsujikawa Don't inject validator into BtMessage in metadataGetMode because diff --git a/src/ProtocolDetector.cc b/src/ProtocolDetector.cc index 9937ac03..5af33d8f 100644 --- a/src/ProtocolDetector.cc +++ b/src/ProtocolDetector.cc @@ -33,12 +33,15 @@ */ /* copyright --> */ #include "ProtocolDetector.h" -#include "Request.h" -#include "File.h" + #include #include #include +#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()) { diff --git a/src/ProtocolDetector.h b/src/ProtocolDetector.h index a1a6806e..9bc48823 100644 --- a/src/ProtocolDetector.h +++ b/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:... + 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; diff --git a/test/ProtocolDetectorTest.cc b/test/ProtocolDetectorTest.cc index 7b6ccd99..822bd3b8 100644 --- a/test/ProtocolDetectorTest.cc +++ b/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;