Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
routes.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright 2015 Cloudius Systems
20 */
21
22#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <boost/program_options/variables_map.hpp>
26#include <unordered_map>
27#endif
28
29#include <seastar/http/matchrules.hh>
30#include <seastar/http/handlers.hh>
31#include <seastar/http/common.hh>
32#include <seastar/http/reply.hh>
33#include <seastar/util/modules.hh>
34
35namespace seastar {
36
37namespace httpd {
38
39SEASTAR_MODULE_EXPORT_BEGIN
43class url {
44public:
48 url(url&&) = default;
49
54 url(const sstring& path)
55 : _path(path) {
56 }
57
63 url& remainder(const sstring& param) {
64 this->_param = param;
65 return *this;
66 }
67
68 sstring _path;
69 sstring _param;
70};
71
72struct path_description;
73
81class routes {
82public:
87
98 routes& put(operation_type type, const sstring& url, handler_base* handler);
99
105 handler_base* drop(operation_type type, const sstring& url);
106
118 routes& add(match_rule* rule, operation_type type = GET) {
119 _rules[type][_rover++] = rule;
120 return *this;
121 }
122
133 routes& add(operation_type type, const url& url, handler_base* handler);
134
143
154 future<std::unique_ptr<http::reply> > handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep);
155
161 handler_base* get_exact_match(operation_type type, const sstring& url) const {
162 auto i = _map[type].find(url);
163 return (i == _map[type].end()) ? nullptr : i->second;
164 }
165
173 handler_base* get_handler(operation_type type, const sstring& url,
174 parameters& params);
175
176private:
183 sstring normalize_url(const sstring& url);
184
185 std::unordered_map<sstring, handler_base*> _map[NUM_OPERATION];
186public:
187 using rule_cookie = uint64_t;
188private:
189 rule_cookie _rover = 0;
190 std::map<rule_cookie, match_rule*> _rules[NUM_OPERATION];
191 //default Handler -- for any HTTP Method and Path (/*)
192 handler_base* _default_handler = nullptr;
193public:
194 using exception_handler_fun = std::function<std::unique_ptr<http::reply>(std::exception_ptr eptr)>;
195 using exception_handler_id = size_t;
196private:
197 std::map<exception_handler_id, exception_handler_fun> _exceptions;
198 exception_handler_id _exception_id = 0;
199 // for optimization reason, the lambda function
200 // that calls the exception_reply of the current object
201 // is stored
202 exception_handler_fun _general_handler;
203public:
209 exception_handler_id register_exeption_handler(exception_handler_fun fun) {
210 auto current = _exception_id++;
211 _exceptions[current] = fun;
212 return current;
213 }
214
215 void remove_exception_handler(exception_handler_id id) {
216 _exceptions.erase(id);
217 }
218
219 std::unique_ptr<http::reply> exception_reply(std::exception_ptr eptr);
220
221 routes();
222
229 void add_alias(const path_description& old_path, const path_description& new_path);
230
239 rule_cookie add_cookie(match_rule* rule, operation_type type) {
240 auto pos = _rover++;
241 _rules[type][pos] = rule;
242 return pos;
243 }
244
252 match_rule* del_cookie(rule_cookie cookie, operation_type type);
253};
254
260 routes& _routes;
261 const sstring _url;
262 operation_type _op;
263
264public:
272 handler_registration(routes& rs, handler_base& h, const sstring& url, operation_type op = GET);
273
278};
279
285 routes& _routes;
286 operation_type _op;
287 routes::rule_cookie _cookie;
288
289public:
296 rule_registration(routes& rs, match_rule& rule, operation_type op = GET);
297
302};
303
304SEASTAR_MODULE_EXPORT_END
305}
306
307}
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Definition: handlers.hh:42
Definition: routes.hh:259
handler_registration(routes &rs, handler_base &h, const sstring &url, operation_type op=GET)
Definition: matchrules.hh:42
Definition: common.hh:48
Definition: routes.hh:81
routes & add(operation_type type, const url &url, handler_base *handler)
void add_alias(const path_description &old_path, const path_description &new_path)
add an alias to an already registered path. After registering a handler to a path,...
exception_handler_id register_exeption_handler(exception_handler_fun fun)
Definition: routes.hh:209
handler_base * get_exact_match(operation_type type, const sstring &url) const
Definition: routes.hh:161
future< std::unique_ptr< http::reply > > handle(const sstring &path, std::unique_ptr< http::request > req, std::unique_ptr< http::reply > rep)
rule_cookie add_cookie(match_rule *rule, operation_type type)
Definition: routes.hh:239
routes & put(operation_type type, const sstring &url, handler_base *handler)
match_rule * del_cookie(rule_cookie cookie, operation_type type)
handler_base * get_handler(operation_type type, const sstring &url, parameters &params)
routes & add_default_handler(handler_base *handler)
routes & add(match_rule *rule, operation_type type=GET)
Definition: routes.hh:118
handler_base * drop(operation_type type, const sstring &url)
Definition: routes.hh:284
rule_registration(routes &rs, match_rule &rule, operation_type op=GET)
Definition: routes.hh:43
url(url &&)=default
url(const sstring &path)
Definition: routes.hh:54
url & remainder(const sstring &param)
Definition: routes.hh:63
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: json_path.hh:82