/* */ #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 "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" #include "SessionSerializer.h" #include "MessageDigest.h" #include "message_digest_helper.h" #include "OpenedFileCounter.h" #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 char VLB_TRUE[] = "true"; const char VLB_FALSE[] = "false"; const char VLB_ACTIVE[] = "active"; const char VLB_WAITING[] = "waiting"; const char VLB_PAUSED[] = "paused"; const char VLB_REMOVED[] = "removed"; const char VLB_ERROR[] = "error"; const char VLB_COMPLETE[] = "complete"; const char VLB_USED[] = "used"; const char VLB_ZERO[] = "0"; const char KEY_GID[] = "gid"; const char KEY_ERROR_CODE[] = "errorCode"; const char KEY_STATUS[] = "status"; const char KEY_TOTAL_LENGTH[] = "totalLength"; const char KEY_COMPLETED_LENGTH[] = "completedLength"; const char KEY_DOWNLOAD_SPEED[] = "downloadSpeed"; const char KEY_UPLOAD_SPEED[] = "uploadSpeed"; const char KEY_UPLOAD_LENGTH[] = "uploadLength"; const char KEY_CONNECTIONS[] = "connections"; const char KEY_BITFIELD[] = "bitfield"; const char KEY_PIECE_LENGTH[] = "pieceLength"; const char KEY_NUM_PIECES[] = "numPieces"; const char KEY_FOLLOWED_BY[] = "followedBy"; const char KEY_BELONGS_TO[] = "belongsTo"; const char KEY_INFO_HASH[] = "infoHash"; const char KEY_NUM_SEEDERS[] = "numSeeders"; const char KEY_PEER_ID[] = "peerId"; const char KEY_IP[] = "ip"; const char KEY_PORT[] = "port"; const char KEY_AM_CHOKING[] = "amChoking"; const char KEY_PEER_CHOKING[] = "peerChoking"; const char KEY_SEEDER[] = "seeder"; const char KEY_INDEX[] = "index"; const char KEY_PATH[] = "path"; const char KEY_SELECTED[] = "selected"; const char KEY_LENGTH[] = "length"; const char KEY_URI[] = "uri"; const char KEY_CURRENT_URI[] = "currentUri"; const char KEY_VERSION[] = "version"; const char KEY_ENABLED_FEATURES[] = "enabledFeatures"; const char KEY_METHOD_NAME[] = "methodName"; const char KEY_PARAMS[] = "params"; const char KEY_SESSION_ID[] = "sessionId"; const char KEY_FILES[] = "files"; const char KEY_DIR[] = "dir"; const char KEY_URIS[] = "uris"; const char KEY_BITTORRENT[] = "bittorrent"; const char KEY_INFO[] = "info"; const char KEY_NAME[] = "name"; const char KEY_ANNOUNCE_LIST[] = "announceList"; const char KEY_COMMENT[] = "comment"; const char KEY_CREATION_DATE[] = "creationDate"; const char KEY_MODE[] = "mode"; const char KEY_SERVERS[] = "servers"; const char KEY_NUM_WAITING[] = "numWaiting"; const char KEY_NUM_STOPPED[] = "numStopped"; const char KEY_NUM_ACTIVE[] = "numActive"; const char KEY_NUM_STOPPED_TOTAL[] = "numStoppedTotal"; } // namespace namespace { std::unique_ptr createGIDResponse(a2_gid_t gid) { return String::g(GroupId::toHex(gid)); } } // namespace namespace { std::unique_ptr createOKResponse() { return String::g("OK"); } } // namespace namespace { std::unique_ptr addRequestGroup(const std::shared_ptr& 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 { 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); if(str->s().size() > sizeof(a2_gid_t)*2) { throw DL_ABORT_EX(fmt("Invalid GID %s", str->s().c_str())); } a2_gid_t n; switch(GroupId::expandUnique(n, str->s().c_str())) { case GroupId::ERR_NOT_UNIQUE: throw DL_ABORT_EX(fmt("GID %s is not unique", str->s().c_str())); case GroupId::ERR_NOT_FOUND: throw DL_ABORT_EX(fmt("GID %s is not found", str->s().c_str())); case GroupId::ERR_INVALID: throw DL_ABORT_EX(fmt("Invalid GID %s", str->s().c_str())); } return n; } } // namespace namespace { template void extractUris(OutputIterator out, const List* src) { if(src) { for(auto & elem : *src) { const String* uri = downcast(elem); if(uri) { out++ = uri->s(); } } } } } // namespace std::unique_ptr 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."); } auto requestOption = std::make_shared