forked from mrtazz/restclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.h
More file actions
164 lines (144 loc) · 5.38 KB
/
connection.h
File metadata and controls
164 lines (144 loc) · 5.38 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @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
* @var RequestInfo::totalTime
* Member 'totalTime' contains the total time of the last request in
* seconds Total time of previous transfer. See CURLINFO_TOTAL_TIME
* @var RequestInfo::nameLookupTime
* Member 'nameLookupTime' contains the time spent in DNS lookup in
* seconds Time from start until name resolving completed. See
* CURLINFO_NAMELOOKUP_TIME
* @var RequestInfo::connectTime
* Member 'connectTime' contains the time it took until Time from start
* until remote host or proxy completed. See CURLINFO_CONNECT_TIME
* @var RequestInfo::appConnectTime
* Member 'appConnectTime' contains the time from start until SSL/SSH
* handshake completed. See CURLINFO_APPCONNECT_TIME
* @var RequestInfo::preTransferTime
* Member 'preTransferTime' contains the total time from start until
* just before the transfer begins. See CURLINFO_PRETRANSFER_TIME
* @var RequestInfo::startTransferTime
* Member 'startTransferTime' contains the total time from start until
* just when the first byte is received. See CURLINFO_STARTTRANSFER_TIME
* @var RequestInfo::redirectTime
* Member 'redirectTime' contains the total time taken for all redirect
* steps before the final transfer. See CURLINFO_REDIRECT_TIME
* @var RequestInfo::redirectCount
* Member 'redirectCount' contains the number of redirects followed. See
* CURLINFO_REDIRECT_COUNT
*/
typedef struct {
double totalTime;
double nameLookupTime;
double connectTime;
double appConnectTime;
double preTransferTime;
double startTransferTime;
double redirectTime;
int redirectCount;
} RequestInfo;
/**
* @struct Info
* @brief holds some diagnostics information
* about the connection object it came from
* @var Info::baseUrl
* Member 'baseUrl' contains the base URL for the connection object
* @var Info::headers
* Member 'headers' contains the HeaderFields map
* @var Info::timeout
* Member 'timeout' contains the configured timeout
* @var Info::followRedirects
* Member 'followRedirects' contains whether or not to follow redirects
* @var Info::basicAuth
* Member 'basicAuth' contains information about basic auth
* @var basicAuth::username
* Member 'username' contains the basic auth username
* @var basicAuth::password
* Member 'password' contains the basic auth password
* @var Info::customUserAgent
* Member 'customUserAgent' contains the custom user agent
* @var Info::lastRequest
* Member 'lastRequest' contains metrics about the last request
*/
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_