From 643d2aaf8d5617487c26ba4d02af65dfcd3e0d88 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 14 Jul 2015 01:04:41 +0200 Subject: o Adding web server to serve responses. --- http_support.h | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 http_support.h (limited to 'http_support.h') 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 +#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 -- cgit v1.2.3