summaryrefslogtreecommitdiff
path: root/project2/cgi/cgiProgRouter.cpp
blob: 983e8cdf8f0389b19db9dc9ec1e9e084ce67ddd1 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <pch.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "cgiRequestContext.h"
#include "commonObjects.h"
#include "rowProcessor.h"
#include "presenter.h"
#include "safeMapFind.h"
#include "scriptStorage.h"
#include "rowSet.h"
#include "exceptions.h"
#include <factory.impl.h>

typedef std::map<std::string, std::string> VarMap;

class RoutingTable {
	public:
		class Route;
		typedef AdHoc::Factory<RoutingTable::Route, std::shared_ptr<const ScriptNode>> RouteFactory;

		void loadRoutesFromFile(const std::string & routeFile) {
			routeScriptPath = routeFile;
			if (routeFile.empty()) {
				routes.clear();
				routeScript.reset();
			}
			else {
				setRouteScript();
			}
		}

		void clearRoutes() {
			routes.clear();
		}

		void onBefore()
		{
			if (routeScript && !routeScript->isCurrent()) {
				setRouteScript();
			}
		}
		void setRouteScript()
		{
			routeScript = ScriptReader::resolveScript(CommonObjects::datasourceRoot, routeScriptPath, true);
			routeScript->loader.addLoadTarget(routeScript->root(), Storer::into<RouteFactory>(&routes));
			routes.clear();
			routeScript->load(NULL, true);
		}
		ScriptReaderPtr routeScript;
		std::string routeScriptPath;

		const std::string & present(const std::string & path, VarMap & vars) const {
			for (const auto & route : routes) {
				vars.clear();
				if (route->matches(path, vars)) {
					return route->present;
				}
			}
			throw ScriptNotFound("routed", path);
		}

		class RouteElem {
			public:
				virtual bool matches(const std::string &, VarMap & vars) const = 0;
		};
		typedef std::shared_ptr<RouteElem> RouteElemPtr;

		class RouteLiteral : public RouteElem {
			public:
				RouteLiteral(const std::string & v) : value(v) { }
				bool matches(const std::string & path, VarMap &) const {
					return value == path;
				}
				const std::string value;
		};

		class RouteVar : public RouteElem {
			public:
				RouteVar(const std::string & v) : variable(v.substr(1, v.length() - 2)) { }
				bool matches(const std::string & value, VarMap & vars) const {
					vars[variable] = value;
					return true;
				}
				const std::string variable;
		};

		class Route : public SourceObject {
			public:
				Route(ScriptNodePtr s) :
					SourceObject(s),
					present(s->value("present", NULL).as<std::string>()),
					path(s->value("path", NULL).as<std::string>())
				{
					boost::filesystem::path fspath = path;
					boost::filesystem::path::iterator p = fspath.begin();
					p++;
					while(p != fspath.end() && p->string() != ".") {
						switch (p->string().front()) {
							case '{':
								routeElems.push_back(std::make_shared<RouteVar>(p->string()));
								break;
							default:
								routeElems.push_back(std::make_shared<RouteLiteral>(p->string()));
								break;
						}
						p++;
					}
				}
				bool matches(const boost::filesystem::path & path, VarMap & vars) const {
					boost::filesystem::path::iterator p = path.begin();
					p++;
					for (RouteElems::const_iterator re = routeElems.begin(); re != routeElems.end() || p != path.end(); re++) {
						if (re == routeElems.end() || p == path.end() || !(*re)->matches(p->string(), vars)) {
							return false;
						}
						while (++p != path.end() && p->string() == ".") ;
					}
					return true;
				}
				typedef std::list<RouteElemPtr> RouteElems;
				RouteElems routeElems;
				const std::string present;
				const std::string path;
		};
		typedef std::shared_ptr<Route> RoutePtr;

		std::list<RoutePtr> routes;
};

NAMEDFACTORY("route", RoutingTable::Route, RoutingTable::RouteFactory);

class ProgRouter;
class ProgRouterFactory : public RouterFactory::For<ProgRouter>, public LifeCycle {
	public:
		void onBefore() override
		{
			routingTable.onBefore();
		}

		static RoutingTable routingTable;

		INITOPTIONS;
};

RoutingTable ProgRouterFactory::routingTable;

DECLARE_OPTIONS(ProgRouterFactory, "CGI Programmable Router options")
("cgi.progRouter.routes", Options::functions(
		boost::bind(&RoutingTable::loadRoutesFromFile, &routingTable, _1),
		boost::bind(&RoutingTable::clearRoutes, &routingTable)),
 "Script file defining web service routes")
END_OPTIONS(ProgRouterFactory);

SimpleMessageException(UriElementNotFound);

class ProgRouter : public Router {
	public:
		ProgRouter(const std::string & p) :
			path(p) {
		}
		std::string route() const {
			return ProgRouterFactory::routingTable.present(path, vars);
		}
		bool isDefault() const {
			return false;
		}
		VariableType routeParameter(const VariableType & vp) const {
			return AdHoc::safeMapLookup<UriElementNotFound>(vars, vp);
		}
		unsigned int parameterCount() const {
			return vars.size();
		}
		void present(const MultiRowSetPresenter * p) const {
			p->addNewArray("uriElem", true);
			boost::filesystem::path y(path);
			boost::filesystem::path::iterator pathPart = y.begin();
			while(++pathPart != y.end()) {
				p->addNamedValue("uriElem", pathPart->string());
			}
			p->finishArray(true);
			p->addNewArray("uriParam", true);
			for (const auto & v : vars) {
				p->addNewRow("uriParam");
				p->addNamedValue(v.first, v.second);
				p->finishRow();
			}
			p->finishArray(true);
		}

	private:
		const std::string path;
		mutable VarMap vars;
};

NAMEDPLUGIN("progRouter", ProgRouterFactory, RouterFactory);

class Routes : public RowSet {
	public:
		Routes(ScriptNodePtr s) :
			SourceObject(s),
			RowSet(s) { }
		class RouteRowState : public RowState {
			public:
				RouteRowState() {
					columns.insert(std::make_shared<Column>(0, "present"));
					columns.insert(std::make_shared<Column>(1, "path"));
					fields.resize(2);
				}
				const Columns & getColumns() const { return columns; }
				mutable Columns columns;
				friend class Routes;
		};
		void execute(const Glib::ustring & filter, const RowProcessorCallback & rp, ExecContext *) const
		{
			RouteRowState rs;
			for (const auto & r : ProgRouterFactory::routingTable.routes) {
				if (boost::algorithm::starts_with(r->path, filter)) {
					rs.fields[0] = VariableType(r->present);
					rs.fields[1] = VariableType(r->path);
					rp(&rs);
				}
			}
		}
};

NAMEDFACTORY("routes", Routes, RowSetFactory);
INSTANTIATEFACTORY(RoutingTable::Route, std::shared_ptr<const ScriptNode>);