dhtproxy: add support for legacy http methods

Legacy non-standard http methods: LISTEN, STATS, SIGN, ECNRYPT

NOTE: Supported with RESTinio > 0.5.0 and custom http_parser
- https://github.com/Stiffstream/restinio/issues/26
- https://github.com/binarytrails/http-parser
This commit is contained in:
Seva committed 2019-06-06 13:30:37 -04:00
1 parent fc3686d817
commit f8d62dc6a7
5 files changed
+62 -15

No files matched your search

+7 -4
View File
@@ -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<RestRouterTraits>;
using RequestStatus = restinio::request_handling_status_t;
using ResponseByParts = restinio::chunked_output_t;
+30
View File
@@ -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 );
}
};
}
+4 -4
View File
@@ -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<Value> 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);
+20 -5
View File
@@ -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<DhtRunner> dht, in_port_t port ,const std::string& pushServer, std::shared_ptr<dht::Logger> 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<RestRouter>();
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());
+1 -2
View File
@@ -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)