cmake/Source/CTest/cmCTestCurl.cxx

271 lines
9.0 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmCTestCurl.h"
2020-02-01 23:06:01 +01:00
#include <cstdio>
#include <ostream>
2020-08-30 11:54:41 +02:00
#include <cmext/algorithm>
2015-04-27 22:25:09 +02:00
#include "cmCTest.h"
2018-08-09 18:06:22 +02:00
#include "cmCurl.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2015-04-27 22:25:09 +02: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;
2017-04-14 19:02:05 +02:00
this->Quiet = false;
2015-04-27 22:25:09 +02:00
this->TimeOutSeconds = 0;
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;
}
2016-07-09 11:21:54 +02:00
namespace {
2017-04-14 19:02:05 +02:00
size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
void* data)
2015-04-27 22:25:09 +02:00
{
2018-01-26 17:06:56 +01:00
int realsize = static_cast<int>(size * nmemb);
2015-04-27 22:25:09 +02:00
const char* chPtr = static_cast<char*>(ptr);
2020-08-30 11:54:41 +02:00
cm::append(*static_cast<std::vector<char>*>(data), chPtr, chPtr + realsize);
2015-04-27 22:25:09 +02:00
return realsize;
}
2017-04-14 19:02:05 +02:00
size_t curlDebugCallback(CURL* /*unused*/, curl_infotype /*unused*/,
char* chPtr, size_t size, void* data)
2015-04-27 22:25:09 +02:00
{
2020-08-30 11:54:41 +02:00
cm::append(*static_cast<std::vector<char>*>(data), chPtr, chPtr + size);
return 0;
2015-04-27 22:25:09 +02:00
}
}
void cmCTestCurl::SetCurlOptions(std::vector<std::string> const& args)
{
2018-01-26 17:06:56 +01:00
for (std::string const& arg : args) {
if (arg == "CURLOPT_SSL_VERIFYPEER_OFF") {
2015-04-27 22:25:09 +02:00
this->VerifyPeerOff = true;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
if (arg == "CURLOPT_SSL_VERIFYHOST_OFF") {
2015-04-27 22:25:09 +02:00
this->VerifyHostOff = true;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
bool cmCTestCurl::InitCurl()
{
2016-07-09 11:21:54 +02:00
if (!this->Curl) {
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2018-08-09 18:06:22 +02:00
cmCurlSetCAInfo(this->Curl);
2016-07-09 11:21:54 +02:00
if (this->VerifyPeerOff) {
2015-04-27 22:25:09 +02:00
curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
2016-07-09 11:21:54 +02:00
}
if (this->VerifyHostOff) {
2015-04-27 22:25:09 +02:00
curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
if (!this->HTTPProxy.empty()) {
2015-04-27 22:25:09 +02:00
curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
2016-10-30 18:24:19 +01:00
if (!this->HTTPProxyAuth.empty()) {
2015-04-27 22:25:09 +02:00
curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
this->HTTPProxyAuth.c_str());
}
2016-07-09 11:21:54 +02:00
}
if (this->UseHttp10) {
2015-04-27 22:25:09 +02:00
curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// enable HTTP ERROR parsing
curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
2017-04-14 19:02:05 +02:00
// if there is little to no activity for too long stop submitting
if (this->TimeOutSeconds) {
curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME, this->TimeOutSeconds);
}
2015-04-27 22:25:09 +02:00
return true;
}
bool cmCTestCurl::UploadFile(std::string const& local_file,
2016-07-09 11:21:54 +02:00
std::string const& url, std::string const& fields,
2015-04-27 22:25:09 +02:00
std::string& response)
{
2018-01-26 17:06:56 +01:00
response.clear();
2016-07-09 11:21:54 +02:00
if (!this->InitCurl()) {
2023-05-23 16:38:00 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed\n");
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
/* enable uploading */
curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
2017-04-14 19:02:05 +02:00
2015-04-27 22:25:09 +02:00
::curl_easy_setopt(this->Curl, CURLOPT_VERBOSE, 1);
FILE* ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
2016-07-09 11:21:54 +02:00
if (!ftpfile) {
2015-04-27 22:25:09 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Could not open file for upload: " << local_file << "\n");
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// set the url
2020-02-01 23:06:01 +01:00
std::string upload_url = cmStrCat(url, '?', fields);
2015-04-27 22:25:09 +02:00
::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-07-09 11:21:54 +02:00
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
2017-07-20 19:35:53 +02:00
// Set Content-Type to satisfy fussy modsecurity rules.
2016-07-09 11:21:54 +02:00
struct curl_slist* headers =
2018-01-26 17:06:56 +01:00
::curl_slist_append(nullptr, "Content-Type: text/xml");
2017-07-20 19:35:53 +02:00
// Add any additional headers that the user specified.
2018-01-26 17:06:56 +01:00
for (std::string const& h : this->HttpHeaders) {
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
2018-01-26 17:06:56 +01:00
" Add HTTP Header: \"" << h << "\"" << std::endl,
2017-07-20 19:35:53 +02:00
this->Quiet);
2018-01-26 17:06:56 +01:00
headers = ::curl_slist_append(headers, h.c_str());
2017-07-20 19:35:53 +02:00
}
2015-11-17 17:22:37 +01:00
::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
2015-04-27 22:25:09 +02:00
std::vector<char> responseData;
std::vector<char> debugData;
2018-01-26 17:06:56 +01:00
::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
2015-04-27 22:25:09 +02: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);
2023-07-02 19:51:09 +02:00
::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, nullptr);
2015-11-17 17:22:37 +01:00
::curl_slist_free_all(headers);
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
if (!responseData.empty()) {
2015-04-27 22:25:09 +02:00
response = std::string(responseData.begin(), responseData.end());
2017-04-14 19:02:05 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Curl response: [" << response << "]\n", this->Quiet);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::string curlDebug;
2016-10-30 18:24:19 +01:00
if (!debugData.empty()) {
2015-04-27 22:25:09 +02:00
curlDebug = std::string(debugData.begin(), debugData.end());
2017-04-14 19:02:05 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
"Curl debug: [" << curlDebug << "]\n", this->Quiet);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
if (response.empty()) {
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE,
"No response from server.\n"
2023-05-23 16:38:00 +02:00
<< curlDebug << std::endl);
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return true;
}
bool cmCTestCurl::HttpRequest(std::string const& url,
2016-07-09 11:21:54 +02:00
std::string const& fields, std::string& response)
2015-04-27 22:25:09 +02:00
{
2018-01-26 17:06:56 +01:00
response.clear();
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
"HttpRequest\n"
2017-04-14 19:02:05 +02:00
<< "url: " << url << "\n"
<< "fields " << fields << "\n",
this->Quiet);
2016-07-09 11:21:54 +02:00
if (!this->InitCurl()) {
2023-05-23 16:38:00 +02:00
cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed\n");
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02: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-07-09 11:21:54 +02:00
// set response options
2015-04-27 22:25:09 +02:00
::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
curlWriteMemoryCallback);
2016-07-09 11:21:54 +02:00
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
2015-04-27 22:25:09 +02:00
std::vector<char> responseData;
std::vector<char> debugData;
2018-01-26 17:06:56 +01:00
::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
2015-04-27 22:25:09 +02:00
::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
2017-07-20 19:35:53 +02:00
// Add headers if any were specified.
2018-01-26 17:06:56 +01:00
struct curl_slist* headers = nullptr;
2017-07-20 19:35:53 +02:00
if (!this->HttpHeaders.empty()) {
2018-01-26 17:06:56 +01:00
for (std::string const& h : this->HttpHeaders) {
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
2018-01-26 17:06:56 +01:00
" Add HTTP Header: \"" << h << "\"" << std::endl,
2017-07-20 19:35:53 +02:00
this->Quiet);
2018-01-26 17:06:56 +01:00
headers = ::curl_slist_append(headers, h.c_str());
2017-07-20 19:35:53 +02:00
}
}
::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
2015-04-27 22:25:09 +02:00
CURLcode res = ::curl_easy_perform(this->Curl);
2017-07-20 19:35:53 +02:00
::curl_slist_free_all(headers);
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
if (!responseData.empty()) {
2015-04-27 22:25:09 +02:00
response = std::string(responseData.begin(), responseData.end());
2017-04-14 19:02:05 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
"Curl response: [" << response << "]\n", this->Quiet);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
if (!debugData.empty()) {
2015-04-27 22:25:09 +02:00
std::string curlDebug = std::string(debugData.begin(), debugData.end());
2017-04-14 19:02:05 +02:00
cmCTestOptionalLog(this->CTest, DEBUG,
"Curl debug: [" << curlDebug << "]\n", this->Quiet);
2016-07-09 11:21:54 +02:00
}
2017-04-14 19:02:05 +02:00
cmCTestOptionalLog(this->CTest, DEBUG, "Curl res: " << res << "\n",
this->Quiet);
2015-04-27 22:25:09 +02:00
return (res == 0);
}
void cmCTestCurl::SetProxyType()
{
2018-01-26 17:06:56 +01:00
this->HTTPProxy.clear();
2016-10-30 18:24:19 +01:00
// this is the default
this->HTTPProxyType = CURLPROXY_HTTP;
2018-01-26 17:06:56 +01:00
this->HTTPProxyAuth.clear();
2016-10-30 18:24:19 +01:00
if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
std::string port;
if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
2015-04-27 22:25:09 +02:00
this->HTTPProxy += ":";
2016-10-30 18:24:19 +01:00
this->HTTPProxy += port;
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
std::string type;
if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
2015-04-27 22:25:09 +02:00
// HTTP/SOCKS4/SOCKS5
2016-07-09 11:21:54 +02:00
if (type == "HTTP") {
2015-04-27 22:25:09 +02:00
this->HTTPProxyType = CURLPROXY_HTTP;
2016-07-09 11:21:54 +02:00
} else if (type == "SOCKS4") {
2015-04-27 22:25:09 +02:00
this->HTTPProxyType = CURLPROXY_SOCKS4;
2016-07-09 11:21:54 +02:00
} else if (type == "SOCKS5") {
2015-04-27 22:25:09 +02:00
this->HTTPProxyType = CURLPROXY_SOCKS5;
}
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
std::string passwd;
if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
2015-04-27 22:25:09 +02:00
this->HTTPProxyAuth += ":";
2016-10-30 18:24:19 +01:00
this->HTTPProxyAuth += passwd;
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}