1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include <boost/algorithm/string/predicate.hpp>
5 #include <boost/format.hpp>
6 #include <boost/optional.hpp>
7 #include <boost/utility/in_place_factory.hpp>
9 #include "include/ceph_assert.h"
12 #include "common/Formatter.h"
13 #include "common/utf8.h"
14 #include "common/ceph_json.h"
16 #include "rgw_rest_swift.h"
17 #include "rgw_acl_swift.h"
18 #include "rgw_cors_swift.h"
19 #include "rgw_formats.h"
20 #include "rgw_client_io.h"
23 #include "rgw_swift_auth.h"
25 #include "rgw_request.h"
26 #include "rgw_process.h"
30 #include "services/svc_zone.h"
36 #include <boost/utility/string_ref.hpp>
38 #define dout_context g_ceph_context
39 #define dout_subsys ceph_subsys_rgw
41 int RGWListBuckets_ObjStore_SWIFT::get_params()
43 prefix
= s
->info
.args
.get("prefix");
44 marker
= s
->info
.args
.get("marker");
45 end_marker
= s
->info
.args
.get("end_marker");
46 wants_reversed
= s
->info
.args
.exists("reverse");
49 std::swap(marker
, end_marker
);
52 std::string limit_str
= s
->info
.args
.get("limit");
53 if (!limit_str
.empty()) {
55 long l
= strict_strtol(limit_str
.c_str(), 10, &err
);
60 if (l
> (long)limit_max
|| l
< 0) {
61 return -ERR_PRECONDITION_FAILED
;
67 if (s
->cct
->_conf
->rgw_swift_need_stats
) {
69 int r
= s
->info
.args
.get_bool("stats", &stats
, &exists
);
85 static void dump_account_metadata(struct req_state
* const s
,
86 const RGWUsageStats
& global_stats
,
87 const std::map
<std::string
, RGWUsageStats
> &policies_stats
,
88 /* const */map
<string
, bufferlist
>& attrs
,
89 const RGWQuotaInfo
& quota
,
90 const RGWAccessControlPolicy_SWIFTAcct
&policy
)
92 /* Adding X-Timestamp to keep align with Swift API */
93 dump_header(s
, "X-Timestamp", ceph_clock_now());
95 dump_header(s
, "X-Account-Container-Count", global_stats
.buckets_count
);
96 dump_header(s
, "X-Account-Object-Count", global_stats
.objects_count
);
97 dump_header(s
, "X-Account-Bytes-Used", global_stats
.bytes_used
);
98 dump_header(s
, "X-Account-Bytes-Used-Actual", global_stats
.bytes_used_rounded
);
100 for (const auto& kv
: policies_stats
) {
101 const auto& policy_name
= camelcase_dash_http_attr(kv
.first
);
102 const auto& policy_stats
= kv
.second
;
104 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
105 "-Container-Count", policy_stats
.buckets_count
);
106 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
107 "-Object-Count", policy_stats
.objects_count
);
108 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
109 "-Bytes-Used", policy_stats
.bytes_used
);
110 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
111 "-Bytes-Used-Actual", policy_stats
.bytes_used_rounded
);
114 /* Dump TempURL-related stuff */
115 if (s
->perm_mask
== RGW_PERM_FULL_CONTROL
) {
116 auto iter
= s
->user
->temp_url_keys
.find(0);
117 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
118 dump_header(s
, "X-Account-Meta-Temp-Url-Key", iter
->second
);
121 iter
= s
->user
->temp_url_keys
.find(1);
122 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
123 dump_header(s
, "X-Account-Meta-Temp-Url-Key-2", iter
->second
);
127 /* Dump quota headers. */
129 if (quota
.max_size
>= 0) {
130 dump_header(s
, "X-Account-Meta-Quota-Bytes", quota
.max_size
);
133 /* Limit on the number of objects in a given account is a RadosGW's
134 * extension. Swift's account quota WSGI filter doesn't support it. */
135 if (quota
.max_objects
>= 0) {
136 dump_header(s
, "X-Account-Meta-Quota-Count", quota
.max_objects
);
140 /* Dump user-defined metadata items and generic attrs. */
141 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
142 map
<string
, bufferlist
>::iterator iter
;
143 for (iter
= attrs
.lower_bound(RGW_ATTR_PREFIX
); iter
!= attrs
.end(); ++iter
) {
144 const char *name
= iter
->first
.c_str();
145 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
147 if (geniter
!= rgw_to_http_attrs
.end()) {
148 dump_header(s
, geniter
->second
, iter
->second
);
149 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
150 dump_header_prefixed(s
, "X-Account-Meta-",
151 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
156 /* Dump account ACLs */
157 auto account_acls
= policy
.to_str();
159 dump_header(s
, "X-Account-Access-Control", std::move(*account_acls
));
163 void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets
)
166 set_req_state_err(s
, op_ret
);
167 } else if (!has_buckets
&& s
->format
== RGW_FORMAT_PLAIN
) {
168 op_ret
= STATUS_NO_CONTENT
;
169 set_req_state_err(s
, op_ret
);
172 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
173 /* Adding account stats in the header to keep align with Swift API */
174 dump_account_metadata(s
,
179 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
181 dump_header(s
, "Accept-Ranges", "bytes");
182 end_header(s
, NULL
, NULL
, NO_CONTENT_LENGTH
, true);
187 s
->formatter
->open_array_section_with_attrs("account",
188 FormatterAttrs("name", s
->user
->display_name
.c_str(), NULL
));
194 void RGWListBuckets_ObjStore_SWIFT::handle_listing_chunk(RGWUserBuckets
&& buckets
)
196 if (wants_reversed
) {
197 /* Just store in the reversal buffer. Its content will be handled later,
198 * in send_response_end(). */
199 reverse_buffer
.emplace(std::begin(reverse_buffer
), std::move(buckets
));
201 return send_response_data(buckets
);
205 void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets
& buckets
)
211 /* Take care of the prefix parameter of Swift API. There is no business
212 * in applying the filter earlier as we really need to go through all
213 * entries regardless of it (the headers like X-Account-Container-Count
214 * aren't affected by specifying prefix). */
215 const std::map
<std::string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
216 for (auto iter
= m
.lower_bound(prefix
);
217 iter
!= m
.end() && boost::algorithm::starts_with(iter
->first
, prefix
);
219 dump_bucket_entry(iter
->second
);
223 void RGWListBuckets_ObjStore_SWIFT::dump_bucket_entry(const RGWBucketEnt
& obj
)
225 s
->formatter
->open_object_section("container");
226 s
->formatter
->dump_string("name", obj
.bucket
.name
);
229 s
->formatter
->dump_int("count", obj
.count
);
230 s
->formatter
->dump_int("bytes", obj
.size
);
233 s
->formatter
->close_section();
235 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
236 rgw_flush_formatter(s
, s
->formatter
);
240 void RGWListBuckets_ObjStore_SWIFT::send_response_data_reversed(RGWUserBuckets
& buckets
)
246 /* Take care of the prefix parameter of Swift API. There is no business
247 * in applying the filter earlier as we really need to go through all
248 * entries regardless of it (the headers like X-Account-Container-Count
249 * aren't affected by specifying prefix). */
250 std::map
<std::string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
252 auto iter
= m
.rbegin();
253 for (/* initialized above */;
254 iter
!= m
.rend() && !boost::algorithm::starts_with(iter
->first
, prefix
);
259 for (/* iter carried */;
260 iter
!= m
.rend() && boost::algorithm::starts_with(iter
->first
, prefix
);
262 dump_bucket_entry(iter
->second
);
266 void RGWListBuckets_ObjStore_SWIFT::send_response_end()
268 if (wants_reversed
) {
269 for (auto& buckets
: reverse_buffer
) {
270 send_response_data_reversed(buckets
);
275 s
->formatter
->close_section();
278 if (s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
279 /* Adding account stats in the header to keep align with Swift API */
280 dump_account_metadata(s
,
285 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
287 end_header(s
, nullptr, nullptr, s
->formatter
->get_len(), true);
290 if (sent_data
|| s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
291 rgw_flush_formatter_and_reset(s
, s
->formatter
);
295 int RGWListBucket_ObjStore_SWIFT::get_params()
297 prefix
= s
->info
.args
.get("prefix");
298 marker
= s
->info
.args
.get("marker");
299 end_marker
= s
->info
.args
.get("end_marker");
300 max_keys
= s
->info
.args
.get("limit");
303 s
->info
.args
.get_bool("allow_unordered", &allow_unordered
, false);
305 delimiter
= s
->info
.args
.get("delimiter");
307 op_ret
= parse_max_keys();
311 // S3 behavior is to silently cap the max-keys.
312 // Swift behavior is to abort.
313 if (max
> default_max
)
314 return -ERR_PRECONDITION_FAILED
;
317 if (s
->info
.args
.exists("path")) { // should handle empty path
318 path_args
= s
->info
.args
.get("path");
319 if (!delimiter
.empty() || !prefix
.empty()) {
326 if (path
.size() && path
[path
.size() - 1] != '/')
329 int len
= prefix
.size();
330 int delim_size
= delimiter
.size();
332 if (len
>= delim_size
) {
333 if (prefix
.substr(len
- delim_size
).compare(delimiter
) != 0)
334 prefix
.append(delimiter
);
341 static void dump_container_metadata(struct req_state
*,
344 const RGWBucketWebsiteConf
&);
346 void RGWListBucket_ObjStore_SWIFT::send_response()
348 vector
<rgw_bucket_dir_entry
>::iterator iter
= objs
.begin();
349 map
<string
, bool>::iterator pref_iter
= common_prefixes
.begin();
352 dump_container_metadata(s
, bucket
, bucket_quota
,
353 s
->bucket_info
.website_conf
);
355 s
->formatter
->open_array_section_with_attrs("container",
356 FormatterAttrs("name",
357 s
->bucket
.name
.c_str(),
360 while (iter
!= objs
.end() || pref_iter
!= common_prefixes
.end()) {
361 bool do_pref
= false;
362 bool do_objs
= false;
364 if (iter
!= objs
.end()) {
367 if (pref_iter
== common_prefixes
.end())
369 else if (iter
== objs
.end())
371 else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) == 0) {
374 } else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) <= 0)
379 if (do_objs
&& (allow_unordered
|| marker
.empty() || marker
< key
)) {
380 if (key
.name
.compare(path
) == 0)
383 s
->formatter
->open_object_section("object");
384 s
->formatter
->dump_string("name", key
.name
);
385 s
->formatter
->dump_string("hash", iter
->meta
.etag
);
386 s
->formatter
->dump_int("bytes", iter
->meta
.accounted_size
);
387 if (!iter
->meta
.user_data
.empty())
388 s
->formatter
->dump_string("user_custom_data", iter
->meta
.user_data
);
389 string single_content_type
= iter
->meta
.content_type
;
390 if (iter
->meta
.content_type
.size()) {
391 // content type might hold multiple values, just dump the last one
392 ssize_t pos
= iter
->meta
.content_type
.rfind(',');
395 while (single_content_type
[pos
] == ' ')
397 single_content_type
= single_content_type
.substr(pos
);
399 s
->formatter
->dump_string("content_type", single_content_type
);
401 dump_time(s
, "last_modified", &iter
->meta
.mtime
);
402 s
->formatter
->close_section();
405 if (do_pref
&& (marker
.empty() || pref_iter
->first
.compare(marker
.name
) > 0)) {
406 const string
& name
= pref_iter
->first
;
407 if (name
.compare(delimiter
) == 0)
410 s
->formatter
->open_object_section_with_attrs("subdir", FormatterAttrs("name", name
.c_str(), NULL
));
412 /* swift is a bit inconsistent here */
415 s
->formatter
->dump_string("name", name
);
418 s
->formatter
->dump_string("subdir", name
);
420 s
->formatter
->close_section();
429 s
->formatter
->close_section();
431 int64_t content_len
= 0;
433 content_len
= s
->formatter
->get_len();
434 if (content_len
== 0) {
435 op_ret
= STATUS_NO_CONTENT
;
437 } else if (op_ret
> 0) {
441 set_req_state_err(s
, op_ret
);
443 end_header(s
, this, NULL
, content_len
);
448 rgw_flush_formatter_and_reset(s
, s
->formatter
);
449 } // RGWListBucket_ObjStore_SWIFT::send_response
451 static void dump_container_metadata(struct req_state
*s
,
452 const RGWBucketEnt
& bucket
,
453 const RGWQuotaInfo
& quota
,
454 const RGWBucketWebsiteConf
& ws_conf
)
456 /* Adding X-Timestamp to keep align with Swift API */
457 dump_header(s
, "X-Timestamp", utime_t(s
->bucket_info
.creation_time
));
459 dump_header(s
, "X-Container-Object-Count", bucket
.count
);
460 dump_header(s
, "X-Container-Bytes-Used", bucket
.size
);
461 dump_header(s
, "X-Container-Bytes-Used-Actual", bucket
.size_rounded
);
463 if (s
->object
.empty()) {
464 auto swift_policy
= \
465 static_cast<RGWAccessControlPolicy_SWIFT
*>(s
->bucket_acl
.get());
466 std::string read_acl
, write_acl
;
467 swift_policy
->to_str(read_acl
, write_acl
);
469 if (read_acl
.size()) {
470 dump_header(s
, "X-Container-Read", read_acl
);
472 if (write_acl
.size()) {
473 dump_header(s
, "X-Container-Write", write_acl
);
475 if (!s
->bucket_info
.placement_rule
.name
.empty()) {
476 dump_header(s
, "X-Storage-Policy", s
->bucket_info
.placement_rule
.name
);
478 dump_header(s
, "X-Storage-Class", s
->bucket_info
.placement_rule
.get_storage_class());
480 /* Dump user-defined metadata items and generic attrs. */
481 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
482 map
<string
, bufferlist
>::iterator iter
;
483 for (iter
= s
->bucket_attrs
.lower_bound(RGW_ATTR_PREFIX
);
484 iter
!= s
->bucket_attrs
.end();
486 const char *name
= iter
->first
.c_str();
487 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
489 if (geniter
!= rgw_to_http_attrs
.end()) {
490 dump_header(s
, geniter
->second
, iter
->second
);
491 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
492 dump_header_prefixed(s
, "X-Container-Meta-",
493 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
499 /* Dump container versioning info. */
500 if (! s
->bucket_info
.swift_ver_location
.empty()) {
501 dump_header(s
, "X-Versions-Location",
502 url_encode(s
->bucket_info
.swift_ver_location
));
505 /* Dump quota headers. */
507 if (quota
.max_size
>= 0) {
508 dump_header(s
, "X-Container-Meta-Quota-Bytes", quota
.max_size
);
511 if (quota
.max_objects
>= 0) {
512 dump_header(s
, "X-Container-Meta-Quota-Count", quota
.max_objects
);
516 /* Dump Static Website headers. */
517 if (! ws_conf
.index_doc_suffix
.empty()) {
518 dump_header(s
, "X-Container-Meta-Web-Index", ws_conf
.index_doc_suffix
);
521 if (! ws_conf
.error_doc
.empty()) {
522 dump_header(s
, "X-Container-Meta-Web-Error", ws_conf
.error_doc
);
525 if (! ws_conf
.subdir_marker
.empty()) {
526 dump_header(s
, "X-Container-Meta-Web-Directory-Type",
527 ws_conf
.subdir_marker
);
530 if (! ws_conf
.listing_css_doc
.empty()) {
531 dump_header(s
, "X-Container-Meta-Web-Listings-CSS",
532 ws_conf
.listing_css_doc
);
535 if (ws_conf
.listing_enabled
) {
536 dump_header(s
, "X-Container-Meta-Web-Listings", "true");
539 /* Dump bucket's modification time. Compliance with the Swift API really
541 dump_last_modified(s
, s
->bucket_mtime
);
544 void RGWStatAccount_ObjStore_SWIFT::execute()
546 RGWStatAccount_ObjStore::execute();
547 op_ret
= rgw_get_user_attrs_by_uid(store
, s
->user
->user_id
, attrs
);
550 void RGWStatAccount_ObjStore_SWIFT::send_response()
553 op_ret
= STATUS_NO_CONTENT
;
554 dump_account_metadata(s
,
559 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
562 set_req_state_err(s
, op_ret
);
565 end_header(s
, NULL
, NULL
, 0, true);
570 void RGWStatBucket_ObjStore_SWIFT::send_response()
573 op_ret
= STATUS_NO_CONTENT
;
574 dump_container_metadata(s
, bucket
, bucket_quota
,
575 s
->bucket_info
.website_conf
);
578 set_req_state_err(s
, op_ret
);
581 end_header(s
, this, NULL
, 0, true);
585 static int get_swift_container_settings(req_state
* const s
,
586 RGWRados
* const store
,
587 RGWAccessControlPolicy
* const policy
,
588 bool * const has_policy
,
590 RGWCORSConfiguration
* const cors_config
,
591 bool * const has_cors
)
593 const char * const read_list
= s
->info
.env
->get("HTTP_X_CONTAINER_READ");
594 const char * const write_list
= s
->info
.env
->get("HTTP_X_CONTAINER_WRITE");
598 if (read_list
|| write_list
) {
599 RGWAccessControlPolicy_SWIFT
swift_policy(s
->cct
);
600 const auto r
= swift_policy
.create(store
,
602 s
->user
->display_name
,
610 *policy
= swift_policy
;
616 /*Check and update CORS configuration*/
617 const char *allow_origins
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
618 const char *allow_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
619 const char *expose_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
620 const char *max_age
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
622 RGWCORSConfiguration_SWIFT
*swift_cors
= new RGWCORSConfiguration_SWIFT
;
623 int r
= swift_cors
->create_update(allow_origins
, allow_headers
, expose_headers
, max_age
);
625 dout(0) << "Error creating/updating the cors configuration" << dendl
;
630 *cors_config
= *swift_cors
;
638 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
639 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
640 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
641 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
643 static void get_rmattrs_from_headers(const req_state
* const s
,
644 const char * const put_prefix
,
645 const char * const del_prefix
,
646 set
<string
>& rmattr_names
)
648 const size_t put_prefix_len
= strlen(put_prefix
);
649 const size_t del_prefix_len
= strlen(del_prefix
);
651 for (const auto& kv
: s
->info
.env
->get_map()) {
652 size_t prefix_len
= 0;
653 const char * const p
= kv
.first
.c_str();
655 if (strncasecmp(p
, del_prefix
, del_prefix_len
) == 0) {
656 /* Explicitly requested removal. */
657 prefix_len
= del_prefix_len
;
658 } else if ((strncasecmp(p
, put_prefix
, put_prefix_len
) == 0)
659 && kv
.second
.empty()) {
660 /* Removal requested by putting an empty value. */
661 prefix_len
= put_prefix_len
;
664 if (prefix_len
> 0) {
665 string
name(RGW_ATTR_META_PREFIX
);
666 name
.append(lowercase_dash_http_attr(p
+ prefix_len
));
667 rmattr_names
.insert(name
);
672 static int get_swift_versioning_settings(
674 boost::optional
<std::string
>& swift_ver_location
)
676 /* Removing the Swift's versions location has lower priority than setting
677 * a new one. That's the reason why we're handling it first. */
678 const std::string vlocdel
=
679 s
->info
.env
->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
680 if (vlocdel
.size()) {
681 swift_ver_location
= boost::in_place(std::string());
684 if (s
->info
.env
->exists("HTTP_X_VERSIONS_LOCATION")) {
685 /* If the Swift's versioning is globally disabled but someone wants to
686 * enable it for a given container, new version of Swift will generate
687 * the precondition failed error. */
688 if (! s
->cct
->_conf
->rgw_swift_versioning_enabled
) {
689 return -ERR_PRECONDITION_FAILED
;
692 swift_ver_location
= s
->info
.env
->get("HTTP_X_VERSIONS_LOCATION", "");
698 int RGWCreateBucket_ObjStore_SWIFT::get_params()
701 uint32_t policy_rw_mask
= 0;
703 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
704 &policy_rw_mask
, &cors_config
, &has_cors
);
710 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
713 location_constraint
= store
->svc
.zone
->get_zonegroup().api_name
;
714 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
,
715 CONT_REMOVE_ATTR_PREFIX
, rmattr_names
);
716 placement_rule
.init(s
->info
.env
->get("HTTP_X_STORAGE_POLICY", ""), s
->info
.storage_class
);
718 return get_swift_versioning_settings(s
, swift_ver_location
);
721 static inline int handle_metadata_errors(req_state
* const s
, const int op_ret
)
723 if (op_ret
== -EFBIG
) {
724 /* Handle the custom error message of exceeding maximum custom attribute
725 * (stored as xattr) size. */
726 const auto error_message
= boost::str(
727 boost::format("Metadata value longer than %lld")
728 % s
->cct
->_conf
.get_val
<Option::size_t>("rgw_max_attr_size"));
729 set_req_state_err(s
, EINVAL
, error_message
);
731 } else if (op_ret
== -E2BIG
) {
732 const auto error_message
= boost::str(
733 boost::format("Too many metadata items; max %lld")
734 % s
->cct
->_conf
.get_val
<uint64_t>("rgw_max_attrs_num_in_req"));
735 set_req_state_err(s
, EINVAL
, error_message
);
742 void RGWCreateBucket_ObjStore_SWIFT::send_response()
744 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
745 if (meta_ret
!= op_ret
) {
749 op_ret
= STATUS_CREATED
;
750 } else if (op_ret
== -ERR_BUCKET_EXISTS
) {
751 op_ret
= STATUS_ACCEPTED
;
753 set_req_state_err(s
, op_ret
);
757 /* Propose ending HTTP header with 0 Content-Length header. */
758 end_header(s
, NULL
, NULL
, 0);
759 rgw_flush_formatter_and_reset(s
, s
->formatter
);
762 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
766 r
= STATUS_NO_CONTENT
;
768 set_req_state_err(s
, r
);
770 end_header(s
, this, NULL
, 0);
771 rgw_flush_formatter_and_reset(s
, s
->formatter
);
774 static int get_delete_at_param(req_state
*s
, boost::optional
<real_time
> &delete_at
)
776 /* Handle Swift object expiration. */
777 real_time delat_proposal
;
778 string x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AFTER", "");
780 if (x_delete
.empty()) {
781 x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AT", "");
783 /* X-Delete-After HTTP is present. It means we need add its value
784 * to the current time. */
785 delat_proposal
= real_clock::now();
788 if (x_delete
.empty()) {
789 delete_at
= boost::none
;
790 if (s
->info
.env
->exists("HTTP_X_REMOVE_DELETE_AT")) {
791 delete_at
= boost::in_place(real_time());
796 long ts
= strict_strtoll(x_delete
.c_str(), 10, &err
);
802 delat_proposal
+= make_timespan(ts
);
803 if (delat_proposal
< real_clock::now()) {
807 delete_at
= delat_proposal
;
812 int RGWPutObj_ObjStore_SWIFT::verify_permission()
814 op_ret
= RGWPutObj_ObjStore::verify_permission();
816 /* We have to differentiate error codes depending on whether user is
817 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
818 * (403 Forbidden). */
819 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
826 int RGWPutObj_ObjStore_SWIFT::update_slo_segment_size(rgw_slo_entry
& entry
) {
829 const string
& path
= entry
.path
;
831 /* If the path starts with slashes, strip them all. */
832 const size_t pos_init
= path
.find_first_not_of('/');
834 if (pos_init
== string::npos
) {
838 const size_t pos_sep
= path
.find('/', pos_init
);
839 if (pos_sep
== string::npos
) {
843 string bucket_name
= path
.substr(pos_init
, pos_sep
- pos_init
);
844 string obj_name
= path
.substr(pos_sep
+ 1);
848 if (bucket_name
.compare(s
->bucket
.name
) != 0) {
849 RGWBucketInfo bucket_info
;
850 map
<string
, bufferlist
> bucket_attrs
;
851 auto obj_ctx
= store
->svc
.sysobj
->init_obj_ctx();
852 r
= store
->get_bucket_info(obj_ctx
, s
->user
->user_id
.tenant
,
853 bucket_name
, bucket_info
, nullptr,
856 ldpp_dout(this, 0) << "could not get bucket info for bucket="
857 << bucket_name
<< dendl
;
860 bucket
= bucket_info
.bucket
;
865 /* fetch the stored size of the seg (or error if not valid) */
866 rgw_obj_key
slo_key(obj_name
);
867 rgw_obj
slo_seg(bucket
, slo_key
);
870 RGWObjectCtx
obj_ctx(store
);
871 obj_ctx
.set_atomic(slo_seg
);
873 RGWRados::Object
op_target(store
, s
->bucket_info
, obj_ctx
, slo_seg
);
874 RGWRados::Object::Read
read_op(&op_target
);
877 RGWCompressionInfo cs_info
;
878 map
<std::string
, buffer::list
> attrs
;
879 uint64_t size_bytes
{0};
881 read_op
.params
.attrs
= &attrs
;
882 read_op
.params
.obj_size
= &size_bytes
;
884 r
= read_op
.prepare();
889 r
= rgw_compression_info_from_attrset(attrs
, compressed
, cs_info
);
895 size_bytes
= cs_info
.orig_size
;
898 /* "When the PUT operation sees the multipart-manifest=put query
899 * parameter, it reads the request body and verifies that each
900 * segment object exists and that the sizes and ETags match. If
901 * there is a mismatch, the PUT operation fails."
903 if (entry
.size_bytes
&&
904 (entry
.size_bytes
!= size_bytes
)) {
908 entry
.size_bytes
= size_bytes
;
911 } /* RGWPutObj_ObjStore_SWIFT::update_slo_segment_sizes */
913 int RGWPutObj_ObjStore_SWIFT::get_params()
915 if (s
->has_bad_meta
) {
920 const char *encoding
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
921 if (!encoding
|| strcmp(encoding
, "chunked") != 0) {
922 ldout(s
->cct
, 20) << "neither length nor chunked encoding" << dendl
;
923 return -ERR_LENGTH_REQUIRED
;
926 chunked_upload
= true;
929 supplied_etag
= s
->info
.env
->get("HTTP_ETAG");
931 if (!s
->generic_attrs
.count(RGW_ATTR_CONTENT_TYPE
)) {
932 ldout(s
->cct
, 5) << "content type wasn't provided, trying to guess" << dendl
;
933 const char *suffix
= strrchr(s
->object
.name
.c_str(), '.');
937 string
suffix_str(suffix
);
938 const char *mime
= rgw_find_mime_by_ext(suffix_str
);
940 s
->generic_attrs
[RGW_ATTR_CONTENT_TYPE
] = mime
;
946 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
948 int r
= get_delete_at_param(s
, delete_at
);
950 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
954 if (!s
->cct
->_conf
->rgw_swift_custom_header
.empty()) {
955 string custom_header
= s
->cct
->_conf
->rgw_swift_custom_header
;
956 if (s
->info
.env
->exists(custom_header
.c_str())) {
957 user_data
= s
->info
.env
->get(custom_header
.c_str());
961 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
963 string multipart_manifest
= s
->info
.args
.get("multipart-manifest", &exists
);
965 if (multipart_manifest
!= "put") {
966 ldout(s
->cct
, 5) << "invalid multipart-manifest http param: " << multipart_manifest
<< dendl
;
970 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
971 uint64_t max_len
= s
->cct
->_conf
->rgw_max_slo_entries
* MAX_SLO_ENTRY_SIZE
;
973 slo_info
= new RGWSLOInfo
;
976 std::tie(r
, slo_info
->raw_data
) = rgw_rest_get_json_input_keep_data(s
->cct
, s
, slo_info
->entries
, max_len
);
978 ldout(s
->cct
, 5) << "failed to read input for slo r=" << r
<< dendl
;
982 if ((int64_t)slo_info
->entries
.size() > s
->cct
->_conf
->rgw_max_slo_entries
) {
983 ldout(s
->cct
, 5) << "too many entries in slo request: " << slo_info
->entries
.size() << dendl
;
988 uint64_t total_size
= 0;
989 for (auto& entry
: slo_info
->entries
) {
990 etag_sum
.Update((const unsigned char *)entry
.etag
.c_str(),
991 entry
.etag
.length());
993 /* if size_bytes == 0, it should be replaced with the
994 * real segment size (which could be 0); this follows from the
995 * fact that Swift requires all segments to exist, but permits
996 * the size_bytes element to be omitted from the SLO manifest, see
997 * https://docs.openstack.org/swift/latest/api/large_objects.html
999 r
= update_slo_segment_size(entry
);
1004 total_size
+= entry
.size_bytes
;
1006 ldout(s
->cct
, 20) << "slo_part: " << entry
.path
1007 << " size=" << entry
.size_bytes
1008 << " etag=" << entry
.etag
1011 complete_etag(etag_sum
, &lo_etag
);
1012 slo_info
->total_size
= total_size
;
1014 ofs
= slo_info
->raw_data
.length();
1017 return RGWPutObj_ObjStore::get_params();
1020 void RGWPutObj_ObjStore_SWIFT::send_response()
1022 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
1027 op_ret
= STATUS_CREATED
;
1029 set_req_state_err(s
, op_ret
);
1032 if (! lo_etag
.empty()) {
1033 /* Static Large Object of Swift API has two etags represented by
1034 * following members:
1035 * - etag - for the manifest itself (it will be stored in xattrs),
1036 * - lo_etag - for the content composited from SLO's segments.
1037 * The value is calculated basing on segments' etags.
1038 * In response for PUT request we have to expose the second one.
1039 * The first one may be obtained by GET with "multipart-manifest=get"
1040 * in query string on a given SLO. */
1041 dump_etag(s
, lo_etag
, true /* quoted */);
1046 dump_last_modified(s
, mtime
);
1047 set_req_state_err(s
, op_ret
);
1049 end_header(s
, this);
1050 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1053 static int get_swift_account_settings(req_state
* const s
,
1054 RGWRados
* const store
,
1055 RGWAccessControlPolicy_SWIFTAcct
* const policy
,
1056 bool * const has_policy
)
1058 *has_policy
= false;
1060 const char * const acl_attr
= s
->info
.env
->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
1062 RGWAccessControlPolicy_SWIFTAcct
swift_acct_policy(s
->cct
);
1063 const bool r
= swift_acct_policy
.create(store
,
1065 s
->user
->display_name
,
1071 *policy
= swift_acct_policy
;
1078 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
1080 if (s
->has_bad_meta
) {
1084 int ret
= get_swift_account_settings(s
,
1086 // FIXME: we need to carry unique_ptr in generic class
1087 // and allocate appropriate ACL class in the ctor
1088 static_cast<RGWAccessControlPolicy_SWIFTAcct
*>(&policy
),
1094 get_rmattrs_from_headers(s
, ACCT_PUT_ATTR_PREFIX
, ACCT_REMOVE_ATTR_PREFIX
,
1099 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
1101 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
1102 if (meta_ret
!= op_ret
) {
1106 op_ret
= STATUS_NO_CONTENT
;
1108 set_req_state_err(s
, op_ret
);
1112 end_header(s
, this);
1113 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1116 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
1118 if (s
->has_bad_meta
) {
1122 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
1123 &policy_rw_mask
, &cors_config
, &has_cors
);
1128 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
, CONT_REMOVE_ATTR_PREFIX
,
1130 placement_rule
.init(s
->info
.env
->get("HTTP_X_STORAGE_POLICY", ""), s
->info
.storage_class
);
1132 return get_swift_versioning_settings(s
, swift_ver_location
);
1135 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
1137 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
1138 if (meta_ret
!= op_ret
) {
1141 if (!op_ret
&& (op_ret
!= -EINVAL
)) {
1142 op_ret
= STATUS_NO_CONTENT
;
1144 set_req_state_err(s
, op_ret
);
1148 end_header(s
, this);
1149 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1152 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
1154 if (s
->has_bad_meta
) {
1158 /* Handle Swift object expiration. */
1159 int r
= get_delete_at_param(s
, delete_at
);
1161 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1165 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
1170 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
1172 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
1173 if (meta_ret
!= op_ret
) {
1177 op_ret
= STATUS_ACCEPTED
;
1179 set_req_state_err(s
, op_ret
);
1183 dump_content_length(s
, 0);
1187 end_header(s
, this);
1188 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1191 static void bulkdelete_respond(const unsigned num_deleted
,
1192 const unsigned int num_unfound
,
1193 const std::list
<RGWBulkDelete::fail_desc_t
>& failures
,
1194 const int prot_flags
, /* in */
1195 ceph::Formatter
& formatter
) /* out */
1197 formatter
.open_object_section("delete");
1202 if (!failures
.empty()) {
1203 int reason
= ERR_INVALID_REQUEST
;
1204 for (const auto fail_desc
: failures
) {
1205 if (-ENOENT
!= fail_desc
.err
&& -EACCES
!= fail_desc
.err
) {
1206 reason
= fail_desc
.err
;
1210 set_req_state_err(err
, reason
, prot_flags
);
1211 dump_errno(err
, resp_status
);
1212 } else if (0 == num_deleted
&& 0 == num_unfound
) {
1213 /* 400 Bad Request */
1214 dump_errno(400, resp_status
);
1215 resp_body
= "Invalid bulk delete.";
1218 dump_errno(200, resp_status
);
1221 encode_json("Number Deleted", num_deleted
, &formatter
);
1222 encode_json("Number Not Found", num_unfound
, &formatter
);
1223 encode_json("Response Body", resp_body
, &formatter
);
1224 encode_json("Response Status", resp_status
, &formatter
);
1226 formatter
.open_array_section("Errors");
1227 for (const auto fail_desc
: failures
) {
1228 formatter
.open_array_section("object");
1230 stringstream ss_name
;
1231 ss_name
<< fail_desc
.path
;
1232 encode_json("Name", ss_name
.str(), &formatter
);
1235 set_req_state_err(err
, fail_desc
.err
, prot_flags
);
1237 dump_errno(err
, status
);
1238 encode_json("Status", status
, &formatter
);
1239 formatter
.close_section();
1241 formatter
.close_section();
1243 formatter
.close_section();
1246 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1248 op_ret
= RGWDeleteObj_ObjStore::verify_permission();
1250 /* We have to differentiate error codes depending on whether user is
1251 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1252 * (403 Forbidden). */
1253 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1260 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1262 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1263 multipart_delete
= (mm
.compare("delete") == 0);
1265 return RGWDeleteObj_ObjStore::get_params();
1268 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1272 if (multipart_delete
) {
1275 r
= STATUS_NO_CONTENT
;
1278 set_req_state_err(s
, r
);
1281 if (multipart_delete
) {
1282 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1283 CHUNKED_TRANSFER_ENCODING
);
1286 bulkdelete_respond(deleter
->get_num_deleted(),
1287 deleter
->get_num_unfound(),
1288 deleter
->get_failures(),
1291 } else if (-ENOENT
== op_ret
) {
1292 bulkdelete_respond(0, 1, {}, s
->prot_flags
, *s
->formatter
);
1294 RGWBulkDelete::acct_path_t path
;
1295 path
.bucket_name
= s
->bucket_name
;
1296 path
.obj_key
= s
->object
;
1298 RGWBulkDelete::fail_desc_t fail_desc
;
1299 fail_desc
.err
= op_ret
;
1300 fail_desc
.path
= path
;
1302 bulkdelete_respond(0, 0, { fail_desc
}, s
->prot_flags
, *s
->formatter
);
1305 end_header(s
, this);
1308 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1312 static void get_contype_from_attrs(map
<string
, bufferlist
>& attrs
,
1313 string
& content_type
)
1315 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_CONTENT_TYPE
);
1316 if (iter
!= attrs
.end()) {
1317 content_type
= rgw_bl_str(iter
->second
);
1321 static void dump_object_metadata(struct req_state
* const s
,
1322 const map
<string
, bufferlist
>& attrs
)
1324 map
<string
, string
> response_attrs
;
1326 for (auto kv
: attrs
) {
1327 const char * name
= kv
.first
.c_str();
1328 const auto aiter
= rgw_to_http_attrs
.find(name
);
1330 if (aiter
!= std::end(rgw_to_http_attrs
)) {
1331 response_attrs
[aiter
->second
] = rgw_bl_str(kv
.second
);
1332 } else if (strcmp(name
, RGW_ATTR_SLO_UINDICATOR
) == 0) {
1333 // this attr has an extra length prefix from encode() in prior versions
1334 dump_header(s
, "X-Object-Meta-Static-Large-Object", "True");
1335 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
,
1336 sizeof(RGW_ATTR_META_PREFIX
)-1) == 0) {
1337 name
+= sizeof(RGW_ATTR_META_PREFIX
) - 1;
1338 dump_header_prefixed(s
, "X-Object-Meta-",
1339 camelcase_dash_http_attr(name
), kv
.second
);
1343 /* Handle override and fallback for Content-Disposition HTTP header.
1344 * At the moment this will be used only by TempURL of the Swift API. */
1345 const auto cditer
= rgw_to_http_attrs
.find(RGW_ATTR_CONTENT_DISP
);
1346 if (cditer
!= std::end(rgw_to_http_attrs
)) {
1347 const auto& name
= cditer
->second
;
1349 if (!s
->content_disp
.override
.empty()) {
1350 response_attrs
[name
] = s
->content_disp
.override
;
1351 } else if (!s
->content_disp
.fallback
.empty()
1352 && response_attrs
.find(name
) == std::end(response_attrs
)) {
1353 response_attrs
[name
] = s
->content_disp
.fallback
;
1357 for (const auto kv
: response_attrs
) {
1358 dump_header(s
, kv
.first
, kv
.second
);
1361 const auto iter
= attrs
.find(RGW_ATTR_DELETE_AT
);
1362 if (iter
!= std::end(attrs
)) {
1365 decode(delete_at
, iter
->second
);
1366 if (!delete_at
.is_zero()) {
1367 dump_header(s
, "X-Delete-At", delete_at
.sec());
1369 } catch (buffer::error
& err
) {
1370 ldout(s
->cct
, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1377 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1379 dest_policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1384 int RGWCopyObj_ObjStore_SWIFT::get_params()
1386 if_mod
= s
->info
.env
->get("HTTP_IF_MODIFIED_SINCE");
1387 if_unmod
= s
->info
.env
->get("HTTP_IF_UNMODIFIED_SINCE");
1388 if_match
= s
->info
.env
->get("HTTP_COPY_IF_MATCH");
1389 if_nomatch
= s
->info
.env
->get("HTTP_COPY_IF_NONE_MATCH");
1391 src_tenant_name
= s
->src_tenant_name
;
1392 src_bucket_name
= s
->src_bucket_name
;
1393 src_object
= s
->src_object
;
1394 dest_tenant_name
= s
->bucket_tenant
;
1395 dest_bucket_name
= s
->bucket_name
;
1396 dest_object
= s
->object
.name
;
1398 const char * const fresh_meta
= s
->info
.env
->get("HTTP_X_FRESH_METADATA");
1399 if (fresh_meta
&& strcasecmp(fresh_meta
, "TRUE") == 0) {
1400 attrs_mod
= RGWRados::ATTRSMOD_REPLACE
;
1402 attrs_mod
= RGWRados::ATTRSMOD_MERGE
;
1405 int r
= get_delete_at_param(s
, delete_at
);
1407 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1414 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs
)
1416 if (! sent_header
) {
1418 op_ret
= STATUS_CREATED
;
1419 set_req_state_err(s
, op_ret
);
1421 end_header(s
, this);
1423 /* Send progress information. Note that this diverge from the original swift
1424 * spec. We do this in order to keep connection alive.
1427 s
->formatter
->open_array_section("progress");
1431 s
->formatter
->dump_int("ofs", (uint64_t)ofs
);
1433 rgw_flush_formatter(s
, s
->formatter
);
1436 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1438 /* Dump X-Copied-From. */
1439 dump_header(s
, "X-Copied-From", url_encode(src_bucket
.name
) +
1440 "/" + url_encode(src_object
.name
));
1442 /* Dump X-Copied-From-Account. */
1444 dump_header(s
, "X-Copied-From-Account", url_encode(s
->user
->user_id
.id
));
1446 /* Dump X-Copied-From-Last-Modified. */
1447 dump_time_header(s
, "X-Copied-From-Last-Modified", src_mtime
);
1450 void RGWCopyObj_ObjStore_SWIFT::send_response()
1452 if (! sent_header
) {
1453 string content_type
;
1455 op_ret
= STATUS_CREATED
;
1456 set_req_state_err(s
, op_ret
);
1459 dump_last_modified(s
, mtime
);
1461 get_contype_from_attrs(attrs
, content_type
);
1462 dump_object_metadata(s
, attrs
);
1463 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1464 : "binary/octet-stream");
1466 s
->formatter
->close_section();
1467 rgw_flush_formatter(s
, s
->formatter
);
1471 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1473 op_ret
= RGWGetObj_ObjStore::verify_permission();
1475 /* We have to differentiate error codes depending on whether user is
1476 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1477 * (403 Forbidden). */
1478 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1485 int RGWGetObj_ObjStore_SWIFT::get_params()
1487 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1488 skip_manifest
= (mm
.compare("get") == 0);
1490 return RGWGetObj_ObjStore::get_params();
1493 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1495 std::string error_content
;
1496 op_ret
= error_handler(op_ret
, &error_content
);
1498 /* The error handler has taken care of the error. */
1502 bufferlist error_bl
;
1503 error_bl
.append(error_content
);
1504 return send_response_data(error_bl
, 0, error_bl
.length());
1507 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist
& bl
,
1511 string content_type
;
1517 if (custom_http_ret
) {
1518 set_req_state_err(s
, 0);
1519 dump_errno(s
, custom_http_ret
);
1521 set_req_state_err(s
, (partial_content
&& !op_ret
) ? STATUS_PARTIAL_CONTENT
1526 end_header(s
, NULL
);
1532 dump_range(s
, ofs
, end
, s
->obj_size
);
1536 end_header(s
, NULL
);
1540 dump_content_length(s
, total_len
);
1541 dump_last_modified(s
, lastmod
);
1542 dump_header(s
, "X-Timestamp", utime_t(lastmod
));
1544 dump_header(s
, "X-Static-Large-Object", "True");
1548 if (! lo_etag
.empty()) {
1549 dump_etag(s
, lo_etag
, true /* quoted */);
1551 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
1552 if (iter
!= attrs
.end()) {
1553 dump_etag(s
, iter
->second
.to_str());
1557 get_contype_from_attrs(attrs
, content_type
);
1558 dump_object_metadata(s
, attrs
);
1561 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1562 : "binary/octet-stream");
1567 if (get_data
&& !op_ret
) {
1568 const auto r
= dump_body(s
, bl
.c_str() + bl_ofs
, bl_len
);
1573 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1578 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1580 string hdrs
, exp_hdrs
;
1581 uint32_t max_age
= CORS_MAX_AGE_INVALID
;
1582 /*EACCES means, there is no CORS registered yet for the bucket
1583 *ENOENT means, there is no match of the Origin in the list of CORSRule
1585 if (op_ret
== -ENOENT
)
1588 set_req_state_err(s
, op_ret
);
1590 end_header(s
, NULL
);
1593 get_response_params(hdrs
, exp_hdrs
, &max_age
);
1595 dump_access_control(s
, origin
, req_meth
, hdrs
.c_str(), exp_hdrs
.c_str(),
1597 end_header(s
, NULL
);
1600 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1601 list
<RGWBulkDelete::acct_path_t
>& items
, bool * const is_truncated
)
1603 constexpr size_t MAX_LINE_SIZE
= 2048;
1605 RGWClientIOStreamBuf
ciosb(static_cast<RGWRestfulIO
&>(*(s
->cio
)),
1606 size_t(s
->cct
->_conf
->rgw_max_chunk_size
));
1607 istream
cioin(&ciosb
);
1609 char buf
[MAX_LINE_SIZE
];
1610 while (cioin
.getline(buf
, sizeof(buf
))) {
1611 string
path_str(buf
);
1613 ldout(s
->cct
, 20) << "extracted Bulk Delete entry: " << path_str
<< dendl
;
1615 RGWBulkDelete::acct_path_t path
;
1617 /* We need to skip all slashes at the beginning in order to preserve
1618 * compliance with Swift. */
1619 const size_t start_pos
= path_str
.find_first_not_of('/');
1621 if (string::npos
!= start_pos
) {
1622 /* Seperator is the first slash after the leading ones. */
1623 const size_t sep_pos
= path_str
.find('/', start_pos
);
1625 if (string::npos
!= sep_pos
) {
1626 path
.bucket_name
= url_decode(path_str
.substr(start_pos
,
1627 sep_pos
- start_pos
));
1628 path
.obj_key
= url_decode(path_str
.substr(sep_pos
+ 1));
1630 /* It's guaranteed here that bucket name is at least one character
1631 * long and is different than slash. */
1632 path
.bucket_name
= url_decode(path_str
.substr(start_pos
));
1635 items
.push_back(path
);
1638 if (items
.size() == MAX_CHUNK_ENTRIES
) {
1639 *is_truncated
= true;
1644 *is_truncated
= false;
1648 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1650 set_req_state_err(s
, op_ret
);
1652 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1653 CHUNKED_TRANSFER_ENCODING
);
1655 bulkdelete_respond(deleter
->get_num_deleted(),
1656 deleter
->get_num_unfound(),
1657 deleter
->get_failures(),
1660 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1664 std::unique_ptr
<RGWBulkUploadOp::StreamGetter
>
1665 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1667 class SwiftStreamGetter
: public StreamGetter
{
1668 const size_t conlen
;
1673 SwiftStreamGetter(req_state
* const s
, const size_t conlen
)
1679 ssize_t
get_at_most(size_t want
, ceph::bufferlist
& dst
) override
{
1680 /* maximum requested by a caller */
1681 /* data provided by client */
1682 /* RadosGW's limit. */
1683 const size_t max_chunk_size
= \
1684 static_cast<size_t>(s
->cct
->_conf
->rgw_max_chunk_size
);
1685 const size_t max_to_read
= std::min({ want
, conlen
- curpos
, max_chunk_size
});
1687 ldout(s
->cct
, 20) << "bulk_upload: get_at_most max_to_read="
1689 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst
.c_str()) << dendl
;
1691 bufferptr
bp(max_to_read
);
1692 const auto read_len
= recv_body(s
, bp
.c_str(), max_to_read
);
1693 dst
.append(bp
, 0, read_len
);
1694 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1700 return curpos
> s
->cct
->_conf
->rgw_max_put_size
? -ERR_TOO_LARGE
1704 ssize_t
get_exactly(size_t want
, ceph::bufferlist
& dst
) override
{
1705 ldout(s
->cct
, 20) << "bulk_upload: get_exactly want=" << want
<< dendl
;
1707 /* FIXME: do this in a loop. */
1708 const auto ret
= get_at_most(want
, dst
);
1709 ldout(s
->cct
, 20) << "bulk_upload: get_exactly ret=" << ret
<< dendl
;
1712 } else if (static_cast<size_t>(ret
) != want
) {
1724 ldout(s
->cct
, 20) << "bulk upload: create_stream for length="
1725 << s
->length
<< dendl
;
1727 const size_t conlen
= atoll(s
->length
);
1728 return std::unique_ptr
<SwiftStreamGetter
>(new SwiftStreamGetter(s
, conlen
));
1732 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1734 set_req_state_err(s
, op_ret
);
1736 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1737 CHUNKED_TRANSFER_ENCODING
);
1738 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1740 s
->formatter
->open_object_section("delete");
1742 std::string resp_status
;
1743 std::string resp_body
;
1745 if (! failures
.empty()) {
1748 const auto last_err
= { failures
.back().err
};
1749 if (boost::algorithm::contains(last_err
, terminal_errors
)) {
1750 /* The terminal errors are affecting the status of the whole upload. */
1751 set_req_state_err(err
, failures
.back().err
, s
->prot_flags
);
1753 set_req_state_err(err
, ERR_INVALID_REQUEST
, s
->prot_flags
);
1756 dump_errno(err
, resp_status
);
1757 } else if (0 == num_created
&& failures
.empty()) {
1758 /* Nothing created, nothing failed. This means the archive contained no
1759 * entity we could understand (regular file or directory). We need to
1760 * send 400 Bad Request to an HTTP client in the internal status field. */
1761 dump_errno(400, resp_status
);
1762 resp_body
= "Invalid Tar File: No Valid Files";
1765 dump_errno(201, resp_status
);
1768 encode_json("Number Files Created", num_created
, s
->formatter
);
1769 encode_json("Response Body", resp_body
, s
->formatter
);
1770 encode_json("Response Status", resp_status
, s
->formatter
);
1772 s
->formatter
->open_array_section("Errors");
1773 for (const auto& fail_desc
: failures
) {
1774 s
->formatter
->open_array_section("object");
1776 encode_json("Name", fail_desc
.path
, s
->formatter
);
1779 set_req_state_err(err
, fail_desc
.err
, s
->prot_flags
);
1781 dump_errno(err
, status
);
1782 encode_json("Status", status
, s
->formatter
);
1784 s
->formatter
->close_section();
1786 s
->formatter
->close_section();
1788 s
->formatter
->close_section();
1789 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1793 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1795 set_req_state_err(s
, op_ret
);
1797 end_header(s
, this, "application/xml");
1799 std::stringstream ss
;
1801 ss
<< R
"(<?xml version="1.0"?>)" << "\n"
1802 << R
"(<!DOCTYPE cross-domain-policy SYSTEM )"
1803 << R
"("http
://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1804 << R
"(<cross-domain-policy>)" << "\n"
1805 << g_conf()->rgw_cross_domain_policy
<< "\n"
1806 << R
"(</cross-domain-policy>)";
1808 dump_body(s
, ss
.str());
1811 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1813 set_req_state_err(s
, op_ret
);
1815 end_header(s
, this, "application/xml");
1818 static constexpr char DISABLED
[] = "DISABLED BY FILE";
1819 dump_body(s
, DISABLED
, strlen(DISABLED
));
1823 const vector
<pair
<string
, RGWInfo_ObjStore_SWIFT::info
>> RGWInfo_ObjStore_SWIFT::swift_info
=
1825 {"bulk_delete", {false, nullptr}},
1826 {"container_quotas", {false, nullptr}},
1827 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data
}},
1828 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data
}},
1829 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data
}},
1830 {"account_quotas", {false, nullptr}},
1831 {"staticweb", {false, nullptr}},
1832 {"tempauth", {false, RGWInfo_ObjStore_SWIFT::list_tempauth_data
}},
1835 void RGWInfo_ObjStore_SWIFT::execute()
1837 bool is_admin_info_enabled
= false;
1839 const string
& swiftinfo_sig
= s
->info
.args
.get("swiftinfo_sig");
1840 const string
& swiftinfo_expires
= s
->info
.args
.get("swiftinfo_expires");
1842 if (!swiftinfo_sig
.empty() &&
1843 !swiftinfo_expires
.empty() &&
1844 !is_expired(swiftinfo_expires
, s
->cct
)) {
1845 is_admin_info_enabled
= true;
1848 s
->formatter
->open_object_section("info");
1850 for (const auto& pair
: swift_info
) {
1851 if(!is_admin_info_enabled
&& pair
.second
.is_admin_info
)
1854 if (!pair
.second
.list_data
) {
1855 s
->formatter
->open_object_section((pair
.first
).c_str());
1856 s
->formatter
->close_section();
1859 pair
.second
.list_data(*(s
->formatter
), s
->cct
->_conf
, *store
);
1863 s
->formatter
->close_section();
1866 void RGWInfo_ObjStore_SWIFT::send_response()
1869 op_ret
= STATUS_NO_CONTENT
;
1871 set_req_state_err(s
, op_ret
);
1873 end_header(s
, this);
1874 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1877 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter
& formatter
,
1878 const ConfigProxy
& config
,
1881 formatter
.open_object_section("swift");
1882 formatter
.dump_int("max_file_size", config
->rgw_max_put_size
);
1883 formatter
.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX
);
1885 string
ceph_version(CEPH_GIT_NICE_VER
);
1886 formatter
.dump_string("version", ceph_version
);
1888 const size_t max_attr_name_len
= \
1889 g_conf().get_val
<Option::size_t>("rgw_max_attr_name_len");
1890 if (max_attr_name_len
) {
1891 const size_t meta_name_limit
= \
1892 max_attr_name_len
- strlen(RGW_ATTR_PREFIX RGW_AMZ_META_PREFIX
);
1893 formatter
.dump_int("max_meta_name_length", meta_name_limit
);
1896 const size_t meta_value_limit
= g_conf().get_val
<Option::size_t>("rgw_max_attr_size");
1897 if (meta_value_limit
) {
1898 formatter
.dump_int("max_meta_value_length", meta_value_limit
);
1901 const size_t meta_num_limit
= \
1902 g_conf().get_val
<uint64_t>("rgw_max_attrs_num_in_req");
1903 if (meta_num_limit
) {
1904 formatter
.dump_int("max_meta_count", meta_num_limit
);
1907 formatter
.open_array_section("policies");
1908 const RGWZoneGroup
& zonegroup
= store
.svc
.zone
->get_zonegroup();
1910 for (const auto& placement_targets
: zonegroup
.placement_targets
) {
1911 formatter
.open_object_section("policy");
1912 if (placement_targets
.second
.name
.compare(zonegroup
.default_placement
.name
) == 0)
1913 formatter
.dump_bool("default", true);
1914 formatter
.dump_string("name", placement_targets
.second
.name
.c_str());
1915 formatter
.close_section();
1917 formatter
.close_section();
1919 formatter
.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN
);
1920 formatter
.dump_bool("strict_cors_mode", true);
1921 formatter
.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN
);
1922 formatter
.close_section();
1925 void RGWInfo_ObjStore_SWIFT::list_tempauth_data(Formatter
& formatter
,
1926 const ConfigProxy
& config
,
1929 formatter
.open_object_section("tempauth");
1930 formatter
.dump_bool("account_acls", true);
1931 formatter
.close_section();
1933 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter
& formatter
,
1934 const ConfigProxy
& config
,
1937 formatter
.open_object_section("tempurl");
1938 formatter
.open_array_section("methods");
1939 formatter
.dump_string("methodname", "GET");
1940 formatter
.dump_string("methodname", "HEAD");
1941 formatter
.dump_string("methodname", "PUT");
1942 formatter
.dump_string("methodname", "POST");
1943 formatter
.dump_string("methodname", "DELETE");
1944 formatter
.close_section();
1945 formatter
.close_section();
1948 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter
& formatter
,
1949 const ConfigProxy
& config
,
1952 formatter
.open_object_section("slo");
1953 formatter
.dump_int("max_manifest_segments", config
->rgw_max_slo_entries
);
1954 formatter
.close_section();
1957 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string
& expires
, CephContext
* cct
)
1960 const utime_t now
= ceph_clock_now();
1961 const uint64_t expiration
= (uint64_t)strict_strtoll(expires
.c_str(),
1964 ldout(cct
, 5) << "failed to parse siginfo_expires: " << err
<< dendl
;
1968 if (expiration
<= (uint64_t)now
.sec()) {
1969 ldout(cct
, 5) << "siginfo expired: " << expiration
<< " <= " << now
.sec() << dendl
;
1977 void RGWFormPost::init(RGWRados
* const store
,
1979 RGWHandler
* const dialect_handler
)
1981 prefix
= std::move(s
->object
.name
);
1982 s
->object
= rgw_obj_key();
1984 return RGWPostObj_ObjStore::init(store
, s
, dialect_handler
);
1987 std::size_t RGWFormPost::get_max_file_size() /*const*/
1989 std::string max_str
= get_part_str(ctrl_parts
, "max_file_size", "0");
1992 const std::size_t max_file_size
=
1993 static_cast<uint64_t>(strict_strtoll(max_str
.c_str(), 10, &err
));
1995 if (! err
.empty()) {
1996 ldout(s
->cct
, 5) << "failed to parse FormPost's max_file_size: " << err
2001 return max_file_size
;
2004 bool RGWFormPost::is_non_expired()
2006 std::string expires
= get_part_str(ctrl_parts
, "expires", "0");
2009 const uint64_t expires_timestamp
=
2010 static_cast<uint64_t>(strict_strtoll(expires
.c_str(), 10, &err
));
2012 if (! err
.empty()) {
2013 dout(5) << "failed to parse FormPost's expires: " << err
<< dendl
;
2017 const utime_t now
= ceph_clock_now();
2018 if (expires_timestamp
<= static_cast<uint64_t>(now
.sec())) {
2019 dout(5) << "FormPost form expired: "
2020 << expires_timestamp
<< " <= " << now
.sec() << dendl
;
2027 bool RGWFormPost::is_integral()
2029 const std::string form_signature
= get_part_str(ctrl_parts
, "signature");
2032 get_owner_info(s
, *s
->user
);
2033 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
2035 ldout(s
->cct
, 5) << "cannot get user_info of account's owner" << dendl
;
2039 for (const auto& kv
: s
->user
->temp_url_keys
) {
2040 const int temp_url_key_num
= kv
.first
;
2041 const string
& temp_url_key
= kv
.second
;
2043 if (temp_url_key
.empty()) {
2047 SignatureHelper sig_helper
;
2048 sig_helper
.calc(temp_url_key
,
2049 s
->info
.request_uri
,
2050 get_part_str(ctrl_parts
, "redirect"),
2051 get_part_str(ctrl_parts
, "max_file_size", "0"),
2052 get_part_str(ctrl_parts
, "max_file_count", "0"),
2053 get_part_str(ctrl_parts
, "expires", "0"));
2055 const auto local_sig
= sig_helper
.get_signature();
2057 ldout(s
->cct
, 20) << "FormPost signature [" << temp_url_key_num
<< "]"
2058 << " (calculated): " << local_sig
<< dendl
;
2060 if (sig_helper
.is_equal_to(form_signature
)) {
2063 ldout(s
->cct
, 5) << "FormPost's signature mismatch: "
2064 << local_sig
<< " != " << form_signature
<< dendl
;
2071 void RGWFormPost::get_owner_info(const req_state
* const s
,
2072 RGWUserInfo
& owner_info
) const
2074 /* We cannot use req_state::bucket_name because it isn't available
2075 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
2076 const string
& bucket_name
= s
->init_state
.url_bucket
;
2078 /* TempURL in Formpost only requires that bucket name is specified. */
2079 if (bucket_name
.empty()) {
2083 string bucket_tenant
;
2084 if (!s
->account_name
.empty()) {
2088 const rgw_user
uid(s
->account_name
);
2089 if (uid
.tenant
.empty()) {
2090 const rgw_user
tenanted_uid(uid
.id
, uid
.id
);
2092 if (rgw_get_user_info_by_uid(store
, tenanted_uid
, uinfo
) >= 0) {
2094 bucket_tenant
= uinfo
.user_id
.tenant
;
2099 if (!found
&& rgw_get_user_info_by_uid(store
, uid
, uinfo
) < 0) {
2102 bucket_tenant
= uinfo
.user_id
.tenant
;
2106 /* Need to get user info of bucket owner. */
2107 RGWBucketInfo bucket_info
;
2108 int ret
= store
->get_bucket_info(*s
->sysobj_ctx
,
2109 bucket_tenant
, bucket_name
,
2110 bucket_info
, nullptr);
2115 ldout(s
->cct
, 20) << "temp url user (bucket owner): " << bucket_info
.owner
2118 if (rgw_get_user_info_by_uid(store
, bucket_info
.owner
, owner_info
) < 0) {
2123 int RGWFormPost::get_params()
2125 /* The parentt class extracts boundary info from the Content-Type. */
2126 int ret
= RGWPostObj_ObjStore::get_params();
2131 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
2133 /* Let's start parsing the HTTP body by parsing each form part step-
2134 * by-step till encountering the first part with file data. */
2136 struct post_form_part part
;
2137 ret
= read_form_part_header(&part
, stream_done
);
2142 if (s
->cct
->_conf
->subsys
.should_gather
<ceph_subsys_rgw
, 20>()) {
2143 ldout(s
->cct
, 20) << "read part header -- part.name="
2144 << part
.name
<< dendl
;
2146 for (const auto& pair
: part
.fields
) {
2147 ldout(s
->cct
, 20) << "field.name=" << pair
.first
<< dendl
;
2148 ldout(s
->cct
, 20) << "field.val=" << pair
.second
.val
<< dendl
;
2149 ldout(s
->cct
, 20) << "field.params:" << dendl
;
2151 for (const auto& param_pair
: pair
.second
.params
) {
2152 ldout(s
->cct
, 20) << " " << param_pair
.first
2153 << " -> " << param_pair
.second
<< dendl
;
2159 /* Unexpected here. */
2160 err_msg
= "Malformed request";
2164 const auto field_iter
= part
.fields
.find("Content-Disposition");
2165 if (std::end(part
.fields
) != field_iter
&&
2166 std::end(field_iter
->second
.params
) != field_iter
->second
.params
.find("filename")) {
2167 /* First data part ahead. */
2168 current_data_part
= std::move(part
);
2170 /* Stop the iteration. We can assume that all control parts have been
2171 * already parsed. The rest of HTTP body should contain data parts
2172 * only. They will be picked up by ::get_data(). */
2175 /* Control part ahead. Receive, parse and store for later usage. */
2177 ret
= read_data(part
.data
, s
->cct
->_conf
->rgw_max_chunk_size
,
2178 boundary
, stream_done
);
2181 } else if (! boundary
) {
2182 err_msg
= "Couldn't find boundary";
2186 ctrl_parts
[part
.name
] = std::move(part
);
2188 } while (! stream_done
);
2191 max_len
= get_max_file_size();
2193 if (! current_data_part
) {
2194 err_msg
= "FormPost: no files to process";
2198 if (! is_non_expired()) {
2199 err_msg
= "FormPost: Form Expired";
2203 if (! is_integral()) {
2204 err_msg
= "FormPost: Invalid Signature";
2211 std::string
RGWFormPost::get_current_filename() const
2214 const auto& field
= current_data_part
->fields
.at("Content-Disposition");
2215 const auto iter
= field
.params
.find("filename");
2217 if (std::end(field
.params
) != iter
) {
2218 return prefix
+ iter
->second
;
2220 } catch (std::out_of_range
&) {
2227 std::string
RGWFormPost::get_current_content_type() const
2230 const auto& field
= current_data_part
->fields
.at("Content-Type");
2232 } catch (std::out_of_range
&) {
2236 return std::string();
2239 bool RGWFormPost::is_next_file_to_upload()
2241 if (! stream_done
) {
2242 /* We have at least one additional part in the body. */
2243 struct post_form_part part
;
2244 int r
= read_form_part_header(&part
, stream_done
);
2249 const auto field_iter
= part
.fields
.find("Content-Disposition");
2250 if (std::end(part
.fields
) != field_iter
) {
2251 const auto& params
= field_iter
->second
.params
;
2252 const auto& filename_iter
= params
.find("filename");
2254 if (std::end(params
) != filename_iter
&& ! filename_iter
->second
.empty()) {
2255 current_data_part
= std::move(part
);
2264 int RGWFormPost::get_data(ceph::bufferlist
& bl
, bool& again
)
2268 int r
= read_data(bl
, s
->cct
->_conf
->rgw_max_chunk_size
,
2269 boundary
, stream_done
);
2274 /* Tell RGWPostObj::execute() that it has some data to put. */
2280 void RGWFormPost::send_response()
2282 std::string redirect
= get_part_str(ctrl_parts
, "redirect");
2283 if (! redirect
.empty()) {
2284 op_ret
= STATUS_REDIRECT
;
2287 set_req_state_err(s
, op_ret
);
2288 s
->err
.err_code
= err_msg
;
2290 if (! redirect
.empty()) {
2291 dump_redirect(s
, redirect
);
2293 end_header(s
, this);
2296 bool RGWFormPost::is_formpost_req(req_state
* const s
)
2298 std::string content_type
;
2299 std::map
<std::string
, std::string
> params
;
2301 parse_boundary_params(s
->info
.env
->get("CONTENT_TYPE", ""),
2302 content_type
, params
);
2304 return boost::algorithm::iequals(content_type
, "multipart/form-data") &&
2305 params
.count("boundary") > 0;
2309 RGWOp
*RGWHandler_REST_Service_SWIFT::op_get()
2311 return new RGWListBuckets_ObjStore_SWIFT
;
2314 RGWOp
*RGWHandler_REST_Service_SWIFT::op_head()
2316 return new RGWStatAccount_ObjStore_SWIFT
;
2319 RGWOp
*RGWHandler_REST_Service_SWIFT::op_put()
2321 if (s
->info
.args
.exists("extract-archive")) {
2322 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2327 RGWOp
*RGWHandler_REST_Service_SWIFT::op_post()
2329 if (s
->info
.args
.exists("bulk-delete")) {
2330 return new RGWBulkDelete_ObjStore_SWIFT
;
2332 return new RGWPutMetadataAccount_ObjStore_SWIFT
;
2335 RGWOp
*RGWHandler_REST_Service_SWIFT::op_delete()
2337 if (s
->info
.args
.exists("bulk-delete")) {
2338 return new RGWBulkDelete_ObjStore_SWIFT
;
2343 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret
,
2344 const std::string error_doc
)
2346 /* Try to throw it all away. */
2347 s
->formatter
->reset();
2349 class RGWGetErrorPage
: public RGWGetObj_ObjStore_SWIFT
{
2351 RGWGetErrorPage(RGWRados
* const store
,
2352 RGWHandler_REST
* const handler
,
2354 const int http_ret
) {
2355 /* Calling a virtual from the base class is safe as the subobject should
2356 * be properly initialized and we haven't overridden the init method. */
2357 init(store
, s
, handler
);
2359 set_custom_http_response(http_ret
);
2362 int error_handler(const int err_no
,
2363 std::string
* const error_content
) override
{
2364 /* Enforce that any error generated while getting the error page will
2365 * not be send to a client. This allows us to recover from the double
2366 * fault situation by sending the original message. */
2369 } get_errpage_op(store
, handler
, s
, http_ret
);
2371 s
->object
= std::to_string(http_ret
) + error_doc
;
2373 RGWOp
* newop
= &get_errpage_op
;
2375 return rgw_process_authenticated(handler
, newop
, &req
, s
, true);
2378 int RGWSwiftWebsiteHandler::error_handler(const int err_no
,
2379 std::string
* const error_content
)
2381 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2383 if (can_be_website_req() && ! ws_conf
.error_doc
.empty()) {
2384 set_req_state_err(s
, err_no
);
2385 return serve_errordoc(s
->err
.http_ret
, ws_conf
.error_doc
);
2388 /* Let's go to the default, no-op handler. */
2392 bool RGWSwiftWebsiteHandler::is_web_mode() const
2394 const boost::string_ref webmode
= s
->info
.env
->get("HTTP_X_WEB_MODE", "");
2395 return boost::algorithm::iequals(webmode
, "true");
2398 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2400 /* Static website works only with the GET or HEAD method. Nothing more. */
2401 static const std::set
<boost::string_ref
> ws_methods
= { "GET", "HEAD" };
2402 if (ws_methods
.count(s
->info
.method
) == 0) {
2406 /* We also need to handle early failures from the auth system. In such cases
2407 * req_state::auth.identity may be empty. Let's treat that the same way as
2408 * the anonymous access. */
2409 if (! s
->auth
.identity
) {
2413 /* Swift serves websites only for anonymous requests unless client explicitly
2414 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2415 if (s
->auth
.identity
->is_anonymous() || is_web_mode()) {
2422 RGWOp
* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2424 class RGWMovedPermanently
: public RGWOp
{
2425 const std::string location
;
2427 explicit RGWMovedPermanently(const std::string
& location
)
2428 : location(location
) {
2431 int verify_permission() override
{
2435 void execute() override
{
2436 op_ret
= -ERR_PERMANENT_REDIRECT
;
2440 void send_response() override
{
2441 set_req_state_err(s
, op_ret
);
2443 dump_content_length(s
, 0);
2444 dump_redirect(s
, location
);
2445 end_header(s
, this);
2448 const char* name() const override
{
2449 return "RGWMovedPermanently";
2453 return new RGWMovedPermanently(s
->info
.request_uri
+ '/');
2456 RGWOp
* RGWSwiftWebsiteHandler::get_ws_index_op()
2458 /* Retarget to get obj on requested index file. */
2459 if (! s
->object
.empty()) {
2460 s
->object
= s
->object
.name
+
2461 s
->bucket_info
.website_conf
.get_index_doc();
2463 s
->object
= s
->bucket_info
.website_conf
.get_index_doc();
2466 auto getop
= new RGWGetObj_ObjStore_SWIFT
;
2467 getop
->set_get_data(boost::algorithm::equals("GET", s
->info
.method
));
2472 RGWOp
* RGWSwiftWebsiteHandler::get_ws_listing_op()
2474 class RGWWebsiteListing
: public RGWListBucket_ObjStore_SWIFT
{
2475 const std::string prefix_override
;
2477 int get_params() override
{
2478 prefix
= prefix_override
;
2484 void send_response() override
{
2485 /* Generate the header now. */
2486 set_req_state_err(s
, op_ret
);
2488 dump_container_metadata(s
, bucket
, bucket_quota
,
2489 s
->bucket_info
.website_conf
);
2490 end_header(s
, this, "text/html");
2495 /* Now it's the time to start generating HTML bucket listing.
2496 * All the crazy stuff with crafting tags will be delegated to
2497 * RGWSwiftWebsiteListingFormatter. */
2498 std::stringstream ss
;
2499 RGWSwiftWebsiteListingFormatter
htmler(ss
, prefix
);
2501 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2502 htmler
.generate_header(s
->decoded_uri
,
2503 ws_conf
.listing_css_doc
);
2505 for (const auto& pair
: common_prefixes
) {
2506 std::string subdir_name
= pair
.first
;
2507 if (! subdir_name
.empty()) {
2508 /* To be compliant with Swift we need to remove the trailing
2510 subdir_name
.pop_back();
2513 htmler
.dump_subdir(subdir_name
);
2516 for (const rgw_bucket_dir_entry
& obj
: objs
) {
2517 if (! common_prefixes
.count(obj
.key
.name
+ '/')) {
2518 htmler
.dump_object(obj
);
2522 htmler
.generate_footer();
2523 dump_body(s
, ss
.str());
2526 /* Taking prefix_override by value to leverage std::string r-value ref
2527 * ctor and thus avoid extra memory copying/increasing ref counter. */
2528 explicit RGWWebsiteListing(std::string prefix_override
)
2529 : prefix_override(std::move(prefix_override
)) {
2533 std::string prefix
= std::move(s
->object
.name
);
2534 s
->object
= rgw_obj_key();
2536 return new RGWWebsiteListing(std::move(prefix
));
2539 bool RGWSwiftWebsiteHandler::is_web_dir() const
2541 std::string subdir_name
= url_decode(s
->object
.name
);
2543 /* Remove character from the subdir name if it is "/". */
2544 if (subdir_name
.empty()) {
2546 } else if (subdir_name
.back() == '/') {
2547 subdir_name
.pop_back();
2550 rgw_obj
obj(s
->bucket
, std::move(subdir_name
));
2552 /* First, get attrset of the object we'll try to retrieve. */
2553 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2554 obj_ctx
.set_atomic(obj
);
2555 obj_ctx
.set_prefetch_data(obj
);
2557 RGWObjState
* state
= nullptr;
2558 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2562 /* A nonexistent object cannot be a considered as a marker representing
2563 * the emulation of catalog in FS hierarchy. */
2564 if (! state
->exists
) {
2568 /* Decode the content type. */
2569 std::string content_type
;
2570 get_contype_from_attrs(state
->attrset
, content_type
);
2572 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2573 const std::string subdir_marker
= ws_conf
.subdir_marker
.empty()
2574 ? "application/directory"
2575 : ws_conf
.subdir_marker
;
2576 return subdir_marker
== content_type
&& state
->size
<= 1;
2579 bool RGWSwiftWebsiteHandler::is_index_present(const std::string
& index
)
2581 rgw_obj
obj(s
->bucket
, index
);
2583 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2584 obj_ctx
.set_atomic(obj
);
2585 obj_ctx
.set_prefetch_data(obj
);
2587 RGWObjState
* state
= nullptr;
2588 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2592 /* A nonexistent object cannot be a considered as a viable index. We will
2593 * try to list the bucket or - if this is impossible - return an error. */
2594 return state
->exists
;
2597 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp
* op
, RGWOp
** new_op
)
2599 ldout(s
->cct
, 10) << "Starting retarget" << dendl
;
2600 RGWOp
* op_override
= nullptr;
2602 /* In Swift static web content is served if the request is anonymous or
2603 * has X-Web-Mode HTTP header specified to true. */
2604 if (can_be_website_req()) {
2605 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2606 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2608 if (s
->decoded_uri
.back() != '/') {
2609 op_override
= get_ws_redirect_op();
2610 } else if (! index
.empty() && is_index_present(index
)) {
2611 op_override
= get_ws_index_op();
2612 } else if (ws_conf
.listing_enabled
) {
2613 op_override
= get_ws_listing_op();
2618 handler
->put_op(op
);
2619 op_override
->init(store
, s
, handler
);
2621 *new_op
= op_override
;
2626 /* Return 404 Not Found is the request has web mode enforced but we static web
2627 * wasn't able to serve it accordingly. */
2628 return ! op_override
&& is_web_mode() ? -ENOENT
: 0;
2631 int RGWSwiftWebsiteHandler::retarget_object(RGWOp
* op
, RGWOp
** new_op
)
2633 ldout(s
->cct
, 10) << "Starting object retarget" << dendl
;
2634 RGWOp
* op_override
= nullptr;
2636 /* In Swift static web content is served if the request is anonymous or
2637 * has X-Web-Mode HTTP header specified to true. */
2638 if (can_be_website_req() && is_web_dir()) {
2639 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2640 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2642 if (s
->decoded_uri
.back() != '/') {
2643 op_override
= get_ws_redirect_op();
2644 } else if (! index
.empty() && is_index_present(index
)) {
2645 op_override
= get_ws_index_op();
2646 } else if (ws_conf
.listing_enabled
) {
2647 op_override
= get_ws_listing_op();
2650 /* A regular request or the specified object isn't a subdirectory marker.
2651 * We don't need any re-targeting. Error handling (like sending a custom
2652 * error page) will be performed by error_handler of the actual RGWOp. */
2657 handler
->put_op(op
);
2658 op_override
->init(store
, s
, handler
);
2660 *new_op
= op_override
;
2665 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2666 return ! op_override
? -ENOENT
: 0;
2670 RGWOp
*RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data
)
2673 return new RGWGetACLs_ObjStore_SWIFT
;
2677 return new RGWListBucket_ObjStore_SWIFT
;
2679 return new RGWStatBucket_ObjStore_SWIFT
;
2682 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_get()
2684 return get_obj_op(true);
2687 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_head()
2689 return get_obj_op(false);
2692 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_put()
2695 return new RGWPutACLs_ObjStore_SWIFT
;
2697 if(s
->info
.args
.exists("extract-archive")) {
2698 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2700 return new RGWCreateBucket_ObjStore_SWIFT
;
2703 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_delete()
2705 return new RGWDeleteBucket_ObjStore_SWIFT
;
2708 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_post()
2710 if (RGWFormPost::is_formpost_req(s
)) {
2711 return new RGWFormPost
;
2713 return new RGWPutMetadataBucket_ObjStore_SWIFT
;
2717 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_options()
2719 return new RGWOptionsCORS_ObjStore_SWIFT
;
2723 RGWOp
*RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data
)
2726 return new RGWGetACLs_ObjStore_SWIFT
;
2729 RGWGetObj_ObjStore_SWIFT
*get_obj_op
= new RGWGetObj_ObjStore_SWIFT
;
2730 get_obj_op
->set_get_data(get_data
);
2734 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_get()
2736 return get_obj_op(true);
2739 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_head()
2741 return get_obj_op(false);
2744 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_put()
2747 return new RGWPutACLs_ObjStore_SWIFT
;
2749 if(s
->info
.args
.exists("extract-archive")) {
2750 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2752 if (s
->init_state
.src_bucket
.empty())
2753 return new RGWPutObj_ObjStore_SWIFT
;
2755 return new RGWCopyObj_ObjStore_SWIFT
;
2758 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_delete()
2760 return new RGWDeleteObj_ObjStore_SWIFT
;
2763 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_post()
2765 if (RGWFormPost::is_formpost_req(s
)) {
2766 return new RGWFormPost
;
2768 return new RGWPutMetadataObject_ObjStore_SWIFT
;
2772 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_copy()
2774 return new RGWCopyObj_ObjStore_SWIFT
;
2777 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_options()
2779 return new RGWOptionsCORS_ObjStore_SWIFT
;
2783 int RGWHandler_REST_SWIFT::authorize(const DoutPrefixProvider
*dpp
)
2785 return rgw::auth::Strategy::apply(dpp
, auth_strategy
, s
);
2788 int RGWHandler_REST_SWIFT::postauth_init()
2790 struct req_init_state
* t
= &s
->init_state
;
2792 /* XXX Stub this until Swift Auth sets account into URL. */
2793 s
->bucket_tenant
= s
->user
->user_id
.tenant
;
2794 s
->bucket_name
= t
->url_bucket
;
2796 dout(10) << "s->object=" <<
2797 (!s
->object
.empty() ? s
->object
: rgw_obj_key("<NULL>"))
2799 << rgw_make_bucket_entry_name(s
->bucket_tenant
, s
->bucket_name
)
2803 ret
= rgw_validate_tenant_name(s
->bucket_tenant
);
2806 ret
= validate_bucket_name(s
->bucket_name
);
2809 ret
= validate_object_name(s
->object
.name
);
2813 if (!t
->src_bucket
.empty()) {
2815 * We don't allow cross-tenant copy at present. It requires account
2816 * names in the URL for Swift.
2818 s
->src_tenant_name
= s
->user
->user_id
.tenant
;
2819 s
->src_bucket_name
= t
->src_bucket
;
2821 ret
= validate_bucket_name(s
->src_bucket_name
);
2825 ret
= validate_object_name(s
->src_object
.name
);
2834 int RGWHandler_REST_SWIFT::validate_bucket_name(const string
& bucket
)
2836 const size_t len
= bucket
.size();
2838 if (len
> MAX_BUCKET_NAME_LEN
) {
2839 /* Bucket Name too long. Generate custom error message and bind it
2840 * to an R-value reference. */
2841 const auto msg
= boost::str(
2842 boost::format("Container name length of %lld longer than %lld")
2843 % len
% int(MAX_BUCKET_NAME_LEN
));
2844 set_req_state_err(s
, ERR_INVALID_BUCKET_NAME
, msg
);
2845 return -ERR_INVALID_BUCKET_NAME
;
2848 const auto ret
= RGWHandler_REST::validate_bucket_name(bucket
);
2856 if (bucket
[0] == '.')
2857 return -ERR_INVALID_BUCKET_NAME
;
2859 if (check_utf8(bucket
.c_str(), len
))
2860 return -ERR_INVALID_UTF8
;
2862 const char *s
= bucket
.c_str();
2864 for (size_t i
= 0; i
< len
; ++i
, ++s
) {
2865 if (*(unsigned char *)s
== 0xff)
2866 return -ERR_INVALID_BUCKET_NAME
;
2867 if (*(unsigned char *)s
== '/')
2868 return -ERR_INVALID_BUCKET_NAME
;
2874 static void next_tok(string
& str
, string
& tok
, char delim
)
2876 if (str
.size() == 0) {
2881 int pos
= str
.find(delim
);
2883 tok
= str
.substr(0, pos
);
2884 str
= str
.substr(pos
+ 1);
2890 int RGWHandler_REST_SWIFT::init_from_header(struct req_state
* const s
,
2891 const std::string
& frontend_prefix
)
2896 s
->prot_flags
|= RGW_REST_SWIFT
;
2898 char reqbuf
[frontend_prefix
.length() + s
->decoded_uri
.length() + 1];
2899 sprintf(reqbuf
, "%s%s", frontend_prefix
.c_str(), s
->decoded_uri
.c_str());
2900 const char *req_name
= reqbuf
;
2904 if (*req_name
== '?') {
2907 p
= s
->info
.request_params
.c_str();
2910 s
->info
.args
.set(p
);
2911 s
->info
.args
.parse();
2913 /* Skip the leading slash of URL hierarchy. */
2914 if (req_name
[0] != '/') {
2920 if ('\0' == req_name
[0]) {
2921 return g_conf()->rgw_swift_url_prefix
== "/" ? -ERR_BAD_URL
: 0;
2926 size_t pos
= req
.find('/');
2927 if (std::string::npos
!= pos
&& g_conf()->rgw_swift_url_prefix
!= "/") {
2928 bool cut_url
= g_conf()->rgw_swift_url_prefix
.length();
2929 first
= req
.substr(0, pos
);
2931 if (first
.compare(g_conf()->rgw_swift_url_prefix
) == 0) {
2933 /* Rewind to the "v1/..." part. */
2934 next_tok(req
, first
, '/');
2937 } else if (req
.compare(g_conf()->rgw_swift_url_prefix
) == 0) {
2938 s
->formatter
= new RGWFormatter_Plain
;
2939 return -ERR_BAD_URL
;
2944 std::string tenant_path
;
2945 if (! g_conf()->rgw_swift_tenant_name
.empty()) {
2946 tenant_path
= "/AUTH_";
2947 tenant_path
.append(g_conf()->rgw_swift_tenant_name
);
2950 /* verify that the request_uri conforms with what's expected */
2951 char buf
[g_conf()->rgw_swift_url_prefix
.length() + 16 + tenant_path
.length()];
2953 if (g_conf()->rgw_swift_url_prefix
== "/") {
2954 blen
= sprintf(buf
, "/v1%s", tenant_path
.c_str());
2956 blen
= sprintf(buf
, "/%s/v1%s",
2957 g_conf()->rgw_swift_url_prefix
.c_str(), tenant_path
.c_str());
2960 if (strncmp(reqbuf
, buf
, blen
) != 0) {
2964 int ret
= allocate_formatter(s
, RGW_FORMAT_PLAIN
, true);
2970 next_tok(req
, ver
, '/');
2972 if (!tenant_path
.empty() || g_conf()->rgw_swift_account_in_url
) {
2973 string account_name
;
2974 next_tok(req
, account_name
, '/');
2976 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2977 const vector
<string
> skipped_prefixes
= { "AUTH_", "KEY_" };
2979 for (const auto pfx
: skipped_prefixes
) {
2980 const size_t comp_len
= min(account_name
.length(), pfx
.length());
2981 if (account_name
.compare(0, comp_len
, pfx
) == 0) {
2982 /* Prefix is present. Drop it. */
2983 account_name
= account_name
.substr(comp_len
);
2988 if (account_name
.empty()) {
2989 return -ERR_PRECONDITION_FAILED
;
2991 s
->account_name
= account_name
;
2995 next_tok(req
, first
, '/');
2997 dout(10) << "ver=" << ver
<< " first=" << first
<< " req=" << req
<< dendl
;
2998 if (first
.size() == 0)
3001 s
->info
.effective_uri
= "/" + first
;
3003 // Save bucket to tide us over until token is parsed.
3004 s
->init_state
.url_bucket
= first
;
3008 rgw_obj_key(req
, s
->info
.env
->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
3009 s
->info
.effective_uri
.append("/" + s
->object
.name
);
3015 int RGWHandler_REST_SWIFT::init(RGWRados
* store
, struct req_state
* s
,
3016 rgw::io::BasicClient
*cio
)
3018 struct req_init_state
*t
= &s
->init_state
;
3020 s
->dialect
= "swift";
3022 std::string copy_source
=
3023 url_decode(s
->info
.env
->get("HTTP_X_COPY_FROM", ""));
3024 if (! copy_source
.empty()) {
3025 bool result
= RGWCopyObj::parse_copy_location(copy_source
, t
->src_bucket
,
3028 return -ERR_BAD_URL
;
3031 if (s
->op
== OP_COPY
) {
3032 std::string req_dest
=
3033 url_decode(s
->info
.env
->get("HTTP_DESTINATION", ""));
3034 if (req_dest
.empty())
3035 return -ERR_BAD_URL
;
3037 std::string dest_bucket_name
;
3038 rgw_obj_key dest_obj_key
;
3040 RGWCopyObj::parse_copy_location(req_dest
, dest_bucket_name
,
3043 return -ERR_BAD_URL
;
3045 std::string dest_object
= dest_obj_key
.name
;
3047 /* convert COPY operation into PUT */
3048 t
->src_bucket
= t
->url_bucket
;
3049 s
->src_object
= s
->object
;
3050 t
->url_bucket
= dest_bucket_name
;
3051 s
->object
= rgw_obj_key(dest_object
);
3055 s
->info
.storage_class
= s
->info
.env
->get("HTTP_X_OBJECT_STORAGE_CLASS", "");
3057 return RGWHandler_REST::init(store
, s
, cio
);
3061 RGWRESTMgr_SWIFT::get_handler(struct req_state
* const s
,
3062 const rgw::auth::StrategyRegistry
& auth_registry
,
3063 const std::string
& frontend_prefix
)
3065 int ret
= RGWHandler_REST_SWIFT::init_from_header(s
, frontend_prefix
);
3067 ldout(s
->cct
, 10) << "init_from_header returned err=" << ret
<< dendl
;
3071 const auto& auth_strategy
= auth_registry
.get_swift();
3073 if (s
->init_state
.url_bucket
.empty()) {
3074 return new RGWHandler_REST_Service_SWIFT(auth_strategy
);
3077 if (s
->object
.empty()) {
3078 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy
);
3081 return new RGWHandler_REST_Obj_SWIFT(auth_strategy
);
3084 RGWHandler_REST
* RGWRESTMgr_SWIFT_Info::get_handler(
3085 struct req_state
* const s
,
3086 const rgw::auth::StrategyRegistry
& auth_registry
,
3087 const std::string
& frontend_prefix
)
3089 s
->prot_flags
|= RGW_REST_SWIFT
;
3090 const auto& auth_strategy
= auth_registry
.get_swift();
3091 return new RGWHandler_REST_SWIFT_Info(auth_strategy
);