]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 | #include <vector> | |
25 | #include <unordered_map> | |
26 | #include <tuple> | |
27 | #include <seastar/http/common.hh> | |
28 | #include <seastar/core/sstring.hh> | |
29 | #include <seastar/http/routes.hh> | |
30 | #include <seastar/http/function_handlers.hh> | |
31 | ||
32 | namespace seastar { | |
33 | ||
34 | namespace httpd { | |
35 | ||
36 | /** | |
37 | * A json_operation contain a method and a nickname. | |
38 | * operation are associated to a path, that can | |
39 | * have multiple methods | |
40 | */ | |
41 | struct json_operation { | |
42 | /** | |
43 | * default constructor | |
44 | */ | |
45 | json_operation() | |
46 | : method(GET) { | |
47 | } | |
48 | ||
49 | /** | |
50 | * Construct with assignment | |
51 | * @param method the http method type | |
52 | * @param nickname the http nickname | |
53 | */ | |
54 | json_operation(operation_type method, const sstring& nickname) | |
55 | : method(method), nickname(nickname) { | |
56 | } | |
57 | ||
58 | operation_type method; | |
59 | sstring nickname; | |
60 | ||
61 | }; | |
62 | ||
63 | /** | |
64 | * path description holds the path in the system. | |
65 | * It maps a nickname to an operation, which allows | |
66 | * defining the operation (path and method) by its | |
67 | * nickname. | |
68 | * | |
69 | * A path_description has a type, a base path and a list of | |
70 | * url components. | |
71 | * Each component can be a regular path parameter, a path parameter that | |
72 | * contains everything until the end of the path or a fixed string. | |
73 | * | |
74 | * the description are taken from the json swagger | |
75 | * definition file, during auto code generation in the | |
76 | * compilation. | |
77 | */ | |
78 | struct path_description { | |
79 | // | |
80 | enum class url_component_type { | |
81 | PARAM, // a normal path parameter (starts with / and end with / or end of path) | |
82 | PARAM_UNTIL_END_OF_PATH, // a parameter that contains all the path entil its end | |
83 | FIXED_STRING, // a fixed string inside the path, must be a full match and does not count | |
84 | // as a parameter | |
85 | }; | |
86 | ||
87 | // path_part is either a parameter or a fixed string | |
88 | struct path_part { | |
89 | sstring name; | |
90 | url_component_type type = url_component_type::PARAM; | |
91 | }; | |
92 | ||
93 | /** | |
94 | * default empty constructor | |
95 | */ | |
96 | path_description() = default; | |
97 | ||
98 | /** | |
99 | * constructor for path with parameters | |
100 | * The constructor is used by | |
101 | * @param path the url path | |
102 | * @param method the http method | |
103 | * @param nickname the nickname | |
f67539c2 TL |
104 | * @param path_parameters path parameters and url parts of the path |
105 | * @param mandatory_params the names of the mandatory query parameters | |
11fdf7f2 TL |
106 | */ |
107 | path_description(const sstring& path, operation_type method, | |
108 | const sstring& nickname, | |
109 | const std::vector<std::pair<sstring, bool>>& path_parameters, | |
110 | const std::vector<sstring>& mandatory_params); | |
111 | ||
112 | /** | |
113 | * constructor for path with parameters | |
114 | * The constructor is used by | |
115 | * @param path the url path | |
116 | * @param method the http method | |
117 | * @param nickname the method nickname | |
f67539c2 TL |
118 | * @param path_parameters path parameters and url parts of the path |
119 | * @param mandatory_params the names of the mandatory query parameters | |
11fdf7f2 TL |
120 | */ |
121 | path_description(const sstring& path, operation_type method, | |
122 | const sstring& nickname, | |
123 | const std::initializer_list<path_part>& path_parameters, | |
124 | const std::vector<sstring>& mandatory_params); | |
125 | ||
126 | /** | |
127 | * Add a parameter to the path definition | |
128 | * for example, if the url should match /file/{path} | |
129 | * The constructor would be followed by a call to | |
130 | * pushparam("path") | |
131 | * | |
132 | * @param param the name of the parameters, this name will | |
133 | * be used by the handler to identify the parameters. | |
134 | * A name can appear at most once in a description | |
135 | * @param all_path when set to true the parameter will assume to match | |
136 | * until the end of the url. | |
137 | * This is useful for situation like file path with | |
138 | * a rule like /file/{path} and a url /file/etc/hosts. | |
139 | * path should be equal to /ets/hosts and not only /etc | |
140 | * @return the current path description | |
141 | */ | |
142 | path_description* pushparam(const sstring& param, | |
143 | bool all_path = false) { | |
144 | params.push_back( { param, (all_path) ? url_component_type::PARAM_UNTIL_END_OF_PATH : url_component_type::PARAM}); | |
145 | return this; | |
146 | } | |
147 | ||
148 | /*! | |
149 | * \brief adds a fixed string as part of the path | |
150 | * This will allow to combine fixed string URL parts and path parameters. | |
151 | * | |
152 | * For example to map a path like: | |
153 | * /mypath/{param1}/morepath/{param2} | |
154 | * path_description p("/mypath", operation_type::GET); | |
155 | * p.pushparam("param1)->pushurl("morepath")->pushparam("param2"); | |
156 | */ | |
157 | path_description* push_static_path_part(const sstring& url) { | |
158 | params.push_back( { url, url_component_type::FIXED_STRING}); | |
159 | return this; | |
160 | } | |
161 | /** | |
162 | * adds a mandatory query parameter to the path | |
163 | * this parameter will be check before calling a handler | |
164 | * @param param the parameter to head | |
165 | * @return a pointer to the current path description | |
166 | */ | |
167 | path_description* pushmandatory_param(const sstring& param) { | |
168 | mandatory_queryparams.push_back(param); | |
169 | return this; | |
170 | } | |
171 | ||
172 | std::vector<path_part> params; | |
173 | sstring path; | |
174 | json_operation operations; | |
f67539c2 | 175 | mutable routes::rule_cookie _cookie; |
11fdf7f2 TL |
176 | |
177 | std::vector<sstring> mandatory_queryparams; | |
178 | ||
179 | void set(routes& _routes, handler_base* handler) const; | |
180 | ||
181 | void set(routes& _routes, const json_request_function& f) const; | |
182 | ||
183 | void set(routes& _routes, const future_json_function& f) const; | |
f67539c2 TL |
184 | |
185 | void unset(routes& _routes) const; | |
11fdf7f2 TL |
186 | }; |
187 | ||
188 | } | |
189 | ||
190 | } |