From 37c732b715108084363c57d77cbd72c264f88595 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Thu, 1 Oct 2020 20:39:05 -0400
Subject: [PATCH] http: add Request::await

---
 include/opendht/http.h |  3 +++
 src/http.cpp           | 16 ++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/include/opendht/http.h b/include/opendht/http.h
index 46b722ce..85284185 100644
--- a/include/opendht/http.h
+++ b/include/opendht/http.h
@@ -290,6 +290,9 @@ public:
 
     void send();
 
+    /** Send and block for response */
+    const Response& await();
+
     /**
      * User action to cancel the Request and call the completion callbacks.
      */
diff --git a/src/http.cpp b/src/http.cpp
index b878aa31..ad1981a9 100644
--- a/src/http.cpp
+++ b/src/http.cpp
@@ -1096,5 +1096,21 @@ Request::getRelativePath(const Url& origin, const std::string& path)
     return newPath.toString();
 }
 
+const Response&
+Request::await()
+{
+    std::mutex mtx;
+    std::unique_lock<std::mutex> lock(mtx);
+    std::condition_variable cv;
+    bool ok {false};
+    add_on_done_callback([&](const Response& resp){
+        std::lock_guard<std::mutex> lk(mtx);
+        ok = true;
+        cv.notify_all();
+    });
+    cv.wait(lock, [&]{ return ok; });
+    return response_;
+}
+
 } // namespace http
 } // namespace dht
-- 
GitLab