-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.hpp
455 lines (387 loc) · 12.5 KB
/
http.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#pragma once
#include <event2/event.h>
#include <event2/thread.h>
#include <event2/http.h>
#include <event2/buffer.h>
#include <event2/http_struct.h>
#include <unordered_map>
#include <functional>
#include <memory>
#include <unistd.h>
#include <future>
#include <vector>
#include <string.h>
#include <sys/eventfd.h>
#include <errno.h>
#include <chrono>
#include <type_traits>
namespace http {
namespace internal {
timeval* setTimevalFromMs(timeval& tv, int64_t ms) {
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
return &tv;
}
template <typename Duration>
timeval* setTimevalFromDuration(timeval& tv, const Duration d) {
return setTimevalFromMs(tv, std::chrono::duration_cast<std::chrono::milliseconds>(d).count());
}
template <typename T>
struct is_chrono_duration {
static constexpr bool value = false;
};
template <typename Rep, typename Period>
struct is_chrono_duration<std::chrono::duration<Rep, Period>> {
static constexpr bool value = true;
};
template <typename T, void(*Free)(T*)>
class Handle {
static void SafeFree(T *t) {
if(t) {
Free(t);
}
}
public:
Handle(T *t) : handle_(t, SafeFree) {}
Handle() : handle_(nullptr, SafeFree) {}
operator bool() {
return handle_.get() != nullptr;
}
operator T*() {
return handle_.get();
}
T* operator->() const {
return handle_.get();
}
private:
std::shared_ptr<T> handle_;
};
const char* evhttp_err2str(evhttp_request_error error) {
switch(error) {
case EVREQ_HTTP_TIMEOUT:
return "Timeout reached";
case EVREQ_HTTP_EOF:
return "EOF reached";
case EVREQ_HTTP_INVALID_HEADER:
return "Error while reading header, or invalid header";
case EVREQ_HTTP_BUFFER_ERROR:
return "Error encountered while reading or writing";
case EVREQ_HTTP_REQUEST_CANCEL:
return "The evhttp_cancel_request() called on this request";
case EVREQ_HTTP_DATA_TOO_LONG:
return "Body is greater then max_size";
default:
return "Unknown error";
}
}
using EvHttpConnection = Handle<evhttp_connection, evhttp_connection_free>;
using EvHttpUri = Handle<evhttp_uri, evhttp_uri_free>;
using EventBase = Handle<event_base, event_base_free>;
using Event = Handle<event, event_free>;
}
class EventLoop;
typedef std::shared_ptr<EventLoop> EventLoopPtr;
class EventLoop : public std::enable_shared_from_this<EventLoop> {
public:
class Timer;
typedef std::function<bool()> TimeoutFunc;
static EventLoopPtr New() {
EventLoopPtr loop(new EventLoop());
loop->RunInBackground();
return loop;
}
EventLoop(const EventLoop&) = delete;
EventLoop& operator=(const EventLoop&) = delete;
EventLoop(EventLoop&&) = delete;
EventLoop& operator=(EventLoop&&) = delete;
~EventLoop() {
Stop();
WaitBackground();
event_del(notify_ev_);
close(notify_fd_);
}
event_base* GetEventBase() {
return base_;
}
void Interrupt() {
uint64_t dummy = 1;
dummy = write(notify_fd_, &dummy, sizeof(dummy));
}
template <typename Duration, typename Func>
void Cron(const Duration& duration, Func&& func) {
static_assert(internal::is_chrono_duration<Duration>::value, "arg0 must be a std::chrono::duration");
auto *timer = new Timer(shared_from_this(), std::forward<Func>(func));
timer->RunAfter(duration);
}
void Run() {
if(!WaitBackground()) {
event_base_loop(base_, 0);
}
}
void Stop(int64_t ms = 0) {
timeval tv;
event_base_loopexit(base_, ms ? internal::setTimevalFromMs(tv, ms) : NULL);
}
void RunInBackground() {
if(!bg_.joinable()) {
bg_ = std::thread(event_base_loop, static_cast<event_base*>(base_), 0);
}
}
template <typename Func>
Timer* NewTimer(Func&& func) {
return new Timer(shared_from_this(), std::forward<Func>(func));
};
Timer* NewTimer() {
return new Timer(shared_from_this());
};
class Timer {
public:
Timer(EventLoopPtr loop) : loop_(loop) {
timer_ev_ = event_new(loop_->GetEventBase(), -1, EV_PERSIST, &Timer::Timeout, this);
}
template <typename Func>
Timer(EventLoopPtr loop, Func&& func) : Timer(loop) {
func_ = std::forward<Func>(func);
}
~Timer() {
Stop();
}
void Stop() {
event_del(timer_ev_);
}
template <typename Duration>
void RunAfter(const Duration& duration) {
static_assert(internal::is_chrono_duration<Duration>::value, "arg0 must be a std::chrono::duration");
timeval tv;
evtimer_add(timer_ev_, internal::setTimevalFromDuration(tv, duration));
loop_->Interrupt();
}
template <typename Duration, typename Func>
void RunAfter(const Duration& duration, Func&& func) {
func_ = std::forward<Func>(func);
RunAfter(duration);
}
EventLoopPtr GetEventLoop() {
return loop_;
}
private:
static void Timeout(evutil_socket_t fd, short event, void *arg) {
Timer *timer = static_cast<Timer*>(arg);
if(!timer->func_()) {
delete timer;
}
}
EventLoopPtr loop_;
internal::Event timer_ev_;
TimeoutFunc func_;
};
private:
EventLoop() {
evthread_use_pthreads();
base_ = event_base_new();
notify_fd_ = eventfd(0, EFD_NONBLOCK);
notify_ev_ = event_new(base_, notify_fd_, EV_READ | EV_PERSIST, Wakeup, nullptr);
event_add(notify_ev_, nullptr);
}
bool WaitBackground() {
if(bg_.joinable()) {
if(bg_.get_id() == std::this_thread::get_id()) {
bg_.detach();
} else {
bg_.join();
}
return true;
}
return false;
}
static void Wakeup(evutil_socket_t fd, short event, void *arg) {
uint64_t dummy;
dummy = read(fd, &dummy, sizeof(dummy));
}
private:
int notify_fd_ = -1;
internal::Event notify_ev_;
internal::EventBase base_;
std::thread bg_;
};
class Response {
public:
struct StringSlice {
char *content;
size_t length;
char& operator[](int n) {
return content[n];
}
};
Response(evhttp_request *req) : req_(req) {}
int GetResponseCode() const {
return req_->response_code;
}
const char* GetHeader(const char *key) const {
return evhttp_find_header(req_->input_headers, key);
}
StringSlice GetBody() {
auto *buf = req_->input_buffer;
if(buf) {
return {reinterpret_cast<char*>(evbuffer_pullup(buf, -1)),
req_->body_size};
}
return {nullptr, 0};
}
private:
evhttp_request *req_;
};
class Get {
Get(const Get&) = delete;
Get& operator=(const Get&) = delete;
struct ExecuteContext;
public:
typedef std::function<void(Response&&)> FinishFunc;
typedef std::function<void(const char*)> ErrorFunc;
public:
Get(Get&&) = default;
Get& operator=(Get&&) = default;
Get(const std::string& url) : Get(url.c_str()) {}
Get(const std::string& url, EventLoopPtr& loop) : Get(url.c_str(), loop) {}
Get(const char *url) {
loop_ = EventLoop::New();
Init(url);
}
Get(const char *url, EventLoopPtr loop) {
loop_ = loop;
Init(url);
}
Get& SetTimeoutMs(int ms) {
timeval tv;
evhttp_connection_set_timeout_tv(conn_, internal::setTimevalFromMs(tv, ms));
return *this;
}
Get& SetRetryMax(int times) {
evhttp_connection_set_retries(conn_, times);
return *this;
}
Get& AddHeader(const char *key, std::string value) {
headers_[key] = std::move(value);
return *this;
}
Get& SetRetryIntervalMs(int ms) {
timeval tv;
evhttp_connection_set_initial_retry_tv(conn_, internal::setTimevalFromMs(tv, ms));
return *this;
}
Get& SetMaxBodySize(ssize_t size) {
evhttp_connection_set_max_body_size(conn_, size);
return *this;
}
Get& SetMaxHeaderSize(ssize_t size) {
evhttp_connection_set_max_body_size(conn_, size);
return *this;
}
template <typename T>
void Execute(T&& finish_func) {
static_assert(std::is_convertible<T, FinishFunc>::value, "arg0 must be a http::Get::FinishFunc");
auto *context = new ExecuteContext();
context->finish_func = std::forward<T>(finish_func);
Execute(context);
}
template <typename T, typename U>
void Execute(T&& finish_func, U&& error_func) {
static_assert(std::is_convertible<T, FinishFunc>::value, "arg0 must be a http::Get::FinishFunc");
static_assert(std::is_convertible<U, ErrorFunc>::value, "arg1 must be a http::Get::ErrorFunc");
auto *context = new ExecuteContext();
context->finish_func = std::forward<T>(finish_func);
context->error_func = std::forward<U>(error_func);
Execute(context);
}
Response Execute() {
std::promise<Response> promise;
return Execute(promise).get();
}
std::future<Response> Execute(std::promise<Response>& promise) {
try {
Execute([&promise](Response&& res) mutable {
promise.set_value(std::move(res));
}, [&promise](const char *msg) mutable {
promise.set_exception(std::make_exception_ptr(std::runtime_error(msg)));
});
} catch(...) {
promise.set_exception(std::current_exception());
}
return promise.get_future();
}
private:
void Init(const char *url) {
uri_ = evhttp_uri_parse(url);
if(!uri_) {
throw std::invalid_argument("invalid url");
}
const auto *host = evhttp_uri_get_host(uri_);
const int port = evhttp_uri_get_port(uri_);
if(!host) {
throw std::invalid_argument("can't get host");
}
conn_ = evhttp_connection_base_new(loop_->GetEventBase(), nullptr, host, port < 0 ? 80 : port);
if(!conn_) {
throw std::invalid_argument((std::string("can't resolve host: ") + host).c_str());
}
}
void Execute(ExecuteContext *context) {
std::string path_query;
auto *req = evhttp_request_new(&GetComplete, context);
auto *path = evhttp_uri_get_path(uri_);
auto *query = evhttp_uri_get_query(uri_);
path_query += path[0] != '\0' ? path : "/";
if(query && query[0] != '\0') {
path_query += "?";
path_query += query;
}
if(context->error_func) {
evhttp_request_set_error_cb(req, GetError);
}
for(auto& header : headers_) {
evhttp_add_header(req->output_headers, header.first.c_str(), header.second.c_str());
}
evhttp_add_header(req->output_headers, "Host", evhttp_uri_get_host(uri_));
if(evhttp_make_request(conn_, req, EVHTTP_REQ_GET, path_query.c_str()) < 0) {
delete context;
throw std::runtime_error(strerror(errno));
}
context->uri = uri_;
context->conn = conn_;
context->loop = loop_;
loop_->Interrupt();
}
private:
struct ExecuteContext{
bool has_error = false;
Get::FinishFunc finish_func;
Get::ErrorFunc error_func;
internal::EvHttpUri uri;
internal::EvHttpConnection conn;
EventLoopPtr loop;
};
static void GetError(evhttp_request_error error, void *arg) {
ExecuteContext *context = static_cast<ExecuteContext*>(arg);
context->error_func(internal::evhttp_err2str(error));
context->has_error = true;
}
static void GetComplete(evhttp_request *req, void *arg) {
ExecuteContext *context = static_cast<ExecuteContext*>(arg);
if(!context->has_error) {
if(req && req->response_code) {
context->finish_func(Response(req));
} else {
if(context->error_func) {
context->error_func(internal::evhttp_err2str(EVREQ_HTTP_TIMEOUT));
}
}
}
delete context;
}
private:
internal::EvHttpUri uri_;
internal::EvHttpConnection conn_;
EventLoopPtr loop_;
std::unordered_map<std::string, std::string> headers_;
};
}