diff --git a/src/meson.build b/src/meson.build index 6799f33..3856309 100644 --- a/src/meson.build +++ b/src/meson.build @@ -105,6 +105,7 @@ executable(meson.project_name(), 'oro/originsdb.cpp', 'nap/page_fetch.cpp', 'nap/http_header_parse.cpp', + 'nap/quick_rest.cpp', project_config_file, install: true, dependencies: lib_deps, diff --git a/src/nap/http_response.hpp b/src/nap/http_response.hpp new file mode 100644 index 0000000..3859ab6 --- /dev/null +++ b/src/nap/http_response.hpp @@ -0,0 +1,36 @@ +/* Copyright 2020, Michele Santullo + * This file is part of orotool. + * + * Orotool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Orotool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Orotool. If not, see . + */ + +#pragma once + +#include +#include +#include +#include + +namespace nap { + +struct HttpResponse { + std::string raw; + std::vector> header; + std::string_view body; + std::string_view http_ver; + std::string_view code_desc; + unsigned int code; +}; + +} //namespace nap diff --git a/src/nap/page_fetch.cpp b/src/nap/page_fetch.cpp index 4e4a51c..ab64e1c 100644 --- a/src/nap/page_fetch.cpp +++ b/src/nap/page_fetch.cpp @@ -36,7 +36,10 @@ namespace { } } //unnamed namespace -HttpResponse page_fetch (std::string_view url, std::string_view user_agent) { +HttpResponse page_fetch ( + const std::string& url, + const std::string& user_agent +) { using curl::curl_pair; std::ostringstream body_oss; @@ -49,7 +52,7 @@ HttpResponse page_fetch (std::string_view url, std::string_view user_agent) { easy.add(header.get_stream()); easy.add(body.get_stream()); - easy.add(curl_pair(CURLOPT_URL, std::string(url))); + easy.add(curl_pair(CURLOPT_URL, url)); easy.add(1L); if (is_https(url)) { easy.add(true); @@ -57,7 +60,7 @@ HttpResponse page_fetch (std::string_view url, std::string_view user_agent) { } easy.add("gzip"); easy.add(1L); - easy.add(curl_pair(CURLOPT_USERAGENT, std::string(user_agent))); + easy.add(curl_pair(CURLOPT_USERAGENT, user_agent)); easy.perform(); diff --git a/src/nap/private/page_fetch.hpp b/src/nap/private/page_fetch.hpp index 990fb32..66312ad 100644 --- a/src/nap/private/page_fetch.hpp +++ b/src/nap/private/page_fetch.hpp @@ -17,20 +17,14 @@ #pragma once -#include +#include "nap/http_response.hpp" #include -#include -#include namespace nap { -struct HttpResponse { - std::string raw; - std::vector> header; - std::string_view body; - std::string_view http_ver; - std::string_view code_desc; - unsigned int code; -}; -HttpResponse page_fetch (std::string_view url, std::string_view user_agent); +HttpResponse page_fetch ( + const std::string& url, + const std::string& user_agent +); + } //namespace nap diff --git a/src/nap/quick_rest.cpp b/src/nap/quick_rest.cpp new file mode 100644 index 0000000..3a4d511 --- /dev/null +++ b/src/nap/quick_rest.cpp @@ -0,0 +1,37 @@ +/* Copyright 2020, Michele Santullo + * This file is part of orotool. + * + * Orotool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Orotool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Orotool. If not, see . + */ + +#include "quick_rest.hpp" +#include "private/page_fetch.hpp" + +namespace nap { + +void QuickRest::add_headers (std::initializer_list headers) { + for (const auto& entry : headers) { + using std::string; + m_header.emplace_back(string(entry.first), string(entry.second)); + } +} + +void QuickRest::set_user_agent (std::string&& name) { + m_user_agent = std::move(name); +} + +HttpResponse QuickRest::fetch (std::string_view url) { + return page_fetch(std::string(url), m_user_agent); +} +} //namespace nap diff --git a/src/nap/quick_rest.hpp b/src/nap/quick_rest.hpp new file mode 100644 index 0000000..2c7fa0e --- /dev/null +++ b/src/nap/quick_rest.hpp @@ -0,0 +1,46 @@ +/* Copyright 2020, Michele Santullo + * This file is part of orotool. + * + * Orotool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Orotool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Orotool. If not, see . + */ + +#pragma once + +#include "http_response.hpp" +#include +#include +#include +#include +#include +#include + +namespace nap { + +class QuickRest { +public: + typedef std::pair HeaderPairView; + typedef std::pair HeaderPair; + + QuickRest() = default; + + void add_headers (std::initializer_list headers); + void set_user_agent (std::string&& name); + HttpResponse fetch (std::string_view url); + +private: + std::string m_user_agent; + std::vector m_header; +}; + +} //namespace nap