|
|
|
@ -53,27 +53,38 @@ int main(int argc, char *argv[]) { |
|
|
|
|
|
|
|
|
|
|
|
http_server.resource["^/session(\\?.*)?\\/?$"]["PATCH"]=[&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { |
|
|
|
http_server.resource["^/session(\\?.*)?\\/?$"]["PATCH"]=[&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { |
|
|
|
auto response = http::response(); |
|
|
|
auto response = http::response(); |
|
|
|
|
|
|
|
if (!session.is_valid()) { |
|
|
|
auto content_type=req->header.find("Content-Type"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto json=util::json::parse(req->content); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (session.is_valid()) { |
|
|
|
|
|
|
|
const auto settings = session.get_settings(); |
|
|
|
|
|
|
|
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); |
|
|
|
auto response_code = http::code(http::service_unavailable); |
|
|
|
response.set_body({{response_code.first, response_code.second}}); |
|
|
|
response.set_body({{response_code.first, response_code.second}}); |
|
|
|
response.set_status(response_code.first); |
|
|
|
response.set_status(response_code.first); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
auto response_code = http::code(http::bad_request); |
|
|
|
|
|
|
|
response.set_body({{response_code.first,response_code.second}}); |
|
|
|
|
|
|
|
response.set_status(response_code.first); |
|
|
|
} |
|
|
|
} |
|
|
|
*resp << response; |
|
|
|
*resp << response; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|