forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server_test.cpp
141 lines (119 loc) · 3.78 KB
/
http_server_test.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
/*
* sample http server
* more detail see examples/httpd
*
*/
#include "HttpServer.h"
#include "hthread.h" // import hv_gettid
#include "hasync.h" // import hv::async
using namespace hv;
/*
* #define TEST_HTTPS 1
*
* @build ./configure --with-openssl && make clean && make
*
* @server bin/http_server_test 8080
*
* @client curl -v http://127.0.0.1:8080/ping
* curl -v https://127.0.0.1:8443/ping --insecure
* bin/curl -v http://127.0.0.1:8080/ping
* bin/curl -v https://127.0.0.1:8443/ping
*
*/
#define TEST_HTTPS 0
int main(int argc, char** argv) {
HV_MEMCHECK;
int port = 0;
if (argc > 1) {
port = atoi(argv[1]);
}
if (port == 0) port = 8080;
HttpService router;
/* Static file service */
// curl -v http://ip:port/
router.Static("/", "./html");
/* Forward proxy service */
router.EnableForwardProxy();
// curl -v http://httpbin.org/get --proxy http://127.0.0.1:8080
router.AddTrustProxy("*httpbin.org");
/* Reverse proxy service */
// curl -v http://ip:port/httpbin/get
router.Proxy("/httpbin/", "http://httpbin.org/");
/* API handlers */
// curl -v http://ip:port/ping
router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
return resp->String("pong");
});
// curl -v http://ip:port/data
router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
static char data[] = "0123456789";
return resp->Data(data, 10 /*, false */);
});
// curl -v http://ip:port/paths
router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
return resp->Json(router.Paths());
});
// curl -v http://ip:port/get?env=1
router.GET("/get", [](const HttpContextPtr& ctx) {
hv::Json resp;
resp["origin"] = ctx->ip();
resp["url"] = ctx->url();
resp["args"] = ctx->params();
resp["headers"] = ctx->headers();
return ctx->send(resp.dump(2));
});
// curl -v http://ip:port/echo -d "hello,world!"
router.POST("/echo", [](const HttpContextPtr& ctx) {
return ctx->send(ctx->body(), ctx->type());
});
// curl -v http://ip:port/user/123
router.GET("/user/{id}", [](const HttpContextPtr& ctx) {
hv::Json resp;
resp["id"] = ctx->param("id");
return ctx->send(resp.dump(2));
});
// curl -v http://ip:port/async
router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
writer->Begin();
writer->WriteHeader("X-Response-tid", hv_gettid());
writer->WriteHeader("Content-Type", "text/plain");
writer->WriteBody("This is an async response.\n");
writer->End();
});
// middleware
router.AllowCORS();
router.Use([](HttpRequest* req, HttpResponse* resp) {
resp->SetHeader("X-Request-tid", hv::to_string(hv_gettid()));
return HTTP_STATUS_NEXT;
});
HttpServer server;
server.service = &router;
server.port = port;
#if TEST_HTTPS
server.https_port = 8443;
hssl_ctx_opt_t param;
memset(¶m, 0, sizeof(param));
param.crt_file = "cert/server.crt";
param.key_file = "cert/server.key";
param.endpoint = HSSL_SERVER;
if (server.newSslCtx(¶m) != 0) {
fprintf(stderr, "new SSL_CTX failed!\n");
return -20;
}
#endif
// uncomment to test multi-processes
// server.setProcessNum(4);
// uncomment to test multi-threads
// server.setThreadNum(4);
// 设置使用单线程
// server.setProcessNum(0);
// server.setThreadNum(0);
// auto single_loop = std::make_shared<EventLoop>();
// server.start(nullptr, single_loop);
// 默认
server.start();
// press Enter to stop
while (getchar() != '\n');
hv::async::cleanup();
return 0;
}