2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2015-01-14 18:54:25 +03:00
|
|
|
#include "cmCTestCurl.h"
|
|
|
|
|
|
|
|
#include "cmCTest.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmSystemTools.h"
|
2015-01-14 18:54:25 +03:00
|
|
|
|
2016-08-24 23:01:40 +03:00
|
|
|
#include <cmConfigure.h>
|
|
|
|
#include <ostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-14 18:54:25 +03:00
|
|
|
cmCTestCurl::cmCTestCurl(cmCTest* ctest)
|
|
|
|
{
|
|
|
|
this->CTest = ctest;
|
|
|
|
this->SetProxyType();
|
|
|
|
this->UseHttp10 = false;
|
|
|
|
// In windows, this will init the winsock stuff
|
|
|
|
::curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
// default is to verify https
|
|
|
|
this->VerifyPeerOff = false;
|
|
|
|
this->VerifyHostOff = false;
|
|
|
|
this->TimeOutSeconds = 0;
|
2015-01-29 22:42:44 +03:00
|
|
|
this->Curl = curl_easy_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
cmCTestCurl::~cmCTestCurl()
|
|
|
|
{
|
|
|
|
::curl_easy_cleanup(this->Curl);
|
|
|
|
::curl_global_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmCTestCurl::Escape(std::string const& source)
|
|
|
|
{
|
|
|
|
char* data1 = curl_easy_escape(this->Curl, source.c_str(), 0);
|
|
|
|
std::string ret = data1;
|
|
|
|
curl_free(data1);
|
|
|
|
return ret;
|
2015-01-14 18:54:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
namespace {
|
|
|
|
static size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
|
|
|
|
void* data)
|
2015-01-14 18:54:25 +03:00
|
|
|
{
|
|
|
|
int realsize = (int)(size * nmemb);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<char>* vec = static_cast<std::vector<char>*>(data);
|
2015-01-14 18:54:25 +03:00
|
|
|
const char* chPtr = static_cast<char*>(ptr);
|
|
|
|
vec->insert(vec->end(), chPtr, chPtr + realsize);
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
static size_t curlDebugCallback(CURL* /*unused*/, curl_infotype /*unused*/,
|
|
|
|
char* chPtr, size_t size, void* data)
|
2015-01-14 18:54:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<char>* vec = static_cast<std::vector<char>*>(data);
|
2015-01-14 18:54:25 +03:00
|
|
|
vec->insert(vec->end(), chPtr, chPtr + size);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCTestCurl::SetCurlOptions(std::vector<std::string> const& args)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator i = args.begin();
|
|
|
|
i != args.end(); ++i) {
|
|
|
|
if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->VerifyPeerOff = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*i == "CURLOPT_SSL_VERIFYHOST_OFF") {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->VerifyHostOff = true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCTestCurl::InitCurl()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Curl) {
|
2015-01-14 18:54:25 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->VerifyPeerOff) {
|
2015-01-14 18:54:25 +03:00
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->VerifyHostOff) {
|
2015-01-14 18:54:25 +03:00
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-05-27 00:05:30 +03:00
|
|
|
if (!this->HTTPProxy.empty()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
|
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
|
2016-05-27 00:05:30 +03:00
|
|
|
if (!this->HTTPProxyAuth.empty()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
|
|
|
|
this->HTTPProxyAuth.c_str());
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->UseHttp10) {
|
2015-01-14 18:54:25 +03:00
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
// enable HTTP ERROR parsing
|
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCTestCurl::UploadFile(std::string const& local_file,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string const& url, std::string const& fields,
|
2015-01-14 18:54:25 +03:00
|
|
|
std::string& response)
|
|
|
|
{
|
|
|
|
response = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->InitCurl()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
/* enable uploading */
|
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
|
|
|
|
// if there is little to no activity for too long stop submitting
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->TimeOutSeconds) {
|
2015-01-14 18:54:25 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME,
|
|
|
|
this->TimeOutSeconds);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
/* HTTP PUT please */
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_PUT, 1);
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_VERBOSE, 1);
|
|
|
|
|
|
|
|
FILE* ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!ftpfile) {
|
2015-01-14 18:54:25 +03:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE,
|
|
|
|
"Could not open file for upload: " << local_file << "\n");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
// set the url
|
|
|
|
std::string upload_url = url;
|
|
|
|
upload_url += "?";
|
|
|
|
upload_url += fields;
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_URL, upload_url.c_str());
|
|
|
|
// now specify which file to upload
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_INFILE, ftpfile);
|
|
|
|
unsigned long filelen = cmSystemTools::FileLength(local_file);
|
|
|
|
// and give the size of the upload (optional)
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_INFILESIZE,
|
|
|
|
static_cast<long>(filelen));
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
|
|
|
|
curlWriteMemoryCallback);
|
2016-05-16 17:34:04 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
|
2015-10-08 00:51:26 +03:00
|
|
|
// Be sure to set Content-Type to satisfy fussy modsecurity rules
|
2016-05-16 17:34:04 +03:00
|
|
|
struct curl_slist* headers =
|
2016-06-27 23:44:16 +03:00
|
|
|
::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml");
|
2015-10-08 00:51:26 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
|
2015-01-14 18:54:25 +03:00
|
|
|
std::vector<char> responseData;
|
|
|
|
std::vector<char> debugData;
|
2016-05-16 17:34:04 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
|
2015-01-14 18:54:25 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
|
|
|
|
// Now run off and do what you've been told!
|
|
|
|
::curl_easy_perform(this->Curl);
|
|
|
|
::fclose(ftpfile);
|
2015-10-08 00:51:26 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, NULL);
|
|
|
|
::curl_slist_free_all(headers);
|
2015-01-14 18:54:25 +03:00
|
|
|
|
2016-05-27 00:05:30 +03:00
|
|
|
if (!responseData.empty()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
response = std::string(responseData.begin(), responseData.end());
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Curl response: ["
|
|
|
|
<< response << "]\n");
|
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
std::string curlDebug;
|
2016-05-27 00:05:30 +03:00
|
|
|
if (!debugData.empty()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
curlDebug = std::string(debugData.begin(), debugData.end());
|
|
|
|
cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-05-27 00:05:30 +03:00
|
|
|
if (response.empty()) {
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n"
|
|
|
|
<< curlDebug);
|
2015-01-14 18:54:25 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCTestCurl::HttpRequest(std::string const& url,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string const& fields, std::string& response)
|
2015-01-14 18:54:25 +03:00
|
|
|
{
|
|
|
|
response = "";
|
|
|
|
cmCTestLog(this->CTest, DEBUG, "HttpRequest\n"
|
2016-05-16 17:34:04 +03:00
|
|
|
<< "url: " << url << "\n"
|
|
|
|
<< "fields " << fields << "\n");
|
|
|
|
if (!this->InitCurl()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_POST, 1);
|
|
|
|
curl_easy_setopt(this->Curl, CURLOPT_POSTFIELDS, fields.c_str());
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_URL, url.c_str());
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_FOLLOWLOCATION, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
// set response options
|
2015-01-14 18:54:25 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
|
|
|
|
curlWriteMemoryCallback);
|
2016-05-16 17:34:04 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
|
2015-01-14 18:54:25 +03:00
|
|
|
std::vector<char> responseData;
|
|
|
|
std::vector<char> debugData;
|
2016-05-16 17:34:04 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
|
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
|
2015-01-14 18:54:25 +03:00
|
|
|
::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
|
|
|
|
|
|
|
|
CURLcode res = ::curl_easy_perform(this->Curl);
|
|
|
|
|
2016-05-27 00:05:30 +03:00
|
|
|
if (!responseData.empty()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
response = std::string(responseData.begin(), responseData.end());
|
|
|
|
cmCTestLog(this->CTest, DEBUG, "Curl response: [" << response << "]\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-05-27 00:05:30 +03:00
|
|
|
if (!debugData.empty()) {
|
2015-01-14 18:54:25 +03:00
|
|
|
std::string curlDebug = std::string(debugData.begin(), debugData.end());
|
|
|
|
cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
cmCTestLog(this->CTest, DEBUG, "Curl res: " << res << "\n");
|
|
|
|
return (res == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCTestCurl::SetProxyType()
|
|
|
|
{
|
2016-07-08 00:54:05 +03:00
|
|
|
this->HTTPProxy = "";
|
|
|
|
// this is the default
|
|
|
|
this->HTTPProxyType = CURLPROXY_HTTP;
|
|
|
|
this->HTTPProxyAuth = "";
|
|
|
|
if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
|
|
|
|
std::string port;
|
|
|
|
if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->HTTPProxy += ":";
|
2016-07-08 00:54:05 +03:00
|
|
|
this->HTTPProxy += port;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-07-08 00:54:05 +03:00
|
|
|
std::string type;
|
|
|
|
if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
|
2015-01-14 18:54:25 +03:00
|
|
|
// HTTP/SOCKS4/SOCKS5
|
2016-05-16 17:34:04 +03:00
|
|
|
if (type == "HTTP") {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->HTTPProxyType = CURLPROXY_HTTP;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (type == "SOCKS4") {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->HTTPProxyType = CURLPROXY_SOCKS4;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (type == "SOCKS5") {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->HTTPProxyType = CURLPROXY_SOCKS5;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-07-08 00:54:05 +03:00
|
|
|
cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
|
|
|
|
std::string passwd;
|
|
|
|
if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
|
2015-01-14 18:54:25 +03:00
|
|
|
this->HTTPProxyAuth += ":";
|
2016-07-08 00:54:05 +03:00
|
|
|
this->HTTPProxyAuth += passwd;
|
2015-01-14 18:54:25 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-14 18:54:25 +03:00
|
|
|
}
|