Files
spark/src/lib/config/ConfigDownloader.cpp
T

77 lines
2.3 KiB
C++

#include "lib/utils/windows_wrapper.h"
#include "ConfigDownloader.h"
#include <boost/algorithm/string.hpp>
#include <sstream>
#include <stdio.h>
#include "lib/utils/bts_assert.h"
using namespace std;
namespace bts {
namespace config {
namespace internal {
ConfigDownloader::ConfigDownloader() : _curl(NULL) {
curl_global_init(CURL_GLOBAL_DEFAULT);
_curl = curl_easy_init();
bts_assert(_curl);
}
ConfigDownloader::~ConfigDownloader() { curl_easy_cleanup(_curl); }
size_t curl_write(char *buffer, size_t size, size_t count, void *stream) {
stringstream *out = static_cast<stringstream *>(stream);
bts_assert(out);
size_t bytes = size * count;
out->write(buffer, bytes);
return count;
}
bool is_http(const string &url) { return url.substr(0, 7) == "http://"; }
int no_linger(void *, curl_socket_t socket, curlsocktype) {
linger l;
l.l_onoff = 1;
l.l_linger = 0;
setsockopt(socket, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l));
return CURL_SOCKOPT_OK;
}
ConfigDownloader::Result
ConfigDownloader::get(const string &url, stringstream &out_stream) {
curl_easy_setopt(_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &out_stream);
curl_easy_setopt(_curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(_curl, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(_curl, CURLOPT_CONNECTTIMEOUT, 6);
curl_easy_setopt(_curl, CURLOPT_SOCKOPTFUNCTION, no_linger);
CURLcode res = curl_easy_perform(_curl);
long http_code = 0;
CURLcode r2 = curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &http_code);
bts_assert(r2 == CURLE_OK);
bool used_http = is_http(url);
if (res == CURLE_OK && (http_code == 200 || !used_http)) {
return SUCCESS;
} else if (
res == CURLE_OPERATION_TIMEDOUT || res == CURLE_COULDNT_RESOLVE_HOST ||
res == CURLE_COULDNT_RESOLVE_PROXY || res == CURLE_COULDNT_CONNECT ||
res == CURLE_REMOTE_ACCESS_DENIED || res == CURLE_FTP_CANT_GET_HOST) {
// Cannot rely on logging before config is initialized
// LOG(Error, Config) << "Permanent failure loading '" << url << "' - CURL
// error: " << curl_easy_strerror(res);
return PERMANENT_FAILURE;
}
return FAILURE;
}
} // namespace internal
} // namespace config
} // namespace bts