]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_cors.h
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / rgw / rgw_cors.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 /*
5 * Ceph - scalable distributed file system
6 *
7 * Copyright (C) 2013 eNovance SAS <licensing@enovance.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
16 #pragma once
17
18 #include <map>
19 #include <string>
20 #include <include/types.h>
21
22 #define RGW_CORS_GET 0x1
23 #define RGW_CORS_PUT 0x2
24 #define RGW_CORS_HEAD 0x4
25 #define RGW_CORS_POST 0x8
26 #define RGW_CORS_DELETE 0x10
27 #define RGW_CORS_COPY 0x20
28 #define RGW_CORS_ALL (RGW_CORS_GET | \
29 RGW_CORS_PUT | \
30 RGW_CORS_HEAD | \
31 RGW_CORS_POST | \
32 RGW_CORS_DELETE | \
33 RGW_CORS_COPY)
34
35 #define CORS_MAX_AGE_INVALID ((uint32_t)-1)
36
37 class RGWCORSRule
38 {
39 protected:
40 uint32_t max_age;
41 uint8_t allowed_methods;
42 std::string id;
43 std::set<std::string> allowed_hdrs; /* If you change this, you need to discard lowercase_allowed_hdrs */
44 std::set<std::string> lowercase_allowed_hdrs; /* Not built until needed in RGWCORSRule::is_header_allowed */
45 std::set<std::string> allowed_origins;
46 std::list<std::string> exposable_hdrs;
47
48 public:
49 RGWCORSRule() : max_age(CORS_MAX_AGE_INVALID),allowed_methods(0) {}
50 RGWCORSRule(std::set<std::string>& o, std::set<std::string>& h,
51 std::list<std::string>& e, uint8_t f, uint32_t a)
52 :max_age(a),
53 allowed_methods(f),
54 allowed_hdrs(h),
55 allowed_origins(o),
56 exposable_hdrs(e) {}
57 virtual ~RGWCORSRule() {}
58
59 std::string& get_id() { return id; }
60 uint32_t get_max_age() { return max_age; }
61 uint8_t get_allowed_methods() { return allowed_methods; }
62
63 void encode(bufferlist& bl) const {
64 ENCODE_START(1, 1, bl);
65 encode(max_age, bl);
66 encode(allowed_methods, bl);
67 encode(id, bl);
68 encode(allowed_hdrs, bl);
69 encode(allowed_origins, bl);
70 encode(exposable_hdrs, bl);
71 ENCODE_FINISH(bl);
72 }
73 void decode(bufferlist::const_iterator& bl) {
74 DECODE_START(1, bl);
75 decode(max_age, bl);
76 decode(allowed_methods, bl);
77 decode(id, bl);
78 decode(allowed_hdrs, bl);
79 decode(allowed_origins, bl);
80 decode(exposable_hdrs, bl);
81 DECODE_FINISH(bl);
82 }
83 bool has_wildcard_origin();
84 bool is_origin_present(const char *o);
85 void format_exp_headers(std::string& s);
86 void erase_origin_if_present(std::string& origin, bool *rule_empty);
87 void dump_origins();
88 void dump(Formatter *f) const;
89 bool is_header_allowed(const char *hdr, size_t len);
90 };
91 WRITE_CLASS_ENCODER(RGWCORSRule)
92
93 class RGWCORSConfiguration
94 {
95 protected:
96 std::list<RGWCORSRule> rules;
97 public:
98 RGWCORSConfiguration() {}
99 ~RGWCORSConfiguration() {}
100
101 void encode(bufferlist& bl) const {
102 ENCODE_START(1, 1, bl);
103 encode(rules, bl);
104 ENCODE_FINISH(bl);
105 }
106 void decode(bufferlist::const_iterator& bl) {
107 DECODE_START(1, bl);
108 decode(rules, bl);
109 DECODE_FINISH(bl);
110 }
111 void dump(Formatter *f) const;
112 std::list<RGWCORSRule>& get_rules() {
113 return rules;
114 }
115 bool is_empty() {
116 return rules.empty();
117 }
118 void get_origins_list(const char *origin, std::list<std::string>& origins);
119 RGWCORSRule * host_name_rule(const char *origin);
120 void erase_host_name_rule(std::string& origin);
121 void dump();
122 void stack_rule(RGWCORSRule& r) {
123 rules.push_front(r);
124 }
125 };
126 WRITE_CLASS_ENCODER(RGWCORSConfiguration)
127
128 static inline int validate_name_string(std::string_view o) {
129 if (o.length() == 0)
130 return -1;
131 if (o.find_first_of("*") != o.find_last_of("*"))
132 return -1;
133 return 0;
134 }
135
136 static inline uint8_t get_cors_method_flags(const char *req_meth) {
137 uint8_t flags = 0;
138
139 if (strcmp(req_meth, "GET") == 0) flags = RGW_CORS_GET;
140 else if (strcmp(req_meth, "POST") == 0) flags = RGW_CORS_POST;
141 else if (strcmp(req_meth, "PUT") == 0) flags = RGW_CORS_PUT;
142 else if (strcmp(req_meth, "DELETE") == 0) flags = RGW_CORS_DELETE;
143 else if (strcmp(req_meth, "HEAD") == 0) flags = RGW_CORS_HEAD;
144
145 return flags;
146 }