connection.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @file connection.h
  3. * @brief header definitions for restclient-cpp connection class
  4. * @author Daniel Schauenberg <d@unwiredcouch.com>
  5. * @version
  6. * @date 2010-10-11
  7. */
  8. #ifndef INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
  9. #define INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
  10. #include <curl/curl.h>
  11. #include <string>
  12. #include <map>
  13. #include <cstdlib>
  14. #include "restclient-cpp/restclient.h"
  15. /**
  16. * @brief namespace for all RestClient definitions
  17. */
  18. namespace RestClient {
  19. /**
  20. * @brief Connection object for advanced usage
  21. */
  22. class Connection {
  23. public:
  24. /**
  25. * @struct RequestInfo
  26. * @brief holds some diagnostics information
  27. * about a request
  28. * @var RequestInfo::totalTime
  29. * Member 'totalTime' contains the total time of the last request in
  30. * seconds Total time of previous transfer. See CURLINFO_TOTAL_TIME
  31. * @var RequestInfo::nameLookupTime
  32. * Member 'nameLookupTime' contains the time spent in DNS lookup in
  33. * seconds Time from start until name resolving completed. See
  34. * CURLINFO_NAMELOOKUP_TIME
  35. * @var RequestInfo::connectTime
  36. * Member 'connectTime' contains the time it took until Time from start
  37. * until remote host or proxy completed. See CURLINFO_CONNECT_TIME
  38. * @var RequestInfo::appConnectTime
  39. * Member 'appConnectTime' contains the time from start until SSL/SSH
  40. * handshake completed. See CURLINFO_APPCONNECT_TIME
  41. * @var RequestInfo::preTransferTime
  42. * Member 'preTransferTime' contains the total time from start until
  43. * just before the transfer begins. See CURLINFO_PRETRANSFER_TIME
  44. * @var RequestInfo::startTransferTime
  45. * Member 'startTransferTime' contains the total time from start until
  46. * just when the first byte is received. See CURLINFO_STARTTRANSFER_TIME
  47. * @var RequestInfo::redirectTime
  48. * Member 'redirectTime' contains the total time taken for all redirect
  49. * steps before the final transfer. See CURLINFO_REDIRECT_TIME
  50. * @var RequestInfo::redirectCount
  51. * Member 'redirectCount' contains the number of redirects followed. See
  52. * CURLINFO_REDIRECT_COUNT
  53. */
  54. typedef struct {
  55. double totalTime;
  56. double nameLookupTime;
  57. double connectTime;
  58. double appConnectTime;
  59. double preTransferTime;
  60. double startTransferTime;
  61. double redirectTime;
  62. int redirectCount;
  63. } RequestInfo;
  64. /**
  65. * @struct Info
  66. * @brief holds some diagnostics information
  67. * about the connection object it came from
  68. * @var Info::baseUrl
  69. * Member 'baseUrl' contains the base URL for the connection object
  70. * @var Info::headers
  71. * Member 'headers' contains the HeaderFields map
  72. * @var Info::timeout
  73. * Member 'timeout' contains the configured timeout
  74. * @var Info::followRedirects
  75. * Member 'followRedirects' contains whether or not to follow redirects
  76. * @var Info::maxRedirects
  77. * Member 'maxRedirects' contains the maximum number of redirect to follow (-1 unlimited)
  78. * @var Info::basicAuth
  79. * Member 'basicAuth' contains information about basic auth
  80. * @var basicAuth::username
  81. * Member 'username' contains the basic auth username
  82. * @var basicAuth::password
  83. * Member 'password' contains the basic auth password
  84. * @var Info::certPath
  85. * Member 'certPath' contains the certificate file path
  86. * @var Info::certType
  87. * Member 'certType' contains the certificate type
  88. * @var Info::keyPath
  89. * Member 'keyPath' contains the SSL key file path
  90. * @var Info::keyPassword
  91. * Member 'keyPassword' contains the SSL key password
  92. * @var Info::customUserAgent
  93. * Member 'customUserAgent' contains the custom user agent
  94. * @var Info::uriProxy
  95. * Member 'uriProxy' contains the HTTP proxy address
  96. * @var Info::lastRequest
  97. * Member 'lastRequest' contains metrics about the last request
  98. */
  99. typedef struct {
  100. std::string baseUrl;
  101. RestClient::HeaderFields headers;
  102. int timeout;
  103. bool followRedirects;
  104. int maxRedirects;
  105. bool noSignal;
  106. struct {
  107. std::string username;
  108. std::string password;
  109. } basicAuth;
  110. std::string certPath;
  111. std::string certType;
  112. std::string keyPath;
  113. std::string keyPassword;
  114. std::string customUserAgent;
  115. std::string uriProxy;
  116. RequestInfo lastRequest;
  117. } Info;
  118. explicit Connection(const std::string& baseUrl);
  119. ~Connection();
  120. // Instance configuration methods
  121. // configure basic auth
  122. void SetBasicAuth(const std::string& username,
  123. const std::string& password);
  124. // set connection timeout to seconds
  125. void SetTimeout(int seconds);
  126. // set to not use signals
  127. void SetNoSignal(bool no);
  128. // set whether to follow redirects
  129. void FollowRedirects(bool follow);
  130. // set whether to follow redirects (-1 for unlimited)
  131. void FollowRedirects(bool follow, int maxRedirects);
  132. // set custom user agent
  133. // (this will result in the UA "foo/cool restclient-cpp/VERSION")
  134. void SetUserAgent(const std::string& userAgent);
  135. // set the Certificate Authority (CA) Info which is the path to file holding
  136. // certificates to be used to verify peers. See CURLOPT_CAINFO
  137. void SetCAInfoFilePath(const std::string& caInfoFilePath);
  138. // set CURLOPT_SSLCERT
  139. void SetCertPath(const std::string& cert);
  140. // set CURLOPT_SSLCERTTYPE
  141. void SetCertType(const std::string& type);
  142. // set CURLOPT_SSLKEY. Default format is PEM
  143. void SetKeyPath(const std::string& keyPath);
  144. // set CURLOPT_KEYPASSWD.
  145. void SetKeyPassword(const std::string& keyPassword);
  146. // set CURLOPT_PROXY
  147. void SetProxy(const std::string& uriProxy);
  148. std::string GetUserAgent();
  149. RestClient::Connection::Info GetInfo();
  150. // set headers
  151. void SetHeaders(RestClient::HeaderFields headers);
  152. // get headers
  153. RestClient::HeaderFields GetHeaders();
  154. // append additional headers
  155. void AppendHeader(const std::string& key,
  156. const std::string& value);
  157. // Basic HTTP verb methods
  158. RestClient::Response get(const std::string& uri);
  159. RestClient::Response post(const std::string& uri,
  160. const std::string& data);
  161. RestClient::Response put(const std::string& uri,
  162. const std::string& data);
  163. RestClient::Response del(const std::string& uri);
  164. RestClient::Response head(const std::string& uri);
  165. RestClient::Response download(const std::string& uri, const std::string& file_to_save);
  166. private:
  167. CURL* curlHandle;
  168. std::string baseUrl;
  169. RestClient::HeaderFields headerFields;
  170. int timeout;
  171. bool followRedirects;
  172. int maxRedirects;
  173. bool noSignal;
  174. struct {
  175. std::string username;
  176. std::string password;
  177. } basicAuth;
  178. std::string customUserAgent;
  179. std::string caInfoFilePath;
  180. RequestInfo lastRequest;
  181. std::string certPath;
  182. std::string certType;
  183. std::string keyPath;
  184. std::string keyPassword;
  185. std::string uriProxy;
  186. RestClient::Response performCurlRequest(const std::string& uri, const std::string method_type = "");
  187. };
  188. }; // namespace RestClient
  189. #endif // INCLUDE_RESTCLIENT_CPP_CONNECTION_H_