1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Yehuda Sadeh <yehuda@redhat.com>
7 * Copyright (C) 2015 Robin H. Johnson <robin.johnson@dreamhost.com>
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.
15 #include "common/debug.h"
16 #include "common/ceph_json.h"
23 #include "include/types.h"
24 #include "rgw_website.h"
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);
35 void RGWBWRoutingRule::apply_rule(const string& default_protocol, const string& default_hostname,
36 const string& key, string *new_url, int *redirect_code)
38 RGWRedirectInfo& redirect = redirect_info.redirect;
40 string protocol = (!redirect.protocol.empty() ? redirect.protocol : default_protocol);
41 string hostname = (!redirect.hostname.empty() ? redirect.hostname : default_hostname);
43 *new_url = protocol + "://" + hostname + "/";
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;
54 if(redirect.http_redirect_code > 0)
55 *redirect_code = redirect.http_redirect_code;
58 bool RGWBWRoutingRules::check_key_and_error_code_condition(const string &key, int error_code, RGWBWRoutingRule **rule)
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)) {
69 bool RGWBWRoutingRules::check_key_condition(const string& key, RGWBWRoutingRule **rule)
71 for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) {
72 if (iter->check_key_condition(key)) {
80 bool RGWBWRoutingRules::check_error_code_condition(const int http_error_code, RGWBWRoutingRule **rule)
82 for (list<RGWBWRoutingRule>::iterator iter = rules.begin(); iter != rules.end(); ++iter) {
83 if (iter->check_error_code_condition(http_error_code)) {
91 bool RGWBucketWebsiteConf::should_redirect(const string& key, const int http_error_code, RGWBWRoutingRule *redirect)
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;
100 } else if (!routing_rules.check_key_and_error_code_condition(key, http_error_code, &rule)) {
109 void RGWBucketWebsiteConf::get_effective_key(const string& key, string *effective_key, bool is_file) const
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;
119 *effective_key = key;