summaryrefslogtreecommitdiff
path: root/lib/curl.c
blob: 93cb4a367ef640fa3f9d00cd534900ad09b1a25a (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * curlfuncs.cpp
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "curl.h"

//***************************************************************************
// Singelton
//***************************************************************************

cCurl curl;

std::string cCurl::sBuf = "";
int cCurl::curlInitialized = no;
cSystemNotification* cCurl::sysNotification = 0;

//***************************************************************************
// Callbacks
//***************************************************************************

size_t collect_data(void *ptr, size_t size, size_t nmemb, void* stream)
{
   std::string sTmp;
   size_t actualsize = size * nmemb;

   if (!stream)
   {
      sTmp.assign((char*)ptr, actualsize);
      cCurl::sBuf += sTmp;
   }
   else
   {
      fwrite(ptr, size, nmemb, (FILE*)stream);
   }

   return actualsize;
}

//***************************************************************************
// Object
//***************************************************************************

cCurl::cCurl()
{
   handle = 0;
}

cCurl::~cCurl()
{
   exit();
}

//***************************************************************************
// Create / Destroy
//***************************************************************************

int cCurl::create()
{
   if (!curlInitialized)
   {
      // call only once per process and *before* any thread is started!

      if (curl_global_init(CURL_GLOBAL_SSL /*CURL_GLOBAL_ALL*/) != 0)
      {
         tell(0, "Error, something went wrong with curl_global_init()");
         return fail;
      }

      curlInitialized = yes;
   }

   return done;
}

int cCurl::destroy()
{
   if (curlInitialized)
      curl_global_cleanup();

   curlInitialized = no;

   return done;
}

//***************************************************************************
// Init / Exit
//***************************************************************************

int cCurl::init(const char* httpproxy)
{
   if (!handle)
   {
      if (!(handle = curl_easy_init()))
      {
         tell(0, "Could not create new curl instance");
         return fail;
      }
   }

   // Reset Options

   if (!isEmpty(httpproxy))
   {
      curl_easy_setopt(handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
      curl_easy_setopt(handle, CURLOPT_PROXY, httpproxy);   // Specify HTTP proxy
   }

   curl_easy_setopt(handle, CURLOPT_HTTPHEADER, 0);
   curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, collect_data);
   curl_easy_setopt(handle, CURLOPT_WRITEDATA, 0);                        // Set option to write to string
   curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, yes);
   curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, 0);                   // Send header to this function
   curl_easy_setopt(handle, CURLOPT_WRITEHEADER, 0);                      // Pass some header details to this struct
   curl_easy_setopt(handle, CURLOPT_MAXFILESIZE, 100*1024*1024);          // Set maximum file size to get (bytes)
   curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1);                       // No progress meter
   curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);                         // No signaling
   curl_easy_setopt(handle, CURLOPT_TIMEOUT, 30);                         // Set timeout
   curl_easy_setopt(handle, CURLOPT_NOBODY, 0);                           //
   curl_easy_setopt(handle, CURLOPT_USERAGENT, CURL_USERAGENT);           // Some servers don't like requests
   curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "");

   return success;
}

int cCurl::exit()
{
   if (handle)
      curl_easy_cleanup(handle);

   handle = 0;

   return done;
}

//***************************************************************************
// Get Url
//***************************************************************************

int cCurl::GetUrl(const char* url, std::string* sOutput, const std::string& sReferer)
{
   CURLcode res;

   init();

   curl_easy_setopt(handle, CURLOPT_URL, url);            // Set the URL to get

   if (sReferer != "")
      curl_easy_setopt(handle, CURLOPT_REFERER, sReferer.c_str());

   curl_easy_setopt(handle, CURLOPT_HTTPGET, yes);
   curl_easy_setopt(handle, CURLOPT_FAILONERROR, yes);
   curl_easy_setopt(handle, CURLOPT_WRITEDATA, 0);       // Set option to write to string
   sBuf = "";

   if ((res = curl_easy_perform(handle)) != CURLE_OK)
   {
      long httpCode = 0;

      curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpCode);
      tell(1, "Error: Getting URL failed; %s (%d); http code was (%ld) [%s]",
           curl_easy_strerror(res), res, httpCode, url);

      *sOutput = "";

      return 0;
   }

   *sOutput = sBuf;

   return 1;
}

