-
Notifications
You must be signed in to change notification settings - Fork 2
/
influxdb.hpp
executable file
·299 lines (278 loc) · 12.9 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
#include <string>
#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 <netinet/in.h>
#include <arpa/inet.h>
#define closesocket close
#endif
#define FMT_BUF_LEN 25 // double 24 bytes, int64_t 21 bytes
#define FMT_APPEND(...) \
lines_.resize(lines_.length() + FMT_BUF_LEN);\
lines_.resize(lines_.length() - FMT_BUF_LEN + snprintf(&lines_[lines_.length() - FMT_BUF_LEN], FMT_BUF_LEN, ##__VA_ARGS__));
namespace influxdb_cpp {
struct server_info {
std::string host_;
int port_;
std::string db_;
std::string usr_;
std::string pwd_;
server_info(const std::string& host, int port, const std::string& db = "", const std::string& usr = "", const std::string& pwd = "")
: host_(host), port_(port), db_(db), usr_(usr), pwd_(pwd) {}
};
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*);
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) {
std::string qs("&q=");
detail::inner::url_encode(qs, query);
return detail::inner::http_request("GET", "query", qs, "", si, &resp);
}
inline int create_db(std::string& resp, const std::string& db_name, const server_info& si) {
std::string qs("&q=create+database+");
detail::inner::url_encode(qs, db_name);
return detail::inner::http_request("POST", "query", qs, "", si, &resp);
}
struct builder {
detail::tag_caller& meas(const std::string& m) {
lines_.clear();
lines_.reserve(0x100);
return _m(m);
}
protected:
detail::tag_caller& _m(const std::string& m) {
_escape(m, ", ");
return (detail::tag_caller&)*this;
}
detail::tag_caller& _t(const std::string& k, const std::string& v) {
lines_ += ',';
_escape(k, ",= ");
lines_ += '=';
_escape(v, ",= ");
return (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 (detail::field_caller&)*this;
}
detail::field_caller& _f_i(char delim, const std::string& k, long long v) {
lines_ += delim;
_escape(k, ",= ");
lines_ += '=';
FMT_APPEND("%lldi", v);
return (detail::field_caller&)*this;
}
detail::field_caller& _f_f(char delim, const std::string& k, double v, int prec) {
lines_ += delim;
_escape(k, ",= ");
lines_ += '=';
FMT_APPEND("%.*lf", prec, v);
return (detail::field_caller&)*this;
}
detail::field_caller& _f_b(char delim, const std::string& k, bool v) {
lines_ += delim;
_escape(k, ",= ");
lines_ += '=';
lines_ += (v ? 't' : 'f');
return (detail::field_caller&)*this;
}
detail::ts_caller& _ts(long long ts) {
FMT_APPEND(" %lld", ts);
return (detail::ts_caller&)*this;
}
int _post_http(const server_info& si, std::string* resp) {
return detail::inner::http_request("POST", "write", "", lines_, si, resp);
}
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;
if(sendto(sock, &lines_[0], lines_.length(), 0, (struct sockaddr *)&addr, sizeof(addr)) < (int)lines_.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_.append(src.c_str() + start, pos - start);
lines_ += '\\';
lines_ += src[pos];
start = ++pos;
}
lines_.append(src.c_str() + start, src.length() - start);
}
std::string lines_;
};
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) { return _post_http(si, resp); }
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); }
};
void inner::url_encode(std::string& out, const std::string& src) {
size_t pos = 0, start = 0;
while((pos = src.find_first_not_of("abcdefghijklmnopqrstuvwxyqABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~", start)) != std::string::npos) {
out.append(src.c_str() + start, pos - start);
if(src[pos] == ' ')
out += "+";
else {
out += '%';
out += to_hex((unsigned char)src[pos] >> 4);
out += to_hex((unsigned char)src[pos] & 0xF);
}
start = ++pos;
}
out.append(src.c_str() + start, src.length() - start);
}
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) {
std::string header;
struct iovec iv[2];
struct sockaddr_in addr;
int sock, ret_code = 0, content_length = 0, len = 0;
char ch;
bool chunked = false;
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;
if(connect(sock, (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&u=%s&p=%s%s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n",
method, uri, si.db_.c_str(), si.usr_.c_str(), si.pwd_.c_str(),
querystring.c_str(), si.host_.c_str(), (int)body.length());
if((int)iv[0].iov_len > len)
header.resize(len *= 2);
else
break;
}
iv[0].iov_base = &header[0];
iv[1].iov_base = (void*)&body[0];
iv[1].iov_len = body.length();
if(writev(sock, iv, 2) < (int)(iv[0].iov_len + iv[1].iov_len)) {
ret_code = -6;
goto END;
}
iv[0].iov_len = len;
#define _NO_MORE() (len >= (int)iv[0].iov_len && \
(iv[0].iov_len = recv(sock, &header[0], header.length(), len = 0)) == 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 = true;
break;
case '\r':__('\n')
switch(chunked) {
do {__('\r')__('\n')
case true:
_GET_CHUNKED_LEN(content_length, '\r')__('\n')
if(!content_length) {
__('\r')__('\n')
goto END;
}
case false:
while(content_length > 0 && !_NO_MORE()) {
content_length -= (iv[1].iov_len = std::min(content_length, (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 __
}
}
}
#undef FMT_BUF_LEN
#undef FMT_APPEND