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
pull/1/head
Tatsuhiro Tsujikawa 2009-11-22 14:54:51 +00:00
parent c1f9032441
commit c73d235ab8
4 changed files with 33 additions and 2 deletions

View File

@ -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> 2009-11-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Don't inject validator into BtMessage in metadataGetMode because Don't inject validator into BtMessage in metadataGetMode because

View File

@ -33,12 +33,15 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "ProtocolDetector.h" #include "ProtocolDetector.h"
#include "Request.h"
#include "File.h"
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include "Request.h"
#include "File.h"
#include "util.h"
namespace aria2 { namespace aria2 {
ProtocolDetector::ProtocolDetector() {} 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 bool ProtocolDetector::guessMetalinkFile(const std::string& uri) const
{ {
if(!File(uri).isFile()) { if(!File(uri).isFile()) {

View File

@ -53,6 +53,10 @@ public:
// metainfo file, otherwise returns false. // metainfo file, otherwise returns false.
bool guessTorrentFile(const std::string& uri) const; 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 // Returns true if ProtocolDetector thinks uri is a path of Metalink XML
// file, otherwise return false. // file, otherwise return false.
bool guessMetalinkFile(const std::string& uri) const; bool guessMetalinkFile(const std::string& uri) const;

View File

@ -11,6 +11,7 @@ class ProtocolDetectorTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ProtocolDetectorTest); CPPUNIT_TEST_SUITE(ProtocolDetectorTest);
CPPUNIT_TEST(testIsStreamProtocol); CPPUNIT_TEST(testIsStreamProtocol);
CPPUNIT_TEST(testGuessTorrentFile); CPPUNIT_TEST(testGuessTorrentFile);
CPPUNIT_TEST(testGuessTorrentMagnet);
CPPUNIT_TEST(testGuessMetalinkFile); CPPUNIT_TEST(testGuessMetalinkFile);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
public: public:
@ -20,6 +21,7 @@ public:
void testIsStreamProtocol(); void testIsStreamProtocol();
void testGuessTorrentFile(); void testGuessTorrentFile();
void testGuessTorrentMagnet();
void testGuessMetalinkFile(); void testGuessMetalinkFile();
}; };
@ -42,6 +44,15 @@ void ProtocolDetectorTest::testGuessTorrentFile()
CPPUNIT_ASSERT(!detector.guessTorrentFile("test.xml")); 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() void ProtocolDetectorTest::testGuessMetalinkFile()
{ {
ProtocolDetector detector; ProtocolDetector detector;