mirror of
https://github.com/nghttp2/nghttp2.git
synced 2025-12-06 18:18:52 +08:00
asio: Add serve_mux class to route incoming requet by path
serve_mux is direct port of ServeMux from go
This commit is contained in:
@@ -62,10 +62,16 @@ int main(int argc, char *argv[]) {
|
||||
server.tls(argv[3], argv[4]);
|
||||
}
|
||||
|
||||
server.listen("*", port, [](const request &req, const response &res) {
|
||||
server.handle("/", [](const request &req, const response &res) {
|
||||
res.write_head(200, {{"foo", {"bar"}}});
|
||||
res.end("hello, world");
|
||||
});
|
||||
server.handle("/secret/", [](const request &req, const response &res) {
|
||||
res.write_head(200);
|
||||
res.end("under construction!");
|
||||
});
|
||||
server.listen("*", port);
|
||||
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << "exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
@@ -66,8 +66,7 @@ int main(int argc, char *argv[]) {
|
||||
server.tls(argv[4], argv[5]);
|
||||
}
|
||||
|
||||
server.listen("*", port,
|
||||
[&docroot](const request &req, const response &res) {
|
||||
server.handle("/", [&docroot](const request &req, const response &res) {
|
||||
auto path = percent_decode(req.uri().path);
|
||||
if (!check_path(path)) {
|
||||
res.write_head(404);
|
||||
@@ -99,6 +98,9 @@ int main(int argc, char *argv[]) {
|
||||
res.write_head(200, std::move(header));
|
||||
res.end(file_reader_from_fd(fd));
|
||||
});
|
||||
|
||||
server.listen("*", port);
|
||||
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << "exception: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user