cmCTestCurl.cxx 8.08 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
11
#include <cmConfigure.h>
#include <ostream>
#include <stdio.h>

12
13
14
15
16
17
18
19
20
21
22
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;
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
44
45
{
  int realsize = (int)(size * nmemb);

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
66
  for (std::vector<std::string>::const_iterator i = args.begin();
       i != args.end(); ++i) {
    if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
67
      this->VerifyPeerOff = true;
68
69
    }
    if (*i == "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
  }
  if (this->VerifyPeerOff) {
81
    curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
82
83
  }
  if (this->VerifyHostOff) {
84
    curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
85
  }
86
  if (!this->HTTPProxy.empty()) {
87
88
    curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
    curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
89
    if (!this->HTTPProxyAuth.empty()) {
90
91
92
      curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
                       this->HTTPProxyAuth.c_str());
    }
93
94
  }
  if (this->UseHttp10) {
95
    curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
96
  }
97
98
99
100
101
102
  // enable HTTP ERROR parsing
  curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  return true;
}

bool cmCTestCurl::UploadFile(std::string const& local_file,
103
                             std::string const& url, std::string const& fields,
104
105
106
                             std::string& response)
{
  response = "";
107
  if (!this->InitCurl()) {
108
109
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
110
  }
111
112
113
  /* enable uploading */
  curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
  // if there is little to no activity for too long stop submitting
114
  if (this->TimeOutSeconds) {
115
116
117
    ::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
    ::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME,
                       this->TimeOutSeconds);
118
  }
119
120
121
122
123
  /* 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");
124
  if (!ftpfile) {
125
126
127
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "Could not open file for upload: " << local_file << "\n");
    return false;
128
  }
129
130
131
132
133
134
135
136
137
138
139
140
141
  // 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);
142
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
143
  // Be sure to set Content-Type to satisfy fussy modsecurity rules
144
  struct curl_slist* headers =
Daniel Pfeifer's avatar
Daniel Pfeifer committed
145
    ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml");
146
  ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
147
148
  std::vector<char> responseData;
  std::vector<char> debugData;
149
150
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
151
152
153
154
  ::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);
155
156
  ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, NULL);
  ::curl_slist_free_all(headers);
157

158
  if (!responseData.empty()) {
159
    response = std::string(responseData.begin(), responseData.end());
160
161
162
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Curl response: ["
                 << response << "]\n");
  }
163
  std::string curlDebug;
164
  if (!debugData.empty()) {
165
166
    curlDebug = std::string(debugData.begin(), debugData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
167
  }
168
  if (response.empty()) {
169
170
    cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n"
                 << curlDebug);
171
    return false;
172
  }
173
174
175
176
  return true;
}

bool cmCTestCurl::HttpRequest(std::string const& url,
177
                              std::string const& fields, std::string& response)
178
179
180
{
  response = "";
  cmCTestLog(this->CTest, DEBUG, "HttpRequest\n"
181
182
183
               << "url: " << url << "\n"
               << "fields " << fields << "\n");
  if (!this->InitCurl()) {
184
185
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
186
  }
187
188
189
190
  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);
191
  // set response options
192
193
  ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
                     curlWriteMemoryCallback);
194
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
195
196
  std::vector<char> responseData;
  std::vector<char> debugData;
197
198
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
199
200
201
202
  ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);

  CURLcode res = ::curl_easy_perform(this->Curl);

203
  if (!responseData.empty()) {
204
205
    response = std::string(responseData.begin(), responseData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl response: [" << response << "]\n");
206
  }
207
  if (!debugData.empty()) {
208
209
    std::string curlDebug = std::string(debugData.begin(), debugData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
210
  }
211
212
213
214
215
216
  cmCTestLog(this->CTest, DEBUG, "Curl res: " << res << "\n");
  return (res == 0);
}

void cmCTestCurl::SetProxyType()
{
217
218
219
220
221
222
223
  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)) {
224
      this->HTTPProxy += ":";
225
      this->HTTPProxy += port;
226
    }
227
228
    std::string type;
    if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
229
      // HTTP/SOCKS4/SOCKS5
230
      if (type == "HTTP") {
231
        this->HTTPProxyType = CURLPROXY_HTTP;
232
      } else if (type == "SOCKS4") {
233
        this->HTTPProxyType = CURLPROXY_SOCKS4;
234
      } else if (type == "SOCKS5") {
235
236
        this->HTTPProxyType = CURLPROXY_SOCKS5;
      }
237
    }
238
239
240
    cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
    std::string passwd;
    if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
241
      this->HTTPProxyAuth += ":";
242
      this->HTTPProxyAuth += passwd;
243
    }
244
  }
245
}