]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
9f95a23c | 2 | // vim: ts=8 sw=2 smarttab ft=cpp |
11fdf7f2 | 3 | |
7c673cae FG |
4 | /* |
5 | * Ceph - scalable distributed file system | |
6 | * | |
7 | * Copyright (C) 2015 Yehuda Sadeh <yehuda@redhat.com> | |
8 | * Copyright (C) 2015 Robin H. Johnson <robin.johnson@dreamhost.com> | |
9 | * | |
10 | * This is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License version 2.1, as published by the Free Software | |
13 | * Foundation. See file COPYING. | |
14 | * | |
15 | */ | |
11fdf7f2 | 16 | |
7c673cae | 17 | #include "common/debug.h" |
20effc67 | 18 | |
7c673cae | 19 | #include "common/ceph_json.h" |
20effc67 | 20 | #include "common/Formatter.h" |
7c673cae FG |
21 | |
22 | #include "acconfig.h" | |
23 | ||
24 | #include <errno.h> | |
25 | #include <string> | |
26 | #include <list> | |
27 | #include "include/types.h" | |
28 | #include "rgw_website.h" | |
20effc67 TL |
29 | #include "rgw_common.h" |
30 | #include "rgw_xml.h" | |
7c673cae | 31 | |
20effc67 | 32 | using namespace std; |
7c673cae FG |
33 | |
34 | bool RGWBWRoutingRuleCondition::check_key_condition(const string& key) { | |
35 | return (key.size() >= key_prefix_equals.size() && | |
36 | key.compare(0, key_prefix_equals.size(), key_prefix_equals) == 0); | |
37 | } | |
38 | ||
39 | ||
40 | void RGWBWRoutingRule::apply_rule(const string& default_protocol, const string& default_hostname, | |
41 | const string& key, string *new_url, int *redirect_code) | |
42 | { | |
43 | RGWRedirectInfo& redirect = redirect_info.redirect; | |
44 | ||
45 | string protocol = (!redirect.protocol.empty() ? redirect.protocol : default_protocol); | |
46 | string hostname = (!redirect.hostname.empty() ? redirect.hostname : default_hostname); | |
47 | ||
48 | *new_url = protocol + "://" + hostname + "/"; | |
49 | ||
50 | if (!redirect_info.replace_key_prefix_with.empty()) { | |
51 | *new_url += redirect_info.replace_key_prefix_with; | |
522d829b TL |
52 | if (key.size() > condition.key_prefix_equals.size()) { |
53 | *new_url += key.substr(condition.key_prefix_equals.size()); | |
54 | } | |
7c673cae FG |
55 | } else if (!redirect_info.replace_key_with.empty()) { |
56 | *new_url += redirect_info.replace_key_with; | |
57 | } else { | |
58 | *new_url += key; | |
59 | } | |
60 | ||
61 | if(redirect.http_redirect_code > 0) | |
62 | *redirect_code = redirect.http_redirect_code; | |
63 | } | |
64 | ||
65 | bool RGWBWRoutingRules::check_key_and_error_code_condition(const string &key, int error_code, RGWBWRoutingRule **rule) | |
66 | { | |
67 | for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) { | |
68 | if (iter->check_key_condition(key) && iter->check_error_code_condition(error_code)) { | |
69 | *rule = &(*iter); | |
70 | return true; | |
71 | } | |
72 | } | |
73 | return false; | |
74 | } | |
75 | ||
76 | bool RGWBWRoutingRules::check_key_condition(const string& key, RGWBWRoutingRule **rule) | |
77 | { | |
78 | for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) { | |
79 | if (iter->check_key_condition(key)) { | |
80 | *rule = &(*iter); | |
81 | return true; | |
82 | } | |
83 | } | |
84 | return false; | |
85 | } | |
86 | ||
87 | bool RGWBWRoutingRules::check_error_code_condition(const int http_error_code, RGWBWRoutingRule **rule) | |
88 | { | |
89 | for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) { | |
90 | if (iter->check_error_code_condition(http_error_code)) { | |
91 | *rule = &(*iter); | |
92 | return true; | |
93 | } | |
94 | } | |
95 | return false; | |
96 | } | |
97 | ||
98 | bool RGWBucketWebsiteConf::should_redirect(const string& key, const int http_error_code, RGWBWRoutingRule *redirect) | |
99 | { | |
100 | RGWBWRoutingRule *rule; | |
101 | if(!redirect_all.hostname.empty()) { | |
102 | RGWBWRoutingRule redirect_all_rule; | |
103 | redirect_all_rule.redirect_info.redirect = redirect_all; | |
104 | redirect_all.http_redirect_code = 301; | |
105 | *redirect = redirect_all_rule; | |
106 | return true; | |
107 | } else if (!routing_rules.check_key_and_error_code_condition(key, http_error_code, &rule)) { | |
108 | return false; | |
109 | } | |
110 | ||
111 | *redirect = *rule; | |
112 | ||
113 | return true; | |
114 | } | |
115 | ||
eafe8130 | 116 | bool RGWBucketWebsiteConf::get_effective_key(const string& key, string *effective_key, bool is_file) const |
7c673cae | 117 | { |
eafe8130 TL |
118 | if (index_doc_suffix.empty()) { |
119 | return false; | |
120 | } | |
7c673cae FG |
121 | |
122 | if (key.empty()) { | |
123 | *effective_key = index_doc_suffix; | |
124 | } else if (key[key.size() - 1] == '/') { | |
125 | *effective_key = key + index_doc_suffix; | |
224ce89b WB |
126 | } else if (! is_file) { |
127 | *effective_key = key + "/" + index_doc_suffix; | |
7c673cae FG |
128 | } else { |
129 | *effective_key = key; | |
130 | } | |
eafe8130 TL |
131 | |
132 | return true; | |
7c673cae | 133 | } |
20effc67 TL |
134 | |
135 | void RGWRedirectInfo::dump(Formatter *f) const | |
136 | { | |
137 | encode_json("protocol", protocol, f); | |
138 | encode_json("hostname", hostname, f); | |
139 | encode_json("http_redirect_code", (int)http_redirect_code, f); | |
140 | } | |
141 | ||
142 | void RGWRedirectInfo::decode_json(JSONObj *obj) { | |
143 | JSONDecoder::decode_json("protocol", protocol, obj); | |
144 | JSONDecoder::decode_json("hostname", hostname, obj); | |
145 | int code; | |
146 | JSONDecoder::decode_json("http_redirect_code", code, obj); | |
147 | http_redirect_code = code; | |
148 | } | |
149 | ||
150 | void RGWBWRedirectInfo::dump(Formatter *f) const | |
151 | { | |
152 | encode_json("redirect", redirect, f); | |
153 | encode_json("replace_key_prefix_with", replace_key_prefix_with, f); | |
154 | encode_json("replace_key_with", replace_key_with, f); | |
155 | } | |
156 | ||
157 | void RGWBWRedirectInfo::decode_json(JSONObj *obj) { | |
158 | JSONDecoder::decode_json("redirect", redirect, obj); | |
159 | JSONDecoder::decode_json("replace_key_prefix_with", replace_key_prefix_with, obj); | |
160 | JSONDecoder::decode_json("replace_key_with", replace_key_with, obj); | |
161 | } | |
162 | ||
163 | void RGWBWRoutingRuleCondition::dump(Formatter *f) const | |
164 | { | |
165 | encode_json("key_prefix_equals", key_prefix_equals, f); | |
166 | encode_json("http_error_code_returned_equals", (int)http_error_code_returned_equals, f); | |
167 | } | |
168 | ||
169 | void RGWBWRoutingRuleCondition::decode_json(JSONObj *obj) { | |
170 | JSONDecoder::decode_json("key_prefix_equals", key_prefix_equals, obj); | |
171 | int code; | |
172 | JSONDecoder::decode_json("http_error_code_returned_equals", code, obj); | |
173 | http_error_code_returned_equals = code; | |
174 | } | |
175 | ||
176 | void RGWBWRoutingRule::dump(Formatter *f) const | |
177 | { | |
178 | encode_json("condition", condition, f); | |
179 | encode_json("redirect_info", redirect_info, f); | |
180 | } | |
181 | ||
182 | void RGWBWRoutingRule::decode_json(JSONObj *obj) { | |
183 | JSONDecoder::decode_json("condition", condition, obj); | |
184 | JSONDecoder::decode_json("redirect_info", redirect_info, obj); | |
185 | } | |
186 | ||
187 | void RGWBWRoutingRules::dump(Formatter *f) const | |
188 | { | |
189 | encode_json("rules", rules, f); | |
190 | } | |
191 | ||
192 | void RGWBWRoutingRules::decode_json(JSONObj *obj) { | |
193 | JSONDecoder::decode_json("rules", rules, obj); | |
194 | } | |
195 | ||
196 | void RGWBucketWebsiteConf::dump(Formatter *f) const | |
197 | { | |
198 | if (!redirect_all.hostname.empty()) { | |
199 | encode_json("redirect_all", redirect_all, f); | |
200 | } else { | |
201 | encode_json("index_doc_suffix", index_doc_suffix, f); | |
202 | encode_json("error_doc", error_doc, f); | |
203 | encode_json("routing_rules", routing_rules, f); | |
204 | } | |
205 | } | |
206 | ||
207 | void RGWBucketWebsiteConf::decode_json(JSONObj *obj) { | |
208 | JSONDecoder::decode_json("redirect_all", redirect_all, obj); | |
209 | JSONDecoder::decode_json("index_doc_suffix", index_doc_suffix, obj); | |
210 | JSONDecoder::decode_json("error_doc", error_doc, obj); | |
211 | JSONDecoder::decode_json("routing_rules", routing_rules, obj); | |
212 | } | |
213 | ||
214 | void RGWBWRedirectInfo::dump_xml(Formatter *f) const | |
215 | { | |
216 | if (!redirect.protocol.empty()) { | |
217 | encode_xml("Protocol", redirect.protocol, f); | |
218 | } | |
219 | if (!redirect.hostname.empty()) { | |
220 | encode_xml("HostName", redirect.hostname, f); | |
221 | } | |
222 | if (redirect.http_redirect_code > 0) { | |
223 | encode_xml("HttpRedirectCode", (int)redirect.http_redirect_code, f); | |
224 | } | |
225 | if (!replace_key_prefix_with.empty()) { | |
226 | encode_xml("ReplaceKeyPrefixWith", replace_key_prefix_with, f); | |
227 | } | |
228 | if (!replace_key_with.empty()) { | |
229 | encode_xml("ReplaceKeyWith", replace_key_with, f); | |
230 | } | |
231 | } | |
232 | ||
233 | #define WEBSITE_HTTP_REDIRECT_CODE_MIN 300 | |
234 | #define WEBSITE_HTTP_REDIRECT_CODE_MAX 400 | |
235 | void RGWBWRedirectInfo::decode_xml(XMLObj *obj) { | |
236 | RGWXMLDecoder::decode_xml("Protocol", redirect.protocol, obj); | |
237 | RGWXMLDecoder::decode_xml("HostName", redirect.hostname, obj); | |
238 | int code = 0; | |
239 | bool has_http_redirect_code = RGWXMLDecoder::decode_xml("HttpRedirectCode", code, obj); | |
240 | if (has_http_redirect_code && | |
241 | !(code > WEBSITE_HTTP_REDIRECT_CODE_MIN && | |
242 | code < WEBSITE_HTTP_REDIRECT_CODE_MAX)) { | |
243 | throw RGWXMLDecoder::err("The provided HTTP redirect code is not valid. Valid codes are 3XX except 300."); | |
244 | } | |
245 | redirect.http_redirect_code = code; | |
246 | bool has_replace_key_prefix_with = RGWXMLDecoder::decode_xml("ReplaceKeyPrefixWith", replace_key_prefix_with, obj); | |
247 | bool has_replace_key_with = RGWXMLDecoder::decode_xml("ReplaceKeyWith", replace_key_with, obj); | |
248 | if (has_replace_key_prefix_with && has_replace_key_with) { | |
249 | throw RGWXMLDecoder::err("You can only define ReplaceKeyPrefix or ReplaceKey but not both."); | |
250 | } | |
251 | } | |
252 | ||
253 | void RGWBWRoutingRuleCondition::dump_xml(Formatter *f) const | |
254 | { | |
255 | if (!key_prefix_equals.empty()) { | |
256 | encode_xml("KeyPrefixEquals", key_prefix_equals, f); | |
257 | } | |
258 | if (http_error_code_returned_equals > 0) { | |
259 | encode_xml("HttpErrorCodeReturnedEquals", (int)http_error_code_returned_equals, f); | |
260 | } | |
261 | } | |
262 | ||
263 | #define WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MIN 400 | |
264 | #define WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MAX 600 | |
265 | void RGWBWRoutingRuleCondition::decode_xml(XMLObj *obj) { | |
266 | RGWXMLDecoder::decode_xml("KeyPrefixEquals", key_prefix_equals, obj); | |
267 | int code = 0; | |
268 | bool has_http_error_code_returned_equals = RGWXMLDecoder::decode_xml("HttpErrorCodeReturnedEquals", code, obj); | |
269 | if (has_http_error_code_returned_equals && | |
270 | !(code >= WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MIN && | |
271 | code < WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MAX)) { | |
272 | throw RGWXMLDecoder::err("The provided HTTP redirect code is not valid. Valid codes are 4XX or 5XX."); | |
273 | } | |
274 | http_error_code_returned_equals = code; | |
275 | } | |
276 | ||
277 | void RGWBWRoutingRule::dump_xml(Formatter *f) const | |
278 | { | |
279 | encode_xml("Condition", condition, f); | |
280 | encode_xml("Redirect", redirect_info, f); | |
281 | } | |
282 | ||
283 | void RGWBWRoutingRule::decode_xml(XMLObj *obj) { | |
284 | RGWXMLDecoder::decode_xml("Condition", condition, obj); | |
285 | RGWXMLDecoder::decode_xml("Redirect", redirect_info, obj); | |
286 | } | |
287 | ||
288 | static void encode_xml(const char *name, const std::list<RGWBWRoutingRule>& l, ceph::Formatter *f) | |
289 | { | |
290 | do_encode_xml("RoutingRules", l, "RoutingRule", f); | |
291 | } | |
292 | ||
293 | void RGWBucketWebsiteConf::dump_xml(Formatter *f) const | |
294 | { | |
295 | if (!redirect_all.hostname.empty()) { | |
296 | f->open_object_section("RedirectAllRequestsTo"); | |
297 | encode_xml("HostName", redirect_all.hostname, f); | |
298 | if (!redirect_all.protocol.empty()) { | |
299 | encode_xml("Protocol", redirect_all.protocol, f); | |
300 | } | |
301 | f->close_section(); | |
302 | } | |
303 | if (!index_doc_suffix.empty()) { | |
304 | f->open_object_section("IndexDocument"); | |
305 | encode_xml("Suffix", index_doc_suffix, f); | |
306 | f->close_section(); | |
307 | } | |
308 | if (!error_doc.empty()) { | |
309 | f->open_object_section("ErrorDocument"); | |
310 | encode_xml("Key", error_doc, f); | |
311 | f->close_section(); | |
312 | } | |
313 | if (!routing_rules.rules.empty()) { | |
314 | encode_xml("RoutingRules", routing_rules.rules, f); | |
315 | } | |
316 | } | |
317 | ||
318 | void decode_xml_obj(list<RGWBWRoutingRule>& l, XMLObj *obj) | |
319 | { | |
320 | do_decode_xml_obj(l, "RoutingRule", obj); | |
321 | } | |
322 | ||
323 | void RGWBucketWebsiteConf::decode_xml(XMLObj *obj) { | |
324 | XMLObj *o = obj->find_first("RedirectAllRequestsTo"); | |
325 | if (o) { | |
326 | is_redirect_all = true; | |
327 | RGWXMLDecoder::decode_xml("HostName", redirect_all.hostname, o, true); | |
328 | RGWXMLDecoder::decode_xml("Protocol", redirect_all.protocol, o); | |
329 | } else { | |
330 | o = obj->find_first("IndexDocument"); | |
331 | if (o) { | |
332 | is_set_index_doc = true; | |
333 | RGWXMLDecoder::decode_xml("Suffix", index_doc_suffix, o); | |
334 | } | |
335 | o = obj->find_first("ErrorDocument"); | |
336 | if (o) { | |
337 | RGWXMLDecoder::decode_xml("Key", error_doc, o); | |
338 | } | |
339 | RGWXMLDecoder::decode_xml("RoutingRules", routing_rules.rules, obj); | |
340 | } | |
341 | } |