diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-07-14 01:04:41 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-07-14 01:04:41 +0200 |
commit | 643d2aaf8d5617487c26ba4d02af65dfcd3e0d88 (patch) | |
tree | 7e6be55672de7c45ae85f86863372dd94fc7605a /http_support.h | |
parent | f2ff3cfcdc503be98b7d4b9f24f313c5732a0c17 (diff) | |
download | mqtt-cassandra-bridge-643d2aaf8d5617487c26ba4d02af65dfcd3e0d88.tar.gz mqtt-cassandra-bridge-643d2aaf8d5617487c26ba4d02af65dfcd3e0d88.tar.bz2 mqtt-cassandra-bridge-643d2aaf8d5617487c26ba4d02af65dfcd3e0d88.tar.xz mqtt-cassandra-bridge-643d2aaf8d5617487c26ba4d02af65dfcd3e0d88.zip |
o Adding web server to serve responses.
Diffstat (limited to 'http_support.h')
-rw-r--r-- | http_support.h | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/http_support.h b/http_support.h new file mode 100644 index 0000000..f3896f8 --- /dev/null +++ b/http_support.h @@ -0,0 +1,99 @@ +#ifndef MQTT_CASSANDRA_BRIDGE_HTTP_SUPPORT_H +#define MQTT_CASSANDRA_BRIDGE_HTTP_SUPPORT_H + +#include <nghttp2/asio_http2_server.h> +#include <iostream> +#include <string> +#include <boost/algorithm/string.hpp> +#include <boost/algorithm/string/split.hpp> + +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<string>::const_iterator paths, vector<string>::const_iterator end) { + return paths == end; +} + +template<typename T, typename... Args> +bool matches(vector<string>::const_iterator paths, vector<string>::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<string> paths) { + cout << "matches(), paths=" << boost::algorithm::join(paths, "/") << endl; + return paths.size() == 1 && paths[0] == ""; +} + +template<typename T, typename... Args> +bool matches(vector<string> 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 |