]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
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 FG |
17 | #include "common/debug.h" |
18 | #include "common/ceph_json.h" | |
19 | ||
20 | #include "acconfig.h" | |
21 | ||
22 | #include <errno.h> | |
23 | #include <string> | |
24 | #include <list> | |
25 | #include "include/types.h" | |
26 | #include "rgw_website.h" | |
27 | ||
7c673cae FG |
28 | |
29 | ||
30 | bool RGWBWRoutingRuleCondition::check_key_condition(const string& key) { | |
31 | return (key.size() >= key_prefix_equals.size() && | |
32 | key.compare(0, key_prefix_equals.size(), key_prefix_equals) == 0); | |
33 | } | |
34 | ||
35 | ||
36 | void RGWBWRoutingRule::apply_rule(const string& default_protocol, const string& default_hostname, | |
37 | const string& key, string *new_url, int *redirect_code) | |
38 | { | |
39 | RGWRedirectInfo& redirect = redirect_info.redirect; | |
40 | ||
41 | string protocol = (!redirect.protocol.empty() ? redirect.protocol : default_protocol); | |
42 | string hostname = (!redirect.hostname.empty() ? redirect.hostname : default_hostname); | |
43 | ||
44 | *new_url = protocol + "://" + hostname + "/"; | |
45 | ||
46 | if (!redirect_info.replace_key_prefix_with.empty()) { | |
47 | *new_url += redirect_info.replace_key_prefix_with; | |
48 | *new_url += key.substr(condition.key_prefix_equals.size()); | |
49 | } else if (!redirect_info.replace_key_with.empty()) { | |
50 | *new_url += redirect_info.replace_key_with; | |
51 | } else { | |
52 | *new_url += key; | |
53 | } | |
54 | ||
55 | if(redirect.http_redirect_code > 0) | |
56 | *redirect_code = redirect.http_redirect_code; | |
57 | } | |
58 | ||
59 | bool RGWBWRoutingRules::check_key_and_error_code_condition(const string &key, int error_code, RGWBWRoutingRule **rule) | |
60 | { | |
61 | for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) { | |
62 | if (iter->check_key_condition(key) && iter->check_error_code_condition(error_code)) { | |
63 | *rule = &(*iter); | |
64 | return true; | |
65 | } | |
66 | } | |
67 | return false; | |
68 | } | |
69 | ||
70 | bool RGWBWRoutingRules::check_key_condition(const string& key, RGWBWRoutingRule **rule) | |
71 | { | |
72 | for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) { | |
73 | if (iter->check_key_condition(key)) { | |
74 | *rule = &(*iter); | |
75 | return true; | |
76 | } | |
77 | } | |
78 | return false; | |
79 | } | |
80 | ||
81 | bool RGWBWRoutingRules::check_error_code_condition(const int http_error_code, RGWBWRoutingRule **rule) | |
82 | { | |
83 | for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) { | |
84 | if (iter->check_error_code_condition(http_error_code)) { | |
85 | *rule = &(*iter); | |
86 | return true; | |
87 | } | |
88 | } | |
89 | return false; | |
90 | } | |
91 | ||
92 | bool RGWBucketWebsiteConf::should_redirect(const string& key, const int http_error_code, RGWBWRoutingRule *redirect) | |
93 | { | |
94 | RGWBWRoutingRule *rule; | |
95 | if(!redirect_all.hostname.empty()) { | |
96 | RGWBWRoutingRule redirect_all_rule; | |
97 | redirect_all_rule.redirect_info.redirect = redirect_all; | |
98 | redirect_all.http_redirect_code = 301; | |
99 | *redirect = redirect_all_rule; | |
100 | return true; | |
101 | } else if (!routing_rules.check_key_and_error_code_condition(key, http_error_code, &rule)) { | |
102 | return false; | |
103 | } | |
104 | ||
105 | *redirect = *rule; | |
106 | ||
107 | return true; | |
108 | } | |
109 | ||
224ce89b | 110 | void RGWBucketWebsiteConf::get_effective_key(const string& key, string *effective_key, bool is_file) const |
7c673cae FG |
111 | { |
112 | ||
113 | if (key.empty()) { | |
114 | *effective_key = index_doc_suffix; | |
115 | } else if (key[key.size() - 1] == '/') { | |
116 | *effective_key = key + index_doc_suffix; | |
224ce89b WB |
117 | } else if (! is_file) { |
118 | *effective_key = key + "/" + index_doc_suffix; | |
7c673cae FG |
119 | } else { |
120 | *effective_key = key; | |
121 | } | |
122 | } |