libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
HttpClient.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef HTTPCLIENT_H_
32 #define HTTPCLIENT_H_
33 
34 #include "../Exception.h"
35 #include "../Managers/FileDescriptorManager.h"
36 #include "../Encoding/Http.h"
37 #include "TcpSocket.h"
38 
39 namespace BaseLib {
47  private:
48  int32_t _responseCode = -1;
49  public:
50  explicit HttpClientException(const std::string &message) : Exception(message) {}
51  HttpClientException(const std::string &message, int32_t responseCode) : Exception(message), _responseCode(responseCode) {}
52 
53  int32_t responseCode() const { return _responseCode; }
54 };
55 
63  public:
64  explicit HttpClientTimeOutException(const std::string &message) : HttpClientException(message) {}
65 };
66 
74  public:
75  explicit HttpClientSocketClosedException(const std::string &message) : HttpClientException(message) {}
76 };
77 
83 class HttpClient {
84  public:
98  HttpClient(BaseLib::SharedObjects *baseLib, std::string hostname, int32_t port = 80, bool keepAlive = true, bool useSSL = false, std::string caFile = "", bool verifyCertificate = true, std::string certPath = "", std::string keyPath = "");
99 
117  std::string hostname,
118  int32_t port,
119  bool keepAlive,
120  bool useSSL,
121  bool verifyCertificate,
122  std::string caFile,
123  std::string caData,
124  std::string certPath,
125  std::string certData,
126  std::string keyPath,
127  const std::shared_ptr<Security::SecureVector<uint8_t>> &keyData);
128 
132  virtual ~HttpClient();
133 
138  std::shared_ptr<TcpSocket> getSocket() { return _socket; }
139 
144  void setTimeout(uint32_t value);
145 
146  void setVerifyHostname(bool value);
147 
152  void setUserAgent(const std::string &value);
153 
158  bool connected() { return _socket && _socket->connected(); }
159 
163  void disconnect() { if (_socket) _socket->close(); }
164 
169  void enableRawContent(bool value) { _keepRawContent = value; }
170 
175  std::vector<char> &getRawContent() { return _rawContent; }
176 
181  std::string getIpAddress() { return _socket ? _socket->getIpAddress() : ""; }
182 
183  /*
184  * Sends an HTTP request and returns the response.
185  *
186  * @param[in] request The HTTP request including the full header.
187  * @param[out] response The HTTP response without the header.
188  */
189  void sendRequest(const std::string &request, std::string &response, bool responseIsHeaderOnly = false);
190 
191  /*
192  * Sends an HTTP request and returns the http object.
193  *
194  * @param[in] request The HTTP request including the full header.
195  * @param[out] response The HTTP response.
196  */
197  void sendRequest(const std::string &request, Http &response, bool responseIsHeaderOnly = false);
198 
199  /*
200  * Sends an HTTP GET request and returns the response. This method can be used to download files.
201  *
202  * @param[in] url The path of the file to get.
203  * @param[out] data The data returned.
204  */
205  void delete_(const std::string &path, std::string &data, const std::string &additionalHeaders = "");
206 
207  /*
208  * Sends an HTTP DELETE request and returns the response. This method can be used to download files.
209  *
210  * @param[in] url The path of the file to get.
211  * @param[out] data The data returned.
212  */
213  void delete_(const std::string &path, Http &data, const std::string &additionalHeaders = "");
214 
215  /*
216  * Sends an HTTP DELETE request and returns the response. This method can be used to download files.
217  *
218  * @param[in] url The path of the file to get.
219  * @param[out] data The data returned.
220  */
221  void get(const std::string &path, std::string &data, const std::string &additionalHeaders = "");
222 
223  /*
224  * Sends an HTTP GET request and returns the response. This method can be used to download files.
225  *
226  * @param[in] url The path of the file to get.
227  * @param[out] data The data returned.
228  */
229  void get(const std::string &path, Http &data, const std::string &additionalHeaders = "");
230 
231  /*
232  * Sends an HTTP PATCH request and returns the response.
233  *
234  * @param[in] url The path to post to.
235  * @param[in] dataIn The POST data.
236  * @param[out] dataOut The data returned.
237  */
238  void patch(const std::string &path, std::string &dataIn, std::string &dataOut, const std::string &additionalHeaders = "");
239 
240  /*
241  * Sends an HTTP PATCH request and returns the response.
242  *
243  * @param[in] url The path to post to.
244  * @param[in] dataIn The POST data.
245  * @param[out] dataOut The data returned.
246  */
247  void patch(const std::string &path, std::string &dataIn, Http &dataOut, const std::string &additionalHeaders = "");
248 
249  /*
250  * Sends an HTTP POST request and returns the response.
251  *
252  * @param[in] url The path to post to.
253  * @param[in] dataIn The POST data.
254  * @param[out] dataOut The data returned.
255  */
256  void post(const std::string &path, std::string &dataIn, std::string &dataOut, const std::string &additionalHeaders = "");
257 
258  /*
259  * Sends an HTTP POST request and returns the response.
260  *
261  * @param[in] url The path to post to.
262  * @param[in] dataIn The POST data.
263  * @param[out] dataOut The data returned.
264  */
265  void post(const std::string &path, std::string &dataIn, Http &dataOut, const std::string &additionalHeaders = "");
266 
267  /*
268  * Sends an HTTP PUT request and returns the response.
269  *
270  * @param[in] url The path to post to.
271  * @param[in] dataIn The POST data.
272  * @param[out] dataOut The data returned.
273  */
274  void put(const std::string &path, std::string &dataIn, std::string &dataOut, const std::string &additionalHeaders = "");
275 
276  /*
277  * Sends an HTTP PUT request and returns the response.
278  *
279  * @param[in] url The path to post to.
280  * @param[in] dataIn The POST data.
281  * @param[out] dataOut The data returned.
282  */
283  void put(const std::string &path, std::string &dataIn, Http &dataOut, const std::string &additionalHeaders = "");
284  protected:
288  BaseLib::SharedObjects *_bl = nullptr;
289 
295  std::mutex _socketMutex;
296 
300  std::shared_ptr<TcpSocket> _socket;
301 
305  std::string _hostname = "";
306 
310  int32_t _port = 80;
311 
315  bool _keepAlive = true;
316 
320  bool _keepRawContent = false;
321 
322  std::string _userAgent = "Homegear";
323 
327  std::vector<char> _rawContent;
328 };
329 
330 }
331 #endif
std::vector< char > & getRawContent()
Returns the raw content.
Definition: HttpClient.h:175
This is the base library main class.
Definition: BaseLib.h:95
Exception class for timeouts of the HTTP client.
Definition: HttpClient.h:73
std::vector< char > _rawContent
Stores the raw response.
Definition: HttpClient.h:327
void enableRawContent(bool value)
Enables storage of raw content.
Definition: HttpClient.h:169
The class only makes sure that the vector is not copyable and the data is zeroed on destruction...
Definition: Io.h:40
Definition: BaseLib.cpp:34
HttpClientTimeOutException(const std::string &message)
Definition: HttpClient.h:64
HttpClientSocketClosedException(const std::string &message)
Definition: HttpClient.h:75
int32_t responseCode() const
Definition: HttpClient.h:53
PVariable value
Definition: UiElements.h:217
std::shared_ptr< TcpSocket > getSocket()
Returns the underlying TcpSocket.
Definition: HttpClient.h:138
std::mutex _socketMutex
Protects _socket to only allow one operation at a time.
Definition: HttpClient.h:295
HttpClientException(const std::string &message)
Definition: HttpClient.h:50
Definition: Http.h:57
Exception class for the HTTP client.
Definition: HttpClient.h:46
std::shared_ptr< TcpSocket > _socket
The socket object.
Definition: HttpClient.h:300
This class provides a basic HTTP client.
Definition: HttpClient.h:83
Exception class for timeouts of the HTTP client.
Definition: HttpClient.h:62
std::string getIpAddress()
Returns the IP address of the HTTP server.
Definition: HttpClient.h:181
Base class for all exceptions defined in Homegear.
Definition: Exception.h:41
void disconnect()
Closes the socket.
Definition: HttpClient.h:163
bool connected()
Returns "true" if the socket is connected, otherwise "false".
Definition: HttpClient.h:158
HttpClientException(const std::string &message, int32_t responseCode)
Definition: HttpClient.h:51