From 2ca2835c9b73e60f9e184a8dd50faea5633a9574 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 8 Nov 2017 09:43:42 -0500
Subject: [PATCH] add options method handler

---
 include/opendht/dht_proxy_server.h |  8 ++++++++
 src/dht_proxy_server.cpp           | 21 ++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h
index 5ac64c4e..733ebdb2 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 f813f239..2b2e34ec 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
 {
-- 
GitLab