cmCTestCurl.cxx 9.09 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
                             std::string& response)
{
112
  response.clear();
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"
180
                 << curlDebug);
181
    return false;
182
  }
183
184
185
186
  return true;
}

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

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

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

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

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