-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrouter.cpp
More file actions
37 lines (34 loc) · 1.22 KB
/
router.cpp
File metadata and controls
37 lines (34 loc) · 1.22 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
//! @example router.cpp
#include "fastly/sdk.h"
#include <iostream>
#include <regex>
// NOTE: Please evaluate the SECURITY of this for your own purposes if you base
// your code on this. `std::regex` is vulnerable to ReDOS and is not suitable
// for user-provided regexes. Other vulnerabilities may be present depending on
// your C++ version.
// Routes
const std::regex HOME("^/?$");
const std::regex BOOK_GET("^/books/(\\d+)$");
const std::regex BOOK_LIST("^/books/?$");
int main() {
auto req{fastly::Request::from_client()};
fastly::http::Body body;
auto path{req.get_path()};
std::smatch match;
if (std::regex_match(path, match, HOME)) {
body << "Welcome Home!";
} else if (std::regex_match(path, match, BOOK_GET)) {
body << "So you want to get book with ID " << match[1]
<< "? I'll look for it later idk.";
} else if (std::regex_match(path, match, BOOK_LIST)) {
body << "There's a bunch of books. I don't feel like listing them right "
"now. That sounds like a pain.";
} else {
fastly::Response::from_status(404)
.with_body("Route not found")
.send_to_client();
return 0;
}
body << std::endl;
fastly::Response::from_body(std::move(body)).send_to_client();
}