2017-01-16 19:39:22 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-09-11 07:38:34 +03:00
|
|
|
#include <algorithm>
|
2017-01-16 19:39:22 +03:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
#include <dlfcn.h>
|
2018-10-10 07:55:10 +03:00
|
|
|
#include <string>
|
2017-01-16 19:39:22 +03:00
|
|
|
#include <unistd.h>
|
2017-05-17 14:34:12 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2018-10-10 07:55:10 +03:00
|
|
|
#include "third_party/curl/curl.h"
|
2017-05-17 14:34:12 +03:00
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
namespace PingSender {
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
2017-05-17 14:34:12 +03:00
|
|
|
using mozilla::Unused;
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
/**
|
|
|
|
* A simple wrapper around libcurl "easy" functions. Provides RAII opening
|
|
|
|
* and initialization of the curl library
|
|
|
|
*/
|
|
|
|
class CurlWrapper {
|
|
|
|
public:
|
|
|
|
CurlWrapper();
|
|
|
|
~CurlWrapper();
|
|
|
|
bool Init();
|
2019-09-11 07:38:34 +03:00
|
|
|
bool IsValidDestination(const string& url);
|
2017-01-16 19:39:22 +03:00
|
|
|
bool Post(const string& url, const string& payload);
|
|
|
|
|
|
|
|
// libcurl functions
|
|
|
|
CURL* (*easy_init)(void);
|
|
|
|
CURLcode (*easy_setopt)(CURL*, CURLoption, ...);
|
|
|
|
CURLcode (*easy_perform)(CURL*);
|
|
|
|
CURLcode (*easy_getinfo)(CURL*, CURLINFO, ...);
|
2017-03-23 17:02:35 +03:00
|
|
|
curl_slist* (*slist_append)(curl_slist*, const char*);
|
|
|
|
void (*slist_free_all)(curl_slist*);
|
2017-01-16 19:39:22 +03:00
|
|
|
const char* (*easy_strerror)(CURLcode);
|
|
|
|
void (*easy_cleanup)(CURL*);
|
2017-05-24 20:27:33 +03:00
|
|
|
void (*global_cleanup)(void);
|
2017-01-16 19:39:22 +03:00
|
|
|
|
2019-09-11 07:38:34 +03:00
|
|
|
CURLU* (*curl_url)();
|
2019-10-06 21:29:55 +03:00
|
|
|
CURLUcode (*curl_url_get)(CURLU*, CURLUPart, char**, unsigned int);
|
|
|
|
CURLUcode (*curl_url_set)(CURLU*, CURLUPart, const char*, unsigned int);
|
|
|
|
void (*curl_free)(char*);
|
|
|
|
void (*curl_url_cleanup)(CURLU*);
|
2019-09-11 07:38:34 +03:00
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
private:
|
|
|
|
void* mLib;
|
|
|
|
void* mCurl;
|
2019-09-11 07:38:34 +03:00
|
|
|
bool mCanParseUrl;
|
2017-01-16 19:39:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
CurlWrapper::CurlWrapper()
|
|
|
|
: easy_init(nullptr),
|
|
|
|
easy_setopt(nullptr),
|
|
|
|
easy_perform(nullptr),
|
|
|
|
easy_getinfo(nullptr),
|
2017-03-23 17:02:35 +03:00
|
|
|
slist_append(nullptr),
|
|
|
|
slist_free_all(nullptr),
|
2017-01-16 19:39:22 +03:00
|
|
|
easy_strerror(nullptr),
|
|
|
|
easy_cleanup(nullptr),
|
2017-05-24 20:27:33 +03:00
|
|
|
global_cleanup(nullptr),
|
2019-09-11 07:38:34 +03:00
|
|
|
curl_url(nullptr),
|
|
|
|
curl_url_get(nullptr),
|
|
|
|
curl_url_set(nullptr),
|
|
|
|
curl_free(nullptr),
|
|
|
|
curl_url_cleanup(nullptr),
|
2017-01-16 19:39:22 +03:00
|
|
|
mLib(nullptr),
|
|
|
|
mCurl(nullptr) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
CurlWrapper::~CurlWrapper() {
|
|
|
|
if (mLib) {
|
|
|
|
if (mCurl && easy_cleanup) {
|
|
|
|
easy_cleanup(mCurl);
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:27:33 +03:00
|
|
|
if (global_cleanup) {
|
|
|
|
global_cleanup();
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
dlclose(mLib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CurlWrapper::Init() {
|
2017-09-22 16:06:22 +03:00
|
|
|
const char* libcurlPaths[] = {
|
2017-09-26 10:35:03 +03:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
// macOS
|
2017-11-24 12:11:47 +03:00
|
|
|
"/usr/lib/libcurl.dylib",
|
|
|
|
"/usr/lib/libcurl.4.dylib",
|
|
|
|
"/usr/lib/libcurl.3.dylib",
|
2017-09-26 10:35:03 +03:00
|
|
|
#else // Linux, *BSD, ...
|
2017-01-16 19:39:22 +03:00
|
|
|
"libcurl.so",
|
|
|
|
"libcurl.so.4",
|
|
|
|
// Debian gives libcurl a different name when it is built against GnuTLS
|
2017-09-22 16:06:22 +03:00
|
|
|
"libcurl-gnutls.so",
|
2017-01-16 19:39:22 +03:00
|
|
|
"libcurl-gnutls.so.4",
|
2017-09-22 16:06:22 +03:00
|
|
|
// Older versions in case we find nothing better
|
2017-01-16 19:39:22 +03:00
|
|
|
"libcurl.so.3",
|
2017-09-22 16:06:22 +03:00
|
|
|
"libcurl-gnutls.so.3", // See above for Debian
|
|
|
|
#endif
|
2017-01-16 19:39:22 +03:00
|
|
|
};
|
|
|
|
|
2017-11-24 12:11:47 +03:00
|
|
|
// libcurl might show up under different names & paths, try them all until
|
|
|
|
// we find it
|
|
|
|
for (const char* libname : libcurlPaths) {
|
|
|
|
mLib = dlopen(libname, RTLD_NOW);
|
2017-01-16 19:39:22 +03:00
|
|
|
|
|
|
|
if (mLib) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLib) {
|
|
|
|
PINGSENDER_LOG("ERROR: Could not find libcurl\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(void**)(&easy_init) = dlsym(mLib, "curl_easy_init");
|
|
|
|
*(void**)(&easy_setopt) = dlsym(mLib, "curl_easy_setopt");
|
|
|
|
*(void**)(&easy_perform) = dlsym(mLib, "curl_easy_perform");
|
|
|
|
*(void**)(&easy_getinfo) = dlsym(mLib, "curl_easy_getinfo");
|
2017-03-23 17:02:35 +03:00
|
|
|
*(void**)(&slist_append) = dlsym(mLib, "curl_slist_append");
|
|
|
|
*(void**)(&slist_free_all) = dlsym(mLib, "curl_slist_free_all");
|
2017-01-16 19:39:22 +03:00
|
|
|
*(void**)(&easy_strerror) = dlsym(mLib, "curl_easy_strerror");
|
|
|
|
*(void**)(&easy_cleanup) = dlsym(mLib, "curl_easy_cleanup");
|
2017-05-24 20:27:33 +03:00
|
|
|
*(void**)(&global_cleanup) = dlsym(mLib, "curl_global_cleanup");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-09-11 07:38:34 +03:00
|
|
|
*(void**)(&curl_url) = dlsym(mLib, "curl_url");
|
|
|
|
*(void**)(&curl_url_set) = dlsym(mLib, "curl_url_set");
|
|
|
|
*(void**)(&curl_url_get) = dlsym(mLib, "curl_url_get");
|
|
|
|
*(void**)(&curl_free) = dlsym(mLib, "curl_free");
|
|
|
|
*(void**)(&curl_url_cleanup) = dlsym(mLib, "curl_url_cleanup");
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
if (!easy_init || !easy_setopt || !easy_perform || !easy_getinfo ||
|
2017-03-23 17:02:35 +03:00
|
|
|
!slist_append || !slist_free_all || !easy_strerror || !easy_cleanup ||
|
2017-05-24 20:27:33 +03:00
|
|
|
!global_cleanup) {
|
2017-01-16 19:39:22 +03:00
|
|
|
PINGSENDER_LOG("ERROR: libcurl is missing one of the required symbols\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-11 07:38:34 +03:00
|
|
|
mCanParseUrl = true;
|
|
|
|
if (!curl_url || !curl_url_get || !curl_url_set || !curl_free ||
|
|
|
|
!curl_url_cleanup) {
|
|
|
|
mCanParseUrl = false;
|
|
|
|
PINGSENDER_LOG("WARNING: Do not have url parsing functions in libcurl\n");
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
mCurl = easy_init();
|
|
|
|
|
|
|
|
if (!mCurl) {
|
|
|
|
PINGSENDER_LOG("ERROR: Could not initialize libcurl\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 14:34:12 +03:00
|
|
|
static size_t DummyWriteCallback(char* ptr, size_t size, size_t nmemb,
|
|
|
|
void* userdata) {
|
|
|
|
Unused << ptr;
|
|
|
|
Unused << size;
|
|
|
|
Unused << nmemb;
|
|
|
|
Unused << userdata;
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
2019-09-11 07:38:34 +03:00
|
|
|
// If we can't use curl's URL parsing (which is safer) we have to fallback
|
|
|
|
// to this handwritten one (which is only as safe as we are clever.)
|
|
|
|
bool FallbackIsValidDestination(const string& aUrl) {
|
|
|
|
// Lowercase the url
|
|
|
|
string url = aUrl;
|
|
|
|
std::transform(url.begin(), url.end(), url.begin(),
|
2019-10-06 21:29:55 +03:00
|
|
|
[](unsigned char c) { return std::tolower(c); });
|
2019-09-11 07:38:34 +03:00
|
|
|
// Strip off the scheme in the beginning
|
2019-12-13 19:33:04 +03:00
|
|
|
if (url.find("http://") == 0) {
|
2019-09-11 07:38:34 +03:00
|
|
|
url = url.substr(7);
|
2019-12-13 19:33:04 +03:00
|
|
|
} else if (url.find("https://") == 0) {
|
2019-09-11 07:38:34 +03:00
|
|
|
url = url.substr(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any user information. If a @ appeared in the userinformation,
|
|
|
|
// it would need to be encoded.
|
|
|
|
unsigned long atStart = url.find_first_of("@");
|
|
|
|
url = (atStart == std::string::npos) ? url : url.substr(atStart + 1);
|
|
|
|
|
2019-10-06 21:29:55 +03:00
|
|
|
// Remove any path or fragment information, leaving us with a url that may
|
|
|
|
// contain host, and port.
|
2019-09-11 07:38:34 +03:00
|
|
|
unsigned long fragStart = url.find_first_of("#");
|
|
|
|
url = (fragStart == std::string::npos) ? url : url.substr(0, fragStart);
|
|
|
|
unsigned long pathStart = url.find_first_of("/");
|
|
|
|
url = (pathStart == std::string::npos) ? url : url.substr(0, pathStart);
|
|
|
|
|
|
|
|
// Remove the port, because we run tests targeting localhost:port
|
|
|
|
unsigned long portStart = url.find_last_of(":");
|
|
|
|
url = (portStart == std::string::npos) ? url : url.substr(0, portStart);
|
|
|
|
|
|
|
|
return ::IsValidDestination(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CurlWrapper::IsValidDestination(const string& aUrl) {
|
|
|
|
if (!mCanParseUrl) {
|
|
|
|
return FallbackIsValidDestination(aUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
2019-10-06 21:29:55 +03:00
|
|
|
CURLU* h = curl_url();
|
2019-09-11 07:38:34 +03:00
|
|
|
if (!h) {
|
|
|
|
return FallbackIsValidDestination(aUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CURLUE_OK != curl_url_set(h, CURLUPART_URL, aUrl.c_str(), 0)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-10-06 21:29:55 +03:00
|
|
|
char* host;
|
2019-09-11 07:38:34 +03:00
|
|
|
if (CURLUE_OK != curl_url_get(h, CURLUPART_HOST, &host, 0)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ::IsValidDestination(host);
|
|
|
|
curl_free(host);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
curl_url_cleanup(h);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
bool CurlWrapper::Post(const string& url, const string& payload) {
|
|
|
|
easy_setopt(mCurl, CURLOPT_URL, url.c_str());
|
|
|
|
easy_setopt(mCurl, CURLOPT_USERAGENT, kUserAgent);
|
2017-05-17 14:34:12 +03:00
|
|
|
easy_setopt(mCurl, CURLOPT_WRITEFUNCTION, DummyWriteCallback);
|
2017-01-16 19:39:22 +03:00
|
|
|
|
2017-03-23 17:02:35 +03:00
|
|
|
// Build the date header.
|
|
|
|
std::string dateHeader = GenerateDateHeader();
|
|
|
|
|
|
|
|
// Set the custom headers.
|
|
|
|
curl_slist* headerChunk = nullptr;
|
|
|
|
headerChunk = slist_append(headerChunk, kCustomVersionHeader);
|
|
|
|
headerChunk = slist_append(headerChunk, kContentEncodingHeader);
|
|
|
|
headerChunk = slist_append(headerChunk, dateHeader.c_str());
|
|
|
|
CURLcode err = easy_setopt(mCurl, CURLOPT_HTTPHEADER, headerChunk);
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
PINGSENDER_LOG("ERROR: Failed to set HTTP headers, %s\n",
|
|
|
|
easy_strerror(err));
|
|
|
|
slist_free_all(headerChunk);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
// Set the size of the POST data
|
|
|
|
easy_setopt(mCurl, CURLOPT_POSTFIELDSIZE, payload.length());
|
|
|
|
|
|
|
|
// Set the contents of the POST data
|
|
|
|
easy_setopt(mCurl, CURLOPT_POSTFIELDS, payload.c_str());
|
|
|
|
|
|
|
|
// Fail if the server returns a 4xx code
|
|
|
|
easy_setopt(mCurl, CURLOPT_FAILONERROR, 1);
|
|
|
|
|
2017-05-30 13:28:54 +03:00
|
|
|
// Override the default connection timeout, which is 5 minutes.
|
|
|
|
easy_setopt(mCurl, CURLOPT_CONNECTTIMEOUT_MS, kConnectionTimeoutMs);
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
// Block until the operation is performend. Ignore the response, if the POST
|
|
|
|
// fails we can't do anything about it.
|
2017-03-23 17:02:35 +03:00
|
|
|
err = easy_perform(mCurl);
|
|
|
|
// Whatever happens, we want to clean up the header memory.
|
|
|
|
slist_free_all(headerChunk);
|
2017-01-16 19:39:22 +03:00
|
|
|
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
PINGSENDER_LOG("ERROR: Failed to send HTTP request, %s\n",
|
|
|
|
easy_strerror(err));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Post(const string& url, const string& payload) {
|
|
|
|
CurlWrapper curl;
|
|
|
|
|
|
|
|
if (!curl.Init()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-11 07:38:34 +03:00
|
|
|
if (!curl.IsValidDestination(url)) {
|
|
|
|
PINGSENDER_LOG("ERROR: Invalid destination host\n");
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-16 19:39:22 +03:00
|
|
|
|
|
|
|
return curl.Post(url, payload);
|
|
|
|
}
|
|
|
|
|
2019-11-29 01:00:15 +03:00
|
|
|
void ChangeCurrentWorkingDirectory(const string& pingPath) {
|
|
|
|
// This is not needed under Linux/macOS
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:39:22 +03:00
|
|
|
} // namespace PingSender
|