]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rest_swift.cc
a3ccdca781a634bb58c2fa1d7ed52ec1e9db3636
[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 const size_t put_prefix_len = strlen(put_prefix);
575 const size_t del_prefix_len = strlen(del_prefix);
576
577 for (const auto& kv : s->info.env->get_map()) {
578 size_t prefix_len = 0;
579 const char * const p = kv.first.c_str();
580
581 if (strncasecmp(p, del_prefix, del_prefix_len) == 0) {
582 /* Explicitly requested removal. */
583 prefix_len = del_prefix_len;
584 } else if ((strncasecmp(p, put_prefix, put_prefix_len) == 0)
585 && kv.second.empty()) {
586 /* Removal requested by putting an empty value. */
587 prefix_len = put_prefix_len;
588 }
589
590 if (prefix_len > 0) {
591 string name(RGW_ATTR_META_PREFIX);
592 name.append(lowercase_dash_http_attr(p + prefix_len));
593 rmattr_names.insert(name);
594 }
595 }
596 }
597
598 static int get_swift_versioning_settings(
599 req_state * const s,
600 boost::optional<std::string>& swift_ver_location)
601 {
602 /* Removing the Swift's versions location has lower priority than setting
603 * a new one. That's the reason why we're handling it first. */
604 const std::string vlocdel =
605 s->info.env->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
606 if (vlocdel.size()) {
607 swift_ver_location = boost::in_place(std::string());
608 }
609
610 if (s->info.env->exists("HTTP_X_VERSIONS_LOCATION")) {
611 /* If the Swift's versioning is globally disabled but someone wants to
612 * enable it for a given container, new version of Swift will generate
613 * the precondition failed error. */
614 if (! s->cct->_conf->rgw_swift_versioning_enabled) {
615 return -ERR_PRECONDITION_FAILED;
616 }
617
618 swift_ver_location = s->info.env->get("HTTP_X_VERSIONS_LOCATION", "");
619 }
620
621 return 0;
622 }
623
624 int RGWCreateBucket_ObjStore_SWIFT::get_params()
625 {
626 bool has_policy;
627 uint32_t policy_rw_mask = 0;
628
629 int r = get_swift_container_settings(s, store, &policy, &has_policy,
630 &policy_rw_mask, &cors_config, &has_cors);
631 if (r < 0) {
632 return r;
633 }
634
635 if (!has_policy) {
636 policy.create_default(s->user->user_id, s->user->display_name);
637 }
638
639 location_constraint = store->get_zonegroup().api_name;
640 get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX,
641 CONT_REMOVE_ATTR_PREFIX, rmattr_names);
642 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
643
644 return get_swift_versioning_settings(s, swift_ver_location);
645 }
646
647 void RGWCreateBucket_ObjStore_SWIFT::send_response()
648 {
649 if (! op_ret)
650 op_ret = STATUS_CREATED;
651 else if (op_ret == -ERR_BUCKET_EXISTS)
652 op_ret = STATUS_ACCEPTED;
653 set_req_state_err(s, op_ret);
654 dump_errno(s);
655 /* Propose ending HTTP header with 0 Content-Length header. */
656 end_header(s, NULL, NULL, 0);
657 rgw_flush_formatter_and_reset(s, s->formatter);
658 }
659
660 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
661 {
662 int r = op_ret;
663 if (!r)
664 r = STATUS_NO_CONTENT;
665
666 set_req_state_err(s, r);
667 dump_errno(s);
668 end_header(s, this, NULL, 0);
669 rgw_flush_formatter_and_reset(s, s->formatter);
670 }
671
672 static int get_delete_at_param(req_state *s, boost::optional<real_time> &delete_at)
673 {
674 /* Handle Swift object expiration. */
675 real_time delat_proposal;
676 string x_delete = s->info.env->get("HTTP_X_DELETE_AFTER", "");
677
678 if (x_delete.empty()) {
679 x_delete = s->info.env->get("HTTP_X_DELETE_AT", "");
680 } else {
681 /* X-Delete-After HTTP is present. It means we need add its value
682 * to the current time. */
683 delat_proposal = real_clock::now();
684 }
685
686 if (x_delete.empty()) {
687 delete_at = boost::none;
688 if (s->info.env->exists("HTTP_X_REMOVE_DELETE_AT")) {
689 delete_at = boost::in_place(real_time());
690 }
691 return 0;
692 }
693 string err;
694 long ts = strict_strtoll(x_delete.c_str(), 10, &err);
695
696 if (!err.empty()) {
697 return -EINVAL;
698 }
699
700 delat_proposal += make_timespan(ts);
701 if (delat_proposal < real_clock::now()) {
702 return -EINVAL;
703 }
704
705 delete_at = delat_proposal;
706
707 return 0;
708 }
709
710 int RGWPutObj_ObjStore_SWIFT::verify_permission()
711 {
712 op_ret = RGWPutObj_ObjStore::verify_permission();
713
714 /* We have to differentiate error codes depending on whether user is
715 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
716 * (403 Forbidden). */
717 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
718 return -EPERM;
719 } else {
720 return op_ret;
721 }
722 }
723
724 int RGWPutObj_ObjStore_SWIFT::get_params()
725 {
726 if (s->has_bad_meta) {
727 return -EINVAL;
728 }
729
730 if (!s->length) {
731 const char *encoding = s->info.env->get("HTTP_TRANSFER_ENCODING");
732 if (!encoding || strcmp(encoding, "chunked") != 0) {
733 ldout(s->cct, 20) << "neither length nor chunked encoding" << dendl;
734 return -ERR_LENGTH_REQUIRED;
735 }
736
737 chunked_upload = true;
738 }
739
740 supplied_etag = s->info.env->get("HTTP_ETAG");
741
742 if (!s->generic_attrs.count(RGW_ATTR_CONTENT_TYPE)) {
743 ldout(s->cct, 5) << "content type wasn't provided, trying to guess" << dendl;
744 const char *suffix = strrchr(s->object.name.c_str(), '.');
745 if (suffix) {
746 suffix++;
747 if (*suffix) {
748 string suffix_str(suffix);
749 const char *mime = rgw_find_mime_by_ext(suffix_str);
750 if (mime) {
751 s->generic_attrs[RGW_ATTR_CONTENT_TYPE] = mime;
752 }
753 }
754 }
755 }
756
757 policy.create_default(s->user->user_id, s->user->display_name);
758
759 int r = get_delete_at_param(s, delete_at);
760 if (r < 0) {
761 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
762 return r;
763 }
764
765 if (!s->cct->_conf->rgw_swift_custom_header.empty()) {
766 string custom_header = s->cct->_conf->rgw_swift_custom_header;
767 if (s->info.env->exists(custom_header.c_str())) {
768 user_data = s->info.env->get(custom_header.c_str());
769 }
770 }
771
772 dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
773 bool exists;
774 string multipart_manifest = s->info.args.get("multipart-manifest", &exists);
775 if (exists) {
776 if (multipart_manifest != "put") {
777 ldout(s->cct, 5) << "invalid multipart-manifest http param: " << multipart_manifest << dendl;
778 return -EINVAL;
779 }
780
781 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
782 uint64_t max_len = s->cct->_conf->rgw_max_slo_entries * MAX_SLO_ENTRY_SIZE;
783
784 slo_info = new RGWSLOInfo;
785
786 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);
787 if (r < 0) {
788 ldout(s->cct, 5) << "failed to read input for slo r=" << r << dendl;
789 return r;
790 }
791
792 if ((int64_t)slo_info->entries.size() > s->cct->_conf->rgw_max_slo_entries) {
793 ldout(s->cct, 5) << "too many entries in slo request: " << slo_info->entries.size() << dendl;
794 return -EINVAL;
795 }
796
797 MD5 etag_sum;
798 uint64_t total_size = 0;
799 for (const auto& entry : slo_info->entries) {
800 etag_sum.Update((const byte *)entry.etag.c_str(),
801 entry.etag.length());
802 total_size += entry.size_bytes;
803
804 ldout(s->cct, 20) << "slo_part: " << entry.path
805 << " size=" << entry.size_bytes
806 << " etag=" << entry.etag
807 << dendl;
808 }
809 complete_etag(etag_sum, &lo_etag);
810 slo_info->total_size = total_size;
811
812 ofs = slo_info->raw_data_len;
813 }
814
815 return RGWPutObj_ObjStore::get_params();
816 }
817
818 void RGWPutObj_ObjStore_SWIFT::send_response()
819 {
820 if (! op_ret) {
821 op_ret = STATUS_CREATED;
822 }
823
824 if (! lo_etag.empty()) {
825 /* Static Large Object of Swift API has two etags represented by
826 * following members:
827 * - etag - for the manifest itself (it will be stored in xattrs),
828 * - lo_etag - for the content composited from SLO's segments.
829 * The value is calculated basing on segments' etags.
830 * In response for PUT request we have to expose the second one.
831 * The first one may be obtained by GET with "multipart-manifest=get"
832 * in query string on a given SLO. */
833 dump_etag(s, lo_etag, true /* quoted */);
834 } else {
835 dump_etag(s, etag);
836 }
837
838 dump_last_modified(s, mtime);
839 set_req_state_err(s, op_ret);
840 dump_errno(s);
841 end_header(s, this);
842 rgw_flush_formatter_and_reset(s, s->formatter);
843 }
844
845 static int get_swift_account_settings(req_state * const s,
846 RGWRados * const store,
847 RGWAccessControlPolicy_SWIFTAcct * const policy,
848 bool * const has_policy)
849 {
850 *has_policy = false;
851
852 const char * const acl_attr = s->info.env->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
853 if (acl_attr) {
854 RGWAccessControlPolicy_SWIFTAcct swift_acct_policy(s->cct);
855 const bool r = swift_acct_policy.create(store,
856 s->user->user_id,
857 s->user->display_name,
858 string(acl_attr));
859 if (r != true) {
860 return -EINVAL;
861 }
862
863 *policy = swift_acct_policy;
864 *has_policy = true;
865 }
866
867 return 0;
868 }
869
870 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
871 {
872 if (s->has_bad_meta) {
873 return -EINVAL;
874 }
875
876 int ret = get_swift_account_settings(s,
877 store,
878 // FIXME: we need to carry unique_ptr in generic class
879 // and allocate appropriate ACL class in the ctor
880 static_cast<RGWAccessControlPolicy_SWIFTAcct *>(&policy),
881 &has_policy);
882 if (ret < 0) {
883 return ret;
884 }
885
886 get_rmattrs_from_headers(s, ACCT_PUT_ATTR_PREFIX, ACCT_REMOVE_ATTR_PREFIX,
887 rmattr_names);
888 return 0;
889 }
890
891 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
892 {
893 if (! op_ret) {
894 op_ret = STATUS_NO_CONTENT;
895 }
896 set_req_state_err(s, op_ret);
897 dump_errno(s);
898 end_header(s, this);
899 rgw_flush_formatter_and_reset(s, s->formatter);
900 }
901
902 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
903 {
904 if (s->has_bad_meta) {
905 return -EINVAL;
906 }
907
908 int r = get_swift_container_settings(s, store, &policy, &has_policy,
909 &policy_rw_mask, &cors_config, &has_cors);
910 if (r < 0) {
911 return r;
912 }
913
914 get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX, CONT_REMOVE_ATTR_PREFIX,
915 rmattr_names);
916 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
917
918 return get_swift_versioning_settings(s, swift_ver_location);
919 }
920
921 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
922 {
923 if (!op_ret && (op_ret != -EINVAL)) {
924 op_ret = STATUS_NO_CONTENT;
925 }
926 set_req_state_err(s, op_ret);
927 dump_errno(s);
928 end_header(s, this);
929 rgw_flush_formatter_and_reset(s, s->formatter);
930 }
931
932 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
933 {
934 if (s->has_bad_meta) {
935 return -EINVAL;
936 }
937
938 /* Handle Swift object expiration. */
939 int r = get_delete_at_param(s, delete_at);
940 if (r < 0) {
941 ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
942 return r;
943 }
944
945 placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
946 dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
947
948 return 0;
949 }
950
951 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
952 {
953 if (! op_ret) {
954 op_ret = STATUS_ACCEPTED;
955 }
956 set_req_state_err(s, op_ret);
957 if (!s->is_err()) {
958 dump_content_length(s, 0);
959 }
960 dump_errno(s);
961 end_header(s, this);
962 rgw_flush_formatter_and_reset(s, s->formatter);
963 }
964
965 static void bulkdelete_respond(const unsigned num_deleted,
966 const unsigned int num_unfound,
967 const std::list<RGWBulkDelete::fail_desc_t>& failures,
968 const int prot_flags, /* in */
969 ceph::Formatter& formatter) /* out */
970 {
971 formatter.open_object_section("delete");
972
973 string resp_status;
974 string resp_body;
975
976 if (!failures.empty()) {
977 int reason = ERR_INVALID_REQUEST;
978 for (const auto fail_desc : failures) {
979 if (-ENOENT != fail_desc.err && -EACCES != fail_desc.err) {
980 reason = fail_desc.err;
981 }
982 }
983 rgw_err err;
984 set_req_state_err(err, reason, prot_flags);
985 dump_errno(err, resp_status);
986 } else if (0 == num_deleted && 0 == num_unfound) {
987 /* 400 Bad Request */
988 dump_errno(400, resp_status);
989 resp_body = "Invalid bulk delete.";
990 } else {
991 /* 200 OK */
992 dump_errno(200, resp_status);
993 }
994
995 encode_json("Number Deleted", num_deleted, &formatter);
996 encode_json("Number Not Found", num_unfound, &formatter);
997 encode_json("Response Body", resp_body, &formatter);
998 encode_json("Response Status", resp_status, &formatter);
999
1000 formatter.open_array_section("Errors");
1001 for (const auto fail_desc : failures) {
1002 formatter.open_array_section("object");
1003
1004 stringstream ss_name;
1005 ss_name << fail_desc.path;
1006 encode_json("Name", ss_name.str(), &formatter);
1007
1008 rgw_err err;
1009 set_req_state_err(err, fail_desc.err, prot_flags);
1010 string status;
1011 dump_errno(err, status);
1012 encode_json("Status", status, &formatter);
1013 formatter.close_section();
1014 }
1015 formatter.close_section();
1016
1017 formatter.close_section();
1018 }
1019
1020 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1021 {
1022 op_ret = RGWDeleteObj_ObjStore::verify_permission();
1023
1024 /* We have to differentiate error codes depending on whether user is
1025 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1026 * (403 Forbidden). */
1027 if (s->auth.identity->is_anonymous() && op_ret == -EACCES) {
1028 return -EPERM;
1029 } else {
1030 return op_ret;
1031 }
1032 }
1033
1034 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1035 {
1036 const string& mm = s->info.args.get("multipart-manifest");
1037 multipart_delete = (mm.compare("delete") == 0);
1038
1039 return RGWDeleteObj_ObjStore::get_params();
1040 }
1041
1042 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1043 {
1044 int r = op_ret;
1045
1046 if (multipart_delete) {
1047 r = 0;
1048 } else if(!r) {
1049 r = STATUS_NO_CONTENT;
1050 }
1051
1052 set_req_state_err(s, r);
1053 dump_errno(s);
1054
1055 if (multipart_delete) {
1056 end_header(s, this /* RGWOp */, nullptr /* contype */,
1057 CHUNKED_TRANSFER_ENCODING);
1058
1059 if (deleter) {
1060 bulkdelete_respond(deleter->get_num_deleted(),
1061 deleter->get_num_unfound(),
1062 deleter->get_failures(),
1063 s->prot_flags,
1064 *s->formatter);
1065 } else if (-ENOENT == op_ret) {
1066 bulkdelete_respond(0, 1, {}, s->prot_flags, *s->formatter);
1067 } else {
1068 RGWBulkDelete::acct_path_t path;
1069 path.bucket_name = s->bucket_name;
1070 path.obj_key = s->object;
1071
1072 RGWBulkDelete::fail_desc_t fail_desc;
1073 fail_desc.err = op_ret;
1074 fail_desc.path = path;
1075
1076 bulkdelete_respond(0, 0, { fail_desc }, s->prot_flags, *s->formatter);
1077 }
1078 } else {
1079 end_header(s, this);
1080 }
1081
1082 rgw_flush_formatter_and_reset(s, s->formatter);
1083
1084 }
1085
1086 static void get_contype_from_attrs(map<string, bufferlist>& attrs,
1087 string& content_type)
1088 {
1089 map<string, bufferlist>::iterator iter = attrs.find(RGW_ATTR_CONTENT_TYPE);
1090 if (iter != attrs.end()) {
1091 content_type = iter->second.c_str();
1092 }
1093 }
1094
1095 static void dump_object_metadata(struct req_state * const s,
1096 map<string, bufferlist> attrs)
1097 {
1098 map<string, string> response_attrs;
1099
1100 for (auto kv : attrs) {
1101 const char * name = kv.first.c_str();
1102 const auto aiter = rgw_to_http_attrs.find(name);
1103
1104 if (aiter != std::end(rgw_to_http_attrs)) {
1105 response_attrs[aiter->second] = kv.second.c_str();
1106 } else if (strcmp(name, RGW_ATTR_SLO_UINDICATOR) == 0) {
1107 // this attr has an extra length prefix from ::encode() in prior versions
1108 dump_header(s, "X-Object-Meta-Static-Large-Object", "True");
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->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->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 path.bucket_name = url_decode(path_str.substr(start_pos,
1401 sep_pos - start_pos));
1402 path.obj_key = url_decode(path_str.substr(sep_pos + 1));
1403 } else {
1404 /* It's guaranteed here that bucket name is at least one character
1405 * long and is different than slash. */
1406 path.bucket_name = url_decode(path_str.substr(start_pos));
1407 }
1408
1409 items.push_back(path);
1410 }
1411
1412 if (items.size() == MAX_CHUNK_ENTRIES) {
1413 *is_truncated = true;
1414 return 0;
1415 }
1416 }
1417
1418 *is_truncated = false;
1419 return 0;
1420 }
1421
1422 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1423 {
1424 set_req_state_err(s, op_ret);
1425 dump_errno(s);
1426 end_header(s, this /* RGWOp */, nullptr /* contype */,
1427 CHUNKED_TRANSFER_ENCODING);
1428
1429 bulkdelete_respond(deleter->get_num_deleted(),
1430 deleter->get_num_unfound(),
1431 deleter->get_failures(),
1432 s->prot_flags,
1433 *s->formatter);
1434 rgw_flush_formatter_and_reset(s, s->formatter);
1435 }
1436
1437
1438 std::unique_ptr<RGWBulkUploadOp::StreamGetter>
1439 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1440 {
1441 class SwiftStreamGetter : public StreamGetter {
1442 const size_t conlen;
1443 size_t curpos;
1444 req_state* const s;
1445
1446 public:
1447 SwiftStreamGetter(req_state* const s, const size_t conlen)
1448 : conlen(conlen),
1449 curpos(0),
1450 s(s) {
1451 }
1452
1453 ssize_t get_at_most(size_t want, ceph::bufferlist& dst) override {
1454 /* maximum requested by a caller */
1455 /* data provided by client */
1456 /* RadosGW's limit. */
1457 const size_t max_chunk_size = \
1458 static_cast<size_t>(s->cct->_conf->rgw_max_chunk_size);
1459 const size_t max_to_read = std::min({ want, conlen - curpos, max_chunk_size });
1460
1461 ldout(s->cct, 20) << "bulk_upload: get_at_most max_to_read="
1462 << max_to_read
1463 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst.c_str()) << dendl;
1464
1465 bufferptr bp(max_to_read);
1466 const auto read_len = recv_body(s, bp.c_str(), max_to_read);
1467 dst.append(bp, 0, read_len);
1468 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1469 if (read_len < 0) {
1470 return read_len;
1471 }
1472
1473 curpos += read_len;
1474 return curpos > s->cct->_conf->rgw_max_put_size ? -ERR_TOO_LARGE
1475 : read_len;
1476 }
1477
1478 ssize_t get_exactly(size_t want, ceph::bufferlist& dst) override {
1479 ldout(s->cct, 20) << "bulk_upload: get_exactly want=" << want << dendl;
1480
1481 /* FIXME: do this in a loop. */
1482 const auto ret = get_at_most(want, dst);
1483 ldout(s->cct, 20) << "bulk_upload: get_exactly ret=" << ret << dendl;
1484 if (ret < 0) {
1485 return ret;
1486 } else if (static_cast<size_t>(ret) != want) {
1487 return -EINVAL;
1488 } else {
1489 return want;
1490 }
1491 }
1492 };
1493
1494 if (! s->length) {
1495 op_ret = -EINVAL;
1496 return nullptr;
1497 } else {
1498 ldout(s->cct, 20) << "bulk upload: create_stream for length="
1499 << s->length << dendl;
1500
1501 const size_t conlen = atoll(s->length);
1502 return std::unique_ptr<SwiftStreamGetter>(new SwiftStreamGetter(s, conlen));
1503 }
1504 }
1505
1506 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1507 {
1508 set_req_state_err(s, op_ret);
1509 dump_errno(s);
1510 end_header(s, this /* RGWOp */, nullptr /* contype */,
1511 CHUNKED_TRANSFER_ENCODING);
1512 rgw_flush_formatter_and_reset(s, s->formatter);
1513
1514 s->formatter->open_object_section("delete");
1515
1516 std::string resp_status;
1517 std::string resp_body;
1518
1519 if (! failures.empty()) {
1520 rgw_err err;
1521
1522 const auto last_err = { failures.back().err };
1523 if (boost::algorithm::contains(last_err, terminal_errors)) {
1524 /* The terminal errors are affecting the status of the whole upload. */
1525 set_req_state_err(err, failures.back().err, s->prot_flags);
1526 } else {
1527 set_req_state_err(err, ERR_INVALID_REQUEST, s->prot_flags);
1528 }
1529
1530 dump_errno(err, resp_status);
1531 } else if (0 == num_created && failures.empty()) {
1532 /* Nothing created, nothing failed. This means the archive contained no
1533 * entity we could understand (regular file or directory). We need to
1534 * send 400 Bad Request to an HTTP client in the internal status field. */
1535 dump_errno(400, resp_status);
1536 resp_body = "Invalid Tar File: No Valid Files";
1537 } else {
1538 /* 200 OK */
1539 dump_errno(201, resp_status);
1540 }
1541
1542 encode_json("Number Files Created", num_created, s->formatter);
1543 encode_json("Response Body", resp_body, s->formatter);
1544 encode_json("Response Status", resp_status, s->formatter);
1545
1546 s->formatter->open_array_section("Errors");
1547 for (const auto& fail_desc : failures) {
1548 s->formatter->open_array_section("object");
1549
1550 encode_json("Name", fail_desc.path, s->formatter);
1551
1552 rgw_err err;
1553 set_req_state_err(err, fail_desc.err, s->prot_flags);
1554 std::string status;
1555 dump_errno(err, status);
1556 encode_json("Status", status, s->formatter);
1557
1558 s->formatter->close_section();
1559 }
1560 s->formatter->close_section();
1561
1562 s->formatter->close_section();
1563 rgw_flush_formatter_and_reset(s, s->formatter);
1564 }
1565
1566
1567 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1568 {
1569 set_req_state_err(s, op_ret);
1570 dump_errno(s);
1571 end_header(s, this, "application/xml");
1572
1573 std::stringstream ss;
1574
1575 ss << R"(<?xml version="1.0"?>)" << "\n"
1576 << R"(<!DOCTYPE cross-domain-policy SYSTEM )"
1577 << R"("http://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1578 << R"(<cross-domain-policy>)" << "\n"
1579 << g_conf->rgw_cross_domain_policy << "\n"
1580 << R"(</cross-domain-policy>)";
1581
1582 dump_body(s, ss.str());
1583 }
1584
1585 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1586 {
1587 set_req_state_err(s, op_ret);
1588 dump_errno(s);
1589 end_header(s, this, "application/xml");
1590
1591 if (op_ret) {
1592 static constexpr char DISABLED[] = "DISABLED BY FILE";
1593 dump_body(s, DISABLED, strlen(DISABLED));
1594 }
1595 }
1596
1597 const vector<pair<string, RGWInfo_ObjStore_SWIFT::info>> RGWInfo_ObjStore_SWIFT::swift_info =
1598 {
1599 {"bulk_delete", {false, nullptr}},
1600 {"container_quotas", {false, nullptr}},
1601 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data}},
1602 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data}},
1603 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data}},
1604 {"account_quotas", {false, nullptr}},
1605 {"staticweb", {false, nullptr}},
1606 {"tempauth", {false, nullptr}},
1607 };
1608
1609 void RGWInfo_ObjStore_SWIFT::execute()
1610 {
1611 bool is_admin_info_enabled = false;
1612
1613 const string& swiftinfo_sig = s->info.args.get("swiftinfo_sig");
1614 const string& swiftinfo_expires = s->info.args.get("swiftinfo_expires");
1615
1616 if (!swiftinfo_sig.empty() &&
1617 !swiftinfo_expires.empty() &&
1618 !is_expired(swiftinfo_expires, s->cct)) {
1619 is_admin_info_enabled = true;
1620 }
1621
1622 s->formatter->open_object_section("info");
1623
1624 for (const auto& pair : swift_info) {
1625 if(!is_admin_info_enabled && pair.second.is_admin_info)
1626 continue;
1627
1628 if (!pair.second.list_data) {
1629 s->formatter->open_object_section((pair.first).c_str());
1630 s->formatter->close_section();
1631 }
1632 else {
1633 pair.second.list_data(*(s->formatter), *(s->cct->_conf), *store);
1634 }
1635 }
1636
1637 s->formatter->close_section();
1638 }
1639
1640 void RGWInfo_ObjStore_SWIFT::send_response()
1641 {
1642 if (op_ret < 0) {
1643 op_ret = STATUS_NO_CONTENT;
1644 }
1645 set_req_state_err(s, op_ret);
1646 dump_errno(s);
1647 end_header(s, this);
1648 rgw_flush_formatter_and_reset(s, s->formatter);
1649 }
1650
1651 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter& formatter,
1652 const md_config_t& config,
1653 RGWRados& store)
1654 {
1655 formatter.open_object_section("swift");
1656 formatter.dump_int("max_file_size", config.rgw_max_put_size);
1657 formatter.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX);
1658
1659 string ceph_version(CEPH_GIT_NICE_VER);
1660 formatter.dump_string("version", ceph_version);
1661 formatter.dump_int("max_meta_name_length", 81);
1662
1663 formatter.open_array_section("policies");
1664 RGWZoneGroup& zonegroup = store.get_zonegroup();
1665
1666 for (const auto& placement_targets : zonegroup.placement_targets) {
1667 formatter.open_object_section("policy");
1668 if (placement_targets.second.name.compare(zonegroup.default_placement) == 0)
1669 formatter.dump_bool("default", true);
1670 formatter.dump_string("name", placement_targets.second.name.c_str());
1671 formatter.close_section();
1672 }
1673 formatter.close_section();
1674
1675 formatter.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN);
1676 formatter.dump_bool("strict_cors_mode", true);
1677 formatter.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN);
1678 formatter.close_section();
1679 }
1680
1681 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter& formatter,
1682 const md_config_t& config,
1683 RGWRados& store)
1684 {
1685 formatter.open_object_section("tempurl");
1686 formatter.open_array_section("methods");
1687 formatter.dump_string("methodname", "GET");
1688 formatter.dump_string("methodname", "HEAD");
1689 formatter.dump_string("methodname", "PUT");
1690 formatter.dump_string("methodname", "POST");
1691 formatter.dump_string("methodname", "DELETE");
1692 formatter.close_section();
1693 formatter.close_section();
1694 }
1695
1696 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter& formatter,
1697 const md_config_t& config,
1698 RGWRados& store)
1699 {
1700 formatter.open_object_section("slo");
1701 formatter.dump_int("max_manifest_segments", config.rgw_max_slo_entries);
1702 formatter.close_section();
1703 }
1704
1705 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string& expires, CephContext* cct)
1706 {
1707 string err;
1708 const utime_t now = ceph_clock_now();
1709 const uint64_t expiration = (uint64_t)strict_strtoll(expires.c_str(),
1710 10, &err);
1711 if (!err.empty()) {
1712 ldout(cct, 5) << "failed to parse siginfo_expires: " << err << dendl;
1713 return true;
1714 }
1715
1716 if (expiration <= (uint64_t)now.sec()) {
1717 ldout(cct, 5) << "siginfo expired: " << expiration << " <= " << now.sec() << dendl;
1718 return true;
1719 }
1720
1721 return false;
1722 }
1723
1724
1725 void RGWFormPost::init(RGWRados* const store,
1726 req_state* const s,
1727 RGWHandler* const dialect_handler)
1728 {
1729 prefix = std::move(s->object.name);
1730 s->object = rgw_obj_key();
1731
1732 return RGWPostObj_ObjStore::init(store, s, dialect_handler);
1733 }
1734
1735 std::size_t RGWFormPost::get_max_file_size() /*const*/
1736 {
1737 std::string max_str = get_part_str(ctrl_parts, "max_file_size", "0");
1738
1739 std::string err;
1740 const std::size_t max_file_size =
1741 static_cast<uint64_t>(strict_strtoll(max_str.c_str(), 10, &err));
1742
1743 if (! err.empty()) {
1744 ldout(s->cct, 5) << "failed to parse FormPost's max_file_size: " << err
1745 << dendl;
1746 return 0;
1747 }
1748
1749 return max_file_size;
1750 }
1751
1752 bool RGWFormPost::is_non_expired()
1753 {
1754 std::string expires = get_part_str(ctrl_parts, "expires", "0");
1755
1756 std::string err;
1757 const uint64_t expires_timestamp =
1758 static_cast<uint64_t>(strict_strtoll(expires.c_str(), 10, &err));
1759
1760 if (! err.empty()) {
1761 dout(5) << "failed to parse FormPost's expires: " << err << dendl;
1762 return false;
1763 }
1764
1765 const utime_t now = ceph_clock_now();
1766 if (expires_timestamp <= static_cast<uint64_t>(now.sec())) {
1767 dout(5) << "FormPost form expired: "
1768 << expires_timestamp << " <= " << now.sec() << dendl;
1769 return false;
1770 }
1771
1772 return true;
1773 }
1774
1775 bool RGWFormPost::is_integral()
1776 {
1777 const std::string form_signature = get_part_str(ctrl_parts, "signature");
1778
1779 try {
1780 get_owner_info(s, *s->user);
1781 s->auth.identity = rgw::auth::transform_old_authinfo(s);
1782 } catch (...) {
1783 ldout(s->cct, 5) << "cannot get user_info of account's owner" << dendl;
1784 return false;
1785 }
1786
1787 for (const auto& kv : s->user->temp_url_keys) {
1788 const int temp_url_key_num = kv.first;
1789 const string& temp_url_key = kv.second;
1790
1791 if (temp_url_key.empty()) {
1792 continue;
1793 }
1794
1795 SignatureHelper sig_helper;
1796 sig_helper.calc(temp_url_key,
1797 s->info.request_uri,
1798 get_part_str(ctrl_parts, "redirect"),
1799 get_part_str(ctrl_parts, "max_file_size", "0"),
1800 get_part_str(ctrl_parts, "max_file_count", "0"),
1801 get_part_str(ctrl_parts, "expires", "0"));
1802
1803 const auto local_sig = sig_helper.get_signature();
1804
1805 ldout(s->cct, 20) << "FormPost signature [" << temp_url_key_num << "]"
1806 << " (calculated): " << local_sig << dendl;
1807
1808 if (sig_helper.is_equal_to(form_signature)) {
1809 return true;
1810 } else {
1811 ldout(s->cct, 5) << "FormPost's signature mismatch: "
1812 << local_sig << " != " << form_signature << dendl;
1813 }
1814 }
1815
1816 return false;
1817 }
1818
1819 void RGWFormPost::get_owner_info(const req_state* const s,
1820 RGWUserInfo& owner_info) const
1821 {
1822 /* We cannot use req_state::bucket_name because it isn't available
1823 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
1824 const string& bucket_name = s->init_state.url_bucket;
1825
1826 /* TempURL in Formpost only requires that bucket name is specified. */
1827 if (bucket_name.empty()) {
1828 throw -EPERM;
1829 }
1830
1831 string bucket_tenant;
1832 if (!s->account_name.empty()) {
1833 RGWUserInfo uinfo;
1834 bool found = false;
1835
1836 const rgw_user uid(s->account_name);
1837 if (uid.tenant.empty()) {
1838 const rgw_user tenanted_uid(uid.id, uid.id);
1839
1840 if (rgw_get_user_info_by_uid(store, tenanted_uid, uinfo) >= 0) {
1841 /* Succeeded. */
1842 bucket_tenant = uinfo.user_id.tenant;
1843 found = true;
1844 }
1845 }
1846
1847 if (!found && rgw_get_user_info_by_uid(store, uid, uinfo) < 0) {
1848 throw -EPERM;
1849 } else {
1850 bucket_tenant = uinfo.user_id.tenant;
1851 }
1852 }
1853
1854 /* Need to get user info of bucket owner. */
1855 RGWBucketInfo bucket_info;
1856 int ret = store->get_bucket_info(*static_cast<RGWObjectCtx *>(s->obj_ctx),
1857 bucket_tenant, bucket_name,
1858 bucket_info, nullptr);
1859 if (ret < 0) {
1860 throw ret;
1861 }
1862
1863 ldout(s->cct, 20) << "temp url user (bucket owner): " << bucket_info.owner
1864 << dendl;
1865
1866 if (rgw_get_user_info_by_uid(store, bucket_info.owner, owner_info) < 0) {
1867 throw -EPERM;
1868 }
1869 }
1870
1871 int RGWFormPost::get_params()
1872 {
1873 /* The parentt class extracts boundary info from the Content-Type. */
1874 int ret = RGWPostObj_ObjStore::get_params();
1875 if (ret < 0) {
1876 return ret;
1877 }
1878
1879 policy.create_default(s->user->user_id, s->user->display_name);
1880
1881 /* Let's start parsing the HTTP body by parsing each form part step-
1882 * by-step till encountering the first part with file data. */
1883 do {
1884 struct post_form_part part;
1885 ret = read_form_part_header(&part, stream_done);
1886 if (ret < 0) {
1887 return ret;
1888 }
1889
1890 if (s->cct->_conf->subsys.should_gather(ceph_subsys_rgw, 20)) {
1891 ldout(s->cct, 20) << "read part header -- part.name="
1892 << part.name << dendl;
1893
1894 for (const auto& pair : part.fields) {
1895 ldout(s->cct, 20) << "field.name=" << pair.first << dendl;
1896 ldout(s->cct, 20) << "field.val=" << pair.second.val << dendl;
1897 ldout(s->cct, 20) << "field.params:" << dendl;
1898
1899 for (const auto& param_pair : pair.second.params) {
1900 ldout(s->cct, 20) << " " << param_pair.first
1901 << " -> " << param_pair.second << dendl;
1902 }
1903 }
1904 }
1905
1906 if (stream_done) {
1907 /* Unexpected here. */
1908 err_msg = "Malformed request";
1909 return -EINVAL;
1910 }
1911
1912 const auto field_iter = part.fields.find("Content-Disposition");
1913 if (std::end(part.fields) != field_iter &&
1914 std::end(field_iter->second.params) != field_iter->second.params.find("filename")) {
1915 /* First data part ahead. */
1916 current_data_part = std::move(part);
1917
1918 /* Stop the iteration. We can assume that all control parts have been
1919 * already parsed. The rest of HTTP body should contain data parts
1920 * only. They will be picked up by ::get_data(). */
1921 break;
1922 } else {
1923 /* Control part ahead. Receive, parse and store for later usage. */
1924 bool boundary;
1925 ret = read_data(part.data, s->cct->_conf->rgw_max_chunk_size,
1926 boundary, stream_done);
1927 if (ret < 0) {
1928 return ret;
1929 } else if (! boundary) {
1930 err_msg = "Couldn't find boundary";
1931 return -EINVAL;
1932 }
1933
1934 ctrl_parts[part.name] = std::move(part);
1935 }
1936 } while (! stream_done);
1937
1938 min_len = 0;
1939 max_len = get_max_file_size();
1940
1941 if (! current_data_part) {
1942 err_msg = "FormPost: no files to process";
1943 return -EINVAL;
1944 }
1945
1946 if (! is_non_expired()) {
1947 err_msg = "FormPost: Form Expired";
1948 return -EPERM;
1949 }
1950
1951 if (! is_integral()) {
1952 err_msg = "FormPost: Invalid Signature";
1953 return -EPERM;
1954 }
1955
1956 return 0;
1957 }
1958
1959 std::string RGWFormPost::get_current_filename() const
1960 {
1961 try {
1962 const auto& field = current_data_part->fields.at("Content-Disposition");
1963 const auto iter = field.params.find("filename");
1964
1965 if (std::end(field.params) != iter) {
1966 return prefix + iter->second;
1967 }
1968 } catch (std::out_of_range&) {
1969 /* NOP */;
1970 }
1971
1972 return prefix;
1973 }
1974
1975 std::string RGWFormPost::get_current_content_type() const
1976 {
1977 try {
1978 const auto& field = current_data_part->fields.at("Content-Type");
1979 return field.val;
1980 } catch (std::out_of_range&) {
1981 /* NOP */;
1982 }
1983
1984 return std::string();
1985 }
1986
1987 bool RGWFormPost::is_next_file_to_upload()
1988 {
1989 if (! stream_done) {
1990 /* We have at least one additional part in the body. */
1991 struct post_form_part part;
1992 int r = read_form_part_header(&part, stream_done);
1993 if (r < 0) {
1994 return false;
1995 }
1996
1997 const auto field_iter = part.fields.find("Content-Disposition");
1998 if (std::end(part.fields) != field_iter) {
1999 const auto& params = field_iter->second.params;
2000 const auto& filename_iter = params.find("filename");
2001
2002 if (std::end(params) != filename_iter && ! filename_iter->second.empty()) {
2003 current_data_part = std::move(part);
2004 return true;
2005 }
2006 }
2007 }
2008
2009 return false;
2010 }
2011
2012 int RGWFormPost::get_data(ceph::bufferlist& bl, bool& again)
2013 {
2014 bool boundary;
2015
2016 int r = read_data(bl, s->cct->_conf->rgw_max_chunk_size,
2017 boundary, stream_done);
2018 if (r < 0) {
2019 return r;
2020 }
2021
2022 /* Tell RGWPostObj::execute() that it has some data to put. */
2023 again = !boundary;
2024
2025 return bl.length();
2026 }
2027
2028 void RGWFormPost::send_response()
2029 {
2030 std::string redirect = get_part_str(ctrl_parts, "redirect");
2031 if (! redirect.empty()) {
2032 op_ret = STATUS_REDIRECT;
2033 }
2034
2035 set_req_state_err(s, op_ret);
2036 s->err.err_code = err_msg;
2037 dump_errno(s);
2038 if (! redirect.empty()) {
2039 dump_redirect(s, redirect);
2040 }
2041 end_header(s, this);
2042 }
2043
2044 bool RGWFormPost::is_formpost_req(req_state* const s)
2045 {
2046 std::string content_type;
2047 std::map<std::string, std::string> params;
2048
2049 parse_boundary_params(s->info.env->get("CONTENT_TYPE", ""),
2050 content_type, params);
2051
2052 return boost::algorithm::iequals(content_type, "multipart/form-data") &&
2053 params.count("boundary") > 0;
2054 }
2055
2056
2057 RGWOp *RGWHandler_REST_Service_SWIFT::op_get()
2058 {
2059 return new RGWListBuckets_ObjStore_SWIFT;
2060 }
2061
2062 RGWOp *RGWHandler_REST_Service_SWIFT::op_head()
2063 {
2064 return new RGWStatAccount_ObjStore_SWIFT;
2065 }
2066
2067 RGWOp *RGWHandler_REST_Service_SWIFT::op_put()
2068 {
2069 if (s->info.args.exists("extract-archive")) {
2070 return new RGWBulkUploadOp_ObjStore_SWIFT;
2071 }
2072 return nullptr;
2073 }
2074
2075 RGWOp *RGWHandler_REST_Service_SWIFT::op_post()
2076 {
2077 if (s->info.args.exists("bulk-delete")) {
2078 return new RGWBulkDelete_ObjStore_SWIFT;
2079 }
2080 return new RGWPutMetadataAccount_ObjStore_SWIFT;
2081 }
2082
2083 RGWOp *RGWHandler_REST_Service_SWIFT::op_delete()
2084 {
2085 if (s->info.args.exists("bulk-delete")) {
2086 return new RGWBulkDelete_ObjStore_SWIFT;
2087 }
2088 return NULL;
2089 }
2090
2091 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret,
2092 const std::string error_doc)
2093 {
2094 /* Try to throw it all away. */
2095 s->formatter->reset();
2096
2097 class RGWGetErrorPage : public RGWGetObj_ObjStore_SWIFT {
2098 public:
2099 RGWGetErrorPage(RGWRados* const store,
2100 RGWHandler_REST* const handler,
2101 req_state* const s,
2102 const int http_ret) {
2103 /* Calling a virtual from the base class is safe as the subobject should
2104 * be properly initialized and we haven't overridden the init method. */
2105 init(store, s, handler);
2106 set_get_data(true);
2107 set_custom_http_response(http_ret);
2108 }
2109
2110 int error_handler(const int err_no,
2111 std::string* const error_content) override {
2112 /* Enforce that any error generated while getting the error page will
2113 * not be send to a client. This allows us to recover from the double
2114 * fault situation by sending the original message. */
2115 return 0;
2116 }
2117 } get_errpage_op(store, handler, s, http_ret);
2118
2119 s->object = std::to_string(http_ret) + error_doc;
2120
2121 RGWOp* newop = &get_errpage_op;
2122 RGWRequest req(0);
2123 return rgw_process_authenticated(handler, newop, &req, s, true);
2124 }
2125
2126 int RGWSwiftWebsiteHandler::error_handler(const int err_no,
2127 std::string* const error_content)
2128 {
2129 const auto& ws_conf = s->bucket_info.website_conf;
2130
2131 if (can_be_website_req() && ! ws_conf.error_doc.empty()) {
2132 set_req_state_err(s, err_no);
2133 return serve_errordoc(s->err.http_ret, ws_conf.error_doc);
2134 }
2135
2136 /* Let's go to the default, no-op handler. */
2137 return err_no;
2138 }
2139
2140 bool RGWSwiftWebsiteHandler::is_web_mode() const
2141 {
2142 const boost::string_ref webmode = s->info.env->get("HTTP_X_WEB_MODE", "");
2143 return boost::algorithm::iequals(webmode, "true");
2144 }
2145
2146 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2147 {
2148 /* Static website works only with the GET or HEAD method. Nothing more. */
2149 static const std::set<boost::string_ref> ws_methods = { "GET", "HEAD" };
2150 if (ws_methods.count(s->info.method) == 0) {
2151 return false;
2152 }
2153
2154 /* We also need to handle early failures from the auth system. In such cases
2155 * req_state::auth.identity may be empty. Let's treat that the same way as
2156 * the anonymous access. */
2157 if (! s->auth.identity) {
2158 return true;
2159 }
2160
2161 /* Swift serves websites only for anonymous requests unless client explicitly
2162 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2163 if (s->auth.identity->is_anonymous() || is_web_mode()) {
2164 return true;
2165 }
2166
2167 return false;
2168 }
2169
2170 RGWOp* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2171 {
2172 class RGWMovedPermanently: public RGWOp {
2173 const std::string location;
2174 public:
2175 RGWMovedPermanently(const std::string& location)
2176 : location(location) {
2177 }
2178
2179 int verify_permission() override {
2180 return 0;
2181 }
2182
2183 void execute() override {
2184 op_ret = -ERR_PERMANENT_REDIRECT;
2185 return;
2186 }
2187
2188 void send_response() override {
2189 set_req_state_err(s, op_ret);
2190 dump_errno(s);
2191 dump_content_length(s, 0);
2192 dump_redirect(s, location);
2193 end_header(s, this);
2194 }
2195
2196 const string name() override {
2197 return "RGWMovedPermanently";
2198 }
2199 };
2200
2201 return new RGWMovedPermanently(s->info.request_uri + '/');
2202 }
2203
2204 RGWOp* RGWSwiftWebsiteHandler::get_ws_index_op()
2205 {
2206 /* Retarget to get obj on requested index file. */
2207 if (! s->object.empty()) {
2208 s->object = s->object.name +
2209 s->bucket_info.website_conf.get_index_doc();
2210 } else {
2211 s->object = s->bucket_info.website_conf.get_index_doc();
2212 }
2213
2214 auto getop = new RGWGetObj_ObjStore_SWIFT;
2215 getop->set_get_data(boost::algorithm::equals("GET", s->info.method));
2216
2217 return getop;
2218 }
2219
2220 RGWOp* RGWSwiftWebsiteHandler::get_ws_listing_op()
2221 {
2222 class RGWWebsiteListing : public RGWListBucket_ObjStore_SWIFT {
2223 const std::string prefix_override;
2224
2225 int get_params() override {
2226 prefix = prefix_override;
2227 max = default_max;
2228 delimiter = "/";
2229 return 0;
2230 }
2231
2232 void send_response() override {
2233 /* Generate the header now. */
2234 set_req_state_err(s, op_ret);
2235 dump_errno(s);
2236 dump_container_metadata(s, bucket, bucket_quota,
2237 s->bucket_info.website_conf);
2238 end_header(s, this, "text/html");
2239 if (op_ret < 0) {
2240 return;
2241 }
2242
2243 /* Now it's the time to start generating HTML bucket listing.
2244 * All the crazy stuff with crafting tags will be delegated to
2245 * RGWSwiftWebsiteListingFormatter. */
2246 std::stringstream ss;
2247 RGWSwiftWebsiteListingFormatter htmler(ss, prefix);
2248
2249 const auto& ws_conf = s->bucket_info.website_conf;
2250 htmler.generate_header(s->decoded_uri,
2251 ws_conf.listing_css_doc);
2252
2253 for (const auto& pair : common_prefixes) {
2254 std::string subdir_name = pair.first;
2255 if (! subdir_name.empty()) {
2256 /* To be compliant with Swift we need to remove the trailing
2257 * slash. */
2258 subdir_name.pop_back();
2259 }
2260
2261 htmler.dump_subdir(subdir_name);
2262 }
2263
2264 for (const rgw_bucket_dir_entry& obj : objs) {
2265 if (! common_prefixes.count(obj.key.name + '/')) {
2266 htmler.dump_object(obj);
2267 }
2268 }
2269
2270 htmler.generate_footer();
2271 dump_body(s, ss.str());
2272 }
2273 public:
2274 /* Taking prefix_override by value to leverage std::string r-value ref
2275 * ctor and thus avoid extra memory copying/increasing ref counter. */
2276 RGWWebsiteListing(std::string prefix_override)
2277 : prefix_override(std::move(prefix_override)) {
2278 }
2279 };
2280
2281 std::string prefix = std::move(s->object.name);
2282 s->object = rgw_obj_key();
2283
2284 return new RGWWebsiteListing(std::move(prefix));
2285 }
2286
2287 bool RGWSwiftWebsiteHandler::is_web_dir() const
2288 {
2289 std::string subdir_name = url_decode(s->object.name);
2290
2291 /* Remove character from the subdir name if it is "/". */
2292 if (subdir_name.empty()) {
2293 return false;
2294 } else if (subdir_name.back() == '/') {
2295 subdir_name.pop_back();
2296 }
2297
2298 rgw_obj obj(s->bucket, std::move(subdir_name));
2299
2300 /* First, get attrset of the object we'll try to retrieve. */
2301 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
2302 obj_ctx.obj.set_atomic(obj);
2303 obj_ctx.obj.set_prefetch_data(obj);
2304
2305 RGWObjState* state = nullptr;
2306 if (store->get_obj_state(&obj_ctx, s->bucket_info, obj, &state, false) < 0) {
2307 return false;
2308 }
2309
2310 /* A nonexistent object cannot be a considered as a marker representing
2311 * the emulation of catalog in FS hierarchy. */
2312 if (! state->exists) {
2313 return false;
2314 }
2315
2316 /* Decode the content type. */
2317 std::string content_type;
2318 get_contype_from_attrs(state->attrset, content_type);
2319
2320 const auto& ws_conf = s->bucket_info.website_conf;
2321 const std::string subdir_marker = ws_conf.subdir_marker.empty()
2322 ? "application/directory"
2323 : ws_conf.subdir_marker;
2324 return subdir_marker == content_type && state->size <= 1;
2325 }
2326
2327 bool RGWSwiftWebsiteHandler::is_index_present(const std::string& index)
2328 {
2329 rgw_obj obj(s->bucket, index);
2330
2331 RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx);
2332 obj_ctx.obj.set_atomic(obj);
2333 obj_ctx.obj.set_prefetch_data(obj);
2334
2335 RGWObjState* state = nullptr;
2336 if (store->get_obj_state(&obj_ctx, s->bucket_info, obj, &state, false) < 0) {
2337 return false;
2338 }
2339
2340 /* A nonexistent object cannot be a considered as a viable index. We will
2341 * try to list the bucket or - if this is impossible - return an error. */
2342 return state->exists;
2343 }
2344
2345 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp* op, RGWOp** new_op)
2346 {
2347 ldout(s->cct, 10) << "Starting retarget" << dendl;
2348 RGWOp* op_override = nullptr;
2349
2350 /* In Swift static web content is served if the request is anonymous or
2351 * has X-Web-Mode HTTP header specified to true. */
2352 if (can_be_website_req()) {
2353 const auto& ws_conf = s->bucket_info.website_conf;
2354 const auto& index = s->bucket_info.website_conf.get_index_doc();
2355
2356 if (s->decoded_uri.back() != '/') {
2357 op_override = get_ws_redirect_op();
2358 } else if (! index.empty() && is_index_present(index)) {
2359 op_override = get_ws_index_op();
2360 } else if (ws_conf.listing_enabled) {
2361 op_override = get_ws_listing_op();
2362 }
2363 }
2364
2365 if (op_override) {
2366 handler->put_op(op);
2367 op_override->init(store, s, handler);
2368
2369 *new_op = op_override;
2370 } else {
2371 *new_op = op;
2372 }
2373
2374 /* Return 404 Not Found is the request has web mode enforced but we static web
2375 * wasn't able to serve it accordingly. */
2376 return ! op_override && is_web_mode() ? -ENOENT : 0;
2377 }
2378
2379 int RGWSwiftWebsiteHandler::retarget_object(RGWOp* op, RGWOp** new_op)
2380 {
2381 ldout(s->cct, 10) << "Starting object retarget" << dendl;
2382 RGWOp* op_override = nullptr;
2383
2384 /* In Swift static web content is served if the request is anonymous or
2385 * has X-Web-Mode HTTP header specified to true. */
2386 if (can_be_website_req() && is_web_dir()) {
2387 const auto& ws_conf = s->bucket_info.website_conf;
2388 const auto& index = s->bucket_info.website_conf.get_index_doc();
2389
2390 if (s->decoded_uri.back() != '/') {
2391 op_override = get_ws_redirect_op();
2392 } else if (! index.empty() && is_index_present(index)) {
2393 op_override = get_ws_index_op();
2394 } else if (ws_conf.listing_enabled) {
2395 op_override = get_ws_listing_op();
2396 }
2397 } else {
2398 /* A regular request or the specified object isn't a subdirectory marker.
2399 * We don't need any re-targeting. Error handling (like sending a custom
2400 * error page) will be performed by error_handler of the actual RGWOp. */
2401 return 0;
2402 }
2403
2404 if (op_override) {
2405 handler->put_op(op);
2406 op_override->init(store, s, handler);
2407
2408 *new_op = op_override;
2409 } else {
2410 *new_op = op;
2411 }
2412
2413 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2414 return ! op_override ? -ENOENT : 0;
2415 }
2416
2417
2418 RGWOp *RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data)
2419 {
2420 if (is_acl_op()) {
2421 return new RGWGetACLs_ObjStore_SWIFT;
2422 }
2423
2424 if (get_data)
2425 return new RGWListBucket_ObjStore_SWIFT;
2426 else
2427 return new RGWStatBucket_ObjStore_SWIFT;
2428 }
2429
2430 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_get()
2431 {
2432 return get_obj_op(true);
2433 }
2434
2435 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_head()
2436 {
2437 return get_obj_op(false);
2438 }
2439
2440 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_put()
2441 {
2442 if (is_acl_op()) {
2443 return new RGWPutACLs_ObjStore_SWIFT;
2444 }
2445 if(s->info.args.exists("extract-archive")) {
2446 return new RGWBulkUploadOp_ObjStore_SWIFT;
2447 }
2448 return new RGWCreateBucket_ObjStore_SWIFT;
2449 }
2450
2451 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_delete()
2452 {
2453 return new RGWDeleteBucket_ObjStore_SWIFT;
2454 }
2455
2456 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_post()
2457 {
2458 if (RGWFormPost::is_formpost_req(s)) {
2459 return new RGWFormPost;
2460 } else {
2461 return new RGWPutMetadataBucket_ObjStore_SWIFT;
2462 }
2463 }
2464
2465 RGWOp *RGWHandler_REST_Bucket_SWIFT::op_options()
2466 {
2467 return new RGWOptionsCORS_ObjStore_SWIFT;
2468 }
2469
2470
2471 RGWOp *RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data)
2472 {
2473 if (is_acl_op()) {
2474 return new RGWGetACLs_ObjStore_SWIFT;
2475 }
2476
2477 RGWGetObj_ObjStore_SWIFT *get_obj_op = new RGWGetObj_ObjStore_SWIFT;
2478 get_obj_op->set_get_data(get_data);
2479 return get_obj_op;
2480 }
2481
2482 RGWOp *RGWHandler_REST_Obj_SWIFT::op_get()
2483 {
2484 return get_obj_op(true);
2485 }
2486
2487 RGWOp *RGWHandler_REST_Obj_SWIFT::op_head()
2488 {
2489 return get_obj_op(false);
2490 }
2491
2492 RGWOp *RGWHandler_REST_Obj_SWIFT::op_put()
2493 {
2494 if (is_acl_op()) {
2495 return new RGWPutACLs_ObjStore_SWIFT;
2496 }
2497 if(s->info.args.exists("extract-archive")) {
2498 return new RGWBulkUploadOp_ObjStore_SWIFT;
2499 }
2500 if (s->init_state.src_bucket.empty())
2501 return new RGWPutObj_ObjStore_SWIFT;
2502 else
2503 return new RGWCopyObj_ObjStore_SWIFT;
2504 }
2505
2506 RGWOp *RGWHandler_REST_Obj_SWIFT::op_delete()
2507 {
2508 return new RGWDeleteObj_ObjStore_SWIFT;
2509 }
2510
2511 RGWOp *RGWHandler_REST_Obj_SWIFT::op_post()
2512 {
2513 if (RGWFormPost::is_formpost_req(s)) {
2514 return new RGWFormPost;
2515 } else {
2516 return new RGWPutMetadataObject_ObjStore_SWIFT;
2517 }
2518 }
2519
2520 RGWOp *RGWHandler_REST_Obj_SWIFT::op_copy()
2521 {
2522 return new RGWCopyObj_ObjStore_SWIFT;
2523 }
2524
2525 RGWOp *RGWHandler_REST_Obj_SWIFT::op_options()
2526 {
2527 return new RGWOptionsCORS_ObjStore_SWIFT;
2528 }
2529
2530
2531 int RGWHandler_REST_SWIFT::authorize()
2532 {
2533 return rgw::auth::Strategy::apply(auth_strategy, s);
2534 }
2535
2536 int RGWHandler_REST_SWIFT::postauth_init()
2537 {
2538 struct req_init_state* t = &s->init_state;
2539
2540 /* XXX Stub this until Swift Auth sets account into URL. */
2541 s->bucket_tenant = s->user->user_id.tenant;
2542 s->bucket_name = t->url_bucket;
2543
2544 dout(10) << "s->object=" <<
2545 (!s->object.empty() ? s->object : rgw_obj_key("<NULL>"))
2546 << " s->bucket="
2547 << rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name)
2548 << dendl;
2549
2550 int ret;
2551 ret = validate_tenant_name(s->bucket_tenant);
2552 if (ret)
2553 return ret;
2554 ret = validate_bucket_name(s->bucket_name);
2555 if (ret)
2556 return ret;
2557 ret = validate_object_name(s->object.name);
2558 if (ret)
2559 return ret;
2560
2561 if (!t->src_bucket.empty()) {
2562 /*
2563 * We don't allow cross-tenant copy at present. It requires account
2564 * names in the URL for Swift.
2565 */
2566 s->src_tenant_name = s->user->user_id.tenant;
2567 s->src_bucket_name = t->src_bucket;
2568
2569 ret = validate_bucket_name(s->src_bucket_name);
2570 if (ret < 0) {
2571 return ret;
2572 }
2573 ret = validate_object_name(s->src_object.name);
2574 if (ret < 0) {
2575 return ret;
2576 }
2577 }
2578
2579 return 0;
2580 }
2581
2582 int RGWHandler_REST_SWIFT::validate_bucket_name(const string& bucket)
2583 {
2584 int ret = RGWHandler_REST::validate_bucket_name(bucket);
2585 if (ret < 0)
2586 return ret;
2587
2588 int len = bucket.size();
2589
2590 if (len == 0)
2591 return 0;
2592
2593 if (bucket[0] == '.')
2594 return -ERR_INVALID_BUCKET_NAME;
2595
2596 if (check_utf8(bucket.c_str(), len))
2597 return -ERR_INVALID_UTF8;
2598
2599 const char *s = bucket.c_str();
2600
2601 for (int i = 0; i < len; ++i, ++s) {
2602 if (*(unsigned char *)s == 0xff)
2603 return -ERR_INVALID_BUCKET_NAME;
2604 }
2605
2606 return 0;
2607 }
2608
2609 static void next_tok(string& str, string& tok, char delim)
2610 {
2611 if (str.size() == 0) {
2612 tok = "";
2613 return;
2614 }
2615 tok = str;
2616 int pos = str.find(delim);
2617 if (pos > 0) {
2618 tok = str.substr(0, pos);
2619 str = str.substr(pos + 1);
2620 } else {
2621 str = "";
2622 }
2623 }
2624
2625 int RGWHandler_REST_SWIFT::init_from_header(struct req_state* const s,
2626 const std::string& frontend_prefix)
2627 {
2628 string req;
2629 string first;
2630
2631 s->prot_flags |= RGW_REST_SWIFT;
2632
2633 char reqbuf[frontend_prefix.length() + s->decoded_uri.length() + 1];
2634 sprintf(reqbuf, "%s%s", frontend_prefix.c_str(), s->decoded_uri.c_str());
2635 const char *req_name = reqbuf;
2636
2637 const char *p;
2638
2639 if (*req_name == '?') {
2640 p = req_name;
2641 } else {
2642 p = s->info.request_params.c_str();
2643 }
2644
2645 s->info.args.set(p);
2646 s->info.args.parse();
2647
2648 /* Skip the leading slash of URL hierarchy. */
2649 if (req_name[0] != '/') {
2650 return 0;
2651 } else {
2652 req_name++;
2653 }
2654
2655 if ('\0' == req_name[0]) {
2656 return g_conf->rgw_swift_url_prefix == "/" ? -ERR_BAD_URL : 0;
2657 }
2658
2659 req = req_name;
2660
2661 size_t pos = req.find('/');
2662 if (std::string::npos != pos && g_conf->rgw_swift_url_prefix != "/") {
2663 bool cut_url = g_conf->rgw_swift_url_prefix.length();
2664 first = req.substr(0, pos);
2665
2666 if (first.compare(g_conf->rgw_swift_url_prefix) == 0) {
2667 if (cut_url) {
2668 /* Rewind to the "v1/..." part. */
2669 next_tok(req, first, '/');
2670 }
2671 }
2672 } else if (req.compare(g_conf->rgw_swift_url_prefix) == 0) {
2673 s->formatter = new RGWFormatter_Plain;
2674 return -ERR_BAD_URL;
2675 } else {
2676 first = req;
2677 }
2678
2679 std::string tenant_path;
2680 if (! g_conf->rgw_swift_tenant_name.empty()) {
2681 tenant_path = "/AUTH_";
2682 tenant_path.append(g_conf->rgw_swift_tenant_name);
2683 }
2684
2685 /* verify that the request_uri conforms with what's expected */
2686 char buf[g_conf->rgw_swift_url_prefix.length() + 16 + tenant_path.length()];
2687 int blen;
2688 if (g_conf->rgw_swift_url_prefix == "/") {
2689 blen = sprintf(buf, "/v1%s", tenant_path.c_str());
2690 } else {
2691 blen = sprintf(buf, "/%s/v1%s",
2692 g_conf->rgw_swift_url_prefix.c_str(), tenant_path.c_str());
2693 }
2694
2695 if (strncmp(reqbuf, buf, blen) != 0) {
2696 return -ENOENT;
2697 }
2698
2699 int ret = allocate_formatter(s, RGW_FORMAT_PLAIN, true);
2700 if (ret < 0)
2701 return ret;
2702
2703 string ver;
2704
2705 next_tok(req, ver, '/');
2706
2707 if (!tenant_path.empty() || g_conf->rgw_swift_account_in_url) {
2708 string account_name;
2709 next_tok(req, account_name, '/');
2710
2711 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2712 const vector<string> skipped_prefixes = { "AUTH_", "KEY_" };
2713
2714 for (const auto pfx : skipped_prefixes) {
2715 const size_t comp_len = min(account_name.length(), pfx.length());
2716 if (account_name.compare(0, comp_len, pfx) == 0) {
2717 /* Prefix is present. Drop it. */
2718 account_name = account_name.substr(comp_len);
2719 break;
2720 }
2721 }
2722
2723 if (account_name.empty()) {
2724 return -ERR_PRECONDITION_FAILED;
2725 } else {
2726 s->account_name = account_name;
2727 }
2728 }
2729
2730 next_tok(req, first, '/');
2731
2732 dout(10) << "ver=" << ver << " first=" << first << " req=" << req << dendl;
2733 if (first.size() == 0)
2734 return 0;
2735
2736 s->info.effective_uri = "/" + first;
2737
2738 // Save bucket to tide us over until token is parsed.
2739 s->init_state.url_bucket = first;
2740
2741 if (req.size()) {
2742 s->object =
2743 rgw_obj_key(req, s->info.env->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2744 s->info.effective_uri.append("/" + s->object.name);
2745 }
2746
2747 return 0;
2748 }
2749
2750 int RGWHandler_REST_SWIFT::init(RGWRados* store, struct req_state* s,
2751 rgw::io::BasicClient *cio)
2752 {
2753 struct req_init_state *t = &s->init_state;
2754
2755 s->dialect = "swift";
2756
2757 const char *copy_source = s->info.env->get("HTTP_X_COPY_FROM");
2758 if (copy_source) {
2759 bool result = RGWCopyObj::parse_copy_location(copy_source, t->src_bucket,
2760 s->src_object);
2761 if (!result)
2762 return -ERR_BAD_URL;
2763 }
2764
2765 if (s->op == OP_COPY) {
2766 const char *req_dest = s->info.env->get("HTTP_DESTINATION");
2767 if (!req_dest)
2768 return -ERR_BAD_URL;
2769
2770 string dest_bucket_name;
2771 rgw_obj_key dest_obj_key;
2772 bool result =
2773 RGWCopyObj::parse_copy_location(req_dest, dest_bucket_name,
2774 dest_obj_key);
2775 if (!result)
2776 return -ERR_BAD_URL;
2777
2778 string dest_object = dest_obj_key.name;
2779
2780 /* convert COPY operation into PUT */
2781 t->src_bucket = t->url_bucket;
2782 s->src_object = s->object;
2783 t->url_bucket = dest_bucket_name;
2784 s->object = rgw_obj_key(dest_object);
2785 s->op = OP_PUT;
2786 }
2787
2788 return RGWHandler_REST::init(store, s, cio);
2789 }
2790
2791 RGWHandler_REST*
2792 RGWRESTMgr_SWIFT::get_handler(struct req_state* const s,
2793 const rgw::auth::StrategyRegistry& auth_registry,
2794 const std::string& frontend_prefix)
2795 {
2796 int ret = RGWHandler_REST_SWIFT::init_from_header(s, frontend_prefix);
2797 if (ret < 0) {
2798 ldout(s->cct, 10) << "init_from_header returned err=" << ret << dendl;
2799 return nullptr;
2800 }
2801
2802 const auto& auth_strategy = auth_registry.get_swift();
2803
2804 if (s->init_state.url_bucket.empty()) {
2805 return new RGWHandler_REST_Service_SWIFT(auth_strategy);
2806 }
2807
2808 if (s->object.empty()) {
2809 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy);
2810 }
2811
2812 return new RGWHandler_REST_Obj_SWIFT(auth_strategy);
2813 }
2814
2815 RGWHandler_REST* RGWRESTMgr_SWIFT_Info::get_handler(
2816 struct req_state* const s,
2817 const rgw::auth::StrategyRegistry& auth_registry,
2818 const std::string& frontend_prefix)
2819 {
2820 s->prot_flags |= RGW_REST_SWIFT;
2821 const auto& auth_strategy = auth_registry.get_swift();
2822 return new RGWHandler_REST_SWIFT_Info(auth_strategy);
2823 }