You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
3.3 KiB

#include <http.hpp>
#include <util.hpp>
namespace tr{
namespace session {
template<class torrent_session, class request, class response>
void get(torrent_session&session,response resp,request req){
auto http_response = http::response();
if (session.is_valid()) {
const auto settings = session.get_settings();
http_response.set_body({
{ "paused", session.is_paused() },
{ "port", session.listen_port() },
{ "dht_enabled", session.is_dht_running() },
{ "down_limit", settings.get_int(settings.download_rate_limit) },
{ "up_limit", settings.get_int(settings.upload_rate_limit) },
{ "torrents", session.get_torrents().size() }, //TODO Optimize
// { "default_download_dir", default_download_dir.filename().string() },
// { "root_dir", root_dir.string() },
});
} else {
auto response_code = http::code(http::service_unavailable);
http_response.set_body({{"code", response_code.first}, { "status", response_code.second}});
http_response.set_status(response_code.first);
}
*resp << http_response;
}
template<class torrent_session, class request, class response>
void patch(torrent_session&session,response resp,request req){
auto http_response = http::response();
if (!session.is_valid()) {
auto response_code = http::code(http::service_unavailable);
http_response.set_body({{"code",response_code.first},{"status", response_code.second}});
http_response.set_status(response_code.first);
} else {
const auto json=util::json::parse(req->content);
if(json.is_object() && !json.is_null()) {
if(util::json::get("paused",json,session.is_paused()))
session.pause();
else
session.resume();
auto settings = session.get_settings();
const auto new_listen_port=util::json::get("port",json,session.listen_port());
if(new_listen_port!=session.listen_port())
settings.set_str(settings.listen_interfaces,"0.0.0.0:"+std::to_string(new_listen_port));
const auto new_dht_enabled=util::json::get("dht_enabled",json,session.is_dht_running());
if(new_dht_enabled!=session.is_dht_running())
settings.set_bool(settings.enable_dht,new_dht_enabled);
auto old_limit=settings.get_int(settings.download_rate_limit);
auto new_limit=util::json::get("down_limit",json,old_limit);
if(new_limit!=old_limit)
settings.set_int(settings.download_rate_limit,new_limit);
old_limit=settings.get_int(settings.upload_rate_limit);
new_limit=util::json::get("up_limit",json,old_limit);
if(new_limit!=old_limit)
settings.set_int(settings.upload_rate_limit,new_limit);
session.apply_settings(settings);
auto response_code = http::code(http::accepted);
http_response.set_body({{"code",response_code.first},{"status", response_code.second}});
} else {
auto response_code = http::code(http::bad_request);
http_response.set_body({{"code",response_code.first},{"status", response_code.second}});
http_response.set_status(response_code.first);
}
}
*resp << http_response;
}
}
}