Skip to content
Snippets Groups Projects
Commit 2ca2835c authored by Sébastien Blin's avatar Sébastien Blin
Browse files

add options method handler

parent bbe922dd
No related branches found
No related tags found
No related merge requests found
...@@ -139,6 +139,14 @@ private: ...@@ -139,6 +139,14 @@ private:
*/ */
void getFiltered(const std::shared_ptr<restbed::Session>& session) const; 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::thread server_thread {};
std::unique_ptr<restbed::Service> service_; std::unique_ptr<restbed::Service> service_;
std::shared_ptr<DhtRunner> dht_; std::shared_ptr<DhtRunner> dht_;
......
...@@ -81,6 +81,12 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port) ...@@ -81,6 +81,12 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port)
} }
); );
#endif // OPENDHT_PROXY_SERVER_IDENTITY #endif // OPENDHT_PROXY_SERVER_IDENTITY
resource->set_method_handler("OPTIONS",
[this](const std::shared_ptr<restbed::Session> session)
{
this->handleOptionsMethod(session);
}
);
service_->publish(resource); service_->publish(resource);
resource = std::make_shared<restbed::Resource>(); resource = std::make_shared<restbed::Resource>();
resource->set_path("/{hash: .*}/{value: .*}"); resource->set_path("/{hash: .*}/{value: .*}");
...@@ -357,7 +363,7 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c ...@@ -357,7 +363,7 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c
auto value = std::make_shared<Value>(root); auto value = std::make_shared<Value>(root);
auto toHash = request->get_path_parameter("to"); auto toHash = request->get_path_parameter("to");
InfoHash toInfoHash(toHash); InfoHash toInfoHash(toHash);
if (toinfoHash) if (toInfoHash)
toInfoHash = InfoHash::get(toHash); toInfoHash = InfoHash::get(toHash);
Json::FastWriter writer; Json::FastWriter writer;
...@@ -378,6 +384,19 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c ...@@ -378,6 +384,19 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c
} }
#endif // OPENDHT_PROXY_SERVER_IDENTITY #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 void
DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) const DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) const
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment