diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index 8ff176db..5f2f5993 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -77,10 +77,13 @@ class opendht_logger_t }; using RestRouter = restinio::router::express_router_t<>; -using RestRouterTraits = restinio::traits_t< - restinio::asio_timer_manager_t, - opendht_logger_t, - RestRouter>; +struct RestRouterTraits : public restinio::default_traits_t +{ + using timer_manager_t = restinio::asio_timer_manager_t; + using http_methods_mapper_t = http::custom_http_methods_t; + using logger_t = opendht_logger_t; + using request_handler_t = RestRouter; +}; using ServerSettings = restinio::run_on_thread_pool_settings_t; using RequestStatus = restinio::request_handling_status_t; using ResponseByParts = restinio::chunked_output_t; diff --git a/include/opendht/http.h b/include/opendht/http.h index 00ba868c..8c25b18b 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -78,4 +78,34 @@ private: uint16_t connId_ {1}; }; +/* Custom HTTP-methods for RESTinio > 0.5.0. + * https://github.com/Stiffstream/restinio/issues/26 + */ +constexpr const restinio::http_method_id_t method_listen{ + HTTP_LISTEN, "LISTEN" }; +constexpr const restinio::http_method_id_t method_stats{ + HTTP_STATS, "STATS" }; +constexpr const restinio::http_method_id_t method_sign{ + HTTP_SIGN, "SIGN" }; +constexpr const restinio::http_method_id_t method_encrypt{ + HTTP_ENCRYPT, "ENCRYPT" }; + +struct custom_http_methods_t +{ + static constexpr restinio::http_method_id_t + from_nodejs( int m ) noexcept + { + if( m == method_listen.raw_id() ) + return method_listen; + else if( m == method_stats.raw_id() ) + return method_stats; + else if( m == method_sign.raw_id() ) + return method_sign; + else if( m == method_encrypt.raw_id() ) + return method_encrypt; + else + return restinio::default_http_methods_t::from_nodejs( m ); + } +}; + } diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp index b8b78ddc..5138c849 100644 --- a/src/dht_proxy_client.cpp +++ b/src/dht_proxy_client.cpp @@ -247,7 +247,7 @@ DhtProxyClient::get(const InfoHash& key, GetCallback cb, DoneCallback donecb, Va DHT_LOG.d(key, "[search %s]: get", key.to_c_str()); restinio::http_request_header_t header; header.request_target("/" + key.toString()); - header.method(restinio::http_method_t::http_get); + header.method(restinio::http_method_get()); auto header_fields = this->initHeaderFields(); auto request = httpClient_.create_request(header, header_fields, restinio::http_connection_header_t::keep_alive, ""/*body*/); @@ -362,7 +362,7 @@ DhtProxyClient::doPut(const InfoHash& key, Sp val, DoneCallback cb, time_ DHT_LOG.d(key, "[search %s] performing put of %s", key.to_c_str(), val->toString().c_str()); restinio::http_request_header_t header; header.request_target("/" + key.toString()); - header.method(restinio::http_method_t::http_post); + header.method(restinio::http_method_post()); auto header_fields = this->initHeaderFields(); auto json = val->toJson(); @@ -703,12 +703,12 @@ DhtProxyClient::listen(const InfoHash& key, ValueCallback cb, Value::Filter filt restinio::http_request_header_t header; if (deviceKey_.empty()){ // listen method = ListenMethod::LISTEN; - header.method(restinio::http_method_t::http_get); + header.method(restinio::http_method_get()); header.request_target("/" + key.toString() + "/listen"); } else { method = ListenMethod::SUBSCRIBE; - header.method(restinio::http_method_t::http_subscribe); + header.method(restinio::http_method_subscribe()); header.request_target("/" + key.toString()); } sendListen(header, /*vcb*/ cb, filter, state, method); diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index 9215dbe5..e8baca5b 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -50,7 +50,6 @@ struct DhtProxyServer::SearchPuts { constexpr const std::chrono::minutes PRINT_STATS_PERIOD {2}; constexpr const size_t IO_THREADS_MAX {64}; - DhtProxyServer::DhtProxyServer(std::shared_ptr dht, in_port_t port ,const std::string& pushServer, std::shared_ptr logger) : dht_(dht), threadPool_(new ThreadPool(IO_THREADS_MAX)), pushServer_(pushServer), httpServer_(restinio::own_io_context(), []( auto & settings ){}) @@ -208,21 +207,37 @@ DhtProxyServer::createRestRouter() using namespace std::placeholders; auto router = std::make_unique(); router->http_get("/", std::bind(&DhtProxyServer::getNodeInfo, this, _1, _2)); + // LEGACY STATS ROUTE + router->add_handler(http::custom_http_methods_t::from_nodejs(http::method_stats.raw_id()), + "/", std::bind(&DhtProxyServer::getStats, 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)); + // LEGACY LISTEN ROUTE + router->add_handler(http::custom_http_methods_t::from_nodejs(http::method_listen.raw_id()), + "/:hash", std::bind(&DhtProxyServer::listen, this, _1, _2)); + // } router->http_get("/:hash/listen", std::bind(&DhtProxyServer::listen, this, _1, _2)); #ifdef OPENDHT_PUSH_NOTIFICATIONS - router->add_handler(restinio::http_method_t::http_subscribe, + router->add_handler(restinio::http_method_subscribe(), "/:hash", std::bind(&DhtProxyServer::subscribe, this, _1, _2)); - router->add_handler(restinio::http_method_t::http_unsubscribe, + router->add_handler(restinio::http_method_unsubscribe(), "/:hash", std::bind(&DhtProxyServer::unsubscribe, this, _1, _2)); #endif //OPENDHT_PUSH_NOTIFICATIONS #ifdef OPENDHT_PROXY_SERVER_IDENTITY + // LEGACY SIGN ROUTE + router->add_handler(http::custom_http_methods_t::from_nodejs(http::method_sign.raw_id()), + "/:hash", std::bind(&DhtProxyServer::putSigned, this, _1, _2)); + // } router->http_post("/:hash/sign", std::bind(&DhtProxyServer::putSigned, this, _1, _2)); + // LEGACY ENCRYPT ROUTE + router->add_handler(http::custom_http_methods_t::from_nodejs(http::method_encrypt.raw_id()), + "/:hash", std::bind(&DhtProxyServer::putEncrypted, this, _1, _2)); + // } router->http_post("/:hash/encrypt", std::bind(&DhtProxyServer::putEncrypted, this, _1, _2)); #endif // OPENDHT_PROXY_SERVER_IDENTITY - router->add_handler(restinio::http_method_t::http_options, + router->add_handler(restinio::http_method_options(), "/:hash", std::bind(&DhtProxyServer::options, this, _1, _2)); router->http_get("/:hash/:value", std::bind(&DhtProxyServer::getFiltered, this, _1, _2)); return router; @@ -593,7 +608,7 @@ DhtProxyServer::sendPushNotification(const std::string& token, Json::Value&& jso restinio::http_request_header_t header; header.request_target("/api/push"); - header.method(restinio::http_method_t::http_post); + header.method(restinio::http_method_post()); restinio::http_header_fields_t header_fields; header_fields.append_field(restinio::http_field_t::host, pushServer_.c_str()); diff --git a/src/http.cpp b/src/http.cpp index bcc92720..a232abb5 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -99,8 +99,7 @@ Client::create_request(const restinio::http_request_header_t header, const std::string body){ std::stringstream request; // first header - request << restinio::method_to_string(header.method()) << " " << - header.request_target() << " " << + request << header.method().c_str() << " " << header.request_target() << " " << "HTTP/" << header.http_major() << "." << header.http_minor() << "\r\n"; // other headers for (auto header_field: header_fields)