32 #include "../../core/encoding/CharEncoding.h" 33 #include "../../../terralib/core/translator/Translator.h" 38 #include <curl/curl.h> 45 #include <boost/algorithm/string.hpp> 75 m_pimpl.reset(
new Impl);
76 m_pimpl->m_curl = std::shared_ptr<CURL>(curl_easy_init(), curl_easy_cleanup);
81 : m_pimpl(
std::move(other.m_pimpl))
89 curl_easy_reset(
m_pimpl->m_curl.get());
95 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_NOSIGNAL, 1);
101 std::FILE *writehere = (std::FILE *)data;
102 return fwrite(ptr, size, nmemb, writehere);
107 size_t WriteResponse(
char* data,
size_t size,
size_t nmemb, std::string* buffer)
109 if(buffer ==
nullptr)
112 buffer->append(data, size * nmemb);
120 std::fstream* stream =
static_cast<std::fstream*
>(instream);
122 if(!stream->is_open())
127 size_t nbytes = size * nitems;
129 size_t bytesWritten = stream->readsome(buffer, nbytes);
134 size_t read_data(
char *bufptr,
size_t size,
size_t nitems,
void *userp)
137 read = fread(bufptr, size, nitems, (FILE*)userp);
142 curl_off_t , curl_off_t )
146 std::stringstream ss;
152 std::string::size_type sz;
153 long numBytes = std::stol(ss.str(), &sz);
155 long numKBytes = numBytes / 1000;
163 curl_off_t ultotal, curl_off_t ulnow)
170 std::stringstream ss;
171 std::string::size_type sz;
175 long numBytesTotal = std::stol(ss.str(), &sz);
177 int numMBytesTotal = (
int) (numBytesTotal / 1000) / 1000;
188 long numBytesNow = std::stol(ss.str(), &sz);
190 int numMBytesNow = (
int) (numBytesNow / 1000) / 1000;
217 return std::unique_lock<std::mutex>(
m_pimpl->m_downloadMutex);
229 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_URL, url.c_str());
235 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_WRITEDATA,
file);
252 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_XFERINFODATA, &progress);
254 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_NOPROGRESS, 0
L);
262 CURLcode status = curl_easy_perform(
m_pimpl->m_curl.get());
268 if(status != CURLE_OK)
271 curl_easy_getinfo(
m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &
m_pimpl->m_responseCode);
274 if (
m_pimpl->m_responseCode == 401)
287 m_pimpl->m_taskMessage = taskMessage;
292 const std::string &postFields,
293 const::std::string &header)
297 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_URL, uri.
uri().c_str());
299 char errbuf[CURL_ERROR_SIZE];
301 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
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);
308 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_POSTFIELDS, postFields.c_str());
312 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &
m_pimpl->m_response);
315 CURLcode status = curl_easy_perform(
m_pimpl->m_curl.get());
317 curl_slist_free_all(headers);
320 if(status != CURLE_OK)
322 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
326 status = curl_easy_getinfo(
m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &
m_pimpl->m_responseCode);
329 if(status != CURLE_OK)
331 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
341 file = fopen(filePath.c_str(),
"rb");
350 putFile(uri, file, header, taskProgress, filesize);
359 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_URL, uri.
uri().c_str());
361 char errbuf[CURL_ERROR_SIZE];
363 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
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);
370 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_UPLOAD, 1
L);
374 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_READDATA,
file);
378 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &
m_pimpl->m_response);
382 if (taskProgress ==
nullptr)
395 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_XFERINFODATA, &progress);
397 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_INFILESIZE_LARGE, (curl_off_t)fileSize);
399 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_NOPROGRESS, 0
L);
402 CURLcode status = curl_easy_perform(
m_pimpl->m_curl.get());
404 if (taskProgress ==
nullptr)
407 curl_slist_free_all(headers);
410 if(status != CURLE_OK)
412 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
416 status = curl_easy_getinfo(
m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &
m_pimpl->m_responseCode);
419 if(status != CURLE_OK)
421 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
431 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_URL, uri.
uri().c_str());
433 char errbuf[CURL_ERROR_SIZE];
435 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
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);
442 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_CUSTOMREQUEST, request.c_str());
446 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_POSTFIELDS, body.c_str());
451 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &
m_pimpl->m_response);
454 CURLcode status = curl_easy_perform(
m_pimpl->m_curl.get());
456 curl_slist_free_all(headers);
458 if(status != CURLE_OK)
460 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
464 status = curl_easy_getinfo(
m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &
m_pimpl->m_responseCode);
467 if(status != CURLE_OK)
469 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
479 std::string url = uri.
uri();
485 char errbuf [CURL_ERROR_SIZE];
487 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
490 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_URL, url.c_str());
494 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, &buffer);
503 CURLcode status = curl_easy_perform(
m_pimpl->m_curl.get());
505 m_pimpl->m_bufferMutex.unlock();
509 if(status != CURLE_OK)
511 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
515 status = curl_easy_getinfo(
m_pimpl->m_curl.get(), CURLINFO_RESPONSE_CODE, &
m_pimpl->m_responseCode);
518 if(status != CURLE_OK)
520 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
528 return m_pimpl->m_responseCode;
549 m_pimpl->m_username = username;
559 m_pimpl->m_password = password;
569 std::string userAndPassword =
m_pimpl->m_username + std::string(
":") +
m_pimpl->m_password;
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());
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());
592 CURLoption code =
static_cast<CURLoption
>(option);
593 curl_easy_setopt(
m_pimpl->m_curl.get(), code, value.c_str());
598 CURLoption code =
static_cast<CURLoption
>(option);
599 curl_easy_setopt(
m_pimpl->m_curl.get(), code, value);
605 CURLoption code =
static_cast<CURLoption
>(option);
606 curl_easy_setopt(
m_pimpl->m_curl.get(), code, value);
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);
615 char errbuf [CURL_ERROR_SIZE];
616 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_ERRORBUFFER, errbuf);
619 auto status = curl_easy_perform(
m_pimpl->m_curl.get());
621 if(status != CURLE_OK)
623 std::string msg = curl_easy_strerror(status) + (
':' + std::string(errbuf));
637 std::string completeUrl = uri.
uri();
638 if(completeUrl.back() !=
'/')
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);
646 curl_easy_setopt(
m_pimpl->m_curl.get(), CURLOPT_WRITEDATA, (
void *)&block);
648 CURLcode status = curl_easy_perform(
m_pimpl->m_curl.get());
650 if (status == CURLE_OK)
652 std::vector<std::string> vectorFiles;
653 boost::split(vectorFiles, block, boost::is_any_of(
"\n"));
655 if(!vectorFiles.empty() && vectorFiles.back().empty())
656 vectorFiles.pop_back();
662 std::string errMsg =
"Could not list files in the FTP server.\n";
663 errMsg + curl_easy_strerror(status);
671 curl_global_init(CURL_GLOBAL_ALL);
676 curl_global_cleanup();
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_
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.
This XML Schema Document named xlinks xsd has been stored here based on the change request
te::common::TaskProgress * m_task
This class can be used to inform the progress of a task.
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
An abstraction to Lib Curl functions.
#define TE_TR(message)
It marks a string in order to get translated.
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
void setTotalSteps(int value)
Set the task total stepes.
std::shared_ptr< CURL > m_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.
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.
std::string m_taskMessage
virtual void setPassword(const std::string &password)
Sets the password that will be used when an athentication method is set.
std::string m_baseMessage
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).
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
virtual void clean()
Reset the curl handle and the class members.
size_t WriteResponse(char *data, size_t size, size_t nmemb, std::string *buffer)
std::mutex m_downloadMutex
size_t WriteFileCallback(void *ptr, size_t size, size_t nmemb, void *data)
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)