123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2009 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
- /* copyright --> */
- #include "XmlRpcMethodImpl.h"
- #include <cassert>
- #include <algorithm>
- #include "Logger.h"
- #include "BDE.h"
- #include "DlAbortEx.h"
- #include "Option.h"
- #include "OptionParser.h"
- #include "OptionHandler.h"
- #include "DownloadEngine.h"
- #include "RequestGroup.h"
- #include "download_helper.h"
- #include "Util.h"
- #include "RequestGroupMan.h"
- #include "StringFormat.h"
- #include "XmlRpcRequest.h"
- #include "PieceStorage.h"
- #include "PeerStorage.h"
- #include "BtContext.h"
- #include "BtRegistry.h"
- #include "Peer.h"
- #include "DiskAdaptor.h"
- #include "FileEntry.h"
- #include "BtProgressInfoFile.h"
- #include "BtRuntime.h"
- #include "BtAnnounce.h"
- #include "prefs.h"
- namespace aria2 {
- namespace xmlrpc {
- static BDE createGIDResponse(int32_t gid)
- {
- return BDE(Util::itos(gid));
- }
- BDE AddUriXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isList() || params[0].empty()) {
- throw DlAbortEx("URI is not provided.");
- }
- std::deque<std::string> uris;
- for(BDE::List::const_iterator i = params[0].listBegin();
- i != params[0].listEnd(); ++i) {
- if((*i).isString()) {
- uris.push_back((*i).s());
- }
- }
- SharedHandle<Option> requestOption(new Option(*e->option));
- if(params.size() > 1 && params[1].isDict()) {
- gatherRequestOption(requestOption, params[1]);
- }
- std::deque<SharedHandle<RequestGroup> > result;
- createRequestGroupForUri(result, requestOption, uris,
- /* ignoreForceSeq = */ true,
- /* ignoreNonURI = */ true);
- if(!result.empty()) {
- e->_requestGroupMan->addReservedGroup(result.front());
- return createGIDResponse(result.front()->getGID());
- } else {
- throw DlAbortEx("No URI to download.");
- }
- }
- BDE AddTorrentXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("Torrent data is not provided.");
- }
-
- std::deque<std::string> uris;
- if(params.size() > 1 && params[1].isList()) {
- for(BDE::List::const_iterator i = params[1].listBegin();
- i != params[1].listEnd(); ++i) {
- if((*i).isString()) {
- uris.push_back((*i).s());
- }
- }
- }
- SharedHandle<Option> requestOption(new Option(*e->option));
- if(params.size() > 2 && params[2].isDict()) {
- gatherRequestOption(requestOption, params[2]);
- }
- std::deque<SharedHandle<RequestGroup> > result;
- createRequestGroupForBitTorrent(result, requestOption,
- uris,
- params[0].s());
- if(!result.empty()) {
- e->_requestGroupMan->addReservedGroup(result.front());
- return createGIDResponse(result.front()->getGID());
- } else {
- throw DlAbortEx("No Torrent to download.");
- }
- }
- BDE AddMetalinkXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("Metalink data is not provided.");
- }
-
- SharedHandle<Option> requestOption(new Option(*e->option));
- if(params.size() > 1 && params[1].isDict()) {
- gatherRequestOption(requestOption, params[1]);
- }
- std::deque<SharedHandle<RequestGroup> > result;
- createRequestGroupForMetalink(result, requestOption, params[0].s());
- if(!result.empty()) {
- e->_requestGroupMan->addReservedGroup(result);
- BDE gids = BDE::list();
- for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
- result.begin(); i != result.end(); ++i) {
- gids << BDE(Util::itos((*i)->getGID()));
- }
- return gids;
- } else {
- throw DlAbortEx("No files to download.");
- }
- }
- BDE RemoveXmlRpcMethod::process(const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("GID is not provided.");
- }
-
- int32_t gid = Util::parseInt(params[0].s());
- SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
- if(group.isNull()) {
- group = e->_requestGroupMan->findReservedGroup(gid);
- if(group.isNull()) {
- throw DlAbortEx
- (StringFormat("Active Download not found for GID#%d", gid).str());
- }
- if(group->isDependencyResolved()) {
- e->_requestGroupMan->removeReservedGroup(gid);
- } else {
- throw DlAbortEx
- (StringFormat("GID#%d cannot be removed now", gid).str());
- }
- } else {
- group->setHaltRequested(true, RequestGroup::USER_REQUEST);
- }
- return createGIDResponse(gid);
- }
- static void gatherProgressCommon
- (BDE& entryDict, const SharedHandle<RequestGroup>& group)
- {
- entryDict["gid"] = Util::itos(group->getGID());
- // This is "filtered" total length if --select-file is used.
- entryDict["totalLength"] = Util::uitos(group->getTotalLength());
- // This is "filtered" total length if --select-file is used.
- entryDict["completedLength"] = Util::uitos(group->getCompletedLength());
- TransferStat stat = group->calculateStat();
- entryDict["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
- entryDict["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
- entryDict["connections"] = Util::uitos(group->getNumConnection());
- SharedHandle<PieceStorage> ps = group->getPieceStorage();
- if(!ps.isNull()) {
- if(ps->getBitfieldLength() > 0) {
- entryDict["bitfield"] = Util::toHex(ps->getBitfield(),
- ps->getBitfieldLength());
- }
- }
- entryDict["pieceLength"] =
- Util::uitos(group->getDownloadContext()->getPieceLength());
- entryDict["numPieces"] =
- Util::uitos(group->getDownloadContext()->getNumPieces());
- }
- static void gatherProgressBitTorrent
- (BDE& entryDict, const SharedHandle<BtContext>& btctx)
- {
- entryDict["infoHash"] = btctx->getInfoHashAsString();
- }
- static void gatherPeer(BDE& peers, const SharedHandle<PeerStorage>& ps)
- {
- std::deque<SharedHandle<Peer> > activePeers;
- ps->getActivePeers(activePeers);
- for(std::deque<SharedHandle<Peer> >::const_iterator i =
- activePeers.begin(); i != activePeers.end(); ++i) {
- BDE peerEntry = BDE::dict();
- peerEntry["peerId"] = Util::torrentUrlencode((*i)->getPeerId(),
- PEER_ID_LENGTH);
- peerEntry["ip"] = (*i)->ipaddr;
- peerEntry["port"] = Util::uitos((*i)->port);
- peerEntry["bitfield"] = Util::toHex((*i)->getBitfield(),
- (*i)->getBitfieldLength());
- peerEntry["amChoking"] = (*i)->amChoking()?BDE("true"):BDE("false");
- peerEntry["peerChoking"] = (*i)->peerChoking()?BDE("true"):BDE("false");
- TransferStat stat = ps->getTransferStatFor(*i);
- peerEntry["downloadSpeed"] = Util::uitos(stat.getDownloadSpeed());
- peerEntry["uploadSpeed"] = Util::uitos(stat.getUploadSpeed());
- peerEntry["seeder"] = (*i)->isSeeder()?BDE("true"):BDE("false");
- peers << peerEntry;
- }
- }
- static void gatherProgress
- (BDE& entryDict, const SharedHandle<RequestGroup>& group, DownloadEngine* e)
- {
- gatherProgressCommon(entryDict, group);
- SharedHandle<BtContext> btctx =
- dynamic_pointer_cast<BtContext>(group->getDownloadContext());
- if(!btctx.isNull()) {
- gatherProgressBitTorrent(entryDict, btctx);
- }
- }
- static void gatherStoppedDownload
- (BDE& entryDict, const SharedHandle<DownloadResult>& ds)
- {
- entryDict["gid"] = Util::itos(ds->gid);
- if(ds->result == DownloadResult::IN_PROGRESS) {
- entryDict["status"] = BDE("removed");
- } else if(ds->result == DownloadResult::FINISHED) {
- entryDict["status"] = BDE("complete");
- } else {
- entryDict["status"] = BDE("error");
- }
- }
- static
- SharedHandle<RequestGroup>
- findRequestGroup(const SharedHandle<RequestGroupMan>& rgman, int32_t gid)
- {
- SharedHandle<RequestGroup> group = rgman->findRequestGroup(gid);
- if(group.isNull()) {
- group = rgman->findReservedGroup(gid);
- }
- return group;
- }
- template<typename InputIterator>
- static void createFileEntry(BDE& files, InputIterator first, InputIterator last)
- {
- size_t index = 1;
- for(; first != last; ++first, ++index) {
- BDE entry = BDE::dict();
- entry["index"] = Util::uitos(index);
- entry["path"] = (*first)->getPath();
- entry["selected"] = (*first)->isRequested()?BDE("true"):BDE("false");
- entry["length"] = Util::uitos((*first)->getLength());
- files << entry;
- }
- }
- BDE GetFilesXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("GID is not provided.");
- }
-
- int32_t gid = Util::parseInt(params[0].s());
- BDE files = BDE::list();
- SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
- if(group.isNull()) {
- SharedHandle<DownloadResult> dr =
- e->_requestGroupMan->findDownloadResult(gid);
- if(dr.isNull()) {
- throw DlAbortEx
- (StringFormat("No file data is available for GID#%d", gid).str());
- } else {
- createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end());
- }
- } else {
- std::deque<SharedHandle<FileEntry> > fileEntries =
- group->getDownloadContext()->getFileEntries();
- createFileEntry(files, fileEntries.begin(), fileEntries.end());
- }
- return files;
- }
- BDE GetUrisXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("GID is not provided.");
- }
-
- int32_t gid = Util::parseInt(params[0].s());
- SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
- if(group.isNull()) {
- throw DlAbortEx
- (StringFormat("No URI data is available for GID#%d", gid).str());
- }
- BDE uriList = BDE::list();
- std::deque<std::string> uris;
- group->getURIs(uris);
- for(std::deque<std::string>::const_iterator i = uris.begin(); i != uris.end();
- ++i) {
- BDE entry = BDE::dict();
- entry["uri"] = *i;
- uriList << entry;
- }
- return uriList;
- }
- BDE GetPeersXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("GID is not provided.");
- }
-
- int32_t gid = Util::parseInt(params[0].s());
- SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
- if(group.isNull()) {
- throw DlAbortEx
- (StringFormat("No peer data is available for GID#%d", gid).str());
- }
- BDE peers = BDE::list();
- SharedHandle<BtContext> btctx =
- dynamic_pointer_cast<BtContext>(group->getDownloadContext());
- if(!btctx.isNull()) {
- SharedHandle<BtRegistry> btreg = e->getBtRegistry();
- SharedHandle<PeerStorage> ps =
- btreg->getPeerStorage(btctx->getInfoHashAsString());
- if(!ps.isNull()) {
- BDE entry = BDE::dict();
- gatherPeer(peers, ps);
- }
- }
- return peers;
- }
- BDE TellStatusXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("GID is not provided.");
- }
-
- int32_t gid = Util::parseInt(params[0].s());
- SharedHandle<RequestGroup> group = e->_requestGroupMan->findRequestGroup(gid);
- BDE entryDict = BDE::dict();
- if(group.isNull()) {
- group = e->_requestGroupMan->findReservedGroup(gid);
- if(group.isNull()) {
- SharedHandle<DownloadResult> ds =
- e->_requestGroupMan->findDownloadResult(gid);
- if(ds.isNull()) {
- throw DlAbortEx
- (StringFormat("No such download for GID#%d", gid).str());
- }
- gatherStoppedDownload(entryDict, ds);
- } else {
- entryDict["status"] = BDE("waiting");
- gatherProgress(entryDict, group, e);
- }
- } else {
- entryDict["status"] = BDE("active");
- gatherProgress(entryDict, group, e);
- }
- return entryDict;
- }
- BDE TellActiveXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- BDE list = BDE::list();
- const std::deque<SharedHandle<RequestGroup> >& groups =
- e->_requestGroupMan->getRequestGroups();
- for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
- groups.begin(); i != groups.end(); ++i) {
- BDE entryDict = BDE::dict();
- entryDict["status"] = BDE("active");
- gatherProgress(entryDict, *i, e);
- list << entryDict;
- }
- return list;
- }
- BDE PurgeDownloadResultXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- e->_requestGroupMan->purgeDownloadResult();
- return BDE("OK");
- }
- BDE ChangeOptionXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isString()) {
- throw DlAbortEx("GID is not provided.");
- }
- int32_t gid = Util::parseInt(params[0].s());
- SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
- if(group.isNull()) {
- throw DlAbortEx
- (StringFormat("Cannot change option for GID#%d", gid).str());
- }
- SharedHandle<Option> option(new Option(*group->getOption().get()));
- if(params.size() > 1 && params[1].isDict()) {
- gatherChangeableOption(option, params[1]);
- }
- if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
- group->setMaxDownloadSpeedLimit(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
- }
- if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
- group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
- }
- return BDE("OK");
- }
- BDE ChangeGlobalOptionXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- const BDE& params = req._params;
- assert(params.isList());
- if(params.empty() || !params[0].isDict()) {
- return BDE("OK");
- }
- SharedHandle<Option> option(new Option(*e->option));
- gatherChangeableGlobalOption(option, params[0]);
- if(option->defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) {
- e->_requestGroupMan->setMaxOverallDownloadSpeedLimit
- (option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
- }
- if(option->defined(PREF_MAX_OVERALL_UPLOAD_LIMIT)) {
- e->_requestGroupMan->setMaxOverallUploadSpeedLimit
- (option->getAsInt(PREF_MAX_OVERALL_UPLOAD_LIMIT));
- }
- return BDE("OK");
- }
- BDE NoSuchMethodXmlRpcMethod::process
- (const XmlRpcRequest& req, DownloadEngine* e)
- {
- throw DlAbortEx
- (StringFormat("No such method: %s", req._methodName.c_str()).str());
- }
- } // namespace xmlrpc
- } // namespace aria2
|