Start working on QuickRest

This commit is contained in:
King_DuckZ 2020-08-29 14:28:47 +01:00
parent c4935ac70d
commit 74e98211a7
6 changed files with 132 additions and 15 deletions

View file

@ -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,

36
src/nap/http_response.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <string_view>
#include <vector>
#include <utility>
namespace nap {
struct HttpResponse {
std::string raw;
std::vector<std::pair<std::string_view, std::string_view>> header;
std::string_view body;
std::string_view http_ver;
std::string_view code_desc;
unsigned int code;
};
} //namespace nap

View file

@ -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<CURLOPT_HEADERDATA>(header.get_stream());
easy.add<CURLOPT_WRITEDATA>(body.get_stream());
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_URL, std::string(url)));
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_URL, url));
easy.add<CURLOPT_FOLLOWLOCATION>(1L);
if (is_https(url)) {
easy.add<CURLOPT_SSL_VERIFYPEER>(true);
@ -57,7 +60,7 @@ HttpResponse page_fetch (std::string_view url, std::string_view user_agent) {
}
easy.add<CURLOPT_ACCEPT_ENCODING>("gzip");
easy.add<CURLOPT_HTTP_CONTENT_DECODING>(1L);
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, std::string(user_agent)));
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_USERAGENT, user_agent));
easy.perform();

View file

@ -17,20 +17,14 @@
#pragma once
#include <string>
#include "nap/http_response.hpp"
#include <string_view>
#include <vector>
#include <utility>
namespace nap {
struct HttpResponse {
std::string raw;
std::vector<std::pair<std::string_view, std::string_view>> 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

37
src/nap/quick_rest.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "quick_rest.hpp"
#include "private/page_fetch.hpp"
namespace nap {
void QuickRest::add_headers (std::initializer_list<HeaderPairView> 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

46
src/nap/quick_rest.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "http_response.hpp"
#include <simdjson.h>
#include <string_view>
#include <string>
#include <initializer_list>
#include <vector>
#include <utility>
namespace nap {
class QuickRest {
public:
typedef std::pair<std::string_view, std::string_view> HeaderPairView;
typedef std::pair<std::string, std::string> HeaderPair;
QuickRest() = default;
void add_headers (std::initializer_list<HeaderPairView> headers);
void set_user_agent (std::string&& name);
HttpResponse fetch (std::string_view url);
private:
std::string m_user_agent;
std::vector<HeaderPair> m_header;
};
} //namespace nap