diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 6e0bdb6..2cf9b78 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -3,9 +3,12 @@ #include #include -#include #include +#include + +namespace tr { +namespace torrent { template static nlohmann::json to_json(torrent_t torrent) { std::stringstream ss; // TODO optimize @@ -23,8 +26,8 @@ static nlohmann::json to_json(torrent_t torrent) { {"down_limit", torrent.download_limit()}, {"name", status.name}}; } +} -namespace tr { namespace session { namespace torrents { namespace id { @@ -42,10 +45,14 @@ void get(torrent_session &session, response resp, request req) { return respond(http::service_unavailable); } - std::string hash = req->path.substr(18, req->path.size() - 18); + const auto hash = req->path.substr(18, req->path.size() - 18); //TODO hacky + const auto torrent = session.find_torrent(util::sha1::string(hash)); + + if (!torrent.is_valid()) { + return respond(http::bad_request); + } - http_response.set_body(nlohmann::json::object( - {{"info_hash", hash}})); + http_response.set_body(torrent::to_json(torrent)); http_response.set_status(http::ok); *resp << http_response; } diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 187aa8d..3f513b6 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -26,7 +26,7 @@ void get(torrent_session &session, response resp, request req) { auto torrents_json = nlohmann::json::array(); for (auto &torrent : torrents) { if (torrent.is_valid()) { - torrents_json.push_back(to_json(torrent)); + torrents_json.push_back(tr::torrent::to_json(torrent)); } } response_object["torrents"] = torrents_json; @@ -82,13 +82,8 @@ void del(torrent_session &session, response resp, request req) { } options = remove_files; } - - std::string sha1_hash_in = obj; - libtorrent::sha1_hash sha1_hash; - std::stringstream ss; - ss << sha1_hash_in; - ss >> sha1_hash; - auto handle = session.find_torrent(sha1_hash); + + const auto handle = session.find_torrent(util::sha1::string(obj)); if (handle.is_valid()) { session.remove_torrent(handle, options); diff --git a/toREST/include/util.hpp b/toREST/include/util.hpp index 8528700..3f6787d 100644 --- a/toREST/include/util.hpp +++ b/toREST/include/util.hpp @@ -2,6 +2,7 @@ #define _TR_UTIL_HPP_ #include +#include #include namespace util { @@ -39,6 +40,22 @@ public: } }; +struct sha1 { + static auto string(const std::string &hex_str) { + libtorrent::sha1_hash sha1_hash; + std::stringstream ss; + ss << hex_str; + ss >> sha1_hash; + return sha1_hash; + }; + static auto info_hash(const libtorrent::sha1_hash &info_hash) { + std::stringstream ss; + ss << info_hash; + std::string res = ss.str(); + return res; + }; +}; + class json { public: /*! @brief Wrapper for nlohmann::json::parse. If the parse fails, result.is_null() will be */ diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 79ea136..9b79ab7 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -20,16 +20,16 @@ class TestTorrent { public: TestTorrent(const libtorrent::add_torrent_params ¶ms); TestTorrent() {} - bool is_valid(); + bool is_valid() const; libtorrent::sha1_hash info_hash() const; TorrentStatus status(int type = 0); int query_name = 1; int query_save_path = 2; bool valid = true; int upload_limit_ = 0; - int upload_limit(); + int upload_limit() const; int download_limit_ = 0; - int download_limit(); + int download_limit() const; libtorrent::sha1_hash hash_; TorrentStatus status_; }; diff --git a/toREST/tests/stubs/TorrentContext.cpp b/toREST/tests/stubs/TorrentContext.cpp index 95df1fb..dfd398c 100644 --- a/toREST/tests/stubs/TorrentContext.cpp +++ b/toREST/tests/stubs/TorrentContext.cpp @@ -4,15 +4,15 @@ TorrentStatus TestTorrent::status(int type) { return status_; } -bool TestTorrent::is_valid() { +bool TestTorrent::is_valid() const { return valid; } -int TestTorrent::upload_limit() { +int TestTorrent::upload_limit() const { return upload_limit_; } -int TestTorrent::download_limit() { +int TestTorrent::download_limit() const { return download_limit_; } diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index e44e036..b6c0e00 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -23,7 +23,7 @@ SCENARIO("We are running a GET /session/torrents/id resource") { torrent_session.theTorrentExists(); THEN("the response should be a json representation of the torrent") { tr::session::torrents::id::get(torrent_session, response, request); - // CommonResponse::ok(response,{torrent_json}); + CommonResponse::ok(response,{torrent_json}); } } }