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:
Tatsuhiro Tsujikawa
2015-03-05 01:52:12 +09:00
parent 8accf3898a
commit e4ce595ebb
13 changed files with 300 additions and 30 deletions

View File

@@ -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";
}

View File

@@ -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";
}