CurlWrapper.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/ws/core/CurlWrapper.cpp
22 
23  \brief A Wrapper for Lib Curl.
24 
25  \author Emerson Moraes
26  \author Vinicius Campanha
27  */
28 
29 #include "CurlWrapper.h"
30 
31 // Terralib
32 #include "../../core/encoding/CharEncoding.h"
33 #include "../../../terralib/core/translator/Translator.h"
34 #include "Exception.h"
35 #include "Utils.h"
36 
37 // LibCurl
38 #include <curl/curl.h>
39 
40 // STL
41 #include <sstream>
42 #include <fstream>
43 
44 // Boost
45 #include <boost/algorithm/string.hpp>
46 
48 {
50  std::shared_ptr<CURL> m_curl;
51  std::string m_baseMessage;
52 
54  {
55  }
56 
57  ~CurlProgress() = default;
58 };
59 
61 {
62  std::shared_ptr<CURL> m_curl;
63  std::mutex m_bufferMutex;
64  std::mutex m_downloadMutex;
65  std::string m_taskMessage;
66  std::string m_response;
68  std::string m_username;
69  std::string m_password;
71 };
72 
74 {
75  m_pimpl.reset(new Impl);
76  m_pimpl->m_curl = std::shared_ptr<CURL>(curl_easy_init(), curl_easy_cleanup);
77  m_pimpl->m_method = AuthenticationMethod::NOT_AUTH;
78 }
79 
81  : m_pimpl(std::move(other.m_pimpl))
82 {
83 }
84 
86 
88 {
89  curl_easy_reset(m_pimpl->m_curl.get());
90 
91  m_pimpl->m_response = "";
92  m_pimpl->m_responseCode = 0;
93 
94 #ifndef NDEBUG
95  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_NOSIGNAL, 1);
96 #endif
97 }
98 
99 size_t WriteFileCallback(void *ptr, size_t size, size_t nmemb, void *data)
100 {
101  std::FILE *writehere = (std::FILE *)data;
102  return fwrite(ptr, size, nmemb, writehere);
103 }
104 
105 
106 
107 size_t WriteResponse(char* data, size_t size, size_t nmemb, std::string* buffer)
108 {
109  if(buffer == nullptr)
110  return 0;
111 
112  buffer->append(data, size * nmemb);
113 
114  return size * nmemb;
115 }
116 
117 
118 size_t read_stream_callback(char *buffer, size_t size, size_t nitems, void *instream)
119 {
120  std::fstream* stream = static_cast<std::fstream*>(instream);
121 
122  if(!stream->is_open())
123  {
124  return 0;
125  }
126 
127  size_t nbytes = size * nitems;
128 
129  size_t bytesWritten = stream->readsome(buffer, nbytes);
130 
131  return bytesWritten;
132 }
133 
134 size_t read_data(char *bufptr, size_t size, size_t nitems, void *userp)
135 {
136  size_t read;
137  read = fread(bufptr, size, nitems, (FILE*)userp);
138  return read;
139 }
140 
141 int DownloadProgress(void* p, curl_off_t /*dltotal*/, curl_off_t dlnow,
142  curl_off_t /*ultotal*/, curl_off_t /*ulnow*/)
143 {
144  CurlProgress* progress = (CurlProgress*) p;
145 
146  std::stringstream ss;
147 
148  progress->m_task->pulse();
149 
150  ss << dlnow;
151 
152  std::string::size_type sz;
153  long numBytes = std::stol(ss.str(), &sz);
154 
155  long numKBytes = numBytes / 1000;
156 
157  progress->m_task->setMessage(progress->m_baseMessage + " " + std::to_string(numKBytes) + TE_TR(" KBytes received."));
158 
159  return 0;
160 }
161 
162 int UploadProgress(void* p, curl_off_t /*dltotal*/, curl_off_t /*dlnow*/,
163  curl_off_t ultotal, curl_off_t ulnow)
164 {
165  CurlProgress* progress = (CurlProgress*) p;
166 
167  if(!progress->m_task->isActive())
168  return 1;
169 
170  std::stringstream ss;
171  std::string::size_type sz;
172 
173  ss << ultotal;
174 
175  long numBytesTotal = std::stol(ss.str(), &sz);
176 
177  int numMBytesTotal = (int) (numBytesTotal / 1000) / 1000;
178 
179  if(numMBytesTotal != progress->m_task->getTotalSteps())
180  {
181  progress->m_task->setTotalSteps(numMBytesTotal);
182  }
183 
184  ss.str("");
185 
186  ss << ulnow;
187 
188  long numBytesNow = std::stol(ss.str(), &sz);
189 
190  int numMBytesNow = (int) (numBytesNow / 1000) / 1000;
191 
192  progress->m_task->setCurrentStep(numMBytesNow);
193 
194  progress->m_task->setMessage(progress->m_baseMessage + " " + std::to_string(numMBytesNow) + TE_TR(" MBytes uploaded."));
195 
196  return 0;
197 }
198 
199 void te::ws::core::CurlWrapper::downloadFile(const std::string& url, const std::string& filePath, te::common::TaskProgress* taskProgress)
200 {
201 
202  std::FILE* file = std::fopen(te::core::CharEncoding::fromUTF8(filePath).c_str(), "wb");
203 
204  if(!file)
205  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Could not open file to write!"));
206 
207  downloadFile(url, file, taskProgress);
208 
209  if (!file)
210  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Error at received file!"));
211  else
212  std::fclose(file);
213 }
214 
215 std::unique_lock<std::mutex> te::ws::core::CurlWrapper::getLock()
216 {
217  return std::unique_lock<std::mutex>(m_pimpl->m_downloadMutex);
218 }
219 
220 void te::ws::core::CurlWrapper::downloadFile(const std::string &url, std::FILE* file, te::common::TaskProgress* taskProgress)
221 {
222  auto lock = getLock();
223  clean();
224  downloadFile_(url, file, taskProgress);
225 }
226 
227 void te::ws::core::CurlWrapper::downloadFile_(const std::string &url, std::FILE* file, te::common::TaskProgress* taskProgress)
228 {
229  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, url.c_str());
230 
231  // Get data to be written
232  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEFUNCTION, WriteFileCallback);
233 
234  // Set a pointer to our file
235  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, file);
236 
237  te::common::TaskProgress* task = nullptr;
238 
239  if (!taskProgress)
241  else
242  task = taskProgress;
243 
244  CurlProgress progress;
245 
246  progress.m_curl = m_pimpl->m_curl;
247  progress.m_task = task;
248  progress.m_baseMessage = m_pimpl->m_taskMessage;
249 
250  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_XFERINFOFUNCTION, DownloadProgress);
251 
252  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_XFERINFODATA, &progress);
253 
254  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_NOPROGRESS, 0L);
255 
256  if(m_pimpl->m_method != AuthenticationMethod::NOT_AUTH)
257  {
258  this->addAuthParameters();
259  }
260 
261  /* Perform the request, status will get the return code */
262  CURLcode status = curl_easy_perform(m_pimpl->m_curl.get());
263 
264  if (!taskProgress)
265  delete task;
266 
267  // Check for errors
268  if(status != CURLE_OK)
269  throw te::ws::core::Exception() << te::ErrorDescription(curl_easy_strerror(status));
270 
271  curl_easy_getinfo(m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &m_pimpl->m_responseCode);
272 
273  //Authentication needed
274  if (m_pimpl->m_responseCode == 401)
275  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Authentication with server failed!"));
276 }
277 
278 
280 {
281  return m_pimpl->m_taskMessage;
282 }
283 
284 
285 void te::ws::core::CurlWrapper::setTaskMessage(const std::string &taskMessage)
286 {
287  m_pimpl->m_taskMessage = taskMessage;
288 }
289 
290 
292  const std::string &postFields,
293  const::std::string &header)
294 {
295  clean();
296 
297  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, uri.uri().c_str());
298 
299  char errbuf[CURL_ERROR_SIZE];
300 
301  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
302  errbuf[0] = 0;
303 
304  struct curl_slist* headers= nullptr;
305  headers = curl_slist_append(headers, header.c_str());
306  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_HTTPHEADER, headers);
307 
308  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_POSTFIELDS, postFields.c_str());
309 
310  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEFUNCTION, WriteResponse);
311 
312  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &m_pimpl->m_response);
313 
314  /* Perform the request, status will get the return code */
315  CURLcode status = curl_easy_perform(m_pimpl->m_curl.get());
316 
317  curl_slist_free_all(headers);
318 
319  // Check for errors
320  if(status != CURLE_OK)
321  {
322  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
324  }
325 
326  status = curl_easy_getinfo(m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &m_pimpl->m_responseCode);
327 
328  // Check for errors
329  if(status != CURLE_OK)
330  {
331  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
333  }
334 }
335 
336 
337 void te::ws::core::CurlWrapper::putFile(const te::core::URI &uri, const std::string &filePath, const std::string &header, te::common::TaskProgress* taskProgress)
338 {
339  FILE* file;
340 
341  file = fopen(filePath.c_str(), "rb");
342 
343  if (!file)
344  {
345  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Can't open the file!."));
346  }
347 
348  long filesize = te::ws::core::GetFileSize(filePath);
349 
350  putFile(uri, file, header, taskProgress, filesize);
351 }
352 
353 
354 void te::ws::core::CurlWrapper::putFile(const te::core::URI &uri, FILE* file, const::std::string &header,
355  te::common::TaskProgress* taskProgress, const long& fileSize)
356 {
357  clean();
358 
359  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, uri.uri().c_str());
360 
361  char errbuf[CURL_ERROR_SIZE];
362 
363  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
364  errbuf[0] = 0;
365 
366  struct curl_slist* headers= nullptr;
367  headers = curl_slist_append(headers, header.c_str());
368  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_HTTPHEADER, headers);
369 
370  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_UPLOAD, 1L);
371 
372  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_READFUNCTION, read_data);
373 
374  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_READDATA, file);
375 
376  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEFUNCTION, WriteResponse);
377 
378  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &m_pimpl->m_response);
379 
380  te::common::TaskProgress* task = nullptr;
381 
382  if (taskProgress == nullptr)
384  else
385  task = taskProgress;
386 
387  CurlProgress progress;
388 
389  progress.m_curl = m_pimpl->m_curl;
390  progress.m_task = task;
391  progress.m_baseMessage = m_pimpl->m_taskMessage;
392 
393  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_XFERINFOFUNCTION, UploadProgress);
394 
395  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_XFERINFODATA, &progress);
396 
397  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
398 
399  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_NOPROGRESS, 0L);
400 
401  /* Perform the request, status will get the return code */
402  CURLcode status = curl_easy_perform(m_pimpl->m_curl.get());
403 
404  if (taskProgress == nullptr)
405  delete task;
406 
407  curl_slist_free_all(headers);
408 
409  // Check for errors
410  if(status != CURLE_OK)
411  {
412  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
414  }
415 
416  status = curl_easy_getinfo(m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &m_pimpl->m_responseCode);
417 
418  // Check for errors
419  if(status != CURLE_OK)
420  {
421  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
423  }
424 }
425 
426 
427 void te::ws::core::CurlWrapper::customRequest(const te::core::URI &uri, const std::string& request, const std::string& body, const::std::string &header)
428 {
429  clean();
430 
431  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, uri.uri().c_str());
432 
433  char errbuf[CURL_ERROR_SIZE];
434 
435  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
436  errbuf[0] = 0;
437 
438  struct curl_slist* headers= nullptr;
439  headers = curl_slist_append(headers, header.c_str());
440  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_HTTPHEADER, headers);
441 
442  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_CUSTOMREQUEST, request.c_str());
443 
444  if(!body.empty())
445  {
446  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_POSTFIELDS, body.c_str());
447  }
448 
449  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEFUNCTION, WriteResponse);
450 
451  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &m_pimpl->m_response);
452 
453  /* Perform the request, status will get the return code */
454  CURLcode status = curl_easy_perform(m_pimpl->m_curl.get());
455 
456  curl_slist_free_all(headers);
457 
458  if(status != CURLE_OK)
459  {
460  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
462  }
463 
464  status = curl_easy_getinfo(m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &m_pimpl->m_responseCode);
465 
466  // Check for errors
467  if(status != CURLE_OK)
468  {
469  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
471  }
472 }
473 
474 
475 void te::ws::core::CurlWrapper::get(const te::core::URI &uri, std::string &buffer)
476 {
477  buffer.clear();
478 
479  std::string url = uri.uri();
480 
481  m_pimpl->m_bufferMutex.lock();
482 
483  clean();
484 
485  char errbuf [CURL_ERROR_SIZE];
486 
487  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
488  errbuf[0] = 0;
489 
490  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, url.c_str());
491 
492  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEFUNCTION, WriteResponse);
493 
494  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &buffer);
495 
496  if(m_pimpl->m_method != AuthenticationMethod::NOT_AUTH)
497  {
498  this->addAuthParameters();
499  }
500 
501 
502  /* Perform the request, status will get the return code */
503  CURLcode status = curl_easy_perform(m_pimpl->m_curl.get());
504 
505  m_pimpl->m_bufferMutex.unlock();
506 
507  m_pimpl->m_response = buffer;
508 
509  if(status != CURLE_OK)
510  {
511  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
513  }
514 
515  status = curl_easy_getinfo(m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &m_pimpl->m_responseCode);
516 
517  // Check for errors
518  if(status != CURLE_OK)
519  {
520  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
522  }
523 }
524 
525 
527 {
528  return m_pimpl->m_responseCode;
529 }
530 
531 
532 const std::string& te::ws::core::CurlWrapper::response() const
533 {
534  return m_pimpl->m_response;
535 }
536 
538 {
539  m_pimpl->m_method = method;
540 }
541 
543 {
544  return m_pimpl->m_method;
545 }
546 
547 void te::ws::core::CurlWrapper::setUsername(const std::string &username)
548 {
549  m_pimpl->m_username = username;
550 }
551 
553 {
554  return m_pimpl->m_username;
555 }
556 
557 void te::ws::core::CurlWrapper::setPassword(const std::string &password)
558 {
559  m_pimpl->m_password = password;
560 }
561 
563 {
564  if(m_pimpl->m_username.empty() || m_pimpl->m_password.empty())
565  {
566  return;
567  }
568 
569  std::string userAndPassword = m_pimpl->m_username + std::string(":") + m_pimpl->m_password;
570 
571  switch(m_pimpl->m_method)
572  {
573 
574  case NOT_AUTH:
575  break;
576 
577  case HTTP_BASIC:
578  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
579  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_USERPWD, userAndPassword.c_str());
580  break;
581 
582  case HTTP_DIGEST:
583  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
584  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_USERPWD, userAndPassword.c_str());
585  break;
586 
587  }
588 }
589 
590 void te::ws::core::CurlWrapper::setOption(int option, std::string value)
591 {
592  CURLoption code = static_cast<CURLoption>(option);
593  curl_easy_setopt(m_pimpl->m_curl.get(), code, value.c_str());
594 }
595 
596 void te::ws::core::CurlWrapper::setOption(int option, char* value)
597 {
598  CURLoption code = static_cast<CURLoption>(option);
599  curl_easy_setopt(m_pimpl->m_curl.get(), code, value);
600 }
601 
602 
603 void te::ws::core::CurlWrapper::setOption(int option, int value)
604 {
605  CURLoption code = static_cast<CURLoption>(option);
606  curl_easy_setopt(m_pimpl->m_curl.get(), code, value);
607 }
608 
609 void te::ws::core::CurlWrapper::verifyURL(std::string url, uint32_t timeout) const
610 {
611  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, url.c_str());
612  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_CONNECTTIMEOUT, timeout);
613  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_NOBODY, 1);
614 
615  char errbuf [CURL_ERROR_SIZE];
616  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
617  errbuf[0] = 0;
618 
619  auto status = curl_easy_perform(m_pimpl->m_curl.get());
620  // Check for errors
621  if(status != CURLE_OK)
622  {
623  std::string msg = curl_easy_strerror(status) + (':' + std::string(errbuf));
625  }
626 }
627 
628 std::vector<std::string> te::ws::core::CurlWrapper::listFiles(const te::core::URI& uri)
629 {
630  clean();
631  return listFiles_(uri);
632 }
633 
634 std::vector<std::string> te::ws::core::CurlWrapper::listFiles_(const te::core::URI& uri)
635 {
636 // An url for a directory must have '/' in the last character, otherwise it won't list the files.
637  std::string completeUrl = uri.uri();
638  if(completeUrl.back() != '/')
639  completeUrl += "/";
640 
641  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_URL, completeUrl.c_str());
642  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_DIRLISTONLY, 1);
643  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEFUNCTION, WriteResponse);
644 
645  std::string block;
646  curl_easy_setopt(m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, (void *)&block);
647 
648  CURLcode status = curl_easy_perform(m_pimpl->m_curl.get());
649 
650  if (status == CURLE_OK)
651  {
652  std::vector<std::string> vectorFiles;
653  boost::split(vectorFiles, block, boost::is_any_of("\n"));
654 
655  if(!vectorFiles.empty() && vectorFiles.back().empty())
656  vectorFiles.pop_back();
657 
658  return vectorFiles;
659  }
660  else
661  {
662  std::string errMsg = "Could not list files in the FTP server.\n";
663  errMsg + curl_easy_strerror(status);
664 
666  }
667 }
668 
670 {
671  curl_global_init(CURL_GLOBAL_ALL);
672 }
673 
675 {
676  curl_global_cleanup();
677 }
678 
virtual AuthenticationMethod getAuthenticationMethod() const
Gets current AuthenticationMethod.
virtual void setTaskMessage(const std::string &taskMessage)
This method sets the message that will be used on Progress Bar when this object is downloading some f...
static CurlGlobalScope curlScope_
Definition: CurlWrapper.h:259
Utils of WS Core Runtime Library.
std::unique_lock< std::mutex > getLock()
void setMessage(const std::string &message)
Set the task message.
size_t read_data(char *bufptr, size_t size, size_t nitems, void *userp)
size_t read_stream_callback(char *buffer, size_t size, size_t nitems, void *instream)
virtual const std::string & response() const
Returns the last operation response if the protocol allows it.
virtual std::string getUsername() const
Gets the user name that will be used when an athentication method is set.
te::common::TaskProgress * m_task
Definition: CurlWrapper.cpp:49
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
virtual void post(const te::core::URI &uri, const std::string &postFields, const ::std::string &header)
Method to request a post in a determined URI.
te::ws::core::AuthenticationMethod m_method
Definition: CurlWrapper.cpp:70
An abstraction to Lib Curl functions.
Definition: CurlWrapper.h:61
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
TEWSCOREEXPORT long GetFileSize(const std::string &filename)
This function gets the size of a file, using C runtime stat struct that works on Windows, Mac and Linux.
bool isActive() const
Verify if the task is active.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
std::unique_ptr< Impl > m_pimpl
Definition: CurlWrapper.h:238
void setTotalSteps(int value)
Set the task total stepes.
std::shared_ptr< CURL > m_curl
Definition: CurlWrapper.cpp:50
A Wrapper for Lib Curl.
virtual void putFile(const te::core::URI &uri, const std::string &filePath, const ::std::string &header, te::common::TaskProgress *taskProgress=0)
Method to request a put with a file in a determined URI.
virtual void addAuthParameters()
Adds HTTP Authentication parameters according to the current AuthenticationMethod.
void setOption(int option, std::string value)
virtual void customRequest(const te::core::URI &uri, const std::string &request, const std::string &body=std::string(""), const ::std::string &header=std::string(""))
Method to make a custom request, useful for performing a HTTP DELETE request.
virtual std::string getTaskMessage() const
This method gets the message that will be used on Progress Bar when this object is downloading some f...
virtual std::vector< std::string > listFiles_(const te::core::URI &uri)
Private unsafe method to list files in a URI.
te::gm::Polygon * p
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
int DownloadProgress(void *p, curl_off_t, curl_off_t dlnow, curl_off_t, curl_off_t)
const std::string & uri() const
Retrieving the full URI.
Definition: URI.cpp:88
virtual void setPassword(const std::string &password)
Sets the password that will be used when an athentication method is set.
std::string m_baseMessage
Definition: CurlWrapper.cpp:51
virtual void get(const te::core::URI &uri, std::string &buffer)
Method to make a GET request.
Base exception class for WS Core Runtime Library.
virtual void setUsername(const std::string &username)
Sets the user name that will be used when an athentication method is set.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
void setCurrentStep(int value)
Set the task current step.
Exception classes for the WS Core Runtime Library.
virtual void downloadFile(const std::string &url, const std::string &filePath, te::common::TaskProgress *taskProgress=0)
Method to download a file retrieved from given URL and save into the specified file path...
virtual void downloadFile_(const std::string &url, std::FILE *file, te::common::TaskProgress *taskProgress=0)
Private unsafe method to download a file retrieved from given URL and save into the specified File...
virtual std::vector< std::string > listFiles(const te::core::URI &uri)
Method to list files in a URI.
std::shared_ptr< CURL > m_curl
Definition: CurlWrapper.cpp:62
virtual void clean()
Reset the curl handle and the class members.
Definition: CurlWrapper.cpp:87
size_t WriteResponse(char *data, size_t size, size_t nmemb, std::string *buffer)
size_t WriteFileCallback(void *ptr, size_t size, size_t nmemb, void *data)
Definition: CurlWrapper.cpp:99
~CurlProgress()=default
virtual const long responseCode() const
Returns the last operation response code.
virtual void setAuthenticationMethod(const AuthenticationMethod &method)
Sets the authentication method that will be used on the requests.
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $
int getTotalSteps() const
Get the task total stepes.
virtual void verifyURL(std::string url, uint32_t timeout) const
Verify if the URI is reacheble.
int UploadProgress(void *p, curl_off_t, curl_off_t, curl_off_t ultotal, curl_off_t ulnow)