#ifndef MQTT_CASSANDRA_BRIDGE_HTTP_SUPPORT_H #define MQTT_CASSANDRA_BRIDGE_HTTP_SUPPORT_H #include #include #include #include #include namespace trygvis { namespace http_support { using namespace std; using namespace nghttp2::asio_http2::server; class param { public: explicit param(const string &name) : name(name) { } param(const param &) = delete; virtual ~param() { } param operator=(param &) = delete; const string name; string value; }; namespace matcher { bool match(string &path, const string &expected_path) { cout << "match (string), path=" << path << ", expected=" << expected_path << endl; if (expected_path.length() == 0) { throw runtime_error("Invalid path: path.length() == 0"); } return path == expected_path; } bool match(string &path, const char *expected_path) { cout << "match (char*), path=" << path << ", expected=" << expected_path << endl; if (*expected_path == '\0') { throw runtime_error("Invalid path: path.length() == 0"); } return path == expected_path; } bool match(string &path, param &expected) { cout << "match (param), path=" << path << ", key=" << expected.name << endl; expected.value = path; return true; } bool matches(vector::const_iterator paths, vector::const_iterator end) { return paths == end; } template bool matches(vector::const_iterator paths, vector::const_iterator end, T ¶m, Args &... params) { // Nothing more to check, but we have parameters. if (paths == end) { return false; } auto path = *paths++; if (!match(path, param)) { return false; } return matches(paths, end, params...); }; } // matcher bool matches(vector paths) { cout << "matches(), paths=" << boost::algorithm::join(paths, "/") << endl; return paths.size() == 1 && paths[0] == ""; } template bool matches(vector paths, T ¶m, Args &... params) { cout << "matches(...), paths=" << boost::algorithm::join(paths, "/") << endl; // if (paths.size() == 1 && paths[0] == "") { // return true; // } return matcher::matches(paths.begin(), paths.end(), param, params...); } void method_not_allowed(const request &req, const response &res) { res.write_head(405); res.end("Method not allowed: " + req.method() + "\r\n"); } } } #endif