CurlWrapper.h
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.h
22 
23  \brief A Wrapper for Lib Curl.
24 
25  \author Emerson Moraes
26  \author Vinicius Campanha
27  */
28 
29 #ifndef __TERRALIB_WS_CORE_CURLWRAPPER_H
30 #define __TERRALIB_WS_CORE_CURLWRAPPER_H
31 
32 //STL
33 #include <memory>
34 #include <mutex>
35 #include <vector>
36 #include <string>
37 
38 #include "Config.h"
39 #include "../../common/progress/TaskProgress.h"
40 #include "../../core/uri/URI.h"
41 
42 namespace te
43 {
44  namespace ws
45  {
46  namespace core
47  {
48 
50  {
51  NOT_AUTH = 0,
53  HTTP_DIGEST = 2
54  };
55 
56  /*!
57  \class CurlWrapper
58 
59  \brief An abstraction to Lib Curl functions.
60  */
62  {
63  public:
66 
67  virtual ~CurlWrapper();
68 
69  /*!
70  \brief Method to download a file retrieved from given URL and save into the specified file path.
71 
72  \param url URL that the file will be downloaded.
73  \param filePath file path where downloaded file will be saved.
74  */
75  virtual void downloadFile(const std::string& url, const std::string& filePath, te::common::TaskProgress* taskProgress = 0);
76 
77  /*!
78  \brief Method to download a file retrieved from given URL and save into the specified File.
79 
80  \param url URL that the file will be downloaded.
81  \param file File where downloaded file will be write.
82  */
83  virtual void downloadFile(const std::string& url, std::FILE* file, te::common::TaskProgress* taskProgress = 0);
84 
85 
86  /*!
87  \brief This method gets the message that will be used on Progress Bar when
88  this object is downloading some file.
89  */
90  virtual std::string getTaskMessage() const;
91 
92  /*!
93  \brief This method sets the message that will be used on Progress Bar when
94  this object is downloading some file.
95 
96  \param taskMessage new message.
97  */
98  virtual void setTaskMessage(const std::string& taskMessage);
99 
100  /*!
101  * \brief Method to request a post in a determined URI
102  * \param uri The URI with the address information to post
103  * \param postFields The parameters and values to post
104  * \param header A header to the post request
105  */
106  virtual void post(const te::core::URI& uri, const std::string &postFields, const::std::string &header);
107 
108  /*!
109  * \brief Method to request a put with a file in a determined URI
110  * \param uri The URI with the address information
111  * \param file Path The full path of the file to put
112  * \param header A header to the put request
113  */
114  virtual void putFile(const te::core::URI& uri, const std::string &filePath, const::std::string &header, te::common::TaskProgress* taskProgress = 0);
115 
116  /*!
117  * \brief Method to request a put a file in a determined URI
118  * \param uri The URI with the address information
119  * \param file Contains the file opened by the fstream
120  * \param header A header to the put request
121  */
122  virtual void putFile(const te::core::URI& uri, FILE* file, const::std::string &header, te::common::TaskProgress* taskProgress = 0, const long& fileSize = 0L);
123 
124  /*!
125  * \brief Method to make a custom request, useful for performing a HTTP DELETE request.
126  * \param uri The URI with the address information
127  * \param request The custom request
128  */
129  virtual void customRequest(const te::core::URI& uri, const std::string& request, const std::string& body = std::string(""), const::std::string &header = std::string(""));
130 
131  /*!
132  * \brief Method to make a GET request.
133  * \param uri The URI with the address information
134  * \param buffer Where the server answer will be write.
135  */
136  virtual void get(const te::core::URI &uri, std::string& buffer);
137 
138 
139  /*!
140  * \brief Returns the last operation response code
141  * \return The response code
142  */
143  virtual const long responseCode() const;
144 
145  /*!
146  * \brief Returns the last operation response if the protocol allows it
147  * \return The response
148  */
149  virtual const std::string& response() const;
150 
151  /*!
152  * \brief Sets the authentication method that will be used on the requests.
153  *
154  * \param method (e. g.: Basic or Digest)
155  */
156  virtual void setAuthenticationMethod(const AuthenticationMethod& method);
157 
158  /*!
159  * \brief Gets current AuthenticationMethod.
160  *
161  * \return current AuthenticationMethod.
162  */
164 
165  /*!
166  * \brief Sets the user name that will be used when an athentication method is set.
167  *
168  * \param username
169  */
170  virtual void setUsername(const std::string& username);
171 
172  /*!
173  * \brief Gets the user name that will be used when an athentication method is set.
174  *
175  * \return username
176  */
177  virtual std::string getUsername() const;
178 
179  /*!
180  * \brief Sets the password that will be used when an athentication method is set.
181  *
182  * \param password
183  */
184  virtual void setPassword(const std::string& password);
185 
186  /*!
187  \brief Verify if the URI is reacheble.
188 
189  \exception te::ws::core::Exception Rise if the URI cannot be reached.
190  */
191  virtual void verifyURL(std::string url, uint32_t timeout) const;
192 
193  /*!
194  \brief Method to list files in a URI.
195 
196  \param url URL that the files will be listed.
197  */
198  virtual std::vector<std::string> listFiles(const te::core::URI& uri);
199 
200  protected:
201 
202  /*!
203  * \brief Reset the curl handle and the class members
204  */
205  virtual void clean();
206 
207  /*!
208  * \brief Adds HTTP Authentication parameters according to the current AuthenticationMethod.
209  */
210  virtual void addAuthParameters();
211 
212  /*!
213  * \brief Sets up the enviroment before any atempt to make a request
214  */
215  virtual void setupEnvironment() const;
216 
217  /*!
218  * \brief Handles the response of a request
219  */
220  virtual bool handleResponse(int curlStatusCode) const;
221 
222  /*!
223  \brief Private unsafe method to download a file retrieved from given URL and save into the specified File.
224 
225  \warning NOT thread safe, call getLock() to acquire lock before using.
226  \warning NOT CURL safe, call clean() before using
227  \param url URL that the file will be downloaded.
228  \param file File where downloaded file will be write.
229  */
230  virtual void downloadFile_(const std::string& url, std::FILE* file, te::common::TaskProgress* taskProgress = 0);
231 
232  /*!
233  \brief Private unsafe method to list files in a URI.
234 
235  \warning NOT thread safe, call getLock() to acquire lock before using.
236  \warning NOT CURL safe, call clean() before using
237  \param url URL that the files will be listed.
238  */
239  virtual std::vector<std::string> listFiles_(const te::core::URI& uri);
240 
241  void setOption(int option, std::string value);
242  void setOption(int option, char* value);
243  void setOption(int option, int value);
244 
245  std::unique_lock<std::mutex> getLock();
246 
247  private:
248  struct Impl;
249 
250  std::unique_ptr<Impl> m_pimpl;
251 
252  /*!
253  \class CurlGlobalScope
254 
255  \brief RAII class to init curl
256  */
258  {
259  public:
262 
263  CurlGlobalScope(const CurlGlobalScope& other) = delete;
264  CurlGlobalScope(CurlGlobalScope&& other) = delete;
265  CurlGlobalScope& operator=(const CurlGlobalScope& other) = delete;
267  };
268 
270  };
271 
272  }
273  }
274 }
275 
276 #endif
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:54
A class to store the proxy information that must be used to access data located in URIs.
Definition: URI.h:50
CurlGlobalScope & operator=(const CurlGlobalScope &other)=delete
CurlGlobalScope(CurlGlobalScope &&other)=delete
CurlGlobalScope(const CurlGlobalScope &other)=delete
CurlGlobalScope & operator=(CurlGlobalScope &&other)=delete
An abstraction to Lib Curl functions.
Definition: CurlWrapper.h:62
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 std::vector< std::string > listFiles(const te::core::URI &uri)
Method to list files in a URI.
virtual void downloadFile(const std::string &url, std::FILE *file, te::common::TaskProgress *taskProgress=0)
Method to download a file retrieved from given URL and save into the specified File.
virtual std::string getTaskMessage() const
This method gets the message that will be used on Progress Bar when this object is downloading some f...
std::unique_ptr< Impl > m_pimpl
Definition: CurlWrapper.h:248
static CurlGlobalScope curlScope_
Definition: CurlWrapper.h:269
virtual void addAuthParameters()
Adds HTTP Authentication parameters according to the current AuthenticationMethod.
void setOption(int option, std::string value)
virtual void verifyURL(std::string url, uint32_t timeout) const
Verify if the URI is reacheble.
virtual AuthenticationMethod getAuthenticationMethod() const
Gets current AuthenticationMethod.
virtual bool handleResponse(int curlStatusCode) const
Handles the response of a request.
CurlWrapper(CurlWrapper &&other)
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 void post(const te::core::URI &uri, const std::string &postFields, const ::std::string &header)
Method to request a post in a determined URI.
virtual const std::string & response() const
Returns the last operation response if the protocol allows it.
virtual void setUsername(const std::string &username)
Sets the user name that will be used when an athentication method is set.
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...
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.
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 void setPassword(const std::string &password)
Sets the password that will be used when an athentication method is set.
std::unique_lock< std::mutex > getLock()
virtual void putFile(const te::core::URI &uri, FILE *file, const ::std::string &header, te::common::TaskProgress *taskProgress=0, const long &fileSize=0L)
Method to request a put a file in a determined URI.
void setOption(int option, int value)
virtual std::string getUsername() const
Gets the user name that will be used when an athentication method is set.
virtual void clean()
Reset the curl handle and the class members.
void setOption(int option, char *value)
virtual std::vector< std::string > listFiles_(const te::core::URI &uri)
Private unsafe method to list files in a URI.
virtual void get(const te::core::URI &uri, std::string &buffer)
Method to make a GET request.
virtual void setupEnvironment() const
Sets up the enviroment before any atempt to make a request.
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.
@ L
Definition: Enums.h:150
TerraLib.
#define TEWSCOREEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:62
Proxy configuration file for TerraView (see terraview_config.h).