32 #include <boost/algorithm/string/predicate.hpp> 33 #include <boost/filesystem/path.hpp> 34 #include <boost/thread/locks.hpp> 35 #include <boost/thread/shared_mutex.hpp> 39 using Parameters = std::map<std::string, std::string>;
77 static std::string
normalize(
const std::string& route) {
78 std::string s = route.substr(0, route.find(
"?"));
79 boost::filesystem::path p(s);
80 if (!p.is_absolute()) {
83 s = p.normalize().string();
84 s = s.substr(0, s.find_last_not_of(
"/. \n\t\r") + 1);
98 auto is_illegal = [](
char c) ->
bool {
99 return !(isalnum(c) || c ==
'_' || c ==
'-' || c ==
'.');
101 return std::find_if(s.begin(), s.end(), is_illegal) == s.end();
110 std::string
resolve(
const std::string& route)
const {
112 boost::shared_lock<boost::shared_mutex> read_lock(access_);
114 if (routes_.count(key)) {
119 boost::filesystem::path p(key);
120 while (!routes_.count(p.string())) {
131 boost::unique_lock<boost::shared_mutex> write_lock(access_);
132 backtrack_ = enabled;
140 std::vector<std::string> routes()
const {
141 std::vector<std::string> vs;
142 boost::shared_lock<boost::shared_mutex> read_lock(access_);
143 for (
const auto& kv : routes_) {
144 if (kv.first.empty()) {
147 vs.push_back(kv.first);
152 bool has(
const std::string& route)
const {
154 boost::shared_lock<boost::shared_mutex> read_lock(access_);
155 return routes_.count(key) != 0;
158 void add(
const std::string& route, T val) {
160 boost::unique_lock<boost::shared_mutex> write_lock(access_);
161 if (routes_.count(key)) {
162 throw std::runtime_error(
"route already exists");
167 void set(
const std::string& route, T val) {
169 boost::unique_lock<boost::shared_mutex> write_lock(access_);
179 std::pair<T, Parameters>
get(
const std::string& route)
const {
182 boost::shared_lock<boost::shared_mutex> read_lock(access_);
183 return std::make_pair(routes_.at(key), p);
186 void set_unsafe(
const std::string& key, T val) {
187 boost::unique_lock<boost::shared_mutex> write_lock(access_);
191 std::pair<T, Parameters> get_unsafe(
const std::string& key)
const {
193 boost::shared_lock<boost::shared_mutex> read_lock(access_);
194 return std::make_pair(routes_.at(key), p);
199 bool backtrack_ =
false;
202 std::map<std::string, T> routes_;
203 mutable boost::shared_mutex access_;
static bool is_identifier(const std::string &s)
Definition: route_muxer.hpp:97
Definition: registrar.hpp:38
static std::string normalize(const std::string &route)
Definition: route_muxer.hpp:77
Definition: route_muxer.hpp:63
void set_backtrack(bool enabled)
Definition: route_muxer.hpp:130
std::string resolve(const std::string &route) const
Definition: route_muxer.hpp:110
void set_default(T def)
Definition: route_muxer.hpp:138