diff --git a/include/opendht/http.h b/include/opendht/http.h index edb97694e2eb32de5532305ca433dedf6ca49a0d..bb70d2aeb71d791bf9b241ca569ffc2e199acea4 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 729747a9a5d3f08689830690689f397297fd57fe..1224fa7f43d4004340d10c23f00de5d27945b289 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() {