forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_dispatcher.cpp
More file actions
288 lines (255 loc) · 7.31 KB
/
url_dispatcher.cpp
File metadata and controls
288 lines (255 loc) · 7.31 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/url_dispatcher.h>
#include <cppcms/application.h>
#include <cppcms/http_request.h>
#include <cppcms/http_context.h>
#include <cppcms/encoding.h>
#include <booster/regex.h>
#include <booster/shared_ptr.h>
namespace cppcms {
namespace /* anon */ {
struct option : public booster::noncopyable {
option(booster::regex const &expr) :
expr_(expr),
match_method_(0)
{
}
option(booster::regex const &expr,std::string const &method) :
expr_(expr),
match_method_(1),
mexpr_(method),
method_(method)
{
for(size_t i=0;i<method.size();i++) {
// common methods
char c=method[i];
if(!('A'<= c && c<='Z')) {
match_method_ =2;
break;
}
}
}
virtual ~option()
{
}
bool matches(std::string const &path,char const *method)
{
if(match_method_==1) {
if(!method || method_ != method)
return false;
}
else if(match_method_ == 2) {
if(!method || !booster::regex_match(method,mexpr_))
return false;
}
return booster::regex_match(path.c_str(),match_,expr_);
}
virtual bool dispatch(std::string const &url,char const *method,application *app) = 0;
protected:
booster::regex expr_;
booster::cmatch match_;
int match_method_;
booster::regex mexpr_;
std::string method_;
};
struct mounted : public option {
mounted(std::string expr,int select,application *app) :
option(expr),
app_(app),
select_(select)
{
}
virtual bool dispatch(std::string const &url,char const *method,application *)
{
if(matches(url,method)) {
app_->main(match_[select_]);
return true;
}
return false;
}
private:
application *app_;
int select_;
};
template<typename H>
struct base_handler : public option {
base_handler(std::string expr,H handle,int a=0,int b=0,int c=0,int d=0,int e=0,int f=0)
: option(expr),handle_(handle)
{
select_[0]=a;
select_[1]=b;
select_[2]=c;
select_[3]=d;
select_[4]=e;
select_[5]=f;
}
virtual bool dispatch(std::string const &url,char const *method,application *)
{
if(matches(url,method)) {
execute_handler(handle_);
return true;
}
return false;
}
private:
void execute_handler(url_dispatcher::handler const &h)
{
h();
}
void execute_handler(url_dispatcher::rhandler const &h)
{
h(match_);
}
void execute_handler(url_dispatcher::handler1 const &h)
{
h(match_[select_[0]]);
}
void execute_handler(url_dispatcher::handler2 const &h)
{
h(match_[select_[0]],match_[select_[1]]);
}
void execute_handler(url_dispatcher::handler3 const &h)
{
h(match_[select_[0]],match_[select_[1]],match_[select_[2]]);
}
void execute_handler(url_dispatcher::handler4 const &h)
{
h(match_[select_[0]],match_[select_[1]],match_[select_[2]],match_[select_[3]]);
}
void execute_handler(url_dispatcher::handler5 const &h)
{
h(match_[select_[0]],match_[select_[1]],match_[select_[2]],match_[select_[3]],match_[select_[4]]);
}
void execute_handler(url_dispatcher::handler6 const &h)
{
h(match_[select_[0]],match_[select_[1]],match_[select_[2]],match_[select_[3]],match_[select_[4]],match_[select_[5]]);
}
int select_[6];
H handle_;
};
struct generic_option : public option {
generic_option(booster::regex const &r,url_dispatcher::generic_handler const &h) :
option(r),
handle_(h)
{
}
generic_option(std::string method,booster::regex const &r,url_dispatcher::generic_handler const &h) :
option(r,method),
handle_(h)
{
}
virtual bool dispatch(std::string const &url,char const *method,application *app)
{
if(!app)
return false;
if(matches(url,method)) {
return handle_(*app,match_);
}
return false;
}
url_dispatcher::generic_handler handle_;
};
template<typename H>
booster::shared_ptr<option> make_handler(std::string expr,H const &handler,int a=0,int b=0,int c=0,int d=0,int e=0,int f=0)
{
return booster::shared_ptr<option>(new base_handler<H>(expr,handler,a,b,c,d,e,f));
}
} // anonynoys
struct url_dispatcher::_data {
_data(application *a) : app(a) {}
application *app;
std::vector<booster::shared_ptr<option> > options;
};
url_dispatcher::url_dispatcher() :
d(new url_dispatcher::_data(0))
{
}
url_dispatcher::url_dispatcher(application *app) :
d(new url_dispatcher::_data(app))
{
}
url_dispatcher::~url_dispatcher()
{
}
bool url_dispatcher::dispatch(std::string url)
{
unsigned i;
std::string method;
char const *cmethod = 0;
application *app = d->app;
if(app && app->has_context()) {
method = app->request().request_method();
cmethod = method.c_str();
}
else {
app = 0;
}
for(i=0;i<d->options.size();i++) {
if(d->options[i]->dispatch(url,cmethod,app))
return true;
}
return false;
}
void url_dispatcher::mount(std::string const &match,application &app,int select)
{
d->options.push_back(booster::shared_ptr<option>(new mounted(match,select,&app)));
}
void url_dispatcher::assign(std::string const &expr,handler h)
{
d->options.push_back(make_handler(expr,h));
}
void url_dispatcher::assign_generic(std::string const &expr,rhandler h)
{
d->options.push_back(make_handler(expr,h));
}
void url_dispatcher::map_generic(std::string const &method,booster::regex const &re,generic_handler const &h)
{
booster::shared_ptr<option> opt(new generic_option(method,re,h));
d->options.push_back(opt);
}
void url_dispatcher::map_generic(booster::regex const &re,generic_handler const &h)
{
booster::shared_ptr<option> opt(new generic_option(re,h));
d->options.push_back(opt);
}
void url_dispatcher::assign(std::string const &expr,handler1 h,int p1)
{
d->options.push_back(make_handler(expr,h,p1));
}
void url_dispatcher::assign(std::string const &expr,handler2 h,int p1,int p2)
{
d->options.push_back(make_handler(expr,h,p1,p2));
}
void url_dispatcher::assign(std::string const &expr,handler3 h,int p1,int p2,int p3)
{
d->options.push_back(make_handler(expr,h,p1,p2,p3));
}
void url_dispatcher::assign(std::string const &expr,handler4 h,int p1,int p2,int p3,int p4)
{
d->options.push_back(make_handler(expr,h,p1,p2,p3,p4));
}
void url_dispatcher::assign(std::string const &expr,handler5 h,int p1,int p2,int p3,int p4,int p5)
{
d->options.push_back(make_handler(expr,h,p1,p2,p3,p4,p5));
}
void url_dispatcher::assign(std::string const &expr,handler6 h,int p1,int p2,int p3,int p4,int p5,int p6)
{
d->options.push_back(make_handler(expr,h,p1,p2,p3,p4,p5,p6));
}
bool url_dispatcher::validate_encoding(application &app,char const *begin,char const *end)
{
size_t unused;
return encoding::valid(app.context().locale(),begin,end,unused);
}
void url_dispatcher::setup_stream(application &app,std::istream &s)
{
s.imbue(app.context().locale());
}
} // namespace cppcms