libcyberradio  22.01.24
HttpsSession.h
1 /***************************************************************************
2  * \file HttpsSession.h
3  * \brief Defines an interface for establishing an HTTPS session.
4  * \author DA
5  * \copyright (c) 2017 CyberRadio Solutions, Inc. All rights reserved.
6  *
7  * \note Requires C++11 compiler support.
8  *
9  ***************************************************************************/
10 
11 #ifndef INCLUDED_LIBCYBERRADIO_HTTPSSESSION_H_
12 #define INCLUDED_LIBCYBERRADIO_HTTPSSESSION_H_
13 
14 #include "LibCyberRadio/Common/Debuggable.h"
15 #include <curl/curl.h>
16 #include <sstream>
17 #include <string>
18 
19 
23 namespace LibCyberRadio
24 {
25 
29  class HttpsSession : public Debuggable
30  {
31  public:
37  bool debug = false
38  );
42  virtual ~HttpsSession();
51  virtual bool get(
52  const std::string& url,
53  bool verify = true
54  );
66  virtual bool post(
67  const std::string& url,
68  void* data,
69  size_t length,
70  const char* contentType = "text/plain",
71  bool verify = true
72  );
77  virtual long getResponseCode() const;
82  virtual std::string getResponseHeader() const;
87  virtual std::string getResponseBody() const;
92  virtual std::string getLastRequestErrorInfo() const;
98  virtual size_t writeHeader(
99  char *ptr,
100  size_t size
101  );
107  virtual size_t writeResponseData(
108  char *ptr,
109  size_t size
110  );
111 
112  protected:
118  virtual CURLcode initializeRequest();
123  virtual const char* debugCurl(
124  CURLcode x
125  );
139  static size_t headerCallback(
140  char *buffer,
141  size_t size,
142  size_t nitems,
143  void *userdata
144  );
158  static size_t writeDataCallback(
159  char *ptr,
160  size_t size,
161  size_t nmemb,
162  void *userdata
163  );
164 
165  protected:
166  // libcurl handle
167  CURL* _session;
168  // HTTPS request data
169  // -- Response code
170  long _responseCode;
171  // -- Response header
172  std::ostringstream _header;
173  // -- Response body
174  std::ostringstream _response;
175  // Error message from the last request
176  std::string _lastReqErrInfo;
177  };
178 
179 } /* namespace LibCyberRadio */
180 
181 #endif /* INCLUDED_LIBCYBERRADIO_HTTPSSESSION_H_ */
virtual CURLcode initializeRequest()
Initializes the session object to handle a new request.
virtual long getResponseCode() const
Gets the HTTPS response code from the last request.
static size_t writeDataCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
Callback function that libcurl "writes" data to when retrieving an HTTPS response body...
virtual size_t writeResponseData(char *ptr, size_t size)
Write data into the response buffer.
virtual std::string getResponseHeader() const
Gets the HTTPS header from the last request.
virtual ~HttpsSession()
Destroys an HttpsSession object.
Class that supports debug output.
Definition: Debuggable.h:38
virtual int debug(const char *format,...)
Outputs debug information.
Definition: Debuggable.cpp:95
virtual std::string getResponseBody() const
Gets the HTTPS response body from the last request.
static size_t headerCallback(char *buffer, size_t size, size_t nitems, void *userdata)
Callback function that libcurl "writes" data to when retrieving an HTTPS response header...
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
virtual size_t writeHeader(char *ptr, size_t size)
Write data into the header buffer.
HttpsSession(bool debug=false)
Constructs an HttpsSession object.
virtual bool post(const std::string &url, void *data, size_t length, const char *contentType="text/plain", bool verify=true)
Posts data to a given URL over HTTPS.
Class that encapsulates an HTTPS session.
Definition: HttpsSession.h:29
virtual const char * debugCurl(CURLcode x)
Returns a string corresponding to a libcurl error code.
virtual std::string getLastRequestErrorInfo() const
Gets the error information for the last request.