cmCTestCurl.cxx 8.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*============================================================================
  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 "cmSystemTools.h"
#include "cmCTest.h"

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}

namespace
{
static size_t
curlWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
  void *data)
{
  int realsize = (int)(size * nmemb);

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

static size_t
curlDebugCallback(CURL *, curl_infotype, char *chPtr,
  size_t size, void *data)
{
  std::vector<char> *vec
    = static_cast<std::vector<char>* >(data);
  vec->insert(vec->end(), chPtr, chPtr + size);

  return size;
}

}

void cmCTestCurl::SetCurlOptions(std::vector<std::string> const& args)
{
  for( std::vector<std::string>::const_iterator i = args.begin();
       i != args.end(); ++i)
    {
    if(*i == "CURLOPT_SSL_VERIFYPEER_OFF")
      {
      this->VerifyPeerOff = true;
      }
    if(*i == "CURLOPT_SSL_VERIFYHOST_OFF")
      {
      this->VerifyHostOff = true;
      }
    }
}

bool cmCTestCurl::InitCurl()
{
  if(!this->Curl)
    {
    return false;
    }
  if(this->VerifyPeerOff)
    {
    curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
    }
  if(this->VerifyHostOff)
    {
    curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
    }
  if(this->HTTPProxy.size())
    {
    curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
    curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
    if (this->HTTPProxyAuth.size() > 0)
      {
      curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
                       this->HTTPProxyAuth.c_str());
      }
    }
  if(this->UseHttp10)
    {
    curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    }
  // enable HTTP ERROR parsing
  curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  return true;
}


bool cmCTestCurl::UploadFile(std::string const& local_file,
                             std::string const& url,
                             std::string const& fields,
                             std::string& response)
{
  response = "";
  if(!this->InitCurl())
    {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
    }
  /* enable uploading */
  curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
  // 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);
    }
  /* 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");
  if(!ftpfile)
    {
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "Could not open file for upload: " << local_file << "\n");
    return false;
    }
  // 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);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION,
                     curlDebugCallback);
  std::vector<char> responseData;
  std::vector<char> debugData;
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void *)&responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void *)&debugData);
  ::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);

  if ( responseData.size() > 0 )
    {
    response = std::string(responseData.begin(), responseData.end());
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
               "Curl response: [" << response << "]\n");
    }
  std::string curlDebug;
  if ( debugData.size() > 0 )
    {
    curlDebug = std::string(debugData.begin(), debugData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
    }
  if(response.size() == 0)
    {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n" <<
      curlDebug);
    return false;
    }
  return true;
}

bool cmCTestCurl::HttpRequest(std::string const& url,
                              std::string const& fields,
                              std::string& response)
{
  response = "";
  cmCTestLog(this->CTest, DEBUG, "HttpRequest\n"
             << "url: " << url << "\n"
             << "fields " << fields << "\n");
  if(!this->InitCurl())
    {
    cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
    return false;
    }
  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);
  //set response options
  ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
                     curlWriteMemoryCallback);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION,
        curlDebugCallback);
  std::vector<char> responseData;
  std::vector<char> debugData;
  ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void *)&responseData);
  ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void *)&debugData);
  ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);

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

  if ( responseData.size() > 0 )
    {
    response = std::string(responseData.begin(), responseData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl response: [" << response << "]\n");
    }
  if ( debugData.size() > 0 )
    {
    std::string curlDebug = std::string(debugData.begin(), debugData.end());
    cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
    }
  cmCTestLog(this->CTest, DEBUG, "Curl res: " << res << "\n");
  return (res == 0);
}

void cmCTestCurl::SetProxyType()
{
  if ( cmSystemTools::GetEnv("HTTP_PROXY") )
    {
    this->HTTPProxy = cmSystemTools::GetEnv("HTTP_PROXY");
    if ( cmSystemTools::GetEnv("HTTP_PROXY_PORT") )
      {
      this->HTTPProxy += ":";
      this->HTTPProxy += cmSystemTools::GetEnv("HTTP_PROXY_PORT");
      }
    if ( cmSystemTools::GetEnv("HTTP_PROXY_TYPE") )
      {
      // this is the default
      this->HTTPProxyType = CURLPROXY_HTTP;
      std::string type = cmSystemTools::GetEnv("HTTP_PROXY_TYPE");
      // HTTP/SOCKS4/SOCKS5
      if ( type == "HTTP" )
        {
        this->HTTPProxyType = CURLPROXY_HTTP;
        }
      else if ( type == "SOCKS4" )
        {
        this->HTTPProxyType = CURLPROXY_SOCKS4;
        }
      else if ( type == "SOCKS5" )
        {
        this->HTTPProxyType = CURLPROXY_SOCKS5;
        }
      }
    if ( cmSystemTools::GetEnv("HTTP_PROXY_USER") )
      {
      this->HTTPProxyAuth = cmSystemTools::GetEnv("HTTP_PROXY_USER");
      }
    if ( cmSystemTools::GetEnv("HTTP_PROXY_PASSWD") )
      {
      this->HTTPProxyAuth += ":";
      this->HTTPProxyAuth += cmSystemTools::GetEnv("HTTP_PROXY_PASSWD");
      }
    }
}