/
connection.h
136 lines (116 loc) · 3.92 KB
/
connection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @file connection.h
* @brief header definitions for restclient-cpp connection class
* @author Daniel Schauenberg <d@unwiredcouch.com>
* @version
* @date 2010-10-11
*/
#ifndef INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
#define INCLUDE_RESTCLIENT_CPP_CONNECTION_H_
#include <curl/curl.h>
#include <string>
#include <map>
#include <cstdlib>
#include "restclient-cpp/restclient.h"
#include "restclient-cpp/version.h"
/**
* @brief namespace for all RestClient definitions
*/
namespace RestClient {
/**
* @brief Connection object for advanced usage
*/
class Connection {
public:
/**
* @struct RequestInfo
* @brief holds some diagnostics information
* about a request
*/
typedef struct {
// total time of the last request in seconds Total time of previous
// transfer. See CURLINFO_TOTAL_TIME
double totalTime;
// time spent in DNS lookup in seconds Time from start until name
// resolving completed. See CURLINFO_NAMELOOKUP_TIME
double nameLookupTime;
// time it took until Time from start until remote host or proxy
// completed. See CURLINFO_CONNECT_TIME
double connectTime;
// Time from start until SSL/SSH handshake completed. See
// CURLINFO_APPCONNECT_TIME
double appConnectTime;
// Time from start until just before the transfer begins. See
// CURLINFO_PRETRANSFER_TIME
double preTransferTime;
// Time from start until just when the first byte is received. See
// CURLINFO_STARTTRANSFER_TIME
double startTransferTime;
// Time taken for all redirect steps before the final transfer. See
// CURLINFO_REDIRECT_TIME
double redirectTime;
// number of redirects followed. See CURLINFO_REDIRECT_COUNT
int redirectCount;
} RequestInfo;
/**
* @struct Info
* @brief holds some diagnostics information
* about the connection object it came from
*/
typedef struct {
std::string baseUrl;
RestClient::HeaderFields headers;
int timeout;
bool followRedirects;
struct {
std::string username;
std::string password;
} basicAuth;
std::string customUserAgent;
RequestInfo lastRequest;
} Info;
explicit Connection(const std::string baseUrl);
~Connection();
// Instance configuration methods
// configure basic auth
void SetBasicAuth(const std::string& username,
const std::string& password);
// set connection timeout to seconds
void SetTimeout(int seconds);
// set whether to follow redirects
void FollowRedirects(bool follow);
// set custom user agent
// (this will result in the UA "foo/cool restclient-cpp/VERSION")
void SetUserAgent(const std::string& userAgent);
std::string GetUserAgent();
RestClient::Connection::Info GetInfo();
// set headers
void SetHeaders(RestClient::HeaderFields headers);
// get headers
RestClient::HeaderFields GetHeaders();
// append additional headers
void AppendHeader(const std::string& key,
const std::string& value);
// Basic HTTP verb methods
RestClient::Response get(const std::string& uri);
RestClient::Response post(const std::string& uri,
const std::string& data);
RestClient::Response put(const std::string& uri,
const std::string& data);
RestClient::Response del(const std::string& uri);
private:
CURL* curlHandle;
std::string baseUrl;
RestClient::HeaderFields headerFields;
int timeout;
bool followRedirects;
struct {
std::string username;
std::string password;
} basicAuth;
std::string customUserAgent;
RequestInfo lastRequest;
RestClient::Response performCurlRequest(const std::string& uri);
};
}; // namespace RestClient
#endif // INCLUDE_RESTCLIENT_CPP_CONNECTION_H_