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) 2013 eNovance SAS <licensing@enovance.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
20 #include "include/types.h"
22 #include "rgw_cors_s3.h"
25 #define dout_context g_ceph_context
26 #define dout_subsys ceph_subsys_rgw
30 void RGWCORSRule_S3::to_xml(XMLFormatter& f) {
32 f.open_object_section("CORSRule");
34 if (id.length() > 0) {
35 f.dump_string("ID", id);
38 if (allowed_methods & RGW_CORS_GET)
39 f.dump_string("AllowedMethod", "GET");
40 if (allowed_methods & RGW_CORS_PUT)
41 f.dump_string("AllowedMethod", "PUT");
42 if (allowed_methods & RGW_CORS_DELETE)
43 f.dump_string("AllowedMethod", "DELETE");
44 if (allowed_methods & RGW_CORS_HEAD)
45 f.dump_string("AllowedMethod", "HEAD");
46 if (allowed_methods & RGW_CORS_POST)
47 f.dump_string("AllowedMethod", "POST");
48 if (allowed_methods & RGW_CORS_COPY)
49 f.dump_string("AllowedMethod", "COPY");
51 for(set<string>::iterator it = allowed_origins.begin();
52 it != allowed_origins.end();
55 f.dump_string("AllowedOrigin", host);
58 for(set<string>::iterator it = allowed_hdrs.begin();
59 it != allowed_hdrs.end(); ++it) {
60 f.dump_string("AllowedHeader", *it);
63 if (max_age != CORS_MAX_AGE_INVALID) {
64 f.dump_unsigned("MaxAgeSeconds", max_age);
67 for(list<string>::iterator it = exposable_hdrs.begin();
68 it != exposable_hdrs.end(); ++it) {
69 f.dump_string("ExposeHeader", *it);
74 bool RGWCORSRule_S3::xml_end(const char *el) {
75 XMLObjIter iter = find("AllowedMethod");
77 /*Check all the allowedmethods*/
78 obj = iter.get_next();
80 for( ; obj; obj = iter.get_next()) {
81 const char *s = obj->get_data().c_str();
82 dout(10) << "RGWCORSRule::xml_end, el : " << el << ", data : " << s << dendl;
83 if (strcasecmp(s, "GET") == 0) {
84 allowed_methods |= RGW_CORS_GET;
85 } else if (strcasecmp(s, "POST") == 0) {
86 allowed_methods |= RGW_CORS_POST;
87 } else if (strcasecmp(s, "DELETE") == 0) {
88 allowed_methods |= RGW_CORS_DELETE;
89 } else if (strcasecmp(s, "HEAD") == 0) {
90 allowed_methods |= RGW_CORS_HEAD;
91 } else if (strcasecmp(s, "PUT") == 0) {
92 allowed_methods |= RGW_CORS_PUT;
93 } else if (strcasecmp(s, "COPY") == 0) {
94 allowed_methods |= RGW_CORS_COPY;
100 /*Check the id's len, it should be less than 255*/
101 XMLObj *xml_id = find_first("ID");
102 if (xml_id != NULL) {
103 string data = xml_id->get_data();
104 if (data.length() > 255) {
105 dout(0) << "RGWCORSRule has id of length greater than 255" << dendl;
108 dout(10) << "RGWCORRule id : " << data << dendl;
111 /*Check if there is atleast one AllowedOrigin*/
112 iter = find("AllowedOrigin");
113 if (!(obj = iter.get_next())) {
114 dout(0) << "RGWCORSRule does not have even one AllowedOrigin" << dendl;
117 for( ; obj; obj = iter.get_next()) {
118 dout(10) << "RGWCORSRule - origin : " << obj->get_data() << dendl;
119 /*Just take the hostname*/
120 string host = obj->get_data();
121 if (validate_name_string(host) != 0)
123 allowed_origins.insert(allowed_origins.end(), host);
126 iter = find("MaxAgeSeconds");
127 if ((obj = iter.get_next())) {
130 unsigned long long ull = strtoull(obj->get_data().c_str(), &end, 10);
131 if (ull >= 0x100000000ull) {
132 max_age = CORS_MAX_AGE_INVALID;
134 max_age = (uint32_t)ull;
136 dout(10) << "RGWCORSRule : max_age : " << max_age << dendl;
138 /*Check and update ExposeHeader*/
139 iter = find("ExposeHeader");
140 if ((obj = iter.get_next())) {
141 for(; obj; obj = iter.get_next()) {
142 dout(10) << "RGWCORSRule - exp_hdr : " << obj->get_data() << dendl;
143 exposable_hdrs.push_back(obj->get_data());
146 /*Check and update AllowedHeader*/
147 iter = find("AllowedHeader");
148 if ((obj = iter.get_next())) {
149 for(; obj; obj = iter.get_next()) {
150 dout(10) << "RGWCORSRule - allowed_hdr : " << obj->get_data() << dendl;
151 string s = obj->get_data();
152 if (validate_name_string(s) != 0)
154 allowed_hdrs.insert(allowed_hdrs.end(), s);
160 void RGWCORSConfiguration_S3::to_xml(ostream& out) {
162 f.open_object_section_in_ns("CORSConfiguration", XMLNS_AWS_S3);
163 for(list<RGWCORSRule>::iterator it = rules.begin();
164 it != rules.end(); ++it) {
165 (static_cast<RGWCORSRule_S3 &>(*it)).to_xml(f);
171 bool RGWCORSConfiguration_S3::xml_end(const char *el) {
172 XMLObjIter iter = find("CORSRule");
174 if (!(obj = static_cast<RGWCORSRule_S3 *>(iter.get_next()))) {
175 dout(0) << "CORSConfiguration should have atleast one CORSRule" << dendl;
178 for(; obj; obj = static_cast<RGWCORSRule_S3 *>(iter.get_next())) {
179 rules.push_back(*obj);
184 class CORSRuleID_S3 : public XMLObj {
187 ~CORSRuleID_S3() override {}
190 class CORSRuleAllowedOrigin_S3 : public XMLObj {
192 CORSRuleAllowedOrigin_S3() {}
193 ~CORSRuleAllowedOrigin_S3() override {}
196 class CORSRuleAllowedMethod_S3 : public XMLObj {
198 CORSRuleAllowedMethod_S3() {}
199 ~CORSRuleAllowedMethod_S3() override {}
202 class CORSRuleAllowedHeader_S3 : public XMLObj {
204 CORSRuleAllowedHeader_S3() {}
205 ~CORSRuleAllowedHeader_S3() override {}
208 class CORSRuleMaxAgeSeconds_S3 : public XMLObj {
210 CORSRuleMaxAgeSeconds_S3() {}
211 ~CORSRuleMaxAgeSeconds_S3() override {}
214 class CORSRuleExposeHeader_S3 : public XMLObj {
216 CORSRuleExposeHeader_S3() {}
217 ~CORSRuleExposeHeader_S3() override {}
220 XMLObj *RGWCORSXMLParser_S3::alloc_obj(const char *el) {
221 if (strcmp(el, "CORSConfiguration") == 0) {
222 return new RGWCORSConfiguration_S3;
223 } else if (strcmp(el, "CORSRule") == 0) {
224 return new RGWCORSRule_S3;
225 } else if (strcmp(el, "ID") == 0) {
226 return new CORSRuleID_S3;
227 } else if (strcmp(el, "AllowedOrigin") == 0) {
228 return new CORSRuleAllowedOrigin_S3;
229 } else if (strcmp(el, "AllowedMethod") == 0) {
230 return new CORSRuleAllowedMethod_S3;
231 } else if (strcmp(el, "AllowedHeader") == 0) {
232 return new CORSRuleAllowedHeader_S3;
233 } else if (strcmp(el, "MaxAgeSeconds") == 0) {
234 return new CORSRuleMaxAgeSeconds_S3;
235 } else if (strcmp(el, "ExposeHeader") == 0) {
236 return new CORSRuleExposeHeader_S3;