From 72a06b142534fccc26dc15c7e634f7ad80d5bf77 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 30 Aug 2019 11:24:51 -0400
Subject: [PATCH] http: add Request::set_auth

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

diff --git a/include/opendht/http.h b/include/opendht/http.h
index edb97694..bb70d2ae 100644
--- a/include/opendht/http.h
+++ b/include/opendht/http.h
@@ -21,10 +21,9 @@
 #include "def.h"
 #include "infohash.h"
 
-#include <asio.hpp>
-#include <asio/ssl.hpp>
-
-#include <restinio/all.hpp>
+#include <asio/ssl/context.hpp>
+#include <restinio/http_headers.hpp>
+#include <restinio/message_builders.hpp>
 
 #include <memory>
 #include <queue>
@@ -221,12 +220,13 @@ public:
     /**
      * Define the HTTP header/body as per https://tools.ietf.org/html/rfc7230.
      */
-    void set_header(const restinio::http_request_header_t header);
-    void set_method(const restinio::http_method_id_t method);
+    void set_header(restinio::http_request_header_t header);
+    void set_method(restinio::http_method_id_t method);
     void set_target(std::string target);
-    void set_header_field(const restinio::http_field_t field, std::string value);
-    void set_connection_type(const restinio::http_connection_header_t connection);
+    void set_header_field(restinio::http_field_t field, std::string value);
+    void set_connection_type(restinio::http_connection_header_t connection);
     void set_body(std::string body);
+    void set_auth(const std::string& username, const std::string& password);
 
     void add_on_status_callback(OnStatusCb cb);
     void add_on_body_callback(OnDataCb cb);
diff --git a/src/http.cpp b/src/http.cpp
index 729747a9..1224fa7f 100644
--- a/src/http.cpp
+++ b/src/http.cpp
@@ -474,13 +474,13 @@ Request::set_logger(std::shared_ptr<dht::Logger> logger)
 }
 
 void
-Request::set_header(const restinio::http_request_header_t header)
+Request::set_header(restinio::http_request_header_t header)
 {
     header_ = header;
 }
 
 void
-Request::set_method(const restinio::http_method_id_t method)
+Request::set_method(restinio::http_method_id_t method)
 {
     header_.method(method);
 }
@@ -492,13 +492,13 @@ Request::set_target(std::string target)
 }
 
 void
-Request::set_header_field(const restinio::http_field_t field, std::string value)
+Request::set_header_field(restinio::http_field_t field, std::string value)
 {
     headers_[field] = std::move(value);
 }
 
 void
-Request::set_connection_type(const restinio::http_connection_header_t connection)
+Request::set_connection_type(restinio::http_connection_header_t connection)
 {
     connection_type_ = connection;
 }
@@ -509,6 +509,17 @@ Request::set_body(std::string body)
     body_ = std::move(body);
 }
 
+void
+Request::set_auth(const std::string& username, const std::string& password)
+{
+    std::vector<uint8_t> creds;
+    creds.reserve(username.size() + password.size() + 1);
+    creds.insert(creds.end(), username.begin(), username.end());
+    creds.emplace_back(':');
+    creds.insert(creds.end(), password.begin(), password.end());
+    set_header_field(restinio::http_field_t::authorization, "Basic " + base64_encode(creds));
+}
+
 void
 Request::build()
 {
-- 
GitLab