Add gateway functionality
This commit is contained in:
44
TriviaBot/bot/http/HTTPHelper.cpp
Normal file
44
TriviaBot/bot/http/HTTPHelper.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "HTTPHelper.hpp"
|
||||
|
||||
size_t HTTPHelper::data_write(void* buf, size_t size, size_t nmemb, void* userp) {
|
||||
if (userp) {
|
||||
std::ostream& os = *static_cast<std::ostream*>(userp);
|
||||
std::streamsize len = size * nmemb;
|
||||
if (os.write(static_cast<char*>(buf), len)) {
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
CURLcode HTTPHelper::curl_read(const std::string& url, std::ostream& os, long timeout = 30) {
|
||||
CURLcode code(CURLE_FAILED_INIT);
|
||||
CURL* curl = curl_easy_init();
|
||||
|
||||
if (curl) {
|
||||
if (CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &data_write))
|
||||
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L))
|
||||
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L))
|
||||
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FILE, &os))
|
||||
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout))
|
||||
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()))) {
|
||||
code = curl_easy_perform(curl);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
std::string HTTPHelper::read_url(std::string url) {
|
||||
std::ostringstream oss;
|
||||
std::string html = "ERROR";
|
||||
if (CURLE_OK == curl_read("http://www.google.co.uk/", oss)) {
|
||||
html = oss.str();
|
||||
}
|
||||
else {
|
||||
std::cout << "CURL error" << std::endl;
|
||||
}
|
||||
|
||||
return html;
|
||||
}
|
14
TriviaBot/bot/http/HTTPHelper.hpp
Normal file
14
TriviaBot/bot/http/HTTPHelper.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include <curl/curl.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
// callback function writes data to a std::ostream
|
||||
class HTTPHelper {
|
||||
public:
|
||||
static std::string read_url(std::string url);
|
||||
|
||||
private:
|
||||
static size_t data_write(void* buf, size_t size, size_t nmemb, void* userp);
|
||||
static CURLcode curl_read(const std::string& url, std::ostream& os, long timeout);
|
||||
};
|
Reference in New Issue
Block a user