-
Notifications
You must be signed in to change notification settings - Fork 83
/
influxdb.hpp
316 lines (290 loc) Β· 14.3 KB
/
influxdb.hpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
influxdb-cpp -- π C++ client for InfluxDB.
Copyright (c) 2010-2018 <http://ez8.co> <[email protected]>
This library is released under the MIT License.
Please see LICENSE file or visit https://github.com/orca-zhang/influxdb-cpp for details.
*/
#ifndef INFLUXDB_CPP_HPP
#define INFLUXDB_CPP_HPP
#include <sstream>
#include <cstring>
#include <cstdio>
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#include <algorithm>
#pragma comment(lib, "ws2_32")
typedef struct iovec { void* iov_base; size_t iov_len; } iovec;
inline __int64 writev(int sock, struct iovec* iov, int cnt) {
__int64 r = send(sock, (const char*)iov->iov_base, iov->iov_len, 0);
return (r < 0 || cnt == 1) ? r : r + writev(sock, iov + 1, cnt - 1);
}
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define closesocket close
#endif
namespace influxdb_cpp {
struct server_info {
std::string host_;
int port_;
std::string db_;
std::string usr_;
std::string pwd_;
std::string precision_;
std::string token_;
server_info(const std::string& host, int port, const std::string& db = "", const std::string& usr = "", const std::string& pwd = "", const std::string& precision = "ms", const std::string& token = "")
: host_(host), port_(port), db_(db), usr_(usr), pwd_(pwd), precision_(precision), token_(token) {}
};
namespace detail {
struct meas_caller;
struct tag_caller;
struct field_caller;
struct ts_caller;
struct inner {
static int http_request(const char*, const char*, const std::string&, const std::string&, const server_info&, std::string*, unsigned timeout_sec = 0);
static inline unsigned char to_hex(unsigned char x) { return x > 9 ? x + 55 : x + 48; }
};
}
static void url_encode(std::string& out, const std::string& src);
inline int query(std::string& resp, const std::string& query, const server_info& si, unsigned timeout_sec = 0) {
std::string qs("&q=");
url_encode(qs, query);
return detail::inner::http_request("GET", "query", qs, "", si, &resp, timeout_sec);
}
inline int create_db(std::string& resp, const std::string& db_name, const server_info& si, unsigned timeout_sec = 0) {
std::string qs("&q=create+database+");
url_encode(qs, db_name);
return detail::inner::http_request("POST", "query", qs, "", si, &resp, timeout_sec);
}
struct builder {
detail::tag_caller& meas(const std::string& m) {
lines_.imbue(std::locale("C"));
lines_.clear();
return _m(m);
}
protected:
detail::tag_caller& _m(const std::string& m) {
_escape(m, ", ");
return reinterpret_cast<detail::tag_caller&>(*this);
}
detail::tag_caller& _t(const std::string& k, const std::string& v) {
lines_ << ',';
_escape(k, ",= ");
lines_ << '=';
_escape(v, ",= ");
return reinterpret_cast<detail::tag_caller&>(*this);
}
detail::field_caller& _f_s(char delim, const std::string& k, const std::string& v) {
lines_ << delim;
_escape(k, ",= ");
lines_ << "=\"";
_escape(v, "\"");
lines_ << '\"';
return reinterpret_cast<detail::field_caller&>(*this);
}
detail::field_caller& _f_i(char delim, const std::string& k, long long v) {
lines_ << delim;
_escape(k, ",= ");
lines_ << '=';
lines_ << v << 'i';
return reinterpret_cast<detail::field_caller&>(*this);
}
detail::field_caller& _f_f(char delim, const std::string& k, double v, int prec) {
lines_ << delim;
_escape(k, ",= ");
lines_.precision(prec);
lines_.setf(std::ios::fixed);
lines_ << '=' << v;
return reinterpret_cast<detail::field_caller&>(*this);
}
detail::field_caller& _f_b(char delim, const std::string& k, bool v) {
lines_ << delim;
_escape(k, ",= ");
lines_ << '=' << (v ? 't' : 'f');
return reinterpret_cast<detail::field_caller&>(*this);
}
detail::ts_caller& _ts(long long ts) {
lines_ << ' ' << ts;
return reinterpret_cast<detail::ts_caller&>(*this);
}
int _post_http(const server_info& si, std::string* resp, unsigned timeout_sec = 0) {
return detail::inner::http_request("POST", "write", "", lines_.str(), si, resp, timeout_sec);
}
int _send_udp(const std::string& host, int port) {
int sock, ret = 0;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if((addr.sin_addr.s_addr = inet_addr(host.c_str())) == INADDR_NONE) return -1;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return -2;
lines_ << '\n';
if(sendto(sock, &lines_.str()[0], lines_.str().length(), 0, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)), static_cast<int>(lines_.str().length()))
ret = -3;
closesocket(sock);
return ret;
}
void _escape(const std::string& src, const char* escape_seq) {
size_t pos = 0, start = 0;
while((pos = src.find_first_of(escape_seq, start)) != std::string::npos) {
lines_.write(src.c_str() + start, pos - start);
lines_ << '\\' << src[pos];
start = ++pos;
}
lines_.write(src.c_str() + start, src.length() - start);
}
std::stringstream lines_;
};
inline void url_encode(std::string& out, const std::string& src) {
size_t pos = 0, start = 0;
while((pos = src.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~", start)) != std::string::npos) {
out.append(src.c_str() + start, pos - start);
if(src[pos] == ' ')
out += "+";
else {
out += '%';
out += detail::inner::to_hex(static_cast<unsigned char>(src[pos]) >> 4);
out += detail::inner::to_hex(static_cast<unsigned char>(src[pos]) & 0xF);
}
start = ++pos;
}
out.append(src.c_str() + start, src.length() - start);
}
namespace detail {
struct tag_caller : public builder {
detail::tag_caller& tag(const std::string& k, const std::string& v) { return _t(k, v); }
detail::field_caller& field(const std::string& k, const std::string& v) { return _f_s(' ', k, v); }
detail::field_caller& field(const std::string& k, bool v) { return _f_b(' ', k, v); }
detail::field_caller& field(const std::string& k, short v) { return _f_i(' ', k, v); }
detail::field_caller& field(const std::string& k, int v) { return _f_i(' ', k, v); }
detail::field_caller& field(const std::string& k, long v) { return _f_i(' ', k, v); }
detail::field_caller& field(const std::string& k, long long v) { return _f_i(' ', k, v); }
detail::field_caller& field(const std::string& k, double v, int prec = 2) { return _f_f(' ', k, v, prec); }
private:
detail::tag_caller& meas(const std::string& m);
};
struct ts_caller : public builder {
detail::tag_caller& meas(const std::string& m) { lines_ << '\n'; return _m(m); }
int post_http(const server_info& si, std::string* resp = NULL,
unsigned timeout_sec = 0) { return _post_http(si, resp, timeout_sec); }
int send_udp(const std::string& host, int port) { return _send_udp(host, port); }
};
struct field_caller : public ts_caller {
detail::field_caller& field(const std::string& k, const std::string& v) { return _f_s(',', k, v); }
detail::field_caller& field(const std::string& k, bool v) { return _f_b(',', k, v); }
detail::field_caller& field(const std::string& k, short v) { return _f_i(',', k, v); }
detail::field_caller& field(const std::string& k, int v) { return _f_i(',', k, v); }
detail::field_caller& field(const std::string& k, long v) { return _f_i(',', k, v); }
detail::field_caller& field(const std::string& k, long long v) { return _f_i(',', k, v); }
detail::field_caller& field(const std::string& k, double v, int prec = 2) { return _f_f(',', k, v, prec); }
detail::ts_caller& timestamp(unsigned long long ts) { return _ts(ts); }
};
inline int inner::http_request(const char* method, const char* uri,
const std::string& querystring, const std::string& body, const server_info& si, std::string* resp, unsigned timeout_sec) {
std::string header;
struct iovec iv[2];
struct sockaddr_in addr;
int sock, ret_code = 0, content_length = 0, len = 0;
char ch;
unsigned char chunked = 0;
addr.sin_family = AF_INET;
addr.sin_port = htons(si.port_);
if((addr.sin_addr.s_addr = inet_addr(si.host_.c_str())) == INADDR_NONE) return -1;
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -2;
struct timeval timeout;
timeout.tv_sec = static_cast<long>(timeout_sec);
timeout.tv_usec = 0;
if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char*> (&timeout), sizeof(timeout)) < 0 ||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*> (&timeout), sizeof(timeout)) < 0) return -2;
if(connect(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
closesocket(sock);
return -3;
}
header.resize(len = 0x100);
for(;;) {
iv[0].iov_len = snprintf(&header[0], len,
"%s /%s?db=%s%s%s%s%s%s%s%s HTTP/1.1\r\nHost: %s%s%s\r\nContent-Length: %d\r\n\r\n",
method, uri, si.db_.c_str(), !si.token_.empty() ? "" : "&u=", !si.token_.empty() ? "" : si.usr_.c_str(), !si.token_.empty() ? "" : "&p=", !si.token_.empty() ? "" : si.pwd_.c_str(),
strcmp(uri, "write") ? "&epoch=" : "&precision=", si.precision_.c_str(), querystring.c_str(), si.host_.c_str(), si.token_.empty() ? "" : "\r\nAuthorization: Token ", si.token_.c_str(), (int)body.length());
if(static_cast<int>(iv[0].iov_len) >= len)
header.resize(len *= 2);
else
break;
}
iv[0].iov_base = &header[0];
iv[1].iov_base = const_cast<char*>(&body[0]);
iv[1].iov_len = body.length();
if(writev(sock, iv, 2) < static_cast<int>(iv[0].iov_len + iv[1].iov_len)) {
ret_code = -6;
goto END;
}
iv[0].iov_len = len;
#define _NO_MORE() (len >= static_cast<int>(iv[0].iov_len) && \
(iv[0].iov_len = recv(sock, &header[0], header.length(), len = 0)) == static_cast<size_t>(-1))
#define _GET_NEXT_CHAR() (ch = _NO_MORE() ? 0 : header[len++])
#define _LOOP_NEXT(statement) for(;;) { if(!(_GET_NEXT_CHAR())) { ret_code = -7; goto END; } statement }
#define _UNTIL(c) _LOOP_NEXT( if(ch == c) break; )
#define _GET_NUMBER(n) _LOOP_NEXT( if(ch >= '0' && ch <= '9') n = n * 10 + (ch - '0'); else break; )
#define _GET_CHUNKED_LEN(n, c) _LOOP_NEXT( if(ch >= '0' && ch <= '9') n = n * 16 + (ch - '0'); \
else if(ch >= 'A' && ch <= 'F') n = n * 16 + (ch - 'A') + 10; \
else if(ch >= 'a' && ch <= 'f') n = n * 16 + (ch - 'a') + 10; else {if(ch != c) { ret_code = -8; goto END; } break;} )
#define _(c) if((_GET_NEXT_CHAR()) != c) break;
#define __(c) if((_GET_NEXT_CHAR()) != c) { ret_code = -9; goto END; }
if(resp) resp->clear();
_UNTIL(' ')_GET_NUMBER(ret_code)
for(;;) {
_UNTIL('\n')
switch(_GET_NEXT_CHAR()) {
case 'C':_('o')_('n')_('t')_('e')_('n')_('t')_('-')
_('L')_('e')_('n')_('g')_('t')_('h')_(':')_(' ')
_GET_NUMBER(content_length)
break;
case 'T':_('r')_('a')_('n')_('s')_('f')_('e')_('r')_('-')
_('E')_('n')_('c')_('o')_('d')_('i')_('n')_('g')_(':')
_(' ')_('c')_('h')_('u')_('n')_('k')_('e')_('d')
chunked = 1;
break;
case '\r':__('\n')
switch(chunked) {
do {__('\r')__('\n')
case 1:
_GET_CHUNKED_LEN(content_length, '\r')__('\n')
if(!content_length) {
__('\r')__('\n')
goto END;
}
case 0:
while(content_length > 0 && !_NO_MORE()) {
content_length -= (iv[1].iov_len = std::min(content_length, static_cast<int>(iv[0].iov_len) - len));
if(resp) resp->append(&header[len], iv[1].iov_len);
len += iv[1].iov_len;
}
} while(chunked);
}
goto END;
}
if(!ch) {
ret_code = -10;
goto END;
}
}
ret_code = -11;
END:
closesocket(sock);
return ret_code / 100 == 2 ? 0 : ret_code;
#undef _NO_MORE
#undef _GET_NEXT_CHAR
#undef _LOOP_NEXT
#undef _UNTIL
#undef _GET_NUMBER
#undef _GET_CHUNKED_LEN
#undef _
#undef __
}
}
}
#endif // INFLUXDB_CPP_HPP