summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_xml_enc.cc
blob: 3c7afcbbdc7b43cc9c05d6bbb29fe803251b2934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2015 Yehuda Sadeh <yehuda@redhat.com>
 * Copyright (C) 2015 Robin H. Johnson <robin.johnson@dreamhost.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include "rgw_common.h"
#include "rgw_xml.h"

#include "common/Formatter.h"

#define dout_subsys ceph_subsys_rgw

void RGWBWRedirectInfo::dump_xml(Formatter *f) const
{
  if (!redirect.protocol.empty()) {
    encode_xml("Protocol", redirect.protocol, f);
  }
  if (!redirect.hostname.empty()) {
    encode_xml("HostName", redirect.hostname, f);
  }
  if (redirect.http_redirect_code > 0) {
    encode_xml("HttpRedirectCode", (int)redirect.http_redirect_code, f);
  }
  if (!replace_key_prefix_with.empty()) {
    encode_xml("ReplaceKeyPrefixWith", replace_key_prefix_with, f);
  }
  if (!replace_key_with.empty()) {
    encode_xml("ReplaceKeyWith", replace_key_with, f);
  }
}

#define WEBSITE_HTTP_REDIRECT_CODE_MIN      300
#define WEBSITE_HTTP_REDIRECT_CODE_MAX      400
void RGWBWRedirectInfo::decode_xml(XMLObj *obj) {
  RGWXMLDecoder::decode_xml("Protocol", redirect.protocol, obj);
  RGWXMLDecoder::decode_xml("HostName", redirect.hostname, obj);
  int code = 0;
  bool has_http_redirect_code = RGWXMLDecoder::decode_xml("HttpRedirectCode", code, obj);
  if (has_http_redirect_code &&
      !(code > WEBSITE_HTTP_REDIRECT_CODE_MIN &&
        code < WEBSITE_HTTP_REDIRECT_CODE_MAX)) {
    throw RGWXMLDecoder::err("The provided HTTP redirect code is not valid. Valid codes are 3XX except 300.");
  }
  redirect.http_redirect_code = code;
  bool has_replace_key_prefix_with = RGWXMLDecoder::decode_xml("ReplaceKeyPrefixWith", replace_key_prefix_with, obj);
  bool has_replace_key_with = RGWXMLDecoder::decode_xml("ReplaceKeyWith", replace_key_with, obj);
  if (has_replace_key_prefix_with && has_replace_key_with) {
    throw RGWXMLDecoder::err("You can only define ReplaceKeyPrefix or ReplaceKey but not both.");
  }
}

void RGWBWRoutingRuleCondition::dump_xml(Formatter *f) const
{
  if (!key_prefix_equals.empty()) {
    encode_xml("KeyPrefixEquals", key_prefix_equals, f);
  }
  if (http_error_code_returned_equals > 0) {
    encode_xml("HttpErrorCodeReturnedEquals", (int)http_error_code_returned_equals, f);
  }
}

#define WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MIN      400
#define WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MAX      600
void RGWBWRoutingRuleCondition::decode_xml(XMLObj *obj) {
  RGWXMLDecoder::decode_xml("KeyPrefixEquals", key_prefix_equals, obj);
  int code = 0;
  bool has_http_error_code_returned_equals = RGWXMLDecoder::decode_xml("HttpErrorCodeReturnedEquals", code, obj);
  if (has_http_error_code_returned_equals &&
      !(code >= WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MIN &&
        code < WEBSITE_HTTP_ERROR_CODE_RETURNED_EQUALS_MAX)) {
    throw RGWXMLDecoder::err("The provided HTTP redirect code is not valid. Valid codes are 4XX or 5XX.");
  }
  http_error_code_returned_equals = code;
}

void RGWBWRoutingRule::dump_xml(Formatter *f) const
{
  encode_xml("Condition", condition, f);
  encode_xml("Redirect", redirect_info, f);
}

void RGWBWRoutingRule::decode_xml(XMLObj *obj) {
  RGWXMLDecoder::decode_xml("Condition", condition, obj);
  RGWXMLDecoder::decode_xml("Redirect", redirect_info, obj);
}

static void encode_xml(const char *name, const std::list<RGWBWRoutingRule>& l, ceph::Formatter *f)
{
  do_encode_xml("RoutingRules", l, "RoutingRule", f);
}

void RGWBucketWebsiteConf::dump_xml(Formatter *f) const
{
  if (!redirect_all.hostname.empty()) {
    f->open_object_section("RedirectAllRequestsTo");
    encode_xml("HostName", redirect_all.hostname, f);
    if (!redirect_all.protocol.empty()) {
      encode_xml("Protocol", redirect_all.protocol, f);
    }
    f->close_section();
  }
  if (!index_doc_suffix.empty()) {
    f->open_object_section("IndexDocument");
    encode_xml("Suffix", index_doc_suffix, f);
    f->close_section();
  }
  if (!error_doc.empty()) {
    f->open_object_section("ErrorDocument");
    encode_xml("Key", error_doc, f);
    f->close_section();
  }
  if (!routing_rules.rules.empty()) {
    encode_xml("RoutingRules", routing_rules.rules, f);
  }
}

void decode_xml_obj(list<RGWBWRoutingRule>& l, XMLObj *obj)
{
  do_decode_xml_obj(l, "RoutingRule", obj);
}

void RGWBucketWebsiteConf::decode_xml(XMLObj *obj) {
  XMLObj *o = obj->find_first("RedirectAllRequestsTo");
  if (o) {
    is_redirect_all = true;
    RGWXMLDecoder::decode_xml("HostName", redirect_all.hostname, o, true);
    RGWXMLDecoder::decode_xml("Protocol", redirect_all.protocol, o);
  } else {
    o = obj->find_first("IndexDocument");
    if (o) {
      is_set_index_doc = true;
      RGWXMLDecoder::decode_xml("Suffix", index_doc_suffix, o);
    }
    o = obj->find_first("ErrorDocument");
    if (o) {
      RGWXMLDecoder::decode_xml("Key", error_doc, o);
    }
    RGWXMLDecoder::decode_xml("RoutingRules", routing_rules.rules, obj);
  }
}