Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_website.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2015 Yehuda Sadeh <yehuda@redhat.com>
7  * Copyright (C) 2015 Robin H. Johnson <robin.johnson@dreamhost.com>
8  *
9  * This is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1, as published by the Free Software 
12  * Foundation.  See file COPYING.
13  * 
14  */
15 #include "common/debug.h"
16 #include "common/ceph_json.h"
17
18 #include "acconfig.h"
19
20 #include <errno.h>
21 #include <string>
22 #include <list>
23 #include "include/types.h"
24 #include "rgw_website.h"
25
26 using namespace std;
27
28
29 bool RGWBWRoutingRuleCondition::check_key_condition(const string& key) {
30   return (key.size() >= key_prefix_equals.size() &&
31           key.compare(0, key_prefix_equals.size(), key_prefix_equals) == 0);
32 }
33
34
35 void RGWBWRoutingRule::apply_rule(const string& default_protocol, const string& default_hostname,
36                                            const string& key, string *new_url, int *redirect_code)
37 {
38   RGWRedirectInfo& redirect = redirect_info.redirect;
39
40   string protocol = (!redirect.protocol.empty() ? redirect.protocol : default_protocol);
41   string hostname = (!redirect.hostname.empty() ? redirect.hostname : default_hostname);
42
43   *new_url = protocol + "://" + hostname + "/";
44
45   if (!redirect_info.replace_key_prefix_with.empty()) {
46     *new_url += redirect_info.replace_key_prefix_with;
47     *new_url += key.substr(condition.key_prefix_equals.size());
48   } else if (!redirect_info.replace_key_with.empty()) {
49     *new_url += redirect_info.replace_key_with;
50   } else {
51     *new_url += key;
52   }
53
54   if(redirect.http_redirect_code > 0) 
55           *redirect_code = redirect.http_redirect_code;
56 }
57
58 bool RGWBWRoutingRules::check_key_and_error_code_condition(const string &key, int error_code, RGWBWRoutingRule **rule)
59 {
60   for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) {
61     if (iter->check_key_condition(key) && iter->check_error_code_condition(error_code)) {
62       *rule = &(*iter);
63       return true;
64     }
65   }
66   return false;
67 }
68
69 bool RGWBWRoutingRules::check_key_condition(const string& key, RGWBWRoutingRule **rule)
70 {
71   for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) {
72     if (iter->check_key_condition(key)) {
73       *rule = &(*iter);
74       return true;
75     }
76   }
77   return false;
78 }
79
80 bool RGWBWRoutingRules::check_error_code_condition(const int http_error_code, RGWBWRoutingRule **rule)
81 {
82   for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) {
83     if (iter->check_error_code_condition(http_error_code)) {
84       *rule = &(*iter);
85       return true;
86     }
87   }
88   return false;
89 }
90
91 bool RGWBucketWebsiteConf::should_redirect(const string& key, const int http_error_code, RGWBWRoutingRule *redirect)
92 {
93   RGWBWRoutingRule *rule;
94   if(!redirect_all.hostname.empty()) {
95         RGWBWRoutingRule redirect_all_rule;
96         redirect_all_rule.redirect_info.redirect = redirect_all;
97         redirect_all.http_redirect_code = 301;
98         *redirect = redirect_all_rule;
99         return true;
100   } else if (!routing_rules.check_key_and_error_code_condition(key, http_error_code, &rule)) {
101     return false;
102   }
103
104   *redirect = *rule;
105
106   return true;
107 }
108
109 void RGWBucketWebsiteConf::get_effective_key(const string& key, string *effective_key, bool is_file) const
110 {
111
112   if (key.empty()) {
113     *effective_key = index_doc_suffix;
114   } else if (key[key.size() - 1] == '/') {
115     *effective_key = key + index_doc_suffix;
116   } else if (! is_file) {
117     *effective_key = key + "/" + index_doc_suffix; 
118   } else {
119     *effective_key = key;
120   }
121 }