Debian Codegraph @

third-party/libhv-v1.3.3/examples/httpd/router.cpp

raw
1 #include "router.h"
2
3 #include "handler.h"
4 #include "hthread.h" // import hv_gettid
5 #include "hasync.h" // import hv::async
6 #include "requests.h" // import requests::async
7
8 void Router::Register(hv::HttpService& router) {
9 /* handler chain */
10 // preprocessor -> middleware -> processor -> postprocessor
11 // processor: pathHandlers -> staticHandler -> errorHandler
12 router.preprocessor = Handler::preprocessor;
13 router.postprocessor = Handler::postprocessor;
14 // router.errorHandler = Handler::errorHandler;
15 // router.largeFileHandler = Handler::sendLargeFile;
16
17 // middleware
18 // router.Use(Handler::Authorization);
19
20 // curl -v http://ip:port/ping
21 router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
22 return resp->String("pong");
23 });
24
25 // curl -v http://ip:port/data
26 router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
27 static char data[] = "0123456789";
28 return resp->Data(data, 10 /*, false */);
29 });
30
31 // curl -v http://ip:port/html/index.html
32 router.GET("/html/index.html", [](HttpRequest* req, HttpResponse* resp) {
33 return resp->File("html/index.html");
34 });
35
36 // curl -v http://ip:port/paths
37 router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
38 return resp->Json(router.Paths());
39 });
40
41 // curl -v http://ip:port/service
42 router.GET("/service", [](const HttpContextPtr& ctx) {
43 ctx->setContentType("application/json");
44 ctx->set("base_url", ctx->service->base_url);
45 ctx->set("document_root", ctx->service->document_root);
46 ctx->set("home_page", ctx->service->home_page);
47 ctx->set("error_page", ctx->service->error_page);
48 ctx->set("index_of", ctx->service->index_of);
49 return 200;
50 });
51
52 // curl -v http://ip:port/get?env=1
53 router.GET("/get", [](const HttpContextPtr& ctx) {
54 hv::Json resp;
55 resp["origin"] = ctx->ip();
56 resp["url"] = ctx->url();
57 resp["args"] = ctx->params();
58 resp["headers"] = ctx->headers();
59 return ctx->send(resp.dump(2));
60 });
61
62 // curl -v http://ip:port/echo -d "hello,world!"
63 router.POST("/echo", [](const HttpContextPtr& ctx) {
64 return ctx->send(ctx->body(), ctx->type());
65 });
66
67 // wildcard *
68 // curl -v http://ip:port/wildcard/any
69 router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp) {
70 std::string str = req->path + " match /wildcard*";
71 return resp->String(str);
72 });
73
74 // curl -v http://ip:port/async
75 router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
76 writer->Begin();
77 writer->WriteHeader("X-Response-tid", hv_gettid());
78 writer->WriteHeader("Content-Type", "text/plain");
79 writer->WriteBody("This is an async response.\n");
80 writer->End();
81 });
82
83 // curl -v http://ip:port/www.*
84 // curl -v http://ip:port/www.example.com
85 router.GET("/www.*", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
86 auto req2 = std::make_shared<HttpRequest>();
87 req2->url = req->path.substr(1);
88 requests::async(req2, [writer](const HttpResponsePtr& resp2){
89 writer->Begin();
90 if (resp2 == NULL) {
91 writer->WriteStatus(HTTP_STATUS_NOT_FOUND);
92 writer->WriteHeader("Content-Type", "text/html");
93 writer->WriteBody("<center><h1>404 Not Found</h1></center>");
94 } else {
95 writer->WriteResponse(resp2.get());
96 }
97 writer->End();
98 });
99 });
100
101 // curl -v http://ip:port/sleep?t=1000
102 router.GET("/sleep", Handler::sleep);
103
104 // curl -v http://ip:port/setTimeout?t=1000
105 router.GET("/setTimeout", Handler::setTimeout);
106
107 // curl -v http://ip:port/query?page_no=1\&page_size=10
108 router.GET("/query", Handler::query);
109
110 // Content-Type: application/x-www-form-urlencoded
111 // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
112 router.POST("/kv", Handler::kv);
113
114 // Content-Type: application/json
115 // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
116 router.POST("/json", Handler::json);
117
118 // Content-Type: multipart/form-data
119 // bin/curl -v http://ip:port/form -F 'user=admin' -F 'pswd=123456'
120 router.POST("/form", Handler::form);
121
122 // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
123 // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
124 // bin/curl -v http://ip:port/test -F 'bool=1' -F 'int=123' -F 'float=3.14' -F 'string=hello'
125 router.POST("/test", Handler::test);
126
127 // Content-Type: application/grpc
128 // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
129 router.POST("/grpc", Handler::grpc);
130
131 // RESTful API: /group/:group_name/user/:user_id
132 // curl -v -X DELETE http://ip:port/group/test/user/123
133 router.Delete("/group/:group_name/user/:user_id", Handler::restful);
134 // router.Delete("/group/{group_name}/user/{user_id}", Handler::restful);
135
136 // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
137 router.POST("/login", Handler::login);
138
139 // curl -v http://ip:port/upload?filename=LICENSE -d '@LICENSE'
140 // curl -v http://ip:port/upload -F 'file=@LICENSE'
141 router.POST("/upload", Handler::upload);
142 // curl -v http://ip:port/upload/README.md -d '@README.md'
143 router.POST("/upload/{filename}", Handler::recvLargeFile);
144
145 // SSE: Server Send Events
146 // @test html/EventSource.html EventSource.onmessage
147 router.GET("/sse", Handler::sse);
148 }