]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rest_swift.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rgw / rgw_rest_swift.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <boost/algorithm/string/predicate.hpp>
5 #include <boost/optional.hpp>
6 #include <boost/utility/in_place_factory.hpp>
7
8 #include "include/assert.h"
9 #include "ceph_ver.h"
10
11 #include "common/Formatter.h"
12 #include "common/utf8.h"
13 #include "common/ceph_json.h"
14
15 #include "rgw_rest_swift.h"
16 #include "rgw_acl_swift.h"
17 #include "rgw_cors_swift.h"
18 #include "rgw_formats.h"
19 #include "rgw_client_io.h"
20
21 #include "rgw_auth.h"
22 #include "rgw_swift_auth.h"
23
24 #include "rgw_request.h"
25 #include "rgw_process.h"
26
27 #include <array>
28 #include <sstream>
29 #include <memory>
30
31 #include <boost/utility/string_ref.hpp>
32
33 #define dout_context g_ceph_context
34 #define dout_subsys ceph_subsys_rgw
35
36 int RGWListBuckets_ObjStore_SWIFT::get_params()
37 {
38 prefix = s->info.args.get("prefix");
39 marker = s->info.args.get("marker");
40 end_marker = s->info.args.get("end_marker");
41
42 string limit_str = s->info.args.get("limit");
43 if (!limit_str.empty()) {
44 string err;
45 long l = strict_strtol(limit_str.c_str(), 10, &err);
46 if (!err.empty()) {
47 return -EINVAL;
48 }
49
50 if (l > (long)limit_max || l < 0) {
51 return -ERR_PRECONDITION_FAILED;
52 }
53
54 limit = (uint64_t)l;
55 }
56
57 if (need_stats) {
58 bool stats, exists;
59 int r = s->info.args.get_bool("stats", &stats, &exists);
60
61 if (r < 0) {
62 return r;
63 }
64
65 if (exists) {
66 need_stats = stats;
67 }
68 }
69
70 return 0;
71 }
72
73 static void dump_account_metadata(struct req_state * const s,
74 const uint32_t buckets_count,
75 const uint64_t buckets_object_count,
76 const uint64_t buckets_size,
77 const uint64_t buckets_size_rounded,
78 /* const */map<string, bufferlist>& attrs,
79 const RGWQuotaInfo& quota,
80 const RGWAccessControlPolicy_SWIFTAcct &policy)
81 {
82 /* Adding X-Timestamp to keep align with Swift API */
83 dump_header(s, "X-Timestamp", ceph_clock_now());
84
85 dump_header(s, "X-Account-Container-Count", buckets_count);
86 dump_header(s, "X-Account-Object-Count", buckets_object_count);
87 dump_header(s, "X-Account-Bytes-Used", buckets_size);
88 dump_header(s, "X-Account-Bytes-Used-Actual", buckets_size_rounded);
89
90 /* Dump TempURL-related stuff */
91 if (s->perm_mask == RGW_PERM_FULL_CONTROL) {
92 auto iter = s->user->temp_url_keys.find(0);
93 if (iter != std::end(s->user->temp_url_keys) && ! iter->second.empty()) {
94 dump_header(s, "X-Account-Meta-Temp-Url-Key", iter->second);
95 }
96
97 iter = s->user->temp_url_keys.find(1);
98 if (iter != std::end(s->user->temp_url_keys) && ! iter->second.empty()) {
99 dump_header(s, "X-Account-Meta-Temp-Url-Key-2", iter->second);
100 }
101 }
102
103 /* Dump quota headers. */
104 if (quota.enabled) {
105 if (quota.max_size >= 0) {
106 dump_header(s, "X-Account-Meta-Quota-Bytes", quota.max_size);
107 }
108
109 /* Limit on the number of objects in a given account is a RadosGW's
110 * extension. Swift's account quota WSGI filter doesn't support it. */
111 if (quota.max_objects >= 0) {
112 dump_header(s, "X-Account-Meta-Quota-Count", quota.max_objects);
113 }
114 }
115
116 /* Dump user-defined metadata items and generic attrs. */
117 const size_t PREFIX_LEN = sizeof(RGW_ATTR_META_PREFIX) - 1;
118 map<string, bufferlist>::iterator iter;
119 for (iter = attrs.lower_bound(RGW_ATTR_PREFIX); iter != attrs.end(); ++iter) {
120 const char *name = iter->first.c_str();
121 map<string, string>::const_iterator geniter = rgw_to_http_attrs.find(name);
122
123 if (geniter != rgw_to_http_attrs.end()) {
124 dump_header(s, geniter->second, iter->second);
125 } else if (strncmp(name, RGW_ATTR_META_PREFIX, PREFIX_LEN) == 0) {
126 dump_header_prefixed(s, "X-Account-Meta-",
127 camelcase_dash_http_attr(name + PREFIX_LEN),
128 iter->second);
129 }
130 }
131
132 /* Dump account ACLs */
133 string acct_acl;
134 policy.to_str(acct_acl);
135 if (acct_acl.size()) {
136 dump_header(s, "X-Account-Access-Control", std::move(acct_acl));
137 }
138 }
139
140 void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets)
141 {
142 if (op_ret) {
143 set_req_state_err(s, op_ret);
144 } else if (!has_buckets && s->format == RGW_FORMAT_PLAIN) {
145 op_ret = STATUS_NO_CONTENT;
146 set_req_state_err(s, op_ret);
147 }
148
149 if (! s->cct->_conf->rgw_swift_enforce_content_length) {
150 /* Adding account stats in the header to keep align with Swift API */
151 dump_account_metadata(s,
152 buckets_count,
153 buckets_objcount,
154 buckets_size,
155 buckets_size_rounded,
156 attrs,
157 user_quota,
158 static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
159 dump_errno(s);
160 end_header(s, NULL, NULL, NO_CONTENT_LENGTH, true);
161 }
162
163 if (! op_ret) {
164 dump_start(s);
165 s->formatter->open_array_section_with_attrs("account",
166 FormatterAttrs("name", s->user->display_name.c_str(), NULL));
167
168 sent_data = true;
169 }
170 }
171
172 void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets& buckets)
173 {
174 if (! sent_data) {
175 return;
176 }
177
178 /* Take care of the prefix parameter of Swift API. There is no business
179 * in applying the filter earlier as we really need to go through all
180 * entries regardless of it (the headers like X-Account-Container-Count
181 * aren't affected by specifying prefix). */
182 const std::map<std::string, RGWBucketEnt>& m = buckets.get_buckets();
183 for (auto iter = m.lower_bound(prefix);
184 iter != m.end() && boost::algorithm::starts_with(iter->first, prefix);
185 ++iter) {
186 const RGWBucketEnt& obj = iter->second;
187
188 s->formatter->open_object_section("container");
189 s->formatter->dump_string("name", obj.bucket.name);
190 if (need_stats) {
191 s->formatter->dump_int("count", obj.count);
192 s->formatter->dump_int("bytes", obj.size);
193 }
194 s->formatter->close_section();
195 if (! s->cct->_conf->rgw_swift_enforce_content_length) {
196 rgw_flush_formatter(s, s->formatter);
197 }
198 }
199 }
200
201 void RGWListBuckets_ObjStore_SWIFT::send_response_end()
202 {
203 if (sent_data) {
204 s->formatter->close_section();
205 }
206
207 if (s->cct->_conf->rgw_swift_enforce_content_length) {
208 /* Adding account stats in the header to keep align with Swift API */
209 dump_account_metadata(s,
210 buckets_count,
211 buckets_objcount,
212 buckets_size,
213 buckets_size_rounded,
214 attrs,
215 user_quota,
216 static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
217 dump_errno(s);
218 end_header(s, NULL, NULL, s->formatter->get_len(), true);
219 }
220
221 if (sent_data || s->cct->_conf->rgw_swift_enforce_content_length) {
222 rgw_flush_formatter_and_reset(s, s->formatter);
223 }
224 }
225
226 int RGWListBucket_ObjStore_SWIFT::get_params()
227 {
228 prefix = s->info.args.get("prefix");
229 marker = s->info.args.get("marker");
230 end_marker = s->info.args.get("end_marker");
231 max_keys = s->info.args.get("limit");
232 op_ret = parse_max_keys();
233 if (op_ret < 0) {
234 return op_ret;
235 }
236 if (max > default_max)
237 return -ERR_PRECONDITION_FAILED;
238
239 delimiter = s->info.args.get("delimiter");
240
241 string path_args;
242 if (s->info.args.exists("path")) { // should handle empty path
243 path_args = s->info.args.get("path");
244 if (!delimiter.empty() || !prefix.empty()) {
245 return -EINVAL;
246 }
247 prefix = path_args;
248 delimiter="/";
249
250 path = prefix;
251 if (path.size() && path[path.size() - 1] != '/')
252 path.append("/");
253
254 int len = prefix.size();
255 int delim_size = delimiter.size();
256
257 if (len >= delim_size) {
258 if (prefix.substr(len - delim_size).compare(delimiter) != 0)
259 prefix.append(delimiter);
260 }
261 }
262
263 return 0;
264 }
265
266 static void dump_container_metadata(struct req_state *,
267 const RGWBucketEnt&,
268 const RGWQuotaInfo&,
269 const RGWBucketWebsiteConf&);
270
271 void RGWListBucket_ObjStore_SWIFT::send_response()
272 {
273 vector<rgw_bucket_dir_entry>::iterator iter = objs.begin();
274 map<string, bool>::iterator pref_iter = common_prefixes.begin();
275
276 dump_start(s);
277 dump_container_metadata(s, bucket, bucket_quota,
278 s->bucket_info.website_conf);
279
280 s->formatter->open_array_section_with_attrs("container", FormatterAttrs("name", s->bucket.name.c_str(), NULL));
281
282 while (iter != objs.end() || pref_iter != common_prefixes.end()) {
283 bool do_pref = false;
284 bool do_objs = false;
285 rgw_obj_key key;
286 if (iter != objs.end()) {
287 key = iter->key;
288 }
289 if (pref_iter == common_prefixes.end())
290 do_objs = true;
291 else if (iter == objs.end())
292 do_pref = true;
293 else if (!key.empty() && key.name.compare(pref_iter->first) == 0) {
294 do_objs = true;
295 ++pref_iter;
296 } else if (!key.empty() && key.name.compare(pref_iter->first) <= 0)
297 do_objs = true;
298 else
299 do_pref = true;
300
301 if (do_objs && (marker.empty() || marker < key)) {
302 if (key.name.compare(path) == 0)
303 goto next;
304
305 s->formatter->open_object_section("object");
306 s->formatter->dump_string("name", key.name);
307 s->formatter->dump_string("hash", iter->meta.etag);
308 s->formatter->dump_int("bytes", iter->meta.accounted_size);
309 if (!iter->meta.user_data.empty())
310 s->formatter->dump_string("user_custom_data", iter->meta.user_data);
311 string single_content_type = iter->meta.content_type;
312 if (iter->meta.content_type.size()) {
313 // content type might hold multiple values, just dump the last one
314 ssize_t pos = iter->meta.content_type.rfind(',');
315 if (pos > 0) {
316 ++pos;
317 while (single_content_type[pos] == ' ')
318 ++pos;
319 single_content_type = single_content_type.substr(pos);
320 }
321 s->formatter->dump_string("content_type", single_content_type);
322 }
323 dump_time(s, "last_modified", &iter->meta.mtime);
324 s->formatter->close_section();
325 }
326
327 if (do_pref && (marker.empty() || pref_iter->first.compare(marker.name) > 0)) {
328 const string& name = pref_iter->first;
329 if (name.compare(delimiter) == 0)
330 goto next;
331
332 s->formatter->open_object_section_with_attrs("subdir", FormatterAttrs("name", name.c_str(), NULL));
333
334 /* swift is a bit inconsistent here */
335 switch (s->format) {
336 case RGW_FORMAT_XML:
337 s->formatter->dump_string("name", name);
338 break;
339 default:
340 s->formatter->dump_string("subdir", name);
341 }
342 s->formatter->close_section();
343 }
344 next:
345 if (do_objs)
346 ++iter;
347 else
348 ++pref_iter;
349 }
350
351 s->formatter->close_section();
352
353 int64_t content_len = 0;
354 if (! op_ret) {
355 content_len = s->formatter->get_len();
356 if (content_len == 0) {
357 op_ret = STATUS_NO_CONTENT;
358 }
359 } else if (op_ret > 0) {
360 op_ret = 0;
361 }
362
363 set_req_state_err(s, op_ret);
364 dump_errno(s);
365 end_header(s, this, NULL, content_len);
366 if (op_ret < 0) {
367 return;
368 }
369
370 rgw_flush_formatter_and_reset(s, s->formatter);
371 }
372
373 static void dump_container_metadata(struct req_state *s,
374 const RGWBucketEnt& bucket,
375 const RGWQuotaInfo& quota,
376 const RGWBucketWebsiteConf& ws_conf)
377 {
378 /* Adding X-Timestamp to keep align with Swift API */
379 dump_header(s, "X-Timestamp", utime_t(s->bucket_info.creation_time));
380
381 dump_header(s, "X-Container-Object-Count", bucket.count);
382 dump_header(s, "X-Container-Bytes-Used", bucket.size);
383 dump_header(s, "X-Container-Bytes-Used-Actual", bucket.size_rounded);
384
385 if (s->object.empty()) {
386 auto swift_policy = static_cast<RGWAccessControlPolicy_SWIFT*>(s->bucket_acl);
387 std::string read_acl, write_acl;
388 swift_policy->to_str(read_acl, write_acl);
389
390 if (read_acl.size()) {
391 dump_header(s, "X-Container-Read", read_acl);
392 }
393 if (write_acl.size()) {
394 dump_header(s, "X-Container-Write", write_acl);
395 }
396 if (!s->bucket_info.placement_rule.empty()) {
397 dump_header(s, "X-Storage-Policy", s->bucket_info.placement_rule);
398 }
399
400 /* Dump user-defined metadata items and generic attrs. */
401 const size_t PREFIX_LEN = sizeof(RGW_ATTR_META_PREFIX) - 1;
402 map<string, bufferlist>::iterator iter;
403 for (iter = s->bucket_attrs.lower_bound(RGW_ATTR_PREFIX);
404 iter != s->bucket_attrs.end();
405 ++iter) {
406 const char *name = iter->first.c_str();
407 map<string, string>::const_iterator geniter = rgw_to_http_attrs.find(name);
408
409 if (geniter != rgw_to_http_attrs.end()) {
410 dump_header(s, geniter->second, iter->second);
411 } else if (strncmp(name, RGW_ATTR_META_PREFIX, PREFIX_LEN) == 0) {
412 dump_header_prefixed(s, "X-Container-Meta-",
413 camelcase_dash_http_attr(name + PREFIX_LEN),
414 iter->second);
415 }
416 }
417 }
418
419 /* Dump container versioning info. */
420 if (! s->bucket_info.swift_ver_location.empty()) {
421 dump_header(s, "X-Versions-Location",
422 url_encode(s->bucket_info.swift_ver_location));
423 }
424
425 /* Dump quota headers. */
426 if (quota.enabled) {
427 if (quota.max_size >= 0) {
428 dump_header(s, "X-Container-Meta-Quota-Bytes", quota.max_size);
429 }
430
431 if (quota.max_objects >= 0) {
432 dump_header(s, "X-Container-Meta-Quota-Count", quota.max_objects);
433 }
434 }
435
436 /* Dump Static Website headers. */
437 if (! ws_conf.index_doc_suffix.empty()) {
438 dump_header(s, "X-Container-Meta-Web-Index", ws_conf.index_doc_suffix);
439 }
440
441 if (! ws_conf.error_doc.empty()) {
442 dump_header(s, "X-Container-Meta-Web-Error", ws_conf.error_doc);
443 }
444
445 if (! ws_conf.subdir_marker.empty()) {
446 dump_header(s, "X-Container-Meta-Web-Directory-Type",
447 ws_conf.subdir_marker);
448 }
449
450 if (! ws_conf.listing_css_doc.empty()) {
451 dump_header(s, "X-Container-Meta-Web-Listings-CSS",
452 ws_conf.listing_css_doc);
453 }
454
455 if (ws_conf.listing_enabled) {
456 dump_header(s, "X-Container-Meta-Web-Listings", "true");
457 }
458 }
459
460 void RGWStatAccount_ObjStore_SWIFT::execute()
461 {
462 RGWStatAccount_ObjStore::execute();
463 op_ret = rgw_get_user_attrs_by_uid(store, s->user->user_id, attrs);
464 }
465
466 void RGWStatAccount_ObjStore_SWIFT::send_response()
467 {
468 if (op_ret >= 0) {
469 op_ret = STATUS_NO_CONTENT;
470 dump_account_metadata(s,
471 buckets_count,
472 buckets_objcount,
473 buckets_size,
474 buckets_size_rounded,
475 attrs,
476 user_quota,
477 static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
478 }
479
480 set_req_state_err(s, op_ret);
481 dump_errno(s);
482
483 end_header(s, NULL, NULL, 0, true);
484
485 dump_start(s);
486 }
487
488 void RGWStatBucket_ObjStore_SWIFT::send_response()
489 {
490 if (op_ret >= 0) {
491 op_ret = STATUS_NO_CONTENT;
492 dump_container_metadata(s, bucket, bucket_quota,
493 s->bucket_info.website_conf);
494 }
495
496 set_req_state_err(s, op_ret);
497 dump_errno(s);
498
499 end_header(s, this, NULL, 0, true);
500 dump_start(s);
501 }
502
503 static int get_swift_container_settings(req_state * const s,
504 RGWRados * const store,
505 RGWAccessControlPolicy * const policy,
506 bool * const has_policy,
507 uint32_t * rw_mask,
508 RGWCORSConfiguration * const cors_config,
509 bool * const has_cors)
510 {
511 string read_list, write_list;
512
513 const char * const read_attr = s->info.env->get("HTTP_X_CONTAINER_READ");
514 if (read_attr) {
515 read_list = read_attr;
516 }
517 const char * const write_attr = s->info.env->get("HTTP_X_CONTAINER_WRITE");
518 if (write_attr) {
519 write_list = write_attr;
520 }
521
522 *has_policy = false;
523
524 if (read_attr || write_attr) {
525 RGWAccessControlPolicy_SWIFT swift_policy(s->cct);
526 const auto r = swift_policy.create(store,
527 s->user->user_id,
528 s->user->display_name,
529 read_list,
530 write_list,
531 *rw_mask);
532 if (r < 0) {
533 return r;
534 }
535
536 *policy = swift_policy;
537 *has_policy = true;
538 }
539
540 *has_cors = false;
541
542 /*Check and update CORS configuration*/
543 const char *allow_origins = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
544 const char *allow_headers = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
545 const char *expose_headers = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
546 const char *max_age = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
547 if (allow_origins) {
548 RGWCORSConfiguration_SWIFT *swift_cors = new RGWCORSConfiguration_SWIFT;
549 int r = swift_cors->create_update(allow_origins, allow_headers, expose_headers, max_age);
550 if (r < 0) {
551 dout(0) << "Error creating/updating the cors configuration" << dendl;
552 delete swift_cors;
553 return r;
554 }
555 *has_cors = true;
556 *cors_config = *swift_cors;
557 cors_config->dump();
558 delete swift_cors;
559 }
560
561 return 0;
562 }
563
564 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
565 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
566 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
567 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
568
569 static void get_rmattrs_from_headers(const req_state * const s,
570 const char * const put_prefix,
571 const char * const del_prefix,
572 set<string>& rmattr_names)
573 {
574 map<string, string, ltstr_nocase>& m = s->info.env->get_map();
575 map<string, string, ltstr_nocase>::const_iterator iter;
576 const size_t put_prefix_len = strlen(put_prefix);
577 const size_t del_prefix_len = strlen(del_prefix);
578
579 for (iter = m.begin(); iter != m.end(); ++iter) {
580 size_t prefix_len = 0;
581 const char * const p = iter->first.c_str();
582
583 if (strncasecmp(p, del_prefix, del_prefix_len) == 0) {
584 /* Explicitly requested removal. */
585 prefix_len = del_prefix_len;
586 } else if ((strncasecmp(p, put_prefix, put_prefix_len) == 0)
587 && iter->second.empty()) {
588 /* Removal requested by putting an empty value. */
589 prefix_len = put_prefix_len;
590 }
591
592 if (prefix_len > 0) {
593 string name(RGW_ATTR_META_PREFIX);
594 name.append(lowercase_dash_http_attr(p + prefix_len));
595 rmattr_names.insert(name);
596 }
597 }
598 }
599
600 static int get_swift_versioning_settings(
601 req_state * const s,
602 boost::optional<std::string>& swift_ver_location)
603 {
604 /* Removing the Swift's versions location has lower priority than setting
605 * a new one. That's the reason why we're handling it first. */
606 const std::string vlocdel =
607 s->info.env->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
608 if (vlocdel.size()) {
609 swift_ver_location = boost::in_place(std::string());
610 }
611
612 if (s->info.env->exists("HTTP_X_VERSIONS_LOCATION")) {
613 /* If the Swift's versioning is globally disabled but someone wants to
614 * enable it for a given container, new version of Swift will generate
615 * the precondition failed error. */
616 if (! s->cct->_conf->rgw_swift_versioning_enabled) {
617 return -ERR_PRECONDITION_FAILED;
618 }
619
620 swift_ver_location = s->info.env->get("HTTP_X_VERSIONS_LOCATION", "");
621 }
622
623 return 0;
624 }
625
626 int RGWCreateBucket_ObjStore_SWIFT::get_params()
627 {
628 bool has_policy;
629 uint32_t policy_rw_mask = 0;
630
631 int r = get_swift_container_settings(s, store, &policy, &has_policy,
632 &policy_rw_mask, &cors_config, &has_cors);
633 if (r < 0) {
634 return r;
635 }
636
637 if (!has_policy) {
638 policy.create_default(s->user->user_id, s->user->display_name);
639 }
640
641 location_constraint = store->get_zonegroup().api_name;
642 get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX,
643 CONT_REMOVE_ATTR_PREFIX, rmattr_names);
644 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
645
646 return get_swift_versioning_settings(s, swift_ver_location);
647 }
648
649 void RGWCreateBucket_ObjStore_SWIFT::send_response()
650 {
651 if (! op_ret)
652 op_ret = STATUS_CREATED;
653 else if (op_ret == -ERR_BUCKET_EXISTS)
654 op_ret = STATUS_ACCEPTED;
655 set_req_state_err(s, op_ret);
656 dump_errno(s);
657 /* Propose ending HTTP header with 0 Content-Length header. */
658 end_header(s, NULL, NULL, 0);
659 rgw_flush_formatter_and_reset(s, s->formatter);
660 }
661
662 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
663 {
664 int r = op_ret;
665 if (!r)
666 r = STATUS_NO_CONTENT;
667
668 set_req_state_err(s, r);
669 dump_errno(s);
670 end_header(s, this, NULL, 0);
671 rgw_flush_formatter_and_reset(s, s->formatter);
672 }
673
674 static int get_delete_at_param(req_state *s, boost::optional<real_time> &delete_at)
675 {
676 /* Handle Swift object expiration. */
677 real_time delat_proposal;
678 string x_delete = s->info.env->get("HTTP_X_DELETE_AFTER", "");
679
680 if (x_delete.empty()) {
681 x_delete = s->info.env->get("HTTP_X_DELETE_AT", "");
682 } else {
683 /* X-Delete-After HTTP is present. It means we need add its value
684 * to the current time. */
685 delat_proposal = real_clock::now();
686 }
687
688 if (x_delete.empty()) {
689 delete_at = boost::none;
690 if (s->info.env->exists("HTTP_X_REMOVE_DELETE_AT")) {
691 delete_at = boost::in_place(real_time());
692 }
693 return 0;
694 }
695 string err;
696 long ts = strict_strtoll(x_delete.c_str(), 10, &err);
697
698 if (!err.empty()) {
699 return -EINVAL;
700 }
701
702 delat_proposal += make_timespan(ts);
703 if (delat_proposal < real_clock::now()) {
704 return -EINVAL;
705 }
706
707 delete_at = delat_proposal;
708
709 return 0;
710 }
711
712 int RGWPutObj_ObjStore_SWIFT::verify_permission()
713 {
714 op_ret = RGWPutObj_ObjStore::verify_permission();
715
716 /* We have to differentiate error codes depending on whether user is
717 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
718 * (403 Forbidden). */
719 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
720 return -EPERM;
721 } else {
722 return op_ret;
723 }
724 }
725
726 int RGWPutObj_ObjStore_SWIFT::get_params()
727 {
728 if (s->has_bad_meta) {
729 return -EINVAL;
730 }
731
732 if (!s->length) {
733 const char *encoding = s->info.env->get("HTTP_TRANSFER_ENCODING");
734 if (!encoding || strcmp(encoding, "chunked") != 0) {
735 ldout(s->cct, 20) << "neither length nor chunked encoding" << dendl;
736 return -ERR_LENGTH_REQUIRED;
737 }
738
739 chunked_upload = true;
740 }
741
742 supplied_etag = s->info.env->get("HTTP_ETAG");
743
744 if (!s->generic_attrs.count(RGW_ATTR_CONTENT_TYPE)) {
745 ldout(s->cct, 5) << "content type wasn't provided, trying to guess" << dendl;
746 const char *suffix = strrchr(s->object.name.c_str(), '.');
747 if (suffix) {
748 suffix++;
749 if (*suffix) {
750 string suffix_str(suffix);
751 const char *mime = rgw_find_mime_by_ext(suffix_str);
752 if (mime) {
753 s->generic_attrs[RGW_ATTR_CONTENT_TYPE] = mime;
754 }
755 }
756 }
757 }
758
759 policy.create_default(s->user->user_id, s->user->display_name);
760
761 int r = get_delete_at_param(s, delete_at);
762 if (r < 0) {
763 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
764 return r;
765 }
766
767 if (!s->cct->_conf->rgw_swift_custom_header.empty()) {
768 string custom_header = s->cct->_conf->rgw_swift_custom_header;
769 if (s->info.env->exists(custom_header.c_str())) {
770 user_data = s->info.env->get(custom_header.c_str());
771 }
772 }
773
774 dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
775 bool exists;
776 string multipart_manifest = s->info.args.get("multipart-manifest", &exists);
777 if (exists) {
778 if (multipart_manifest != "put") {
779 ldout(s->cct, 5) << "invalid multipart-manifest http param: " << multipart_manifest << dendl;
780 return -EINVAL;
781 }
782
783 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
784 uint64_t max_len = s->cct->_conf->rgw_max_slo_entries * MAX_SLO_ENTRY_SIZE;
785
786 slo_info = new RGWSLOInfo;
787
788 int r = rgw_rest_get_json_input_keep_data(s->cct, s, slo_info->entries, max_len, &slo_info->raw_data, &slo_info->raw_data_len);
789 if (r < 0) {
790 ldout(s->cct, 5) << "failed to read input for slo r=" << r << dendl;
791 return r;
792 }
793
794 if ((int64_t)slo_info->entries.size() > s->cct->_conf->rgw_max_slo_entries) {
795 ldout(s->cct, 5) << "too many entries in slo request: " << slo_info->entries.size() << dendl;
796 return -EINVAL;
797 }
798
799 MD5 etag_sum;
800 uint64_t total_size = 0;
801 for (const auto& entry : slo_info->entries) {
802 etag_sum.Update((const byte *)entry.etag.c_str(),
803 entry.etag.length());
804 total_size += entry.size_bytes;
805
806 ldout(s->cct, 20) << "slo_part: " << entry.path
807 << " size=" << entry.size_bytes
808 << " etag=" << entry.etag
809 << dendl;
810 }
811 complete_etag(etag_sum, &lo_etag);
812 slo_info->total_size = total_size;
813
814 ofs = slo_info->raw_data_len;
815 }
816
817 return RGWPutObj_ObjStore::get_params();
818 }
819
820 void RGWPutObj_ObjStore_SWIFT::send_response()
821 {
822 if (! op_ret) {
823 op_ret = STATUS_CREATED;
824 }
825
826 if (! lo_etag.empty()) {
827 /* Static Large Object of Swift API has two etags represented by
828 * following members:
829 * - etag - for the manifest itself (it will be stored in xattrs),
830 * - lo_etag - for the content composited from SLO's segments.
831 * The value is calculated basing on segments' etags.
832 * In response for PUT request we have to expose the second one.
833 * The first one may be obtained by GET with "multipart-manifest=get"
834 * in query string on a given SLO. */
835 dump_etag(s, lo_etag, true /* quoted */);
836 } else {
837 dump_etag(s, etag);
838 }
839
840 dump_last_modified(s, mtime);
841 set_req_state_err(s, op_ret);
842 dump_errno(s);
843 end_header(s, this);
844 rgw_flush_formatter_and_reset(s, s->formatter);
845 }
846
847 static int get_swift_account_settings(req_state * const s,
848 RGWRados * const store,
849 RGWAccessControlPolicy_SWIFTAcct * const policy,
850 bool * const has_policy)
851 {
852 *has_policy = false;
853
854 const char * const acl_attr = s->info.env->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
855 if (acl_attr) {
856 RGWAccessControlPolicy_SWIFTAcct swift_acct_policy(s->cct);
857 const bool r = swift_acct_policy.create(store,
858 s->user->user_id,
859 s->user->display_name,
860 string(acl_attr));
861 if (r != true) {
862 return -EINVAL;
863 }
864
865 *policy = swift_acct_policy;
866 *has_policy = true;
867 }
868
869 return 0;
870 }
871
872 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
873 {
874 if (s->has_bad_meta) {
875 return -EINVAL;
876 }
877
878 int ret = get_swift_account_settings(s,
879 store,
880 // FIXME: we need to carry unique_ptr in generic class
881 // and allocate appropriate ACL class in the ctor
882 static_cast<RGWAccessControlPolicy_SWIFTAcct *>(&policy),
883 &has_policy);
884 if (ret < 0) {
885 return ret;
886 }
887
888 get_rmattrs_from_headers(s, ACCT_PUT_ATTR_PREFIX, ACCT_REMOVE_ATTR_PREFIX,
889 rmattr_names);
890 return 0;
891 }
892
893 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
894 {
895 if (! op_ret) {
896 op_ret = STATUS_NO_CONTENT;
897 }
898 set_req_state_err(s, op_ret);
899 dump_errno(s);
900 end_header(s, this);
901 rgw_flush_formatter_and_reset(s, s->formatter);
902 }
903
904 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
905 {
906 if (s->has_bad_meta) {
907 return -EINVAL;
908 }
909
910 int r = get_swift_container_settings(s, store, &policy, &has_policy,
911 &policy_rw_mask, &cors_config, &has_cors);
912 if (r < 0) {
913 return r;
914 }
915
916 get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX, CONT_REMOVE_ATTR_PREFIX,
917 rmattr_names);
918 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
919
920 return get_swift_versioning_settings(s, swift_ver_location);
921 }
922
923 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
924 {
925 if (!op_ret && (op_ret != -EINVAL)) {
926 op_ret = STATUS_NO_CONTENT;
927 }
928 set_req_state_err(s, op_ret);
929 dump_errno(s);
930 end_header(s, this);
931 rgw_flush_formatter_and_reset(s, s->formatter);
932 }
933
934 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
935 {
936 if (s->has_bad_meta) {
937 return -EINVAL;
938 }
939
940 /* Handle Swift object expiration. */
941 int r = get_delete_at_param(s, delete_at);
942 if (r < 0) {
943 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
944 return r;
945 }
946
947 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
948 dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
949
950 return 0;
951 }
952
953 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
954 {
955 if (! op_ret) {
956 op_ret = STATUS_ACCEPTED;
957 }
958 set_req_state_err(s, op_ret);
959 if (!s->err.is_err()) {
960 dump_content_length(s, 0);
961 }
962 dump_errno(s);
963 end_header(s, this);
964 rgw_flush_formatter_and_reset(s, s->formatter);
965 }
966
967 static void bulkdelete_respond(const unsigned num_deleted,
968 const unsigned int num_unfound,
969 const std::list<RGWBulkDelete::fail_desc_t>& failures,
970 const int prot_flags, /* in */
971 ceph::Formatter& formatter) /* out */
972 {
973 formatter.open_object_section("delete");
974
975 string resp_status;
976 string resp_body;
977
978 if (!failures.empty()) {
979 int reason = ERR_INVALID_REQUEST;
980 for (const auto fail_desc : failures) {
981 if (-ENOENT != fail_desc.err && -EACCES != fail_desc.err) {
982 reason = fail_desc.err;
983 }
984 }
985
986 rgw_err err;
987 set_req_state_err(err, reason, prot_flags);
988 dump_errno(err, resp_status);
989 } else if (0 == num_deleted && 0 == num_unfound) {
990 /* 400 Bad Request */
991 dump_errno(400, resp_status);
992 resp_body = "Invalid bulk delete.";
993 } else {
994 /* 200 OK */
995 dump_errno(200, resp_status);
996 }
997
998 encode_json("Number Deleted", num_deleted, &formatter);
999 encode_json("Number Not Found", num_unfound, &formatter);
1000 encode_json("Response Body", resp_body, &formatter);
1001 encode_json("Response Status", resp_status, &formatter);
1002
1003 formatter.open_array_section("Errors");
1004 for (const auto fail_desc : failures) {
1005 formatter.open_array_section("object");
1006
1007 stringstream ss_name;
1008 ss_name << fail_desc.path;
1009 encode_json("Name", ss_name.str(), &formatter);
1010
1011 rgw_err err;
1012 set_req_state_err(err, fail_desc.err, prot_flags);
1013 string status;
1014 dump_errno(err, status);
1015 encode_json("Status", status, &formatter);
1016 formatter.close_section();
1017 }
1018 formatter.close_section();
1019
1020 formatter.close_section();
1021 }
1022
1023 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1024 {
1025 op_ret = RGWDeleteObj_ObjStore::verify_permission();
1026
1027 /* We have to differentiate error codes depending on whether user is
1028 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1029 * (403 Forbidden). */
1030 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
1031 return -EPERM;
1032 } else {
1033 return op_ret;
1034 }
1035 }
1036
1037 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1038 {
1039 const string& mm = s->info.args.get("multipart-manifest");
1040 multipart_delete = (mm.compare("delete") == 0);
1041
1042 return RGWDeleteObj_ObjStore::get_params();
1043 }
1044
1045 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1046 {
1047 int r = op_ret;
1048
1049 if (multipart_delete) {
1050 r = 0;
1051 } else if(!r) {
1052 r = STATUS_NO_CONTENT;
1053 }
1054
1055 set_req_state_err(s, r);
1056 dump_errno(s);
1057
1058 if (multipart_delete) {
1059 end_header(s, this /* RGWOp */, nullptr /* contype */,
1060 CHUNKED_TRANSFER_ENCODING);
1061
1062 if (deleter) {
1063 bulkdelete_respond(deleter->get_num_deleted(),
1064 deleter->get_num_unfound(),
1065 deleter->get_failures(),
1066 s->prot_flags,
1067 *s->formatter);
1068 } else if (-ENOENT == op_ret) {
1069 bulkdelete_respond(0, 1, {}, s->prot_flags, *s->formatter);
1070 } else {
1071 RGWBulkDelete::acct_path_t path;
1072 path.bucket_name = s->bucket_name;
1073 path.obj_key = s->object;
1074
1075 RGWBulkDelete::fail_desc_t fail_desc;
1076 fail_desc.err = op_ret;
1077 fail_desc.path = path;
1078
1079 bulkdelete_respond(0, 0, { fail_desc }, s->prot_flags, *s->formatter);
1080 }
1081 } else {
1082 end_header(s, this);
1083 }
1084
1085 rgw_flush_formatter_and_reset(s, s->formatter);
1086
1087 }
1088
1089 static void get_contype_from_attrs(map<string, bufferlist>& attrs,
1090 string& content_type)
1091 {
1092 map<string, bufferlist>::iterator iter = attrs.find(RGW_ATTR_CONTENT_TYPE);
1093 if (iter != attrs.end()) {
1094 content_type = iter->second.c_str();
1095 }
1096 }
1097
1098 static void dump_object_metadata(struct req_state * const s,
1099 map<string, bufferlist> attrs)
1100 {
1101 map<string, string> response_attrs;
1102
1103 for (auto kv : attrs) {
1104 const char * name = kv.first.c_str();
1105 const auto aiter = rgw_to_http_attrs.find(name);
1106
1107 if (aiter != std::end(rgw_to_http_attrs)) {
1108 response_attrs[aiter->second] = kv.second.c_str();
1109 } else if (strncmp(name, RGW_ATTR_META_PREFIX,
1110 sizeof(RGW_ATTR_META_PREFIX)-1) == 0) {
1111 name += sizeof(RGW_ATTR_META_PREFIX) - 1;
1112 dump_header_prefixed(s, "X-Object-Meta-",
1113 camelcase_dash_http_attr(name), kv.second);
1114 }
1115 }
1116
1117 /* Handle override and fallback for Content-Disposition HTTP header.
1118 * At the moment this will be used only by TempURL of the Swift API. */
1119 const auto cditer = rgw_to_http_attrs.find(RGW_ATTR_CONTENT_DISP);
1120 if (cditer != std::end(rgw_to_http_attrs)) {
1121 const auto& name = cditer->second;
1122
1123 if (!s->content_disp.override.empty()) {
1124 response_attrs[name] = s->content_disp.override;
1125 } else if (!s->content_disp.fallback.empty()
1126 && response_attrs.find(name) == std::end(response_attrs)) {
1127 response_attrs[name] = s->content_disp.fallback;
1128 }
1129 }
1130
1131 for (const auto kv : response_attrs) {
1132 dump_header(s, kv.first, kv.second);
1133 }
1134
1135 const auto iter = attrs.find(RGW_ATTR_DELETE_AT);
1136 if (iter != std::end(attrs)) {
1137 utime_t delete_at;
1138 try {
1139 ::decode(delete_at, iter->second);
1140 if (!delete_at.is_zero()) {
1141 dump_header(s, "X-Delete-At", delete_at.sec());
1142 }
1143 } catch (buffer::error& err) {
1144 ldout(s->cct, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1145 " attr, ignoring"
1146 << dendl;
1147 }
1148 }
1149 }
1150
1151 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1152 {
1153 dest_policy.create_default(s->user->user_id, s->user->display_name);
1154
1155 return 0;
1156 }
1157
1158 int RGWCopyObj_ObjStore_SWIFT::get_params()
1159 {
1160 if_mod = s->info.env->get("HTTP_IF_MODIFIED_SINCE");
1161 if_unmod = s->info.env->get("HTTP_IF_UNMODIFIED_SINCE");
1162 if_match = s->info.env->get("HTTP_COPY_IF_MATCH");
1163 if_nomatch = s->info.env->get("HTTP_COPY_IF_NONE_MATCH");
1164
1165 src_tenant_name = s->src_tenant_name;
1166 src_bucket_name = s->src_bucket_name;
1167 src_object = s->src_object;
1168 dest_tenant_name = s->bucket_tenant;
1169 dest_bucket_name = s->bucket_name;
1170 dest_object = s->object.name;
1171
1172 const char * const fresh_meta = s->info.env->get("HTTP_X_FRESH_METADATA");
1173 if (fresh_meta && strcasecmp(fresh_meta, "TRUE") == 0) {
1174 attrs_mod = RGWRados::ATTRSMOD_REPLACE;
1175 } else {
1176 attrs_mod = RGWRados::ATTRSMOD_MERGE;
1177 }
1178
1179 int r = get_delete_at_param(s, delete_at);
1180 if (r < 0) {
1181 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
1182 return r;
1183 }
1184
1185 return 0;
1186 }
1187
1188 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs)
1189 {
1190 if (! sent_header) {
1191 if (! op_ret)
1192 op_ret = STATUS_CREATED;
1193 set_req_state_err(s, op_ret);
1194 dump_errno(s);
1195 end_header(s, this);
1196
1197 /* Send progress information. Note that this diverge from the original swift
1198 * spec. We do this in order to keep connection alive.
1199 */
1200 if (op_ret == 0) {
1201 s->formatter->open_array_section("progress");
1202 }
1203 sent_header = true;
1204 } else {
1205 s->formatter->dump_int("ofs", (uint64_t)ofs);
1206 }
1207 rgw_flush_formatter(s, s->formatter);
1208 }
1209
1210 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1211 {
1212 /* Dump X-Copied-From. */
1213 dump_header(s, "X-Copied-From", url_encode(src_bucket.name) +
1214 "/" + url_encode(src_object.name));
1215
1216 /* Dump X-Copied-From-Account. */
1217 /* XXX tenant */
1218 dump_header(s, "X-Copied-From-Account", url_encode(s->user->user_id.id));
1219
1220 /* Dump X-Copied-From-Last-Modified. */
1221 dump_time_header(s, "X-Copied-From-Last-Modified", src_mtime);
1222 }
1223
1224 void RGWCopyObj_ObjStore_SWIFT::send_response()
1225 {
1226 if (! sent_header) {
1227 string content_type;
1228 if (! op_ret)
1229 op_ret = STATUS_CREATED;
1230 set_req_state_err(s, op_ret);
1231 dump_errno(s);
1232 dump_etag(s, etag);
1233 dump_last_modified(s, mtime);
1234 dump_copy_info();
1235 get_contype_from_attrs(attrs, content_type);
1236 dump_object_metadata(s, attrs);
1237 end_header(s, this, !content_type.empty() ? content_type.c_str()
1238 : "binary/octet-stream");
1239 } else {
1240 s->formatter->close_section();
1241 rgw_flush_formatter(s, s->formatter);
1242 }
1243 }
1244
1245 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1246 {
1247 op_ret = RGWGetObj_ObjStore::verify_permission();
1248
1249 /* We have to differentiate error codes depending on whether user is
1250 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1251 * (403 Forbidden). */
1252 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
1253 return -EPERM;
1254 } else {
1255 return op_ret;
1256 }
1257 }
1258
1259 int RGWGetObj_ObjStore_SWIFT::get_params()
1260 {
1261 const string& mm = s->info.args.get("multipart-manifest");
1262 skip_manifest = (mm.compare("get") == 0);
1263
1264 return RGWGetObj_ObjStore::get_params();
1265 }
1266
1267 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1268 {
1269 std::string error_content;
1270 op_ret = error_handler(op_ret, &error_content);
1271 if (! op_ret) {
1272 /* The error handler has taken care of the error. */
1273 return 0;
1274 }
1275
1276 bufferlist error_bl;
1277 error_bl.append(error_content);
1278 return send_response_data(error_bl, 0, error_bl.length());
1279 }
1280
1281 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist& bl,
1282 const off_t bl_ofs,
1283 const off_t bl_len)
1284 {
1285 string content_type;
1286
1287 if (sent_header) {
1288 goto send_data;
1289 }
1290
1291 if (custom_http_ret) {
1292 set_req_state_err(s, 0);
1293 dump_errno(s, custom_http_ret);
1294 } else {
1295 set_req_state_err(s, (partial_content && !op_ret) ? STATUS_PARTIAL_CONTENT
1296 : op_ret);
1297 dump_errno(s);
1298
1299 if (s->err.is_err()) {
1300 end_header(s, NULL);
1301 return 0;
1302 }
1303 }
1304
1305 if (range_str) {
1306 dump_range(s, ofs, end, s->obj_size);
1307 }
1308
1309 if (s->err.is_err()) {
1310 end_header(s, NULL);
1311 return 0;
1312 }
1313
1314 dump_content_length(s, total_len);
1315 dump_last_modified(s, lastmod);
1316 dump_header(s, "X-Timestamp", utime_t(lastmod));
1317 if (is_slo) {
1318 dump_header(s, "X-Static-Large-Object", "True");
1319 }
1320
1321 if (! op_ret) {
1322 if (! lo_etag.empty()) {
1323 dump_etag(s, lo_etag, true /* quoted */);
1324 } else {
1325 auto iter = attrs.find(RGW_ATTR_ETAG);
1326 if (iter != attrs.end()) {
1327 dump_etag(s, iter->second);
1328 }
1329 }
1330
1331 get_contype_from_attrs(attrs, content_type);
1332 dump_object_metadata(s, attrs);
1333 }
1334
1335 end_header(s, this, !content_type.empty() ? content_type.c_str()
1336 : "binary/octet-stream");
1337
1338 sent_header = true;
1339
1340 send_data:
1341 if (get_data && !op_ret) {
1342 const auto r = dump_body(s, bl.c_str() + bl_ofs, bl_len);
1343 if (r < 0) {
1344 return r;
1345 }
1346 }
1347 rgw_flush_formatter_and_reset(s, s->formatter);
1348
1349 return 0;
1350 }
1351
1352 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1353 {
1354 string hdrs, exp_hdrs;
1355 uint32_t max_age = CORS_MAX_AGE_INVALID;
1356 /*EACCES means, there is no CORS registered yet for the bucket
1357 *ENOENT means, there is no match of the Origin in the list of CORSRule
1358 */
1359 if (op_ret == -ENOENT)
1360 op_ret = -EACCES;
1361 if (op_ret < 0) {
1362 set_req_state_err(s, op_ret);
1363 dump_errno(s);
1364 end_header(s, NULL);
1365 return;
1366 }
1367 get_response_params(hdrs, exp_hdrs, &max_age);
1368 dump_errno(s);
1369 dump_access_control(s, origin, req_meth, hdrs.c_str(), exp_hdrs.c_str(),
1370 max_age);
1371 end_header(s, NULL);
1372 }
1373
1374 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1375 list<RGWBulkDelete::acct_path_t>& items, bool * const is_truncated)
1376 {
1377 constexpr size_t MAX_LINE_SIZE = 2048;
1378
1379 RGWClientIOStreamBuf ciosb(static_cast<RGWRestfulIO&>(*(s->cio)),
1380 size_t(s->cct->_conf->rgw_max_chunk_size));
1381 istream cioin(&ciosb);
1382
1383 char buf[MAX_LINE_SIZE];
1384 while (cioin.getline(buf, sizeof(buf))) {
1385 string path_str(buf);
1386
1387 ldout(s->cct, 20) << "extracted Bulk Delete entry: " << path_str << dendl;
1388
1389 RGWBulkDelete::acct_path_t path;
1390
1391 /* We need to skip all slashes at the beginning in order to preserve
1392 * compliance with Swift. */
1393 const size_t start_pos = path_str.find_first_not_of('/');
1394
1395 if (string::npos != start_pos) {
1396 /* Seperator is the first slash after the leading ones. */
1397 const size_t sep_pos = path_str.find('/', start_pos);
1398
1399 if (string::npos != sep_pos) {
1400 string bucket_name;
1401 url_decode(path_str.substr(start_pos, sep_pos - start_pos), bucket_name);
1402
1403 string obj_name;
1404 url_decode(path_str.substr(sep_pos + 1), obj_name);
1405
1406 path.bucket_name = bucket_name;
1407 path.obj_key = obj_name;
1408 } else {
1409 /* It's guaranteed here that bucket name is at least one character
1410 * long and is different than slash. */
1411 url_decode(path_str.substr(start_pos), path.bucket_name);
1412 }
1413
1414 items.push_back(path);
1415 }
1416
1417 if (items.size() == MAX_CHUNK_ENTRIES) {
1418 *is_truncated = true;
1419 return 0;
1420 }
1421 }
1422
1423 *is_truncated = false;
1424 return 0;
1425 }
1426
1427 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1428 {
1429 set_req_state_err(s, op_ret);
1430 dump_errno(s);
1431 end_header(s, this /* RGWOp */, nullptr /* contype */,
1432 CHUNKED_TRANSFER_ENCODING);
1433
1434 bulkdelete_respond(deleter->get_num_deleted(),
1435 deleter->get_num_unfound(),
1436 deleter->get_failures(),
1437 s->prot_flags,
1438 *s->formatter);
1439 rgw_flush_formatter_and_reset(s, s->formatter);
1440 }
1441
1442
1443 std::unique_ptr<RGWBulkUploadOp::StreamGetter>
1444 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1445 {
1446 class SwiftStreamGetter : public StreamGetter {
1447 const size_t conlen;
1448 size_t curpos;
1449 req_state* const s;
1450
1451 public:
1452 SwiftStreamGetter(req_state* const s, const size_t conlen)
1453 : conlen(conlen),
1454 curpos(0),
1455 s(s) {
1456 }
1457
1458 ssize_t get_at_most(size_t want, ceph::bufferlist& dst) override {
1459 /* maximum requested by a caller */
1460 /* data provided by client */
1461 /* RadosGW's limit. */
1462 const size_t max_chunk_size = \
1463 static_cast<size_t>(s->cct->_conf->rgw_max_chunk_size);
1464 const size_t max_to_read = std::min({ want, conlen - curpos, max_chunk_size });
1465
1466 ldout(s->cct, 20) << "bulk_upload: get_at_most max_to_read="
1467 << max_to_read
1468 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst.c_str()) << dendl;
1469
1470 bufferptr bp(max_to_read);
1471 const auto read_len = recv_body(s, bp.c_str(), max_to_read);
1472 dst.append(bp, 0, read_len);
1473 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1474 if (read_len < 0) {
1475 return read_len;
1476 }
1477
1478 curpos += read_len;
1479 return curpos > s->cct->_conf->rgw_max_put_size ? -ERR_TOO_LARGE
1480 : read_len;
1481 }
1482
1483 ssize_t get_exactly(size_t want, ceph::bufferlist& dst) override {
1484 ldout(s->cct, 20) << "bulk_upload: get_exactly want=" << want << dendl;
1485
1486 /* FIXME: do this in a loop. */
1487 const auto ret = get_at_most(want, dst);
1488 ldout(s->cct, 20) << "bulk_upload: get_exactly ret=" << ret << dendl;
1489 if (ret < 0) {
1490 return ret;
1491 } else if (static_cast<size_t>(ret) != want) {
1492 return -EINVAL;
1493 } else {
1494 return want;
1495 }
1496 }
1497 };
1498
1499 if (! s->length) {
1500 op_ret = -EINVAL;
1501 return nullptr;
1502 } else {
1503 ldout(s->cct, 20) << "bulk upload: create_stream for length="
1504 << s->length << dendl;
1505
1506 const size_t conlen = atoll(s->length);
1507 return std::unique_ptr<SwiftStreamGetter>(new SwiftStreamGetter(s, conlen));
1508 }
1509 }
1510
1511 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1512 {
1513 set_req_state_err(s, op_ret);
1514 dump_errno(s);
1515 end_header(s, this /* RGWOp */, nullptr /* contype */,
1516 CHUNKED_TRANSFER_ENCODING);
1517 rgw_flush_formatter_and_reset(s, s->formatter);
1518
1519 s->formatter->open_object_section("delete");
1520
1521 std::string resp_status;
1522 std::string resp_body;
1523
1524 if (! failures.empty()) {
1525 rgw_err err;
1526
1527 const auto last_err = { failures.back().err };
1528 if (boost::algorithm::contains(last_err, terminal_errors)) {
1529 /* The terminal errors are affecting the status of the whole upload. */
1530 set_req_state_err(err, failures.back().err, s->prot_flags);
1531 } else {
1532 set_req_state_err(err, ERR_INVALID_REQUEST, s->prot_flags);
1533 }
1534
1535 dump_errno(err, resp_status);
1536 } else if (0 == num_created && failures.empty()) {
1537 /* Nothing created, nothing failed. This means the archive contained no
1538 * entity we could understand (regular file or directory). We need to
1539 * send 400 Bad Request to an HTTP client in the internal status field. */
1540 dump_errno(400, resp_status);
1541 resp_body = "Invalid Tar File: No Valid Files";
1542 } else {
1543 /* 200 OK */
1544 dump_errno(201, resp_status);
1545 }
1546
1547 encode_json("Number Files Created", num_created, s->formatter);
1548 encode_json("Response Body", resp_body, s->formatter);
1549 encode_json("Response Status", resp_status, s->formatter);
1550
1551 s->formatter->open_array_section("Errors");
1552 for (const auto& fail_desc : failures) {
1553 s->formatter->open_array_section("object");
1554
1555 encode_json("Name", fail_desc.path, s->formatter);
1556
1557 rgw_err err;
1558 set_req_state_err(err, fail_desc.err, s->prot_flags);
1559 std::string status;
1560 dump_errno(err, status);
1561 encode_json("Status", status, s->formatter);
1562
1563 s->formatter->close_section();
1564 }
1565 s->formatter->close_section();
1566
1567 s->formatter->close_section();
1568 rgw_flush_formatter_and_reset(s, s->formatter);
1569 }
1570
1571
1572 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1573 {
1574 set_req_state_err(s, op_ret);
1575 dump_errno(s);
1576 end_header(s, this, "application/xml");
1577
1578 std::stringstream ss;
1579
1580 ss << R"(<?xml version="1.0"?>)" << "\n"
1581 << R"(<!DOCTYPE cross-domain-policy SYSTEM )"
1582 << R"("http://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1583 << R"(<cross-domain-policy>)" << "\n"
1584 << g_conf->rgw_cross_domain_policy << "\n"
1585 << R"(</cross-domain-policy>)";
1586
1587 dump_body(s, ss.str());
1588 }
1589
1590 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1591 {
1592 set_req_state_err(s, op_ret);
1593 dump_errno(s);
1594 end_header(s, this, "application/xml");
1595
1596 if (op_ret) {
1597 static constexpr char DISABLED[] = "DISABLED BY FILE";
1598 dump_body(s, DISABLED, strlen(DISABLED));
1599 }
1600 }
1601
1602 const vector<pair<string, RGWInfo_ObjStore_SWIFT::info>> RGWInfo_ObjStore_SWIFT::swift_info =
1603 {
1604 {"bulk_delete", {false, nullptr}},
1605 {"container_quotas", {false, nullptr}},
1606 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data}},
1607 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data}},
1608 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data}},
1609 {"account_quotas", {false, nullptr}},
1610 {"staticweb", {false, nullptr}},
1611 {"tempauth", {false, nullptr}},
1612 };
1613
1614 void RGWInfo_ObjStore_SWIFT::execute()
1615 {
1616 bool is_admin_info_enabled = false;
1617
1618 const string& swiftinfo_sig = s->info.args.get("swiftinfo_sig");
1619 const string& swiftinfo_expires = s->info.args.get("swiftinfo_expires");
1620
1621 if (!swiftinfo_sig.empty() &&
1622 !swiftinfo_expires.empty() &&
1623 !is_expired(swiftinfo_expires, s->cct)) {
1624 is_admin_info_enabled = true;
1625 }
1626
1627 s->formatter->open_object_section("info");
1628
1629 for (const auto& pair : swift_info) {
1630 if(!is_admin_info_enabled && pair.second.is_admin_info)
1631 continue;
1632
1633 if (!pair.second.list_data) {
1634 s->formatter->open_object_section((pair.first).c_str());
1635 s->formatter->close_section();
1636 }
1637 else {
1638 pair.second.list_data(*(s->formatter), *(s->cct->_conf), *store);
1639 }
1640 }
1641
1642 s->formatter->close_section();
1643 }
1644
1645 void RGWInfo_ObjStore_SWIFT::send_response()
1646 {
1647 if (op_ret < 0) {
1648 op_ret = STATUS_NO_CONTENT;
1649 }
1650 set_req_state_err(s, op_ret);
1651 dump_errno(s);
1652 end_header(s, this);
1653 rgw_flush_formatter_and_reset(s, s->formatter);
1654 }
1655
1656 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter& formatter,
1657 const md_config_t& config,
1658 RGWRados& store)
1659 {
1660 formatter.open_object_section("swift");
1661 formatter.dump_int("max_file_size", config.rgw_max_put_size);
1662 formatter.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX);
1663
1664 string ceph_version(CEPH_GIT_NICE_VER);
1665 formatter.dump_string("version", ceph_version);
1666 formatter.dump_int("max_meta_name_length", 81);
1667
1668 formatter.open_array_section("policies");
1669 RGWZoneGroup& zonegroup = store.get_zonegroup();
1670
1671 for (const auto& placement_targets : zonegroup.placement_targets) {
1672 formatter.open_object_section("policy");
1673 if (placement_targets.second.name.compare(zonegroup.default_placement) == 0)
1674 formatter.dump_bool("default", true);
1675 formatter.dump_string("name", placement_targets.second.name.c_str());
1676 formatter.close_section();
1677 }
1678 formatter.close_section();
1679
1680 formatter.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN);
1681 formatter.dump_bool("strict_cors_mode", true);
1682 formatter.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN);
1683 formatter.close_section();
1684 }
1685
1686 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter& formatter,
1687 const md_config_t& config,
1688 RGWRados& store)
1689 {
1690 formatter.open_object_section("tempurl");
1691 formatter.open_array_section("methods");
1692 formatter.dump_string("methodname", "GET");
1693 formatter.dump_string("methodname", "HEAD");
1694 formatter.dump_string("methodname", "PUT");
1695 formatter.dump_string("methodname", "POST");
1696 formatter.dump_string("methodname", "DELETE");
1697 formatter.close_section();
1698 formatter.close_section();
1699 }
1700
1701 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter& formatter,
1702 const md_config_t& config,
1703 RGWRados& store)
1704 {
1705 formatter.open_object_section("slo");
1706 formatter.dump_int("max_manifest_segments", config.rgw_max_slo_entries);
1707 formatter.close_section();
1708 }
1709
1710 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string& expires, CephContext* cct)
1711 {
1712 string err;
1713 const utime_t now = ceph_clock_now();
1714 const uint64_t expiration = (uint64_t)strict_strtoll(expires.c_str(),
1715 10, &err);
1716 if (!err.empty()) {
1717 ldout(cct, 5) << "failed to parse siginfo_expires: " << err << dendl;
1718 return true;
1719 }
1720
1721 if (expiration <= (uint64_t)now.sec()) {
1722 ldout(cct, 5) << "siginfo expired: " << expiration << " <= " << now.sec() << dendl;
1723 return true;
1724 }
1725
1726 return false;
1727 }
1728
1729
1730 void RGWFormPost::init(RGWRados* const store,
1731 req_state* const s,
1732 RGWHandler* const dialect_handler)
1733 {
1734 prefix = std::move(s->object.name);
1735 s->object = rgw_obj_key();
1736
1737 return RGWPostObj_ObjStore::init(store, s, dialect_handler);
1738 }
1739
1740 std::size_t RGWFormPost::get_max_file_size() /*const*/
1741 {
1742 std::string max_str = get_part_str(ctrl_parts, "max_file_size", "0");
1743
1744 std::string err;
1745 const std::size_t max_file_size =
1746 static_cast<uint64_t>(strict_strtoll(max_str.c_str(), 10, &err));
1747
1748 if (! err.empty()) {
1749 ldout(s->cct, 5) << "failed to parse FormPost's max_file_size: " << err
1750 << dendl;
1751 return 0;
1752 }
1753
1754 return max_file_size;
1755 }
1756
1757 bool RGWFormPost::is_non_expired()
1758 {
1759 std::string expires = get_part_str(ctrl_parts, "expires", "0");
1760
1761 std::string err;
1762 const uint64_t expires_timestamp =
1763 static_cast<uint64_t>(strict_strtoll(expires.c_str(), 10, &err));
1764
1765 if (! err.empty()) {
1766 dout(5) << "failed to parse FormPost's expires: " << err << dendl;
1767 return false;
1768 }
1769
1770 const utime_t now = ceph_clock_now();
1771 if (expires_timestamp <= static_cast<uint64_t>(now.sec())) {
1772 dout(5) << "FormPost form expired: "
1773 << expires_timestamp << " <= " << now.sec() << dendl;
1774 return false;
1775 }
1776
1777 return true;
1778 }
1779
1780 bool RGWFormPost::is_integral()
1781 {
1782 const std::string form_signature = get_part_str(ctrl_parts, "signature");
1783
1784 for (const auto& kv : s->user->temp_url_keys) {
1785 const int temp_url_key_num = kv.first;
1786 const string& temp_url_key = kv.second;
1787
1788 if (temp_url_key.empty()) {
1789 continue;
1790 }
1791
1792 SignatureHelper sig_helper;
1793 sig_helper.calc(temp_url_key,
1794 s->info.request_uri,
1795 get_part_str(ctrl_parts, "redirect"),
1796 get_part_str(ctrl_parts, "max_file_size", "0"),
1797 get_part_str(ctrl_parts, "max_file_count", "0"),
1798 get_part_str(ctrl_parts, "expires", "0"));
1799
1800 const auto local_sig = sig_helper.get_signature();
1801
1802 ldout(s->cct, 20) << "FormPost signature [" << temp_url_key_num << "]"
1803 << " (calculated): " << local_sig << dendl;
1804
1805 if (sig_helper.is_equal_to(form_signature)) {
1806 return true;
1807 } else {
1808 ldout(s->cct, 5) << "FormPost's signature mismatch: "
1809 << local_sig << " != " << form_signature << dendl;
1810 }
1811 }
1812
1813 return false;
1814 }
1815
1816 int RGWFormPost::get_params()
1817 {
1818 /* The parentt class extracts boundary info from the Content-Type. */
1819 int ret = RGWPostObj_ObjStore::get_params();
1820 if (ret < 0) {
1821 return ret;
1822 }
1823
1824 policy.create_default(s->user->user_id, s->user->display_name);
1825
1826 /* Let's start parsing the HTTP body by parsing each form part step-
1827 * by-step till encountering the first part with file data. */
1828 do {
1829 struct post_form_part part;
1830 ret = read_form_part_header(&part, stream_done);
1831 if (ret < 0) {
1832 return ret;
1833 }
1834
1835 if (s->cct->_conf->subsys.should_gather(ceph_subsys_rgw, 20)) {
1836 ldout(s->cct, 20) << "read part header -- part.name="
1837 << part.name << dendl;
1838
1839 for (const auto& pair : part.fields) {
1840 ldout(s->cct, 20) << "field.name=" << pair.first << dendl;
1841 ldout(s->cct, 20) << "field.val=" << pair.second.val << dendl;
1842 ldout(s->cct, 20) << "field.params:" << dendl;
1843
1844 for (const auto& param_pair : pair.second.params) {
1845 ldout(s->cct, 20) << " " << param_pair.first
1846 << " -> " << param_pair.second << dendl;
1847 }
1848 }
1849 }
1850
1851 if (stream_done) {
1852 /* Unexpected here. */
1853 err_msg = "Malformed request";
1854 return -EINVAL;
1855 }
1856
1857 const auto field_iter = part.fields.find("Content-Disposition");
1858 if (std::end(part.fields) != field_iter &&
1859 std::end(field_iter->second.params) != field_iter->second.params.find("filename")) {
1860 /* First data part ahead. */
1861 current_data_part = std::move(part);
1862
1863 /* Stop the iteration. We can assume that all control parts have been
1864 * already parsed. The rest of HTTP body should contain data parts
1865 * only. They will be picked up by ::get_data(). */
1866 break;
1867 } else {
1868 /* Control part ahead. Receive, parse and store for later usage. */
1869 bool boundary;
1870 ret = read_data(part.data, s->cct->_conf->rgw_max_chunk_size,
1871 boundary, stream_done);
1872 if (ret < 0) {
1873 return ret;
1874 } else if (! boundary) {
1875 err_msg = "Couldn't find boundary";
1876 return -EINVAL;
1877 }
1878
1879 ctrl_parts[part.name] = std::move(part);
1880 }
1881 } while (! stream_done);
1882
1883 min_len = 0;
1884 max_len = get_max_file_size();
1885
1886 if (! current_data_part) {
1887 err_msg = "FormPost: no files to process";
1888 return -EINVAL;
1889 }
1890
1891 if (! is_non_expired()) {
1892 err_msg = "FormPost: Form Expired";
1893 return -EPERM;
1894 }
1895
1896 if (! is_integral()) {
1897 err_msg = "FormPost: Invalid Signature";
1898 return -EPERM;
1899 }
1900
1901 return 0;
1902 }
1903
1904 std::string RGWFormPost::get_current_filename() const
1905 {
1906 try {
1907 const auto& field = current_data_part->fields.at("Content-Disposition");
1908 const auto iter = field.params.find("filename");
1909
1910 if (std::end(field.params) != iter) {
1911 return prefix + iter->second;
1912 }
1913 } catch (std::out_of_range&) {
1914 /* NOP */;
1915 }
1916
1917 return prefix;
1918 }
1919
1920 std::string RGWFormPost::get_current_content_type() const
1921 {
1922 try {
1923 const auto& field = current_data_part->fields.at("Content-Type");
1924 return field.val;
1925 } catch (std::out_of_range&) {
1926 /* NOP */;
1927 }
1928
1929 return std::string();
1930 }
1931
1932 bool RGWFormPost::is_next_file_to_upload()
1933 {
1934 if (! stream_done) {
1935 /* We have at least one additional part in the body. */
1936 struct post_form_part part;
1937 int r = read_form_part_header(&part, stream_done);
1938 if (r < 0) {
1939 return false;
1940 }
1941
1942 const auto field_iter = part.fields.find("Content-Disposition");
1943 if (std::end(part.fields) != field_iter) {
1944 const auto& params = field_iter->second.params;
1945
1946 if (std::end(params) != params.find("filename")) {
1947 current_data_part = std::move(part);
1948 return true;
1949 }
1950 }
1951 }
1952
1953 return false;
1954 }
1955
1956 int RGWFormPost::get_data(ceph::bufferlist& bl, bool& again)
1957 {
1958 bool boundary;
1959
1960 int r = read_data(bl, s->cct->_conf->rgw_max_chunk_size,
1961 boundary, stream_done);
1962 if (r < 0) {
1963 return r;
1964 }
1965
1966 /* Tell RGWPostObj::execute() that it has some data to put. */
1967 again = !boundary;
1968
1969 return bl.length();
1970 }
1971
1972 void RGWFormPost::send_response()
1973 {
1974 std::string redirect = get_part_str(ctrl_parts, "redirect");
1975 if (! redirect.empty()) {
1976 op_ret = STATUS_REDIRECT;
1977 }
1978
1979 set_req_state_err(s, op_ret);
1980 s->err.s3_code = err_msg;
1981 dump_errno(s);
1982 if (! redirect.empty()) {
1983 dump_redirect(s, redirect);
1984 }
1985 end_header(s, this);
1986 }
1987
1988 bool RGWFormPost::is_formpost_req(req_state* const s)
1989 {
1990 std::string content_type;
1991 std::map<std::string, std::string> params;
1992
1993 parse_boundary_params(s->info.env->get("CONTENT_TYPE", ""),
1994 content_type, params);
1995
1996 return boost::algorithm::iequals(content_type, "multipart/form-data") &&
1997 params.count("boundary") > 0;
1998 }
1999
2000
2001 RGWOp *RGWHandler_REST_Service_SWIFT::op_get()
2002 {
2003 return new RGWListBuckets_ObjStore_SWIFT;
2004 }
2005
2006 RGWOp *RGWHandler_REST_Service_SWIFT::op_head()
2007 {
2008 return new RGWStatAccount_ObjStore_SWIFT;
2009 }
2010
2011 RGWOp *RGWHandler_REST_Service_SWIFT::op_put()
2012 {
2013 if (s->info.args.exists("extract-archive")) {
2014 return new RGWBulkUploadOp_ObjStore_SWIFT;
2015 }
2016 return nullptr;
2017 }
2018
2019 RGWOp *RGWHandler_REST_Service_SWIFT::op_post()
2020 {
2021 if (s->info.args.exists("bulk-delete")) {
2022 return new RGWBulkDelete_ObjStore_SWIFT;
2023 }
2024 return new RGWPutMetadataAccount_ObjStore_SWIFT;
2025 }
2026
2027 RGWOp *RGWHandler_REST_Service_SWIFT::op_delete()
2028 {
2029 if (s->info.args.exists("bulk-delete")) {
2030 return new RGWBulkDelete_ObjStore_SWIFT;
2031 }
2032 return NULL;
2033 }
2034
2035 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret,
2036 const std::string error_doc)
2037 {
2038 /* Try to throw it all away. */
2039 s->formatter->reset();
2040
2041 class RGWGetErrorPage : public RGWGetObj_ObjStore_SWIFT {
2042 public:
2043 RGWGetErrorPage(RGWRados* const store,
2044 RGWHandler_REST* const handler,
2045 req_state* const s,
2046 const int http_ret) {
2047 /* Calling a virtual from the base class is safe as the subobject should
2048 * be properly initialized and we haven't overridden the init method. */
2049 init(store, s, handler);
2050 set_get_data(true);
2051 set_custom_http_response(http_ret);
2052 }
2053
2054 int error_handler(const int err_no,
2055 std::string* const error_content) override {
2056 /* Enforce that any error generated while getting the error page will
2057 * not be send to a client. This allows us to recover from the double
2058 * fault situation by sending the original message. */
2059 return 0;
2060 }
2061 } get_errpage_op(store, handler, s, http_ret);
2062
2063 s->object = std::to_string(http_ret) + error_doc;
2064
2065 RGWOp* newop = &get_errpage_op;
2066 RGWRequest req(0);
2067 return rgw_process_authenticated(handler, newop, &req, s, true);
2068 }
2069
2070 int RGWSwiftWebsiteHandler::error_handler(const int err_no,
2071 std::string* const error_content)
2072 {
2073 const auto& ws_conf = s->bucket_info.website_conf;
2074
2075 if (can_be_website_req() && ! ws_conf.error_doc.empty()) {
2076 struct rgw_err err;
2077 set_req_state_err(err, err_no, s->prot_flags);
2078 return serve_errordoc(err.http_ret, ws_conf.error_doc);
2079 }
2080
2081 /* Let's go to the default, no-op handler. */
2082 return err_no;
2083 }
2084
2085 bool RGWSwiftWebsiteHandler::is_web_mode() const
2086 {
2087 const boost::string_ref webmode = s->info.env->get("HTTP_X_WEB_MODE", "");
2088 return boost::algorithm::iequals(webmode, "true");
2089 }
2090
2091 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2092 {
2093 /* Static website works only with the GET or HEAD method. Nothing more. */
2094 static const std::set<boost::string_ref> ws_methods = { "GET", "HEAD" };
2095 if (ws_methods.count(s->info.method) == 0) {
2096 return false;
2097 }
2098
2099 /* We also need to handle early failures from the auth system. In such cases
2100 * req_state::auth.identity may be empty. Let's treat that the same way as
2101 * the anonymous access. */
2102 if (! s->auth.identity) {
2103 return true;
2104 }
2105
2106 /* Swift serves websites only for anonymous requests unless client explicitly
2107 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2108 if (s->auth.identity->is_anonymous() || is_web_mode()) {
2109 return true;
2110 }
2111
2112 return false;
2113 }
2114
2115 RGWOp* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2116 {
2117 class RGWMovedPermanently: public RGWOp {
2118 const std::string location;
2119 public:
2120 RGWMovedPermanently(const std::string& location)
2121 : location(location) {
2122 }
2123
2124 int verify_permission() override {
2125 return 0;
2126 }
2127
2128 void execute() override {
2129 op_ret = -ERR_PERMANENT_REDIRECT;
2130 return;
2131 }
2132
2133 void send_response() override {
2134 set_req_state_err(s, op_ret);
2135 dump_errno(s);
2136 dump_content_length(s, 0);
2137 dump_redirect(s, location);
2138 end_header(s, this);
2139 }
2140
2141 const string name() override {
2142 return "RGWMovedPermanently";
2143 }
2144 };
2145
2146 return new RGWMovedPermanently(s->info.request_uri + '/');
2147 }
2148
2149 RGWOp* RGWSwiftWebsiteHandler::get_ws_index_op()
2150 {
2151 /* Retarget to get obj on requested index file. */
2152 if (! s->object.empty()) {
2153 s->object = s->object.name +
2154 s->bucket_info.website_conf.get_index_doc();
2155 } else {
2156 s->object = s->bucket_info.website_conf.get_index_doc();
2157 }
2158
2159 auto getop = new RGWGetObj_ObjStore_SWIFT;
2160 getop->set_get_data(boost::algorithm::equals("GET", s->info.method));
2161
2162 return getop;
2163 }
2164
2165 RGWOp* RGWSwiftWebsiteHandler::get_ws_listing_op()
2166 {
2167 class RGWWebsiteListing : public RGWListBucket_ObjStore_SWIFT {
2168 const std::string prefix_override;
2169
2170 int get_params() override {
2171 prefix = prefix_override;
2172 max = default_max;
2173 delimiter = "/";
2174 return 0;
2175 }
2176
2177 void send_response() override {
2178 /* Generate the header now. */
2179 set_req_state_err(s, op_ret);
2180 dump_errno(s);
2181 dump_container_metadata(s, bucket, bucket_quota,
2182 s->bucket_info.website_conf);
2183 end_header(s, this, "text/html");
2184 if (op_ret < 0) {
2185 return;
2186 }
2187
2188 /* Now it's the time to start generating HTML bucket listing.
2189 * All the crazy stuff with crafting tags will be delegated to
2190 * RGWSwiftWebsiteListingFormatter. */
2191 std::stringstream ss;
2192 RGWSwiftWebsiteListingFormatter htmler(ss, prefix);
2193
2194 const auto& ws_conf = s->bucket_info.website_conf;
2195 htmler.generate_header(s->decoded_uri,
2196 ws_conf.listing_css_doc);
2197
2198 for (const auto& pair : common_prefixes) {
2199 std::string subdir_name = pair.first;
2200 if (! subdir_name.empty()) {
2201 /* To be compliant with Swift we need to remove the trailing
2202 * slash. */
2203 subdir_name.pop_back();
2204 }
2205
2206 htmler.dump_subdir(subdir_name);
2207 }
2208
2209 for (const rgw_bucket_dir_entry& obj : objs) {
2210 if (! common_prefixes.count(obj.key.name + '/')) {
2211 htmler.dump_object(obj);
2212 }
2213 }
2214
2215 htmler.generate_footer();
2216 dump_body(s, ss.str());
2217 }
2218 public:
2219 /* Taking prefix_override by value to leverage std::string r-value ref
2220 * ctor and thus avoid extra memory copying/increasing ref counter. */
2221 RGWWebsiteListing(std::string prefix_override)
2222 : prefix_override(std::move(prefix_override)) {
2223 }
2224 };
2225
2226 std::string prefix = std::move(s->object.name);
2227 s->object = rgw_obj_key();
2228
2229 return new RGWWebsiteListing(std::move(prefix));
2230 }
2231
2232 bool RGWSwiftWebsiteHandler::is_web_dir() const
2233 {
2234 std::string subdir_name;
2235 url_decode(s->object.name, subdir_name);
2236
2237 /* Remove character from the subdir name if it is "/". */
2238 if (subdir_name.empty()) {
2239 return false;
2240 } else if (subdir_name.back() == '/') {
2241 subdir_name.pop_back();
2242 }
2243
2244 rgw_obj obj(s->bucket, subdir_name);
2245
2246 /* First, get attrset of the object we'll try to retrieve. */
2247 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
2248 obj_ctx.obj.set_atomic(obj);
2249 obj_ctx.obj.set_prefetch_data(obj);
2250
2251 RGWObjState* state = nullptr;
2252 if (store->get_obj_state(&obj_ctx, s->bucket_info, obj, &state, false) < 0) {
2253 return false;
2254 }
2255
2256 /* A nonexistent object cannot be a considered as a marker representing
2257 * the emulation of catalog in FS hierarchy. */
2258 if (! state->exists) {
2259 return false;
2260 }
2261
2262 /* Decode the content type. */
2263 std::string content_type;
2264 get_contype_from_attrs(state->attrset, content_type);
2265
2266 const auto& ws_conf = s->bucket_info.website_conf;
2267 const std::string subdir_marker = ws_conf.subdir_marker.empty()
2268 ? "application/directory"
2269 : ws_conf.subdir_marker;
2270 return subdir_marker == content_type && state->size <= 1;
2271 }
2272
2273 bool RGWSwiftWebsiteHandler::is_index_present(const std::string& index)
2274 {
2275 rgw_obj obj(s->bucket, index);
2276
2277 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
2278 obj_ctx.obj.set_atomic(obj);
2279 obj_ctx.obj.set_prefetch_data(obj);
2280
2281 RGWObjState* state = nullptr;
2282 if (store->get_obj_state(&obj_ctx, s->bucket_info, obj, &state, false) < 0) {
2283 return false;
2284 }
2285
2286 /* A nonexistent object cannot be a considered as a viable index. We will
2287 * try to list the bucket or - if this is impossible - return an error. */
2288 return state->exists;
2289 }
2290
2291 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp* op, RGWOp** new_op)
2292 {
2293 ldout(s->cct, 10) << "Starting retarget" << dendl;
2294 RGWOp* op_override = nullptr;
2295
2296 /* In Swift static web content is served if the request is anonymous or
2297 * has X-Web-Mode HTTP header specified to true. */
2298 if (can_be_website_req()) {
2299 const auto& ws_conf = s->bucket_info.website_conf;
2300 const auto& index = s->bucket_info.website_conf.get_index_doc();
2301
2302 if (s->decoded_uri.back() != '/') {
2303 op_override = get_ws_redirect_op();
2304 } else if (! index.empty() && is_index_present(index)) {
2305 op_override = get_ws_index_op();
2306 } else if (ws_conf.listing_enabled) {
2307 op_override = get_ws_listing_op();
2308 }
2309 }
2310
2311 if (op_override) {
2312 handler->put_op(op);
2313 op_override->init(store, s, handler);
2314
2315 *new_op = op_override;
2316 } else {
2317 *new_op = op;
2318 }
2319
2320 /* Return 404 Not Found is the request has web mode enforced but we static web
2321 * wasn't able to serve it accordingly. */
2322 return ! op_override && is_web_mode() ? -ENOENT : 0;
2323 }
2324
2325 int RGWSwiftWebsiteHandler::retarget_object(RGWOp* op, RGWOp** new_op)
2326 {
2327 ldout(s->cct, 10) << "Starting object retarget" << dendl;
2328 RGWOp* op_override = nullptr;
2329
2330 /* In Swift static web content is served if the request is anonymous or
2331 * has X-Web-Mode HTTP header specified to true. */
2332 if (can_be_website_req() && is_web_dir()) {
2333 const auto& ws_conf = s->bucket_info.website_conf;
2334 const auto& index = s->bucket_info.website_conf.get_index_doc();
2335
2336 if (s->decoded_uri.back() != '/') {
2337 op_override = get_ws_redirect_op();
2338 } else if (! index.empty() && is_index_present(index)) {
2339 op_override = get_ws_index_op();
2340 } else if (ws_conf.listing_enabled) {
2341 op_override = get_ws_listing_op();
2342 }
2343 } else {
2344 /* A regular request or the specified object isn't a subdirectory marker.
2345 * We don't need any re-targeting. Error handling (like sending a custom
2346 * error page) will be performed by error_handler of the actual RGWOp. */
2347 return 0;
2348 }
2349
2350 if (op_override) {
2351 handler->put_op(op);
2352 op_override->init(store, s, handler);
2353
2354 *new_op = op_override;
2355 } else {
2356 *new_op = op;
2357 }
2358
2359 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2360 return ! op_override ? -ENOENT : 0;
2361 }
2362
2363
2364 RGWOp *RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data)
2365 {
2366 if (is_acl_op()) {
2367 return new RGWGetACLs_ObjStore_SWIFT;
2368 }
2369
2370 if (get_data)
2371 return new RGWListBucket_ObjStore_SWIFT;
2372 else
2373 return new RGWStatBucket_ObjStore_SWIFT;
2374 }
2375
2376 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_get()
2377 {
2378 return get_obj_op(true);
2379 }
2380
2381 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_head()
2382 {
2383 return get_obj_op(false);
2384 }
2385
2386 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_put()
2387 {
2388 if (is_acl_op()) {
2389 return new RGWPutACLs_ObjStore_SWIFT;
2390 }
2391 if(s->info.args.exists("extract-archive")) {
2392 return new RGWBulkUploadOp_ObjStore_SWIFT;
2393 }
2394 return new RGWCreateBucket_ObjStore_SWIFT;
2395 }
2396
2397 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_delete()
2398 {
2399 return new RGWDeleteBucket_ObjStore_SWIFT;
2400 }
2401
2402 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_post()
2403 {
2404 if (RGWFormPost::is_formpost_req(s)) {
2405 return new RGWFormPost;
2406 } else {
2407 return new RGWPutMetadataBucket_ObjStore_SWIFT;
2408 }
2409 }
2410
2411 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_options()
2412 {
2413 return new RGWOptionsCORS_ObjStore_SWIFT;
2414 }
2415
2416
2417 RGWOp *RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data)
2418 {
2419 if (is_acl_op()) {
2420 return new RGWGetACLs_ObjStore_SWIFT;
2421 }
2422
2423 RGWGetObj_ObjStore_SWIFT *get_obj_op = new RGWGetObj_ObjStore_SWIFT;
2424 get_obj_op->set_get_data(get_data);
2425 return get_obj_op;
2426 }
2427
2428 RGWOp *RGWHandler_REST_Obj_SWIFT::op_get()
2429 {
2430 return get_obj_op(true);
2431 }
2432
2433 RGWOp *RGWHandler_REST_Obj_SWIFT::op_head()
2434 {
2435 return get_obj_op(false);
2436 }
2437
2438 RGWOp *RGWHandler_REST_Obj_SWIFT::op_put()
2439 {
2440 if (is_acl_op()) {
2441 return new RGWPutACLs_ObjStore_SWIFT;
2442 }
2443 if(s->info.args.exists("extract-archive")) {
2444 return new RGWBulkUploadOp_ObjStore_SWIFT;
2445 }
2446 if (s->init_state.src_bucket.empty())
2447 return new RGWPutObj_ObjStore_SWIFT;
2448 else
2449 return new RGWCopyObj_ObjStore_SWIFT;
2450 }
2451
2452 RGWOp *RGWHandler_REST_Obj_SWIFT::op_delete()
2453 {
2454 return new RGWDeleteObj_ObjStore_SWIFT;
2455 }
2456
2457 RGWOp *RGWHandler_REST_Obj_SWIFT::op_post()
2458 {
2459 if (RGWFormPost::is_formpost_req(s)) {
2460 return new RGWFormPost;
2461 } else {
2462 return new RGWPutMetadataObject_ObjStore_SWIFT;
2463 }
2464 }
2465
2466 RGWOp *RGWHandler_REST_Obj_SWIFT::op_copy()
2467 {
2468 return new RGWCopyObj_ObjStore_SWIFT;
2469 }
2470
2471 RGWOp *RGWHandler_REST_Obj_SWIFT::op_options()
2472 {
2473 return new RGWOptionsCORS_ObjStore_SWIFT;
2474 }
2475
2476
2477 int RGWHandler_REST_SWIFT::authorize()
2478 {
2479 try {
2480 auto result = auth_strategy.authenticate(s);
2481
2482 if (result.get_status() != decltype(result)::Status::GRANTED) {
2483 /* Access denied is acknowledged by returning a std::unique_ptr with
2484 * nullptr inside. */
2485 ldout(s->cct, 5) << "auth engine refused to authenicate" << dendl;
2486 return -EPERM;
2487 }
2488
2489 try {
2490 rgw::auth::IdentityApplier::aplptr_t applier = result.get_applier();
2491
2492 /* Account used by a given RGWOp is decoupled from identity employed
2493 * in the authorization phase (RGWOp::verify_permissions). */
2494 applier->load_acct_info(*s->user);
2495 s->perm_mask = applier->get_perm_mask();
2496
2497 /* This is the signle place where we pass req_state as a pointer
2498 * to non-const and thus its modification is allowed. In the time
2499 * of writing only RGWTempURLEngine needed that feature. */
2500 applier->modify_request_state(s);
2501
2502 s->auth.identity = std::move(applier);
2503 s->auth.completer = std::move(result.get_completer());
2504
2505 return 0;
2506 } catch (int err) {
2507 ldout(s->cct, 5) << "applier throwed err=" << err << dendl;
2508 return err;
2509 }
2510 } catch (int err) {
2511 ldout(s->cct, 5) << "auth engine throwed err=" << err << dendl;
2512 return err;
2513 }
2514
2515 /* All engines refused to handle this authentication request by
2516 * returning RGWAuthEngine::Status::UNKKOWN. Rather rare case. */
2517 return -EPERM;
2518 }
2519
2520 int RGWHandler_REST_SWIFT::postauth_init()
2521 {
2522 struct req_init_state* t = &s->init_state;
2523
2524 /* XXX Stub this until Swift Auth sets account into URL. */
2525 s->bucket_tenant = s->user->user_id.tenant;
2526 s->bucket_name = t->url_bucket;
2527
2528 dout(10) << "s->object=" <<
2529 (!s->object.empty() ? s->object : rgw_obj_key("<NULL>"))
2530 << " s->bucket="
2531 << rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name)
2532 << dendl;
2533
2534 int ret;
2535 ret = validate_tenant_name(s->bucket_tenant);
2536 if (ret)
2537 return ret;
2538 ret = validate_bucket_name(s->bucket_name);
2539 if (ret)
2540 return ret;
2541 ret = validate_object_name(s->object.name);
2542 if (ret)
2543 return ret;
2544
2545 if (!t->src_bucket.empty()) {
2546 /*
2547 * We don't allow cross-tenant copy at present. It requires account
2548 * names in the URL for Swift.
2549 */
2550 s->src_tenant_name = s->user->user_id.tenant;
2551 s->src_bucket_name = t->src_bucket;
2552
2553 ret = validate_bucket_name(s->src_bucket_name);
2554 if (ret < 0) {
2555 return ret;
2556 }
2557 ret = validate_object_name(s->src_object.name);
2558 if (ret < 0) {
2559 return ret;
2560 }
2561 }
2562
2563 return 0;
2564 }
2565
2566 int RGWHandler_REST_SWIFT::validate_bucket_name(const string& bucket)
2567 {
2568 int ret = RGWHandler_REST::validate_bucket_name(bucket);
2569 if (ret < 0)
2570 return ret;
2571
2572 int len = bucket.size();
2573
2574 if (len == 0)
2575 return 0;
2576
2577 if (bucket[0] == '.')
2578 return -ERR_INVALID_BUCKET_NAME;
2579
2580 if (check_utf8(bucket.c_str(), len))
2581 return -ERR_INVALID_UTF8;
2582
2583 const char *s = bucket.c_str();
2584
2585 for (int i = 0; i < len; ++i, ++s) {
2586 if (*(unsigned char *)s == 0xff)
2587 return -ERR_INVALID_BUCKET_NAME;
2588 }
2589
2590 return 0;
2591 }
2592
2593 static void next_tok(string& str, string& tok, char delim)
2594 {
2595 if (str.size() == 0) {
2596 tok = "";
2597 return;
2598 }
2599 tok = str;
2600 int pos = str.find(delim);
2601 if (pos > 0) {
2602 tok = str.substr(0, pos);
2603 str = str.substr(pos + 1);
2604 } else {
2605 str = "";
2606 }
2607 }
2608
2609 int RGWHandler_REST_SWIFT::init_from_header(struct req_state* const s,
2610 const std::string& frontend_prefix)
2611 {
2612 string req;
2613 string first;
2614
2615 s->prot_flags |= RGW_REST_SWIFT;
2616
2617 char reqbuf[frontend_prefix.length() + s->decoded_uri.length() + 1];
2618 sprintf(reqbuf, "%s%s", frontend_prefix.c_str(), s->decoded_uri.c_str());
2619 const char *req_name = reqbuf;
2620
2621 const char *p;
2622
2623 if (*req_name == '?') {
2624 p = req_name;
2625 } else {
2626 p = s->info.request_params.c_str();
2627 }
2628
2629 s->info.args.set(p);
2630 s->info.args.parse();
2631
2632 /* Skip the leading slash of URL hierarchy. */
2633 if (req_name[0] != '/') {
2634 return 0;
2635 } else {
2636 req_name++;
2637 }
2638
2639 if ('\0' == req_name[0]) {
2640 return g_conf->rgw_swift_url_prefix == "/" ? -ERR_BAD_URL : 0;
2641 }
2642
2643 req = req_name;
2644
2645 size_t pos = req.find('/');
2646 if (std::string::npos != pos && g_conf->rgw_swift_url_prefix != "/") {
2647 bool cut_url = g_conf->rgw_swift_url_prefix.length();
2648 first = req.substr(0, pos);
2649
2650 if (first.compare(g_conf->rgw_swift_url_prefix) == 0) {
2651 if (cut_url) {
2652 /* Rewind to the "v1/..." part. */
2653 next_tok(req, first, '/');
2654 }
2655 }
2656 } else if (req.compare(g_conf->rgw_swift_url_prefix) == 0) {
2657 s->formatter = new RGWFormatter_Plain;
2658 return -ERR_BAD_URL;
2659 } else {
2660 first = req;
2661 }
2662
2663 std::string tenant_path;
2664 if (! g_conf->rgw_swift_tenant_name.empty()) {
2665 tenant_path = "/AUTH_";
2666 tenant_path.append(g_conf->rgw_swift_tenant_name);
2667 }
2668
2669 /* verify that the request_uri conforms with what's expected */
2670 char buf[g_conf->rgw_swift_url_prefix.length() + 16 + tenant_path.length()];
2671 int blen;
2672 if (g_conf->rgw_swift_url_prefix == "/") {
2673 blen = sprintf(buf, "/v1%s", tenant_path.c_str());
2674 } else {
2675 blen = sprintf(buf, "/%s/v1%s",
2676 g_conf->rgw_swift_url_prefix.c_str(), tenant_path.c_str());
2677 }
2678
2679 if (strncmp(reqbuf, buf, blen) != 0) {
2680 return -ENOENT;
2681 }
2682
2683 int ret = allocate_formatter(s, RGW_FORMAT_PLAIN, true);
2684 if (ret < 0)
2685 return ret;
2686
2687 string ver;
2688
2689 next_tok(req, ver, '/');
2690
2691 if (!tenant_path.empty() || g_conf->rgw_swift_account_in_url) {
2692 string account_name;
2693 next_tok(req, account_name, '/');
2694
2695 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2696 const vector<string> skipped_prefixes = { "AUTH_", "KEY_" };
2697
2698 for (const auto pfx : skipped_prefixes) {
2699 const size_t comp_len = min(account_name.length(), pfx.length());
2700 if (account_name.compare(0, comp_len, pfx) == 0) {
2701 /* Prefix is present. Drop it. */
2702 account_name = account_name.substr(comp_len);
2703 break;
2704 }
2705 }
2706
2707 if (account_name.empty()) {
2708 return -ERR_PRECONDITION_FAILED;
2709 } else {
2710 s->account_name = account_name;
2711 }
2712 }
2713
2714 next_tok(req, first, '/');
2715
2716 dout(10) << "ver=" << ver << " first=" << first << " req=" << req << dendl;
2717 if (first.size() == 0)
2718 return 0;
2719
2720 s->info.effective_uri = "/" + first;
2721
2722 // Save bucket to tide us over until token is parsed.
2723 s->init_state.url_bucket = first;
2724
2725 if (req.size()) {
2726 s->object =
2727 rgw_obj_key(req, s->info.env->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2728 s->info.effective_uri.append("/" + s->object.name);
2729 }
2730
2731 return 0;
2732 }
2733
2734 int RGWHandler_REST_SWIFT::init(RGWRados* store, struct req_state* s,
2735 rgw::io::BasicClient *cio)
2736 {
2737 struct req_init_state *t = &s->init_state;
2738
2739 s->dialect = "swift";
2740
2741 const char *copy_source = s->info.env->get("HTTP_X_COPY_FROM");
2742 if (copy_source) {
2743 bool result = RGWCopyObj::parse_copy_location(copy_source, t->src_bucket,
2744 s->src_object);
2745 if (!result)
2746 return -ERR_BAD_URL;
2747 }
2748
2749 if (s->op == OP_COPY) {
2750 const char *req_dest = s->info.env->get("HTTP_DESTINATION");
2751 if (!req_dest)
2752 return -ERR_BAD_URL;
2753
2754 string dest_bucket_name;
2755 rgw_obj_key dest_obj_key;
2756 bool result =
2757 RGWCopyObj::parse_copy_location(req_dest, dest_bucket_name,
2758 dest_obj_key);
2759 if (!result)
2760 return -ERR_BAD_URL;
2761
2762 string dest_object = dest_obj_key.name;
2763
2764 /* convert COPY operation into PUT */
2765 t->src_bucket = t->url_bucket;
2766 s->src_object = s->object;
2767 t->url_bucket = dest_bucket_name;
2768 s->object = rgw_obj_key(dest_object);
2769 s->op = OP_PUT;
2770 }
2771
2772 return RGWHandler_REST::init(store, s, cio);
2773 }
2774
2775 RGWHandler_REST*
2776 RGWRESTMgr_SWIFT::get_handler(struct req_state* const s,
2777 const rgw::auth::StrategyRegistry& auth_registry,
2778 const std::string& frontend_prefix)
2779 {
2780 int ret = RGWHandler_REST_SWIFT::init_from_header(s, frontend_prefix);
2781 if (ret < 0) {
2782 ldout(s->cct, 10) << "init_from_header returned err=" << ret << dendl;
2783 return nullptr;
2784 }
2785
2786 const auto& auth_strategy = auth_registry.get_swift();
2787
2788 if (s->init_state.url_bucket.empty()) {
2789 return new RGWHandler_REST_Service_SWIFT(auth_strategy);
2790 }
2791
2792 if (s->object.empty()) {
2793 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy);
2794 }
2795
2796 return new RGWHandler_REST_Obj_SWIFT(auth_strategy);
2797 }
2798
2799 RGWHandler_REST* RGWRESTMgr_SWIFT_Info::get_handler(
2800 struct req_state* const s,
2801 const rgw::auth::StrategyRegistry& auth_registry,
2802 const std::string& frontend_prefix)
2803 {
2804 s->prot_flags |= RGW_REST_SWIFT;
2805 const auto& auth_strategy = auth_registry.get_swift();
2806 return new RGWHandler_REST_SWIFT_Info(auth_strategy);
2807 }