Store headers as string lines

curl ultimately expects null-terminated strings and
makes a copy of each, so it's pointless to mess
around with string_views and stuff if I have to make
a copy for curl at the end anyways.
This commit is contained in:
King_DuckZ 2020-08-29 15:18:33 +01:00
parent bc6b20563b
commit 8b09d7da53
4 changed files with 14 additions and 10 deletions

View file

@ -62,12 +62,11 @@ HttpResponse page_fetch (
} }
easy.add<CURLOPT_ACCEPT_ENCODING>("gzip"); easy.add<CURLOPT_ACCEPT_ENCODING>("gzip");
easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L); easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L);
easy.add<CURLOPT_VERBOSE>(0L);
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, user_agent)); easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, user_agent));
curl::curl_header ch; curl::curl_header ch;
for (const auto& entry : headers) { ch.add(headers.begin(), headers.end());
ch.add(entry.first + ": " + entry.second);
}
easy.add<CURLOPT_HTTPHEADER>(ch.get()); easy.add<CURLOPT_HTTPHEADER>(ch.get());
easy.perform(); easy.perform();

View file

@ -21,11 +21,10 @@
#include <string_view> #include <string_view>
#include <string> #include <string>
#include <vector> #include <vector>
#include <utility>
namespace nap { namespace nap {
typedef std::vector<std::pair<std::string, std::string>> PageFetchHeaders; typedef std::vector<std::string> PageFetchHeaders;
HttpResponse page_fetch ( HttpResponse page_fetch (
const std::string& url, const std::string& url,

View file

@ -17,13 +17,20 @@
#include "quick_rest.hpp" #include "quick_rest.hpp"
#include "private/page_fetch.hpp" #include "private/page_fetch.hpp"
#include <cassert>
namespace nap { namespace nap {
void QuickRest::add_headers (std::initializer_list<HeaderPairView> headers) { void QuickRest::add_headers (std::initializer_list<HeaderPairView> headers) {
m_header_lines.reserve(m_header_lines.size() + headers.size());
for (const auto& entry : headers) { for (const auto& entry : headers) {
using std::string; std::string line;
m_header.emplace_back(string(entry.first), string(entry.second)); line.reserve(entry.first.size() + 2 + entry.second.size());
line.append(entry.first);
line.append(": ");
line.append(entry.second);
m_header_lines.push_back(std::move(line));
} }
} }
@ -32,6 +39,6 @@ void QuickRest::set_user_agent (std::string&& name) {
} }
HttpResponse QuickRest::fetch (std::string_view url) { HttpResponse QuickRest::fetch (std::string_view url) {
return page_fetch(std::string(url), m_user_agent, m_header); return page_fetch(std::string(url), m_user_agent, m_header_lines);
} }
} //namespace nap } //namespace nap

View file

@ -30,7 +30,6 @@ namespace nap {
class QuickRest { class QuickRest {
public: public:
typedef std::pair<std::string_view, std::string_view> HeaderPairView; typedef std::pair<std::string_view, std::string_view> HeaderPairView;
typedef std::pair<std::string, std::string> HeaderPair;
QuickRest() = default; QuickRest() = default;
@ -40,7 +39,7 @@ public:
private: private:
std::string m_user_agent; std::string m_user_agent;
std::vector<HeaderPair> m_header; std::vector<std::string> m_header_lines;
}; };
} //namespace nap } //namespace nap