cmCTestCurl.cxx 8.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2015 Kitware, Inc.

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#include "cmCTestCurl.h"

#include "cmCTest.h"
15
#include "cmSystemTools.h"
16

17
18
19
20
#include <cmConfigure.h>
#include <ostream>
#include <stdio.h>

21
22
23
24
25
26
27
28
29
30
31
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;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  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;
47
48
}

49
50
51
namespace {
static size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
                                      void* data)
52
53
54
{
  int realsize = (int)(size * nmemb);

55
  std::vector<char>* vec = static_cast<std::vector<char>*>(data);
56
57
58
59
60
  const char* chPtr = static_cast<char*>(ptr);
  vec->insert(vec->end(), chPtr, chPtr + realsize);
  return realsize;
}

61
62
static size_t curlDebugCallback(CURL* /*unused*/, curl_infotype /*unused*/,
                                char* chPtr, size_t size, void* data)
63
{
64
  std::vector<char>* vec = static_cast<std::vector<char>*>(data);
65
66
67
68
69
70
71
72
  vec->insert(vec->end(), chPtr, chPtr + size);

  return size;
}
}

void cmCTestCurl::SetCurlOptions(std::vector<std::string> const& args)
{
73
74
75
  for (std::vector<std::string>::const_iterator i = args.begin();
       i != args.end(); ++i) {
    if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
76
      this->VerifyPeerOff = true;
77
78
    }
    if (*i == "CURLOPT_SSL_VERIFYHOST_OFF") {
79
80
      this->VerifyHostOff = true;
    }
81
  }
82
83
84
85
}

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

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

167
  if (!responseData.empty()) {
168
    response = std::string(responseData.begin(), responseData.end());
169
170
171
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Curl response: ["
                 << response << "]\n");
  }
172
  std::string curlDebug;
173
  if (!debugData.empty()) {
174
175
    curlDebug = std::string(debugData.begin(), debugData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
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
189
{
  response = "";
  cmCTestLog(this->CTest, DEBUG, "HttpRequest\n"
190
191
192
               << "url: " << url << "\n"
               << "fields " << fields << "\n");
  if (!this->InitCurl()) {
193
194
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
195
  }
196
197
198
199
  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);
200
  // set response options
201
202
  ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
                     curlWriteMemoryCallback);
203
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
204
205
  std::vector<char> responseData;
  std::vector<char> debugData;
206
207
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
208
209
210
211
  ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);

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

212
  if (!responseData.empty()) {
213
214
    response = std::string(responseData.begin(), responseData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl response: [" << response << "]\n");
215
  }
216
  if (!debugData.empty()) {
217
218
    std::string curlDebug = std::string(debugData.begin(), debugData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
219
  }
220
221
222
223
224
225
  cmCTestLog(this->CTest, DEBUG, "Curl res: " << res << "\n");
  return (res == 0);
}

void cmCTestCurl::SetProxyType()
{
226
227
228
229
230
231
232
  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)) {
233
      this->HTTPProxy += ":";
234
      this->HTTPProxy += port;
235
    }
236
237
    std::string type;
    if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
238
      // HTTP/SOCKS4/SOCKS5
239
      if (type == "HTTP") {
240
        this->HTTPProxyType = CURLPROXY_HTTP;
241
      } else if (type == "SOCKS4") {
242
        this->HTTPProxyType = CURLPROXY_SOCKS4;
243
      } else if (type == "SOCKS5") {
244
245
        this->HTTPProxyType = CURLPROXY_SOCKS5;
      }
246
    }
247
248
249
    cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
    std::string passwd;
    if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
250
      this->HTTPProxyAuth += ":";
251
      this->HTTPProxyAuth += passwd;
252
    }
253
  }
254
}