cmCTestCurl.cxx 9.04 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
5
#include "cmCTestCurl.h"

#include "cmCTest.h"
6
#include "cmSystemTools.h"
7

8
9
10
#include <ostream>
#include <stdio.h>

11
12
13
14
15
16
17
18
19
20
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;
21
  this->Quiet = false;
22
  this->TimeOutSeconds = 0;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  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;
38
39
}

40
namespace {
41
42
size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
                               void* data)
43
{
Daniel Pfeifer's avatar
Daniel Pfeifer committed
44
  int realsize = static_cast<int>(size * nmemb);
45

46
  std::vector<char>* vec = static_cast<std::vector<char>*>(data);
47
48
49
50
51
  const char* chPtr = static_cast<char*>(ptr);
  vec->insert(vec->end(), chPtr, chPtr + realsize);
  return realsize;
}

52
53
size_t curlDebugCallback(CURL* /*unused*/, curl_infotype /*unused*/,
                         char* chPtr, size_t size, void* data)
54
{
55
  std::vector<char>* vec = static_cast<std::vector<char>*>(data);
56
57
58
59
60
61
62
63
  vec->insert(vec->end(), chPtr, chPtr + size);

  return size;
}
}

void cmCTestCurl::SetCurlOptions(std::vector<std::string> const& args)
{
64
65
  for (std::string const& arg : args) {
    if (arg == "CURLOPT_SSL_VERIFYPEER_OFF") {
66
      this->VerifyPeerOff = true;
67
    }
68
    if (arg == "CURLOPT_SSL_VERIFYHOST_OFF") {
69
70
      this->VerifyHostOff = true;
    }
71
  }
72
73
74
75
}

bool cmCTestCurl::InitCurl()
{
76
  if (!this->Curl) {
77
    return false;
78
79
  }
  if (this->VerifyPeerOff) {
80
    curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
81
82
  }
  if (this->VerifyHostOff) {
83
    curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
84
  }
85
  if (!this->HTTPProxy.empty()) {
86
87
    curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
    curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
88
    if (!this->HTTPProxyAuth.empty()) {
89
90
91
      curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
                       this->HTTPProxyAuth.c_str());
    }
92
93
  }
  if (this->UseHttp10) {
94
    curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
95
  }
96
97
  // enable HTTP ERROR parsing
  curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
98
99
100
101
102
103
104

  // 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);
  }

105
106
107
108
  return true;
}

bool cmCTestCurl::UploadFile(std::string const& local_file,
109
                             std::string const& url, std::string const& fields,
110
111
112
                             std::string& response)
{
  response = "";
113
  if (!this->InitCurl()) {
114
115
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
116
  }
117
118
  /* enable uploading */
  curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
119

120
121
122
123
124
  /* 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");
125
  if (!ftpfile) {
126
127
128
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "Could not open file for upload: " << local_file << "\n");
    return false;
129
  }
130
131
132
133
134
135
136
137
138
139
140
141
142
  // 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);
143
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
144
  // Set Content-Type to satisfy fussy modsecurity rules.
145
  struct curl_slist* headers =
Daniel Pfeifer's avatar
Daniel Pfeifer committed
146
    ::curl_slist_append(nullptr, "Content-Type: text/xml");
147
  // Add any additional headers that the user specified.
148
  for (std::string const& h : this->HttpHeaders) {
149
    cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
150
                       "   Add HTTP Header: \"" << h << "\"" << std::endl,
151
                       this->Quiet);
152
    headers = ::curl_slist_append(headers, h.c_str());
153
  }
154
  ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
155
156
  std::vector<char> responseData;
  std::vector<char> debugData;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
157
158
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
159
160
161
162
  ::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);
163
164
  ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, NULL);
  ::curl_slist_free_all(headers);
165

166
  if (!responseData.empty()) {
167
    response = std::string(responseData.begin(), responseData.end());
168
169
    cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
                       "Curl response: [" << response << "]\n", this->Quiet);
170
  }
171
  std::string curlDebug;
172
  if (!debugData.empty()) {
173
    curlDebug = std::string(debugData.begin(), debugData.end());
174
175
    cmCTestOptionalLog(this->CTest, DEBUG,
                       "Curl debug: [" << curlDebug << "]\n", this->Quiet);
176
  }
177
  if (response.empty()) {
178
179
    cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n"
                 << curlDebug);
180
    return false;
181
  }
182
183
184
185
  return true;
}

bool cmCTestCurl::HttpRequest(std::string const& url,
186
                              std::string const& fields, std::string& response)
187
188
{
  response = "";
189
190
191
192
  cmCTestOptionalLog(this->CTest, DEBUG, "HttpRequest\n"
                       << "url: " << url << "\n"
                       << "fields " << fields << "\n",
                     this->Quiet);
193
  if (!this->InitCurl()) {
194
195
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
196
  }
197
198
199
200
  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);
201
  // set response options
202
203
  ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
                     curlWriteMemoryCallback);
204
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
205
206
  std::vector<char> responseData;
  std::vector<char> debugData;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
207
208
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
209
210
  ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);

211
  // Add headers if any were specified.
Daniel Pfeifer's avatar
Daniel Pfeifer committed
212
  struct curl_slist* headers = nullptr;
213
  if (!this->HttpHeaders.empty()) {
214
    for (std::string const& h : this->HttpHeaders) {
215
      cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
216
                         "   Add HTTP Header: \"" << h << "\"" << std::endl,
217
                         this->Quiet);
218
      headers = ::curl_slist_append(headers, h.c_str());
219
220
221
222
    }
  }

  ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
223
  CURLcode res = ::curl_easy_perform(this->Curl);
224
  ::curl_slist_free_all(headers);
225

226
  if (!responseData.empty()) {
227
    response = std::string(responseData.begin(), responseData.end());
228
229
    cmCTestOptionalLog(this->CTest, DEBUG,
                       "Curl response: [" << response << "]\n", this->Quiet);
230
  }
231
  if (!debugData.empty()) {
232
    std::string curlDebug = std::string(debugData.begin(), debugData.end());
233
234
    cmCTestOptionalLog(this->CTest, DEBUG,
                       "Curl debug: [" << curlDebug << "]\n", this->Quiet);
235
  }
236
237
  cmCTestOptionalLog(this->CTest, DEBUG, "Curl res: " << res << "\n",
                     this->Quiet);
238
239
240
241
242
  return (res == 0);
}

void cmCTestCurl::SetProxyType()
{
243
244
245
246
247
248
249
  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)) {
250
      this->HTTPProxy += ":";
251
      this->HTTPProxy += port;
252
    }
253
254
    std::string type;
    if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
255
      // HTTP/SOCKS4/SOCKS5
256
      if (type == "HTTP") {
257
        this->HTTPProxyType = CURLPROXY_HTTP;
258
      } else if (type == "SOCKS4") {
259
        this->HTTPProxyType = CURLPROXY_SOCKS4;
260
      } else if (type == "SOCKS5") {
261
262
        this->HTTPProxyType = CURLPROXY_SOCKS5;
      }
263
    }
264
265
266
    cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
    std::string passwd;
    if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
267
      this->HTTPProxyAuth += ":";
268
      this->HTTPProxyAuth += passwd;
269
    }
270
  }
271
}