From bc6b20563be6b50aabf7bd36058521ba2a440ffe Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 29 Aug 2020 14:30:08 +0100 Subject: [PATCH] Send user specified headers with the request --- src/nap/page_fetch.cpp | 10 +++++++++- src/nap/private/page_fetch.hpp | 8 +++++++- src/nap/quick_rest.cpp | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/nap/page_fetch.cpp b/src/nap/page_fetch.cpp index ab64e1c..4273968 100644 --- a/src/nap/page_fetch.cpp +++ b/src/nap/page_fetch.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,8 @@ namespace { HttpResponse page_fetch ( const std::string& url, - const std::string& user_agent + const std::string& user_agent, + const PageFetchHeaders& headers ) { using curl::curl_pair; @@ -62,6 +64,12 @@ HttpResponse page_fetch ( easy.add(1L); easy.add(curl_pair(CURLOPT_USERAGENT, user_agent)); + curl::curl_header ch; + for (const auto& entry : headers) { + ch.add(entry.first + ": " + entry.second); + } + easy.add(ch.get()); + easy.perform(); HttpResponse resp; diff --git a/src/nap/private/page_fetch.hpp b/src/nap/private/page_fetch.hpp index 66312ad..c213bb5 100644 --- a/src/nap/private/page_fetch.hpp +++ b/src/nap/private/page_fetch.hpp @@ -19,12 +19,18 @@ #include "nap/http_response.hpp" #include +#include +#include +#include namespace nap { +typedef std::vector> PageFetchHeaders; + HttpResponse page_fetch ( const std::string& url, - const std::string& user_agent + const std::string& user_agent, + const PageFetchHeaders& headers ); } //namespace nap diff --git a/src/nap/quick_rest.cpp b/src/nap/quick_rest.cpp index 3a4d511..dcb78bc 100644 --- a/src/nap/quick_rest.cpp +++ b/src/nap/quick_rest.cpp @@ -32,6 +32,6 @@ void QuickRest::set_user_agent (std::string&& name) { } HttpResponse QuickRest::fetch (std::string_view url) { - return page_fetch(std::string(url), m_user_agent); + return page_fetch(std::string(url), m_user_agent, m_header); } } //namespace nap