Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
420 lines (371 loc) · 12.5 KB
/
Copy pathmain.cpp
File metadata and controls
420 lines (371 loc) · 12.5 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
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
#include <iostream>
#include "../include/cinatra.hpp"
using namespace cinatra;
struct log_t
{
bool before(request& req, response& res) {
std::cout << "before log" << std::endl;
return true;
}
bool after(request& req, response& res) {
std::cout << "after log" << std::endl;
res.add_header("aaaa", "bbcc");
return true;
}
};
struct person
{
void foo(request& req, response& res)
{
std::cout << i << std::endl;
res.render_string("ok");
}
void foo1(request& req, response& res)
{
std::cout << i << std::endl;
res.render_string("ok");
}
int i=0;
};
void test_ssl_server(){
#ifdef CINATRA_ENABLE_SSL
//you should open macro CINATRA_ENABLE_SSL at first
http_ssl_server server(2);
server.set_ssl_conf({ "server.crt", "server.key" });
int r = server.listen("0.0.0.0", "9001");
if (r < 0) {
return;
}
server.set_http_handler<GET, POST>("/", [](request& req, response& res) {
auto str = req.get_conn<cinatra::SSL>()->remote_address();
res.set_status_and_content(status_type::ok, "hello world from 9001");
});
server.run();
#endif
}
void print(const response_data& result) {
print(result.ec, result.status, result.resp_body, result.resp_headers.second);
}
void test_sync_client() {
auto client = cinatra::client_factory::instance().new_client();
std::string uri = "http://www.baidu.com";
std::string uri1 = "http://cn.bing.com";
std::string uri2 = "https://www.baidu.com";
std::string uri3 = "https://cn.bing.com";
response_data result = client->get(uri);
print(result);
response_data result1 = client->get(uri1);
print(result1);
print(client->post(uri, "hello", req_content_type::json));
print(client->post(uri1, "hello"));
#ifdef CINATRA_ENABLE_SSL
response_data result2 = client->get(uri2);
print(result2);
response_data result3 = client->get(uri3);
print(result3);
response_data result33 = client->get(uri);
print(result33);
response_data result4 = client->get(uri3);
print(result4);
response_data result5 = client->get(uri2);
print(result5);
#endif
}
void test_async_client() {
std::string uri = "http://www.baidu.com";
std::string uri1 = "http://cn.bing.com";
std::string uri2 = "https://www.baidu.com";
std::string uri3 = "https://cn.bing.com";
{
auto client = cinatra::client_factory::instance().new_client();
client->async_get(uri, [](response_data data) {
print(data);
});
}
{
auto client = cinatra::client_factory::instance().new_client();
client->async_get(uri1, [](response_data data) {
print(data);
});
}
{
auto client = cinatra::client_factory::instance().new_client();
client->async_post(uri, "hello", [](response_data data) {
print(data);
});
}
#ifdef CINATRA_ENABLE_SSL
{
auto client = cinatra::client_factory::instance().new_client();
client->async_get(uri2, [](response_data data) {
print(data);
});
}
{
auto client = cinatra::client_factory::instance().new_client();
client->async_get(uri3, [](response_data data) {
print(data);
});
}
#endif
}
void test_download() {
std::string uri = "http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx";
{
auto client = cinatra::client_factory::instance().new_client();
client->download(uri, "test.jpg", [](response_data data) {
if (data.ec) {
std::cout << data.ec.message() << "\n";
return;
}
std::cout << "finished download\n";
});
}
{
auto client = cinatra::client_factory::instance().new_client();
client->download(uri, [](auto ec, auto data) {
if (ec) {
std::cout << ec.message() << "\n";
return;
}
if (data.empty()) {
std::cout << "finished all \n";
}
else {
std::cout << data.size() << "\n";
}
});
}
}
void test_upload() {
std::string uri = "http://cn.bing.com/";
auto client = cinatra::client_factory::instance().new_client();
client->upload(uri, "boost_1_72_0.7z", [](response_data data) {
if (data.ec) {
std::cout << data.ec.message() << "\n";
return;
}
std::cout << data.resp_body << "\n"; //finished upload
});
}
class qps {
public:
void increase() {
counter_.fetch_add(1, std::memory_order_release);
}
qps() : counter_(0) {
thd_ = std::thread([this] {
while (!stop_) {
std::cout << "qps: " << counter_.load(std::memory_order_acquire) << '\n';
std::this_thread::sleep_for(std::chrono::seconds(1));
//counter_.store(0, std::memory_order_release);
}
});
}
~qps() {
stop_ = true;
thd_.join();
}
private:
bool stop_ = false;
std::thread thd_;
std::atomic<uint32_t> counter_;
};
int main() {
test_sync_client();
//test_async_client();
//test_ssl_server();
//test_download();
http_server server(std::thread::hardware_concurrency());
bool r = server.listen("0.0.0.0", "8090");
if (!r) {
//LOG_INFO << "listen failed";
return -1;
}
server.set_http_handler<GET, POST>("/", [](request& req, response& res) mutable{
res.set_status_and_content(status_type::ok, "hello world");
//res.set_status_and_content(status_type::ok, std::move(str));
});
server.set_http_handler<GET>("/plaintext", [](request& req, response& res) {
//res.set_status_and_content<status_type::ok, res_content_type::string>("Hello, World!");
res.set_status_and_content(status_type::ok, "Hello, World!", req_content_type::string);
});
//server.set_http_handler<GET, POST>("/delay", [](request& req, response& res) {
// auto conn = req.get_conn<NonSSL>();
// //monitor an async operation to test response delay
// std::thread thd([conn, &res] {
// std::this_thread::sleep_for(std::chrono::seconds(3));
// if (!conn->has_close()) {
// res.set_status_and_content(status_type::ok, "hello world");
// conn->response_now();
// }
// else {
// std::cout << "has closed\n";
// }
// });
// thd.detach();
// res.set_delay(true);
//});
// person p{ 2 };
// server.set_http_handler<GET, POST>("/a", &person::foo, enable_cache{ false }, log_t{});
//// server.set_http_handler<GET, POST>("/b", &person::foo1, log_t{}, enable_cache{ false });
//
// server.set_http_handler<GET, POST>("/string", [](request& req, response& res) {
// res.render_string(std::to_string(std::time(nullptr)));
// },enable_cache{true});
//
// server.set_http_handler<GET, POST>("/404", [](request& req, response& res) {
// res.render_404();
// },enable_cache{false});
//
// server.set_http_handler<GET, POST>("/404_custom", [](request& req, response& res) {
// res.render_404("./404.html");
// },enable_cache{false});
//
// server.set_http_handler<GET, POST>("/login", [](request& req, response& res) {
// auto session = res.start_session();
// session->set_data("userid", std::string("1"));
// session->set_max_age(-1);
// res.set_status_and_content(status_type::ok, "login");
// },enable_cache{false});
//
// server.set_http_handler<GET, POST>("/islogin", [](request& req, response& res) {
// auto ptr = req.get_session();
// auto session = ptr.lock();
// if (session == nullptr || session->get_data<std::string>("userid") != "1") {
// res.set_status_and_content(status_type::ok, "没有登录", res_content_type::string);
// return;
// }
// res.set_status_and_content(status_type::ok, "已经登录", res_content_type::string);
// },enable_cache{false});
//
// server.set_http_handler<GET, POST>("/html", [](request& req, response& res) {
// res.set_attr("number",1024);
// res.set_attr("test_text","hello,world");
// res.set_attr("header_text","你好 cinatra");
// res.render_view("./www/test.html");
// });
//
// server.set_http_handler<GET, POST,OPTIONS>("/json", [](request& req, response& res) {
// nlohmann::json json;
// res.add_header("Access-Control-Allow-Origin","*");
// if(req.get_method()=="OPTIONS"){
// res.add_header("Access-Control-Allow-Headers","Authorization");
// res.render_string("");
// }else{
// json["abc"] = "abc";
// json["success"] = true;
// json["number"] = 100.005;
// json["name"] = "中文";
// json["time_stamp"] = std::time(nullptr);
// res.render_json(json);
// }
// });
//
// server.set_http_handler<GET,POST>("/redirect",[](request& req, response& res){
// res.redirect("http://www.baidu.com"); // res.redirect("/json");
// });
//
// server.set_http_handler<GET, POST>("/pathinfo/*", [](request& req, response& res) {
// auto s = req.get_query_value(0);
// res.render_string(std::string(s.data(), s.length()));
// });
//
// server.set_http_handler<GET, POST>("/restype", [](request& req, response& res) {
// auto type = req.get_query_value("type");
// auto res_type = cinatra::res_content_type::string;
// if (type == "html")
// {
// res_type = cinatra::res_content_type::html;
// }
// else if (type == "json") {
// res_type = cinatra::res_content_type::json;
// }
// else if (type == "string") {
// //do not anything;
// }
// res.set_status_and_content(status_type::ok, "<a href='http://www.baidu.com'>hello world 百度</a>", res_type);
// });
//
// server.set_http_handler<GET, POST>("/getzh", [](request& req, response& res) {
// auto zh = req.get_query_value("zh");
// res.render_string(std::string(zh.data(),zh.size()));
// });
//
// server.set_http_handler<GET, POST>("/gzip", [](request& req, response& res) {
// auto body = req.body();
// std::cout << body.data() << std::endl;
// res.set_status_and_content(status_type::ok, "hello world", res_content_type::none, content_encoding::gzip);
// });
//
//
// server.set_http_handler<GET, POST>("/test", [](request& req, response& res) {
// auto name = req.get_header_value("name");
// if (name.empty()) {
// res.render_string("no name");
// return;
// }
//
// auto id = req.get_query_value("id");
// if (id.empty()) {
// res.render_404();
// return;
// }
// res.render_string("hello world");
// });
//
// //aspect
// server.set_http_handler<GET, POST>("/aspect", [](request& req, response& res) {
// res.render_string("hello world");
// }, check{}, log_t{});
//
////web socket
//server.set_http_handler<GET, POST>("/ws", [](request& req, response& res) {
// assert(req.get_content_type() == content_type::websocket);
// req.on(ws_open, [](request& req){
// std::cout << "websocket start" << std::endl;
// });
// req.on(ws_message, [](request& req) {
// auto part_data = req.get_part_data();
// //echo
// std::string str = std::string(part_data.data(), part_data.length());
// req.get_conn()->send_ws_string(std::move(str));
// std::cout << part_data.data() << std::endl;
// });
// req.on(ws_error, [](request& req) {
// std::cout << "websocket pack error or network error" << std::endl;
// });
//});
// server.set_http_handler<GET, POST>("/vue_html", [](request& req, response& res) {
// res.render_raw_view("./www/index.html");
// });
//
// server.set_http_handler<GET, POST>("/vue_demo", [](request& req, response& res) {
// res.render_raw_view("./www/dist/index.html");
// });
//
// //http upload(multipart)
//server.set_http_handler<GET, POST>("/upload_multipart", [](request& req, response& res) {
// assert(req.get_content_type() == content_type::multipart);
// auto& files = req.get_upload_files();
// for (auto& file : files) {
// std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
// }
// res.render_string("multipart finished");
//});
//
// //http upload(octet-stream)
// server.set_http_handler<GET, POST>("/upload_octet_stream", [](request& req, response& res) {
// assert(req.get_content_type() == content_type::octet_stream);
// auto& files = req.get_upload_files();
// for (auto& file : files) {
// std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
// }
// res.render_string("octet-stream finished");
// });
//chunked download
//http://127.0.0.1:8080/assets/show.jpg
//cinatra will send you the file, if the file is big file(more than 5M) the file will be downloaded by chunked
server.run();
return 0;
}