diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index 5ac64c4e5d7554515306896fc7e03ee19ebf45aa..733ebdb2954171088a079f8fc2911529f39e6324 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -139,6 +139,14 @@ private: */ void getFiltered(const std::shared_ptr<restbed::Session>& session) const; + /** + * Respond allowed Methods + * Method: OPTIONS "/{hash: .*}" + * Return: HTTP 200 + Allow: allowed methods + * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS + */ + void handleOptionsMethod(const std::shared_ptr<restbed::Session>& session) const; + std::thread server_thread {}; std::unique_ptr<restbed::Service> service_; std::shared_ptr<DhtRunner> dht_; diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index f813f2390c42219898f3529a42cb557e66358659..2b2e34ecfae953d931bb43c682fb455057cedc20 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -81,6 +81,12 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port) } ); #endif // OPENDHT_PROXY_SERVER_IDENTITY + resource->set_method_handler("OPTIONS", + [this](const std::shared_ptr<restbed::Session> session) + { + this->handleOptionsMethod(session); + } + ); service_->publish(resource); resource = std::make_shared<restbed::Resource>(); resource->set_path("/{hash: .*}/{value: .*}"); @@ -357,7 +363,7 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c auto value = std::make_shared<Value>(root); auto toHash = request->get_path_parameter("to"); InfoHash toInfoHash(toHash); - if (toinfoHash) + if (toInfoHash) toInfoHash = InfoHash::get(toHash); Json::FastWriter writer; @@ -378,6 +384,19 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c } #endif // OPENDHT_PROXY_SERVER_IDENTITY +void +DhtProxyServer::handleOptionsMethod(const std::shared_ptr<restbed::Session>& session) const +{ +#if OPENDHT_PROXY_SERVER_IDENTITY + const auto allowed = "OPTIONS, GET, POST, LISTEN, SIGN, ENCRYPT"; +#else + const auto allowed = "OPTIONS, GET, POST, LISTEN"; +#endif //OPENDHT_PROXY_SERVER_IDENTITY + session->close(restbed::OK, {{"Access-Control-Allow-Methods", allowed}, + {"Access-Control-Allow-Headers", "content-type"}, + {"Access-Control-Max-Age", "86400"}}); +} + void DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) const {