1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
5 * Ceph - scalable distributed file system
7 * Copyright (C) 2015 Yehuda Sadeh <yehuda@redhat.com>
8 * Copyright (C) 2015 Robin H. Johnson <robin.johnson@dreamhost.com>
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.
17 #include "common/debug.h"
19 #include "common/ceph_json.h"
20 #include "common/Formatter.h"
27 #include "include/types.h"
28 #include "rgw_website.h"
29 #include "rgw_common.h"
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);
40 void RGWBWRoutingRule::apply_rule(const string
& default_protocol
, const string
& default_hostname
,
41 const string
& key
, string
*new_url
, int *redirect_code
)
43 RGWRedirectInfo
& redirect
= redirect_info
.redirect
;
45 string protocol
= (!redirect
.protocol
.empty() ? redirect
.protocol
: default_protocol
);
46 string hostname
= (!redirect
.hostname
.empty() ? redirect
.hostname
: default_hostname
);
48 *new_url
= protocol
+ "://" + hostname
+ "/";
50 if (!redirect_info
.replace_key_prefix_with
.empty()) {
51 *new_url
+= redirect_info
.replace_key_prefix_with
;
52 if (key
.size() > condition
.key_prefix_equals
.size()) {
53 *new_url
+= key
.substr(condition
.key_prefix_equals
.size());
55 } else if (!redirect_info
.replace_key_with
.empty()) {
56 *new_url
+= redirect_info
.replace_key_with
;
61 if(redirect
.http_redirect_code
> 0)
62 *redirect_code
= redirect
.http_redirect_code
;
65 bool RGWBWRoutingRules::check_key_and_error_code_condition(const string
&key
, int error_code
, RGWBWRoutingRule
**rule
)
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
)) {
76 bool RGWBWRoutingRules::check_key_condition(const string
& key
, RGWBWRoutingRule
**rule
)
78 for (list
<RGWBWRoutingRule
>::iterator iter
= rules
.begin(); iter
!= rules
.end(); ++iter
) {
79 if (iter
->check_key_condition(key
)) {
87 bool RGWBWRoutingRules::check_error_code_condition(const int http_error_code
, RGWBWRoutingRule
**rule
)
89 for (list
<RGWBWRoutingRule
>::iterator iter
= rules
.begin(); iter
!= rules
.end(); ++iter
) {
90 if (iter
->check_error_code_condition(http_error_code
)) {
98 bool RGWBucketWebsiteConf::should_redirect(const string
& key
, const int http_error_code
, RGWBWRoutingRule
*redirect
)
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
;
107 } else if (!routing_rules
.check_key_and_error_code_condition(key
, http_error_code
, &rule
)) {
116 bool RGWBucketWebsiteConf::get_effective_key(const string
& key
, string
*effective_key
, bool is_file
) const
118 if (index_doc_suffix
.empty()) {
123 *effective_key
= index_doc_suffix
;
124 } else if (key
[key
.size() - 1] == '/') {
125 *effective_key
= key
+ index_doc_suffix
;
126 } else if (! is_file
) {
127 *effective_key
= key
+ "/" + index_doc_suffix
;
129 *effective_key
= key
;
135 void RGWRedirectInfo::dump(Formatter
*f
) const
137 encode_json("protocol", protocol
, f
);
138 encode_json("hostname", hostname
, f
);
139 encode_json("http_redirect_code", (int)http_redirect_code
, f
);
142 void RGWRedirectInfo::decode_json(JSONObj
*obj
) {
143 JSONDecoder::decode_json("protocol", protocol
, obj
);
144 JSONDecoder::decode_json("hostname", hostname
, obj
);
146 JSONDecoder::decode_json("http_redirect_code", code
, obj
);
147 http_redirect_code
= code
;
150 void RGWBWRedirectInfo::dump(Formatter
*f
) const
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
);
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
);
163 void RGWBWRoutingRuleCondition::dump(Formatter
*f
) const
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
);
169 void RGWBWRoutingRuleCondition::decode_json(JSONObj
*obj
) {
170 JSONDecoder::decode_json("key_prefix_equals", key_prefix_equals
, obj
);
172 JSONDecoder::decode_json("http_error_code_returned_equals", code
, obj
);
173 http_error_code_returned_equals
= code
;
176 void RGWBWRoutingRule::dump(Formatter
*f
) const
178 encode_json("condition", condition
, f
);
179 encode_json("redirect_info", redirect_info
, f
);
182 void RGWBWRoutingRule::decode_json(JSONObj
*obj
) {
183 JSONDecoder::decode_json("condition", condition
, obj
);
184 JSONDecoder::decode_json("redirect_info", redirect_info
, obj
);
187 void RGWBWRoutingRules::dump(Formatter
*f
) const
189 encode_json("rules", rules
, f
);
192 void RGWBWRoutingRules::decode_json(JSONObj
*obj
) {
193 JSONDecoder::decode_json("rules", rules
, obj
);
196 void RGWBucketWebsiteConf::dump(Formatter
*f
) const
198 if (!redirect_all
.hostname
.empty()) {
199 encode_json("redirect_all", redirect_all
, f
);
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
);
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
);
214 void RGWBWRedirectInfo::dump_xml(Formatter
*f
) const
216 if (!redirect
.protocol
.empty()) {
217 encode_xml("Protocol", redirect
.protocol
, f
);
219 if (!redirect
.hostname
.empty()) {
220 encode_xml("HostName", redirect
.hostname
, f
);
222 if (redirect
.http_redirect_code
> 0) {
223 encode_xml("HttpRedirectCode", (int)redirect
.http_redirect_code
, f
);
225 if (!replace_key_prefix_with
.empty()) {
226 encode_xml("ReplaceKeyPrefixWith", replace_key_prefix_with
, f
);
228 if (!replace_key_with
.empty()) {
229 encode_xml("ReplaceKeyWith", replace_key_with
, f
);
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
);
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.");
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.");
253 void RGWBWRoutingRuleCondition::dump_xml(Formatter
*f
) const
255 if (!key_prefix_equals
.empty()) {
256 encode_xml("KeyPrefixEquals", key_prefix_equals
, f
);
258 if (http_error_code_returned_equals
> 0) {
259 encode_xml("HttpErrorCodeReturnedEquals", (int)http_error_code_returned_equals
, f
);
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
);
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.");
274 http_error_code_returned_equals
= code
;
277 void RGWBWRoutingRule::dump_xml(Formatter
*f
) const
279 encode_xml("Condition", condition
, f
);
280 encode_xml("Redirect", redirect_info
, f
);
283 void RGWBWRoutingRule::decode_xml(XMLObj
*obj
) {
284 RGWXMLDecoder::decode_xml("Condition", condition
, obj
);
285 RGWXMLDecoder::decode_xml("Redirect", redirect_info
, obj
);
288 static void encode_xml(const char *name
, const std::list
<RGWBWRoutingRule
>& l
, ceph::Formatter
*f
)
290 do_encode_xml("RoutingRules", l
, "RoutingRule", f
);
293 void RGWBucketWebsiteConf::dump_xml(Formatter
*f
) const
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
);
303 if (!index_doc_suffix
.empty()) {
304 f
->open_object_section("IndexDocument");
305 encode_xml("Suffix", index_doc_suffix
, f
);
308 if (!error_doc
.empty()) {
309 f
->open_object_section("ErrorDocument");
310 encode_xml("Key", error_doc
, f
);
313 if (!routing_rules
.rules
.empty()) {
314 encode_xml("RoutingRules", routing_rules
.rules
, f
);
318 void decode_xml_obj(list
<RGWBWRoutingRule
>& l
, XMLObj
*obj
)
320 do_decode_xml_obj(l
, "RoutingRule", obj
);
323 void RGWBucketWebsiteConf::decode_xml(XMLObj
*obj
) {
324 XMLObj
*o
= obj
->find_first("RedirectAllRequestsTo");
326 is_redirect_all
= true;
327 RGWXMLDecoder::decode_xml("HostName", redirect_all
.hostname
, o
, true);
328 RGWXMLDecoder::decode_xml("Protocol", redirect_all
.protocol
, o
);
330 o
= obj
->find_first("IndexDocument");
332 is_set_index_doc
= true;
333 RGWXMLDecoder::decode_xml("Suffix", index_doc_suffix
, o
);
335 o
= obj
->find_first("ErrorDocument");
337 RGWXMLDecoder::decode_xml("Key", error_doc
, o
);
339 RGWXMLDecoder::decode_xml("RoutingRules", routing_rules
.rules
, obj
);