summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-11-28 09:58:49 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-11-28 09:58:49 +0000
commit339795311bd3d07da1d49fb2c5c2e7efa8833922 (patch)
tree21c4960e2cb6a04933f70d784351d15c64223605 /src
parent96205abc67e5f425e3bad997959493ba54d83190 (diff)
2009-11-28 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Accept BitTorrent Magnet URI in Metalink resource type bittorrent. Rewritten UTMetadataPostDownloadHandler. To create torrent data from metadata, use new metadata2Torrent() function. * src/BtDependency.cc * src/UTMetadataPostDownloadHandler.cc * src/bittorrent_helper.cc * src/bittorrent_helper.h * test/BittorrentHelperTest.cc * test/BtDependencyTest.cc
Diffstat (limited to 'src')
-rw-r--r--src/BtDependency.cc11
-rw-r--r--src/UTMetadataPostDownloadHandler.cc29
-rw-r--r--src/bittorrent_helper.cc15
-rw-r--r--src/bittorrent_helper.h5
4 files changed, 33 insertions, 27 deletions
diff --git a/src/BtDependency.cc b/src/BtDependency.cc
index 6041a9ef..270e27ca 100644
--- a/src/BtDependency.cc
+++ b/src/BtDependency.cc
@@ -71,8 +71,15 @@ bool BtDependency::resolve()
dependee->getPieceStorage()->getDiskAdaptor();
diskAdaptor->openExistingFile();
std::string content = util::toString(diskAdaptor);
- bittorrent::loadFromMemory
- (content, context, File(dependee->getFirstFilePath()).getBasename());
+ if(dependee->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
+ const BDE& attrs =
+ dependee->getDownloadContext()->getAttribute(bittorrent::BITTORRENT);
+ bittorrent::loadFromMemory
+ (bittorrent::metadata2Torrent(content, attrs), context, "default");
+ } else {
+ bittorrent::loadFromMemory
+ (content, context, File(dependee->getFirstFilePath()).getBasename());
+ }
if(context->getFileEntries().size() !=
_dependant->getDownloadContext()->getFileEntries().size()) {
throw DL_ABORT_EX("The number of file in torrent doesn't match to"
diff --git a/src/UTMetadataPostDownloadHandler.cc b/src/UTMetadataPostDownloadHandler.cc
index 90ced69f..12e0f9dc 100644
--- a/src/UTMetadataPostDownloadHandler.cc
+++ b/src/UTMetadataPostDownloadHandler.cc
@@ -45,6 +45,7 @@
#include "a2functional.h"
#include "DiskAdaptor.h"
#include "PieceStorage.h"
+#include "bencode.h"
namespace aria2 {
@@ -72,35 +73,13 @@ void UTMetadataPostDownloadHandler::getNextRequestGroups
{
SharedHandle<DownloadContext> dctx = requestGroup->getDownloadContext();
const BDE& attrs = dctx->getAttribute(bittorrent::BITTORRENT);
- std::string torrent =
- strconcat("d4:info",
- util::toString(requestGroup->getPieceStorage()->getDiskAdaptor()),
- "e");
+ std::string metadata =
+ util::toString(requestGroup->getPieceStorage()->getDiskAdaptor());
+ std::string torrent = bittorrent::metadata2Torrent(metadata, attrs);
try {
std::deque<SharedHandle<RequestGroup> > newRgs;
createRequestGroupForBitTorrent(newRgs, requestGroup->getOption(),
std::deque<std::string>(), torrent);
- if(attrs.containsKey(bittorrent::ANNOUNCE_LIST)) {
- for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
- newRgs.begin(); i != newRgs.end(); ++i) {
- SharedHandle<DownloadContext> newDctx = (*i)->getDownloadContext();
- if(!newDctx->hasAttribute(bittorrent::BITTORRENT)) {
- continue;
- }
- BDE& newAttrs = newDctx->getAttribute(bittorrent::BITTORRENT);
- if(attrs[bittorrent::INFO_HASH].s() !=
- newAttrs[bittorrent::INFO_HASH].s()) {
- continue;
- }
- assert(newAttrs[bittorrent::ANNOUNCE_LIST].isList());
- if(newAttrs[bittorrent::ANNOUNCE_LIST].size() == 0) {
- newAttrs[bittorrent::ANNOUNCE_LIST] =
- attrs[bittorrent::ANNOUNCE_LIST];
- }
- break;
- }
- }
-
groups.insert(groups.end(), newRgs.begin(), newRgs.end());
} catch(RecoverableException& e) {
_logger->error("Failed to parse BitTorrent metadata.", e);
diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc
index d6685756..8945eebd 100644
--- a/src/bittorrent_helper.cc
+++ b/src/bittorrent_helper.cc
@@ -904,6 +904,21 @@ void loadMagnet
dctx->setAttribute(BITTORRENT, attrs);
}
+std::string metadata2Torrent(const std::string& metadata, const BDE& attrs)
+{
+ std::string torrent = "d";
+ if(attrs.containsKey(bittorrent::ANNOUNCE_LIST)) {
+ const BDE& announceList = attrs[bittorrent::ANNOUNCE_LIST];
+ if(announceList.size() > 0) {
+ torrent += "13:announce-list";
+ torrent += bencode::encode(announceList);
+ }
+ }
+ torrent +=
+ strconcat("4:info", metadata, "e");
+ return torrent;
+}
+
} // namespace bittorrent
} // namespace aria2
diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h
index 06d26058..6893cb6b 100644
--- a/src/bittorrent_helper.h
+++ b/src/bittorrent_helper.h
@@ -230,6 +230,11 @@ void assertID
void generateRandomKey(unsigned char* key);
+// Converts attrs into torrent data. attrs must be a BDE::dict. This
+// function does not guarantee the returned string is valid torrent
+// data.
+std::string metadata2Torrent(const std::string& metadata, const BDE& attrs);
+
} // namespace bittorrent
} // namespace aria2