int cCurl::GetUrlFile(const char* url, const char* filename, const std::string& sReferer)
{
   int nRet = 0;

   init();

   // Point the output to a file

   FILE *fp;

   if ((fp = fopen(filename, "w")) == NULL)
      return 0;

   curl_easy_setopt(handle, CURLOPT_WRITEDATA, fp);       // Set option to write to file
   curl_easy_setopt(handle, CURLOPT_URL, url);            // Set the URL to get

   if (sReferer != "")
      curl_easy_setopt(handle, CURLOPT_REFERER, sReferer.c_str());

   curl_easy_setopt(handle, CURLOPT_HTTPGET, yes);

   if (curl_easy_perform(handle) == 0)
      nRet = 1;
   else
      nRet = 0;

   curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);     // Set option back to default (string)
   fclose(fp);

   return nRet;
}

int cCurl::PostUrl(const char *url, const std::string &sPost, std::string *sOutput, const std::string &sReferer)
{
  init();

  int retval = 1;
  std::string::size_type nStart = 0, nEnd, nPos;
  std::string sTmp, sName, sValue;
  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;

  // Add the POST variables here
  while ((nEnd = sPost.find("##", nStart)) != std::string::npos) {
    sTmp = sPost.substr(nStart, nEnd - nStart);
    if ((nPos = sTmp.find("=")) == std::string::npos)
      return 0;
    sName = sTmp.substr(0, nPos);
    sValue = sTmp.substr(nPos+1);
    curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, sName.c_str(), CURLFORM_COPYCONTENTS, sValue.c_str(), CURLFORM_END);
    nStart = nEnd + 2;
  }
  sTmp = sPost.substr(nStart);
  if ((nPos = sTmp.find("=")) == std::string::npos)
    return 0;
  sName = sTmp.substr(0, nPos);
  sValue = sTmp.substr(nPos+1);
  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, sName.c_str(), CURLFORM_COPYCONTENTS, sValue.c_str(), CURLFORM_END);

  retval = curl.DoPost(url, sOutput, sReferer, formpost, headerlist);

  curl_formfree(formpost);
  curl_slist_free_all(headerlist);
  return retval;
}

int cCurl::PostRaw(const char *url, const std::string &sPost, std::string *sOutput, const std::string &sReferer)
{
  init();

  int retval;
  struct curl_httppost *formpost=NULL;
  struct curl_slist *headerlist=NULL;

  curl_easy_setopt(handle, CURLOPT_POSTFIELDS, sPost.c_str());
  curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, 0); //FIXME: Should this be the size instead, in case this is binary string?

  retval = curl.DoPost(url, sOutput, sReferer, formpost, headerlist);

  curl_formfree(formpost);
  curl_slist_free_all(headerlist);
  return retval;
}

int cCurl::DoPost(const char *url, std::string *sOutput, const std::string &sReferer,
                      struct curl_httppost *formpost, struct curl_slist *headerlist)
{
  headerlist = curl_slist_append(headerlist, "Expect:");

  // Now do the form post
  curl_easy_setopt(handle, CURLOPT_URL, url);
  if (sReferer != "")
    curl_easy_setopt(handle, CURLOPT_REFERER, sReferer.c_str());
  curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);

  curl_easy_setopt(handle, CURLOPT_WRITEDATA, 0); // Set option to write to string
  sBuf = "";
  if (curl_easy_perform(handle) == 0) {
    *sOutput = sBuf;
    return 1;
  }
  else {
    // We have an error here mate!
    *sOutput = "";
    return 0;
  }
}

int cCurl::SetCookieFile(char *filename)
{
  init();

  if (curl_easy_setopt(handle, CURLOPT_COOKIEFILE, filename) != 0)
    return 0;
  if (curl_easy_setopt(handle, CURLOPT_COOKIEJAR, filename) != 0)
    return 0;
  return 1;
}

char* cCurl::EscapeUrl(const char *url)
{
   init();
   return curl_easy_escape(handle, url , strlen(url));
}

void cCurl::Free(char* str)
{
   curl_free(str);
}

//***************************************************************************
// Callbacks
//***************************************************************************

