-
Notifications
You must be signed in to change notification settings - Fork 373
/
main.cpp
614 lines (520 loc) · 19.4 KB
/
main.cpp
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include <cassert>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include "../include/cinatra.hpp"
#include "cinatra/metric_conf.hpp"
using namespace cinatra;
using namespace ylt::metric;
using namespace std::chrono_literals;
void create_file(std::string filename, size_t file_size = 64) {
std::ofstream file(filename, std::ios::binary);
if (file) {
std::string str(file_size, 'A');
file.write(str.data(), str.size());
}
}
async_simple::coro::Lazy<void> byte_ranges_download() {
create_file("test_multiple_range.txt", 64);
coro_http_server server(1, 8090);
server.set_static_res_dir("", "");
server.async_start();
std::this_thread::sleep_for(200ms);
std::string uri = "http://127.0.0.1:8090/test_multiple_range.txt";
{
std::string filename = "test1.txt";
std::error_code ec{};
std::filesystem::remove(filename, ec);
coro_http_client client{};
resp_data result = co_await client.async_download(uri, filename, "1-10");
assert(result.status == 206);
assert(std::filesystem::file_size(filename) == 10);
filename = "test2.txt";
std::filesystem::remove(filename, ec);
result = co_await client.async_download(uri, filename, "10-15");
assert(result.status == 206);
assert(std::filesystem::file_size(filename) == 6);
}
{
coro_http_client client{};
std::string uri = "http://127.0.0.1:8090/test_multiple_range.txt";
client.add_header("Range", "bytes=1-10,20-30");
auto result = co_await client.async_get(uri);
assert(result.status == 206);
assert(result.resp_body.size() == 21);
std::string filename = "test_ranges.txt";
client.add_header("Range", "bytes=0-10,21-30");
result = co_await client.async_download(uri, filename);
assert(result.status == 206);
assert(fs::file_size(filename) == 21);
}
}
async_simple::coro::Lazy<resp_data> chunked_upload1(coro_http_client &client) {
std::string filename = "test.txt";
create_file(filename, 1010);
coro_io::coro_file file{};
file.open(filename, std::ios::in);
std::string buf;
cinatra::detail::resize(buf, 100);
auto fn = [&file, &buf]() -> async_simple::coro::Lazy<read_result> {
auto [ec, size] = co_await file.async_read(buf.data(), buf.size());
co_return read_result{{buf.data(), buf.size()}, file.eof(), ec};
};
auto result = co_await client.async_upload_chunked(
"http://127.0.0.1:9001/chunked"sv, http_method::POST, std::move(fn));
co_return result;
}
async_simple::coro::Lazy<void> chunked_upload_download() {
cinatra::coro_http_server server(1, 9001);
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/chunked",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::chunked);
chunked_result result{};
std::string content;
while (true) {
result = co_await req.get_conn()->read_chunked();
if (result.ec) {
co_return;
}
if (result.eof) {
break;
}
content.append(result.data);
}
std::cout << "content size: " << content.size() << "\n";
std::cout << content << "\n";
resp.set_format_type(format_type::chunked);
resp.set_status_and_content(status_type::ok, "chunked ok");
});
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/write_chunked",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_format_type(format_type::chunked);
bool ok;
if (ok = co_await resp.get_conn()->begin_chunked(); !ok) {
co_return;
}
std::vector<std::string> vec{"hello", " world", " ok"};
for (auto &str : vec) {
if (ok = co_await resp.get_conn()->write_chunked(str); !ok) {
co_return;
}
}
ok = co_await resp.get_conn()->end_chunked();
});
server.async_start();
std::this_thread::sleep_for(200ms);
coro_http_client client{};
auto r = co_await chunked_upload1(client);
assert(r.status == 200);
assert(r.resp_body == "chunked ok");
auto ss = std::make_shared<std::stringstream>();
*ss << "hello world";
auto result = co_await client.async_upload_chunked(
"http://127.0.0.1:9001/chunked"sv, http_method::POST, ss);
assert(result.status == 200);
assert(result.resp_body == "chunked ok");
result = co_await client.async_get("http://127.0.0.1:9001/write_chunked");
assert(result.status == 200);
assert(result.resp_body == "hello world ok");
}
async_simple::coro::Lazy<void> use_websocket() {
coro_http_server server(1, 9001);
server.set_http_handler<cinatra::GET>(
"/ws_echo",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::websocket);
websocket_result result{};
while (true) {
result = co_await req.get_conn()->read_websocket();
if (result.ec) {
break;
}
if (result.type == ws_frame_type::WS_CLOSE_FRAME) {
std::cout << "close frame\n";
break;
}
if (result.type == ws_frame_type::WS_TEXT_FRAME ||
result.type == ws_frame_type::WS_BINARY_FRAME) {
std::cout << result.data << "\n";
}
else if (result.type == ws_frame_type::WS_PING_FRAME ||
result.type == ws_frame_type::WS_PONG_FRAME) {
// ping pong frame just need to continue, no need echo anything,
// because framework has reply ping/pong msg to client
// automatically.
continue;
}
else {
// error frame
break;
}
auto ec = co_await req.get_conn()->write_websocket(result.data);
if (ec) {
break;
}
}
});
server.async_start();
std::this_thread::sleep_for(300ms); // wait for server start
coro_http_client client{};
auto r = co_await client.connect("ws://127.0.0.1:9001/ws_echo");
if (r.net_err) {
co_return;
}
auto result = co_await client.write_websocket("hello websocket");
assert(!result.net_err);
auto data = co_await client.read_websocket();
assert(data.resp_body == "hello websocket");
result = co_await client.write_websocket("test again");
assert(!result.net_err);
data = co_await client.read_websocket();
assert(data.resp_body == "test again");
}
async_simple::coro::Lazy<void> static_file_server() {
std::string filename = "temp.txt";
create_file(filename, 64);
coro_http_server server(1, 9001);
std::string virtual_path = "download";
std::string files_root_path = ""; // current path
server.set_static_res_dir(
virtual_path,
files_root_path); // set this before server start, if you add new files,
// you need restart the server.
server.async_start();
std::this_thread::sleep_for(300ms); // wait for server start
coro_http_client client{};
auto result =
co_await client.async_get("http://127.0.0.1:9001/download/temp.txt");
assert(result.status == 200);
assert(result.resp_body.size() == 64);
}
struct log_t {
bool before(coro_http_request &, coro_http_response &) {
std::cout << "before log" << std::endl;
return true;
}
bool after(coro_http_request &, coro_http_response &res) {
std::cout << "after log" << std::endl;
res.add_header("aaaa", "bbcc");
return true;
}
};
struct get_data {
bool before(coro_http_request &req, coro_http_response &res) {
req.set_aspect_data("hello world");
return true;
}
};
async_simple::coro::Lazy<void> use_aspects() {
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/get",
[](coro_http_request &req, coro_http_response &resp) {
auto &val = req.get_aspect_data();
assert(val[0] == "hello world");
resp.set_status_and_content(status_type::ok, "ok");
},
log_t{}, get_data{});
server.async_start();
std::this_thread::sleep_for(300ms); // wait for server start
coro_http_client client{};
auto result = co_await client.async_get("http://127.0.0.1:9001/get");
assert(result.status == 200);
co_return;
}
struct person_t {
void foo(coro_http_request &, coro_http_response &res) {
res.set_status_and_content(status_type::ok, "ok");
}
};
async_simple::coro::Lazy<void> basic_usage() {
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/get", [](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<GET>(
"/coro",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_status_and_content(status_type::ok, "ok");
co_return;
});
server.set_http_handler<POST>(
"/form_data",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::multipart);
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
resp.set_status_and_content(status_type::bad_request,
"bad_request");
co_return;
}
std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n"; // if form data, no filename
auto part_body = co_await multipart.read_part_body(boundary);
if (part_body.ec) {
co_return;
}
std::cout << part_body.data << "\n";
if (part_body.eof) {
break;
}
}
resp.set_status_and_content(status_type::ok, "multipart finished");
});
server.set_http_handler<GET>(
"/in_thread_pool",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
// will respose in another thread.
co_await coro_io::post([&] {
// do your heavy work here when finished work, response.
resp.set_status_and_content(status_type::ok, "ok");
});
});
server.set_http_handler<POST, PUT>(
"/post", [](coro_http_request &req, coro_http_response &resp) {
auto req_body = req.get_body();
resp.set_status_and_content(status_type::ok, std::string{req_body});
});
server.set_http_handler<GET>(
"/headers", [](coro_http_request &req, coro_http_response &resp) {
auto name = req.get_header_value("name");
auto age = req.get_header_value("age");
assert(name == "tom");
assert(age == "20");
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<GET>(
"/query", [](coro_http_request &req, coro_http_response &resp) {
auto name = req.get_query_value("name");
auto age = req.get_query_value("age");
assert(name == "tom");
assert(age == "20");
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/users/:userid/subscriptions/:subid",
[](coro_http_request &req, coro_http_response &response) {
assert(req.params_["userid"] == "ultramarines");
assert(req.params_["subid"] == "guilliman");
response.set_status_and_content(status_type::ok, "ok");
});
server.use_metrics();
person_t person{};
server.set_http_handler<GET>("/person", &person_t::foo, person);
server.async_start();
std::this_thread::sleep_for(300ms); // wait for server start
coro_http_client client{};
auto result = co_await client.async_get("http://127.0.0.1:9001/get");
assert(result.status == 200);
assert(result.resp_body == "ok");
for (auto [key, val] : result.resp_headers) {
std::cout << key << ": " << val << "\n";
}
result = co_await client.async_get("/coro");
assert(result.status == 200);
client.add_str_part("hello", "form_data");
client.add_str_part("test", "value");
result =
co_await client.async_upload_multipart("http://127.0.0.1:9001/form_data");
assert(result.status == 200);
assert(result.resp_body == "multipart finished");
result = co_await client.async_get("/in_thread_pool");
assert(result.status == 200);
result = co_await client.async_post("/post", "post string",
req_content_type::string);
assert(result.status == 200);
assert(result.resp_body == "post string");
client.add_header("name", "tom");
client.add_header("age", "20");
result = co_await client.async_get("/headers");
assert(result.status == 200);
result = co_await client.async_get("/query?name=tom&age=20");
assert(result.status == 200);
result = co_await client.async_get(
"http://127.0.0.1:9001/users/ultramarines/subscriptions/guilliman");
assert(result.status == 200);
// make sure you have install openssl and enable CINATRA_ENABLE_SSL
#ifdef CINATRA_ENABLE_SSL
coro_http_client client2{};
result = client2.post("https://baidu.com", "test", req_content_type::string);
std::cout << result.resp_body << "\n";
result.net_err.value() assert(result.status == 200);
#endif
}
void use_metric() {
using namespace ylt::metric;
auto c = std::make_shared<counter_t>("request_count", "request count");
auto failed = std::make_shared<gauge_t>("not_found_request_count",
"not found request count");
auto total =
std::make_shared<counter_t>("total_request_count", "total request count");
auto h =
std::make_shared<histogram_t>(std::string("test"), std::string("help"),
std::vector{5.0, 10.0, 20.0, 50.0, 100.0});
auto summary = std::make_shared<summary_t>(std::string("test_summary"),
std::string("summary help"),
std::vector{0.5, 0.9, 0.95, 0.99});
default_static_metric_manager::instance().register_metric(c);
default_static_metric_manager::instance().register_metric(total);
default_static_metric_manager::instance().register_metric(failed);
default_static_metric_manager::instance().register_metric(h);
default_static_metric_manager::instance().register_metric(summary);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 100);
std::thread thd([&] {
while (true) {
c->inc();
total->inc();
h->observe(distr(gen));
summary->observe(distr(gen));
std::this_thread::sleep_for(1s);
}
});
thd.detach();
coro_http_server server(1, 9001);
server.set_default_handler(
[&](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
failed->inc();
total->inc();
resp.set_status_and_content(status_type::not_found, "not found");
co_return;
});
server.set_http_handler<GET>(
"/get", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
c->inc();
total->inc();
});
server.set_http_handler<GET>(
"/test", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
c->inc();
total->inc();
});
server.set_http_handler<GET, POST>(
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
total->inc();
});
server.set_http_handler<GET, POST>(
"/metrics", [](coro_http_request &req, coro_http_response &resp) {
resp.need_date_head(false);
resp.set_status_and_content(status_type::ok, "");
});
server.sync_start();
}
void metrics_example() {
auto get_req_counter = std::make_shared<counter_t>(
"get_req_count", "get req count",
std::map<std::string, std::string>{{"url", "/get"}});
auto get_req_qps = std::make_shared<gauge_t>("get_req_qps", "get req qps");
// default_static_metric_manager::instance().register_metric_static(get_req_counter,
// get_req_qps);
int64_t last = 0;
std::thread thd([&] {
while (true) {
std::this_thread::sleep_for(1s);
auto value = get_req_counter->value();
get_req_qps->update(value - last);
last = value;
}
});
thd.detach();
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/get", [&](coro_http_request &req, coro_http_response &resp) {
// get_req_counter->inc({"/get"});
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<GET>(
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics(true, "/metrics");
server.sync_start();
}
async_simple::coro::Lazy<void> use_channel() {
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics();
server.async_start();
std::this_thread::sleep_for(100ms);
auto channel = std::make_shared<coro_io::load_blancer<coro_http_client>>(
coro_io::load_blancer<coro_http_client>::create(
{"127.0.0.1:9001"}, {.lba = coro_io::load_blance_algorithm::random}));
std::string url = "http://127.0.0.1:9001/";
co_await channel->send_request(
[&url](coro_http_client &client,
std::string_view host) -> async_simple::coro::Lazy<void> {
auto data = co_await client.async_get(url);
std::cout << data.net_err.message() << "\n";
std::cout << data.resp_body << "\n";
});
}
async_simple::coro::Lazy<void> use_pool() {
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "hello world");
});
server.use_metrics();
server.async_start();
auto map = default_static_metric_manager::instance().metric_map();
for (auto &[k, m] : map) {
std::cout << k << ", ";
std::cout << m->help() << "\n";
}
std::string url = "http://127.0.0.1:9001/";
auto pool = coro_io::client_pool<coro_http_client>::create(
url, {std::thread::hardware_concurrency() * 2});
std::atomic<size_t> count = 0;
for (size_t i = 0; i < 10000; i++) {
pool->send_request(
[&](coro_http_client &client) -> async_simple::coro::Lazy<void> {
auto data = co_await client.async_get(url);
std::cout << data.resp_body << "\n";
})
.start([&](auto &&) {
count++;
});
}
while (count != 10000) {
std::this_thread::sleep_for(5ms);
}
int size = pool->free_client_count();
printf("current client count: %d, \n", size);
co_return;
}
int main() {
// use_metric();
// metrics_example();
async_simple::coro::syncAwait(use_channel());
async_simple::coro::syncAwait(use_pool());
async_simple::coro::syncAwait(basic_usage());
async_simple::coro::syncAwait(use_aspects());
async_simple::coro::syncAwait(static_file_server());
async_simple::coro::syncAwait(use_websocket());
async_simple::coro::syncAwait(chunked_upload_download());
async_simple::coro::syncAwait(byte_ranges_download());
return 0;
}