/* */ #include "RpcMethodImpl.h" #include #include #include #include "Logger.h" #include "LogFactory.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 "fmt.h" #include "RpcRequest.h" #include "PieceStorage.h" #include "DownloadContext.h" #include "DiskAdaptor.h" #include "FileEntry.h" #include "prefs.h" #include "message.h" #include "FeatureConfig.h" #include "array_fun.h" #include "RpcMethodFactory.h" #include "RpcResponse.h" #include "SegmentMan.h" #include "TimedHaltCommand.h" #include "PeerStat.h" #include "Base64.h" #include "BitfieldMan.h" #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigest.h" # include "message_digest_helper.h" #endif // ENABLE_MESSAGE_DIGEST #ifdef ENABLE_BITTORRENT # include "bittorrent_helper.h" # include "BtRegistry.h" # include "PeerStorage.h" # include "Peer.h" # include "BtRuntime.h" # include "BtAnnounce.h" #endif // ENABLE_BITTORRENT namespace aria2 { namespace rpc { namespace { const SharedHandle VLB_TRUE = String::g("true"); const SharedHandle VLB_FALSE = String::g("false"); const SharedHandle VLB_OK = String::g("OK"); const SharedHandle VLB_ACTIVE = String::g("active"); const SharedHandle VLB_WAITING = String::g("waiting"); const SharedHandle VLB_PAUSED = String::g("paused"); const SharedHandle VLB_REMOVED = String::g("removed"); const SharedHandle VLB_ERROR = String::g("error"); const SharedHandle VLB_COMPLETE = String::g("complete"); const SharedHandle VLB_USED = String::g("used"); const SharedHandle VLB_ZERO = String::g("0"); const std::string KEY_GID = "gid"; const std::string KEY_ERROR_CODE = "errorCode"; const std::string KEY_STATUS = "status"; const std::string KEY_TOTAL_LENGTH = "totalLength"; const std::string KEY_COMPLETED_LENGTH = "completedLength"; const std::string KEY_DOWNLOAD_SPEED = "downloadSpeed"; const std::string KEY_UPLOAD_SPEED = "uploadSpeed"; const std::string KEY_UPLOAD_LENGTH = "uploadLength"; const std::string KEY_CONNECTIONS = "connections"; const std::string KEY_BITFIELD = "bitfield"; const std::string KEY_PIECE_LENGTH = "pieceLength"; const std::string KEY_NUM_PIECES = "numPieces"; const std::string KEY_FOLLOWED_BY = "followedBy"; const std::string KEY_BELONGS_TO = "belongsTo"; const std::string KEY_INFO_HASH = "infoHash"; const std::string KEY_NUM_SEEDERS = "numSeeders"; const std::string KEY_PEER_ID = "peerId"; const std::string KEY_IP = "ip"; const std::string KEY_PORT = "port"; const std::string KEY_AM_CHOKING = "amChoking"; const std::string KEY_PEER_CHOKING = "peerChoking"; const std::string KEY_SEEDER = "seeder"; const std::string KEY_INDEX = "index"; const std::string KEY_PATH = "path"; const std::string KEY_SELECTED = "selected"; const std::string KEY_LENGTH = "length"; const std::string KEY_URI = "uri"; const std::string KEY_CURRENT_URI = "currentUri"; const std::string KEY_VERSION = "version"; const std::string KEY_ENABLED_FEATURES = "enabledFeatures"; const std::string KEY_METHOD_NAME = "methodName"; const std::string KEY_PARAMS = "params"; const std::string KEY_SESSION_ID = "sessionId"; const std::string KEY_FILES = "files"; const std::string KEY_DIR = "dir"; const std::string KEY_URIS = "uris"; const std::string KEY_BITTORRENT = "bittorrent"; const std::string KEY_INFO = "info"; const std::string KEY_NAME = "name"; const std::string KEY_ANNOUNCE_LIST = "announceList"; const std::string KEY_COMMENT = "comment"; const std::string KEY_CREATION_DATE = "creationDate"; const std::string KEY_MODE = "mode"; const std::string KEY_SERVERS = "servers"; const std::string KEY_NUM_WAITING = "numWaiting"; const std::string KEY_NUM_STOPPED = "numStopped"; const std::string KEY_NUM_ACTIVE = "numActive"; } // namespace namespace { SharedHandle createGIDResponse(a2_gid_t gid) { return String::g(util::itos(gid)); } } // namespace namespace { SharedHandle addRequestGroup(const SharedHandle& group, DownloadEngine* e, bool posGiven, int pos) { if(posGiven) { e->getRequestGroupMan()->insertReservedGroup(pos, group); } else { e->getRequestGroupMan()->addReservedGroup(group); } return createGIDResponse(group->getGID()); } } // namespace namespace { SharedHandle findRequestGroup(const SharedHandle& rgman, a2_gid_t gid) { SharedHandle group = rgman->findRequestGroup(gid); if(!group) { group = rgman->findReservedGroup(gid); } return group; } } // namespace namespace { bool checkPosParam(const Integer* posParam) { if(posParam) { if(posParam->i() >= 0) { return true; } else { throw DL_ABORT_EX("Position must be greater than or equal to 0."); } } return false; } } // namespace namespace { a2_gid_t str2Gid(const String* str) { assert(str); return util::parseLLInt(str->s().begin(), str->s().end()); } } // namespace namespace { template void extractUris(OutputIterator out, const List* src) { if(src) { for(List::ValueType::const_iterator i = src->begin(), eoi = src->end(); i != eoi; ++i) { const String* uri = downcast(*i); if(uri) { out++ = uri->s(); } } } } } // namespace SharedHandle AddUriRpcMethod::process (const RpcRequest& req, DownloadEngine* e) { const List* urisParam = checkRequiredParam(req, 0); const Dict* optsParam = checkParam(req, 1); const Integer* posParam = checkParam(req, 2); std::vector uris; extractUris(std::back_inserter(uris), urisParam); if(uris.empty()) { throw DL_ABORT_EX("URI is not provided."); } SharedHandle