cmCTestCurl.cxx 8.52 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
21
22
23
24
25
26
27

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;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  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;
43
44
}

45
46
47
namespace {
static size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
                                      void* data)
48
49
50
{
  int realsize = (int)(size * nmemb);

51
  std::vector<char>* vec = static_cast<std::vector<char>*>(data);
52
53
54
55
56
  const char* chPtr = static_cast<char*>(ptr);
  vec->insert(vec->end(), chPtr, chPtr + realsize);
  return realsize;
}

57
58
static size_t curlDebugCallback(CURL*, curl_infotype, char* chPtr, size_t size,
                                void* data)
59
{
60
  std::vector<char>* vec = static_cast<std::vector<char>*>(data);
61
62
63
64
65
66
67
68
  vec->insert(vec->end(), chPtr, chPtr + size);

  return size;
}
}

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

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

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

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

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

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

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

void cmCTestCurl::SetProxyType()
{
222
  if (cmSystemTools::GetEnv("HTTP_PROXY")) {
223
    this->HTTPProxy = cmSystemTools::GetEnv("HTTP_PROXY");
224
    if (cmSystemTools::GetEnv("HTTP_PROXY_PORT")) {
225
226
      this->HTTPProxy += ":";
      this->HTTPProxy += cmSystemTools::GetEnv("HTTP_PROXY_PORT");
227
228
    }
    if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE")) {
229
230
231
232
      // this is the default
      this->HTTPProxyType = CURLPROXY_HTTP;
      std::string type = cmSystemTools::GetEnv("HTTP_PROXY_TYPE");
      // HTTP/SOCKS4/SOCKS5
233
      if (type == "HTTP") {
234
        this->HTTPProxyType = CURLPROXY_HTTP;
235
      } else if (type == "SOCKS4") {
236
        this->HTTPProxyType = CURLPROXY_SOCKS4;
237
      } else if (type == "SOCKS5") {
238
239
        this->HTTPProxyType = CURLPROXY_SOCKS5;
      }
240
241
    }
    if (cmSystemTools::GetEnv("HTTP_PROXY_USER")) {
242
      this->HTTPProxyAuth = cmSystemTools::GetEnv("HTTP_PROXY_USER");
243
244
    }
    if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD")) {
245
246
247
      this->HTTPProxyAuth += ":";
      this->HTTPProxyAuth += cmSystemTools::GetEnv("HTTP_PROXY_PASSWD");
    }
248
  }
249
}