Skip to content
Snippets Groups Projects
Commit 72a06b14 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

http: add Request::set_auth

parent e4dacc64
No related branches found
No related tags found
No related merge requests found
...@@ -21,10 +21,9 @@ ...@@ -21,10 +21,9 @@
#include "def.h" #include "def.h"
#include "infohash.h" #include "infohash.h"
#include <asio.hpp> #include <asio/ssl/context.hpp>
#include <asio/ssl.hpp> #include <restinio/http_headers.hpp>
#include <restinio/message_builders.hpp>
#include <restinio/all.hpp>
#include <memory> #include <memory>
#include <queue> #include <queue>
...@@ -221,12 +220,13 @@ public: ...@@ -221,12 +220,13 @@ public:
/** /**
* Define the HTTP header/body as per https://tools.ietf.org/html/rfc7230. * 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_header(restinio::http_request_header_t header);
void set_method(const restinio::http_method_id_t method); void set_method(restinio::http_method_id_t method);
void set_target(std::string target); void set_target(std::string target);
void set_header_field(const restinio::http_field_t field, std::string value); void set_header_field(restinio::http_field_t field, std::string value);
void set_connection_type(const restinio::http_connection_header_t connection); void set_connection_type(restinio::http_connection_header_t connection);
void set_body(std::string body); 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_status_callback(OnStatusCb cb);
void add_on_body_callback(OnDataCb cb); void add_on_body_callback(OnDataCb cb);
......
...@@ -474,13 +474,13 @@ Request::set_logger(std::shared_ptr<dht::Logger> logger) ...@@ -474,13 +474,13 @@ Request::set_logger(std::shared_ptr<dht::Logger> logger)
} }
void void
Request::set_header(const restinio::http_request_header_t header) Request::set_header(restinio::http_request_header_t header)
{ {
header_ = header; header_ = header;
} }
void void
Request::set_method(const restinio::http_method_id_t method) Request::set_method(restinio::http_method_id_t method)
{ {
header_.method(method); header_.method(method);
} }
...@@ -492,13 +492,13 @@ Request::set_target(std::string target) ...@@ -492,13 +492,13 @@ Request::set_target(std::string target)
} }
void 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); headers_[field] = std::move(value);
} }
void 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; connection_type_ = connection;
} }
...@@ -509,6 +509,17 @@ Request::set_body(std::string body) ...@@ -509,6 +509,17 @@ Request::set_body(std::string body)
body_ = std::move(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 void
Request::build() Request::build()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment