From 449801990fd6629ba2a51f7a82383306d707d53e Mon Sep 17 00:00:00 2001 From: Ilya Andreev Date: Mon, 15 Jan 2024 16:57:22 +0300 Subject: [PATCH] Add a getter for a bearer token from a request (#1755) * Add a getter for a bearer token from a request * Replace a method for bearer token getter with a free function --- httplib.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/httplib.h b/httplib.h index 1bb4677..d04ac77 100644 --- a/httplib.h +++ b/httplib.h @@ -745,6 +745,8 @@ void default_socket_options(socket_t sock); const char *status_message(int status); +std::string get_bearer_token_auth(const Request &req); + namespace detail { class MatcherBase { @@ -1943,6 +1945,15 @@ inline const char *status_message(int status) { } } +inline std::string get_bearer_token_auth(const Request &req) { + if (req.has_header("Authorization")) { + static std::string BearerHeaderPrefix = "Bearer "; + return req.get_header_value("Authorization") + .substr(BearerHeaderPrefix.length()); + } + return ""; +} + template inline Server & Server::set_read_timeout(const std::chrono::duration &duration) {