From 7a04c0eb66b3ccedd27c88e83f7d7edecd62a55f Mon Sep 17 00:00:00 2001 From: Seva Date: Thu, 16 May 2019 15:19:26 -0400 Subject: [PATCH] dhtproxy: rewrite getFiltered with restinio --- include/opendht/dht_proxy_server.h | 5 +- src/dht_proxy_server.cpp | 81 +++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index 987f8c82..c64eacf1 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -192,7 +192,7 @@ private: * @param session */ RequestStatus put(restinio::request_handle_t request, - restinio::router::route_params_t params); + restinio::router::route_params_t params); void cancelPut(const InfoHash& key, Value::Id vid); @@ -230,7 +230,8 @@ private: * On error: HTTP 503, body: {"err":"xxxx"} * @param session */ - //void getFiltered(const std::shared_ptr& session) const; + RequestStatus getFiltered(restinio::request_handle_t request, + restinio::router::route_params_t params); /** * Respond allowed Methods diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index 07ecbf02..765c97cd 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -195,8 +195,6 @@ DhtProxyServer::createRestRouter() using namespace std::placeholders; auto router = std::make_unique(); router->http_get("/", std::bind(&DhtProxyServer::getNodeInfo, this, _1, _2)); - router->add_handler(restinio::http_method_t::http_options, - "/:hash", std::bind(&DhtProxyServer::options, this, _1, _2)); router->http_get("/stats", std::bind(&DhtProxyServer::getStats, this, _1, _2)); router->http_get("/:hash", std::bind(&DhtProxyServer::get, this, _1, _2)); router->http_post("/:hash", std::bind(&DhtProxyServer::put, this, _1, _2)); @@ -206,6 +204,9 @@ DhtProxyServer::createRestRouter() router->add_handler(restinio::http_method_t::http_unsubscribe, "/:hash", std::bind(&DhtProxyServer::unsubscribe, this, _1, _2)); #endif //OPENDHT_PUSH_NOTIFICATIONS + router->add_handler(restinio::http_method_t::http_options, + "/:hash", std::bind(&DhtProxyServer::options, this, _1, _2)); + router->http_get("/:hash/:value", std::bind(&DhtProxyServer::getFiltered, this, _1, _2)); return router; } @@ -281,16 +282,22 @@ DhtProxyServer::get(restinio::request_handle_t request, auto response = std::make_shared( this->initHttpResponse(request->create_response())); response->flush(); - - dht_->get(infoHash, [this, response](const dht::Sp& value){ - auto output = Json::writeString(jsonBuilder_, value->toJson()) + "\n"; - response->append_chunk(output); - response->flush(); - return true; - }, - [response] (bool /*ok*/){ - response->done(); - }); + try { + dht_->get(infoHash, [this, response](const dht::Sp& value){ + auto output = Json::writeString(jsonBuilder_, value->toJson()) + "\n"; + response->append_chunk(output); + response->flush(); + return true; + }, + [response] (bool /*ok*/){ + response->done(); + }); + } catch (const std::exception& e) { + auto response = this->initHttpResponse( + request->create_response(restinio::status_internal_server_error())); + response.set_body(this->RESP_MSG_INTERNAL_SERVER_ERRROR); + return response.done(); + } return restinio::request_handling_status_t::accepted; } @@ -310,7 +317,8 @@ struct DhtProxyServer::PushListener { }; RequestStatus -subscribe(restinio::request_handle_t request, restinio::router::route_params_t params) +DhtProxyServer::subscribe(restinio::request_handle_t request, + restinio::router::route_params_t params) { requestNum_++; @@ -443,7 +451,8 @@ subscribe(restinio::request_handle_t request, restinio::router::route_params_t p } RequestStatus -unsubscribe(restinio::request_handle_t request, restinio::router::route_params_t params) +DhtProxyServer::unsubscribe(restinio::request_handle_t request, + restinio::router::route_params_t params) { requestNum_++; @@ -616,9 +625,6 @@ DhtProxyServer::put(restinio::request_handle_t request, } dht_->put(infoHash, value, [this, request, value](bool ok){ if (ok){ - Json::StreamWriterBuilder wbuilder; - wbuilder["commentStyle"] = "None"; - wbuilder["indentation"] = ""; auto output = Json::writeString(jsonBuilder_, value->toJson()) + "\n"; auto response = this->initHttpResponse(request->create_response()); response.append_body(output); @@ -672,7 +678,46 @@ DhtProxyServer::options(restinio::request_handle_t request, return response.done(); } -//DhtProxyServer::getFiltered() +RequestStatus +DhtProxyServer::getFiltered(restinio::request_handle_t request, + restinio::router::route_params_t params) +{ + requestNum_++; + auto value = params["value"].to_string(); + dht::InfoHash infoHash(params["hash"].to_string()); + if (!infoHash) + infoHash = dht::InfoHash::get(params["hash"].to_string()); + + if (!dht_){ + auto response = this->initHttpResponse( + request->create_response(restinio::status_service_unavailable())); + response.set_body(this->RESP_MSG_SERVICE_UNAVAILABLE); + return response.done(); + } + + auto response = std::make_shared( + this->initHttpResponse(request->create_response())); + response->flush(); + try { + dht_->get(infoHash, [this, response](const dht::Sp& value){ + auto output = Json::writeString(jsonBuilder_, value->toJson()) + "\n"; + response->append_chunk(output); + response->flush(); + return true; + }, + [response] (bool /*ok*/){ + response->done(); + }, + {}, value + ); + } catch (const std::exception& e) { + auto response = this->initHttpResponse( + request->create_response(restinio::status_internal_server_error())); + response.set_body(this->RESP_MSG_INTERNAL_SERVER_ERRROR); + return response.done(); + } + return restinio::request_handling_status_t::accepted; +} void DhtProxyServer::removeClosedListeners(bool testSession)