size_t cCurl::WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data)
{
   size_t realsize = size * nmemb;
   MemoryStruct* mem = (MemoryStruct*)data;

   if (sysNotification)
      sysNotification->check();

   if (mem->memory)
      mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
   else
      mem->memory = (char*)malloc(mem->size + realsize + 1);

   if (mem->memory)
   {
      memcpy (&(mem->memory[mem->size]), ptr, realsize);
      mem->size += realsize;
      mem->memory[mem->size] = 0;
   }

   return realsize;
}

size_t cCurl::WriteHeaderCallback(char* ptr, size_t size, size_t nmemb, void* data)
{
   size_t realsize = size * nmemb;
   MemoryStruct* mem = (MemoryStruct*)data;
   char* p;

   if (sysNotification)
      sysNotification->check();

   if (ptr)
   {
      // add to Header to map

      std::string header(ptr);
      std::size_t pos = header.find(": ");

      if(pos != std::string::npos)
      {
         std::string name = header.substr(0, pos);
         std::string value = header.substr(pos+2, std::string::npos);
         mem->headers[name] = value;
      }

      // get filename
      {
         // Content-Disposition: attachment; filename="20140103_20140103_de_qy.zip"

         const char* attribute = "Content-disposition: ";

         if ((p = strcasestr((char*)ptr, attribute)))
         {
            if ((p = strcasestr(p, "filename=")))
            {
               p += strlen("filename=");

               tell(4, "found filename at [%s]", p);

               if (*p == '"')
                  p++;

               sprintf(mem->name, "%s", p);

               if ((p = strchr(mem->name, '\n')))
                  *p = 0;

               if ((p = strchr(mem->name, '\r')))
                  *p = 0;

               if ((p = strchr(mem->name, '"')))
                  *p = 0;

               tell(4, "set name to '%s'", mem->name);
            }
         }
      }

      // since some sources update "ETag" an "Last-Modified:" without changing the contents
      //  we have to use "Content-Length:" to check for updates :(
      {
         const char* attribute = "Content-Length: ";

         if ((p = strcasestr((char*)ptr, attribute)))
         {
            sprintf(mem->tag, "%s", p+strlen(attribute));

            if ((p = strchr(mem->tag, '\n')))
               *p = 0;

            if ((p = strchr(mem->tag, '\r')))
               *p = 0;

            if ((p = strchr(mem->tag, '"')))
               *p = 0;
         }
      }
   }

   return realsize;
}

//***************************************************************************
// Download File
//***************************************************************************

int cCurl::downloadFile(const char* url, int& size, MemoryStruct* data, int timeout,
                        const char* userAgent, struct curl_slist* headerlist)
{
   long code;
   CURLcode res = CURLE_OK;

   size = 0;

   init();

   curl_easy_setopt(handle, CURLOPT_URL, url);                                // Specify URL to get
   curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, yes);
   curl_easy_setopt(handle, CURLOPT_UNRESTRICTED_AUTH, yes);                  // continue to send authentication (user+password) when following locations
   curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);      // Send all data to this function
   curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void*)data);                  // Pass our 'data' struct to the callback function
   curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, WriteHeaderCallback);     // Send header to this function
   curl_easy_setopt(handle, CURLOPT_WRITEHEADER, (void*)data);                // Pass some header details to this struct
   curl_easy_setopt(handle, CURLOPT_MAXFILESIZE, 100*1024*1024);              // Set maximum file size to get (bytes)
   curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1);                           // No progress meter
   curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);                             // No signaling
   curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);                        // Set timeout
   curl_easy_setopt(handle, CURLOPT_NOBODY, data->headerOnly ? 1 : 0);        //
   curl_easy_setopt(handle, CURLOPT_USERAGENT, userAgent);                    // Some servers don't like requests without a user-agent field
//    curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "gzip");                 //
   curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "");

   if (headerlist)
      curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headerlist);

   // perform http-get

   if ((res = curl_easy_perform(handle)) != 0)
   {
      long httpCode = 0;

      curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpCode);
      tell(1, "Error: Download failed; %s (%d); http code was (%ld) [%s]",
           curl_easy_strerror(res), res, httpCode, url);

      data->clear();

      return fail;
   }

   curl_easy_getinfo(handle, CURLINFO_HTTP_CODE, &code);
   data->statusCode = code;

   if (code == 404)
   {
      data->clear();
      return fail;
   }

   size = data->size;

   return success;
}