cmCTestCurl.cxx 9.14 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 "cmCurl.h"
7
#include "cmSystemTools.h"
8

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

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

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

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

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

  return size;
}
}

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

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

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

107
108
109
110
  return true;
}

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

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

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

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

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

  ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
227
  CURLcode res = ::curl_easy_perform(this->Curl);
228
  ::curl_slist_free_all(headers);
229

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

void cmCTestCurl::SetProxyType()
{
247
  this->HTTPProxy.clear();
248
249
  // this is the default
  this->HTTPProxyType = CURLPROXY_HTTP;
250
  this->HTTPProxyAuth.clear();
251
252
253
  if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
    std::string port;
    if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
254
      this->HTTPProxy += ":";
255
      this->HTTPProxy += port;
256
    }
257
258
    std::string type;
    if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
259
      // HTTP/SOCKS4/SOCKS5
260
      if (type == "HTTP") {
261
        this->HTTPProxyType = CURLPROXY_HTTP;
262
      } else if (type == "SOCKS4") {
263
        this->HTTPProxyType = CURLPROXY_SOCKS4;
264
      } else if (type == "SOCKS5") {
265
266
        this->HTTPProxyType = CURLPROXY_SOCKS5;
      }
267
    }
268
269
270
    cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
    std::string passwd;
    if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
271
      this->HTTPProxyAuth += ":";
272
      this->HTTPProxyAuth += passwd;
273
    }
274
  }
275
}