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/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"
32 #include <boost/utility/string_ref.hpp>
34 #define dout_context g_ceph_context
35 #define dout_subsys ceph_subsys_rgw
37 int RGWListBuckets_ObjStore_SWIFT::get_params()
39 prefix
= s
->info
.args
.get("prefix");
40 marker
= s
->info
.args
.get("marker");
41 end_marker
= s
->info
.args
.get("end_marker");
42 wants_reversed
= s
->info
.args
.exists("reverse");
45 std::swap(marker
, end_marker
);
48 std::string limit_str
= s
->info
.args
.get("limit");
49 if (!limit_str
.empty()) {
51 long l
= strict_strtol(limit_str
.c_str(), 10, &err
);
56 if (l
> (long)limit_max
|| l
< 0) {
57 return -ERR_PRECONDITION_FAILED
;
63 if (s
->cct
->_conf
->rgw_swift_need_stats
) {
65 int r
= s
->info
.args
.get_bool("stats", &stats
, &exists
);
81 static void dump_account_metadata(struct req_state
* const s
,
82 const RGWUsageStats
& global_stats
,
83 const std::map
<std::string
, RGWUsageStats
> policies_stats
,
84 /* const */map
<string
, bufferlist
>& attrs
,
85 const RGWQuotaInfo
& quota
,
86 const RGWAccessControlPolicy_SWIFTAcct
&policy
)
88 /* Adding X-Timestamp to keep align with Swift API */
89 dump_header(s
, "X-Timestamp", ceph_clock_now());
91 dump_header(s
, "X-Account-Container-Count", global_stats
.buckets_count
);
92 dump_header(s
, "X-Account-Object-Count", global_stats
.objects_count
);
93 dump_header(s
, "X-Account-Bytes-Used", global_stats
.bytes_used
);
94 dump_header(s
, "X-Account-Bytes-Used-Actual", global_stats
.bytes_used_rounded
);
96 for (const auto& kv
: policies_stats
) {
97 const auto& policy_name
= camelcase_dash_http_attr(kv
.first
);
98 const auto& policy_stats
= kv
.second
;
100 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
101 "-Container-Count", policy_stats
.buckets_count
);
102 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
103 "-Object-Count", policy_stats
.objects_count
);
104 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
105 "-Bytes-Used", policy_stats
.bytes_used
);
106 dump_header_infixed(s
, "X-Account-Storage-Policy-", policy_name
,
107 "-Bytes-Used-Actual", policy_stats
.bytes_used_rounded
);
110 /* Dump TempURL-related stuff */
111 if (s
->perm_mask
== RGW_PERM_FULL_CONTROL
) {
112 auto iter
= s
->user
->temp_url_keys
.find(0);
113 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
114 dump_header(s
, "X-Account-Meta-Temp-Url-Key", iter
->second
);
117 iter
= s
->user
->temp_url_keys
.find(1);
118 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
119 dump_header(s
, "X-Account-Meta-Temp-Url-Key-2", iter
->second
);
123 /* Dump quota headers. */
125 if (quota
.max_size
>= 0) {
126 dump_header(s
, "X-Account-Meta-Quota-Bytes", quota
.max_size
);
129 /* Limit on the number of objects in a given account is a RadosGW's
130 * extension. Swift's account quota WSGI filter doesn't support it. */
131 if (quota
.max_objects
>= 0) {
132 dump_header(s
, "X-Account-Meta-Quota-Count", quota
.max_objects
);
136 /* Dump user-defined metadata items and generic attrs. */
137 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
138 map
<string
, bufferlist
>::iterator iter
;
139 for (iter
= attrs
.lower_bound(RGW_ATTR_PREFIX
); iter
!= attrs
.end(); ++iter
) {
140 const char *name
= iter
->first
.c_str();
141 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
143 if (geniter
!= rgw_to_http_attrs
.end()) {
144 dump_header(s
, geniter
->second
, iter
->second
);
145 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
146 dump_header_prefixed(s
, "X-Account-Meta-",
147 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
152 /* Dump account ACLs */
153 auto account_acls
= policy
.to_str();
155 dump_header(s
, "X-Account-Access-Control", std::move(*account_acls
));
159 void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets
)
162 set_req_state_err(s
, op_ret
);
163 } else if (!has_buckets
&& s
->format
== RGW_FORMAT_PLAIN
) {
164 op_ret
= STATUS_NO_CONTENT
;
165 set_req_state_err(s
, op_ret
);
168 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
169 /* Adding account stats in the header to keep align with Swift API */
170 dump_account_metadata(s
,
175 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
177 end_header(s
, NULL
, NULL
, NO_CONTENT_LENGTH
, true);
182 s
->formatter
->open_array_section_with_attrs("account",
183 FormatterAttrs("name", s
->user
->display_name
.c_str(), NULL
));
189 void RGWListBuckets_ObjStore_SWIFT::handle_listing_chunk(RGWUserBuckets
&& buckets
)
191 if (wants_reversed
) {
192 /* Just store in the reversal buffer. Its content will be handled later,
193 * in send_response_end(). */
194 reverse_buffer
.emplace(std::begin(reverse_buffer
), std::move(buckets
));
196 return send_response_data(buckets
);
200 void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets
& buckets
)
206 /* Take care of the prefix parameter of Swift API. There is no business
207 * in applying the filter earlier as we really need to go through all
208 * entries regardless of it (the headers like X-Account-Container-Count
209 * aren't affected by specifying prefix). */
210 const std::map
<std::string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
211 for (auto iter
= m
.lower_bound(prefix
);
212 iter
!= m
.end() && boost::algorithm::starts_with(iter
->first
, prefix
);
214 dump_bucket_entry(iter
->second
);
218 void RGWListBuckets_ObjStore_SWIFT::dump_bucket_entry(const RGWBucketEnt
& obj
)
220 s
->formatter
->open_object_section("container");
221 s
->formatter
->dump_string("name", obj
.bucket
.name
);
224 s
->formatter
->dump_int("count", obj
.count
);
225 s
->formatter
->dump_int("bytes", obj
.size
);
228 s
->formatter
->close_section();
230 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
231 rgw_flush_formatter(s
, s
->formatter
);
235 void RGWListBuckets_ObjStore_SWIFT::send_response_data_reversed(RGWUserBuckets
& buckets
)
241 /* Take care of the prefix parameter of Swift API. There is no business
242 * in applying the filter earlier as we really need to go through all
243 * entries regardless of it (the headers like X-Account-Container-Count
244 * aren't affected by specifying prefix). */
245 std::map
<std::string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
247 auto iter
= m
.rbegin();
248 for (/* initialized above */;
249 iter
!= m
.rend() && !boost::algorithm::starts_with(iter
->first
, prefix
);
254 for (/* iter carried */;
255 iter
!= m
.rend() && boost::algorithm::starts_with(iter
->first
, prefix
);
257 dump_bucket_entry(iter
->second
);
261 void RGWListBuckets_ObjStore_SWIFT::send_response_end()
263 if (wants_reversed
) {
264 for (auto& buckets
: reverse_buffer
) {
265 send_response_data_reversed(buckets
);
270 s
->formatter
->close_section();
273 if (s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
274 /* Adding account stats in the header to keep align with Swift API */
275 dump_account_metadata(s
,
280 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
282 end_header(s
, nullptr, nullptr, s
->formatter
->get_len(), true);
285 if (sent_data
|| s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
286 rgw_flush_formatter_and_reset(s
, s
->formatter
);
290 int RGWListBucket_ObjStore_SWIFT::get_params()
292 prefix
= s
->info
.args
.get("prefix");
293 marker
= s
->info
.args
.get("marker");
294 end_marker
= s
->info
.args
.get("end_marker");
295 max_keys
= s
->info
.args
.get("limit");
296 op_ret
= parse_max_keys();
300 if (max
> default_max
)
301 return -ERR_PRECONDITION_FAILED
;
303 delimiter
= s
->info
.args
.get("delimiter");
306 if (s
->info
.args
.exists("path")) { // should handle empty path
307 path_args
= s
->info
.args
.get("path");
308 if (!delimiter
.empty() || !prefix
.empty()) {
315 if (path
.size() && path
[path
.size() - 1] != '/')
318 int len
= prefix
.size();
319 int delim_size
= delimiter
.size();
321 if (len
>= delim_size
) {
322 if (prefix
.substr(len
- delim_size
).compare(delimiter
) != 0)
323 prefix
.append(delimiter
);
330 static void dump_container_metadata(struct req_state
*,
333 const RGWBucketWebsiteConf
&);
335 void RGWListBucket_ObjStore_SWIFT::send_response()
337 vector
<rgw_bucket_dir_entry
>::iterator iter
= objs
.begin();
338 map
<string
, bool>::iterator pref_iter
= common_prefixes
.begin();
341 dump_container_metadata(s
, bucket
, bucket_quota
,
342 s
->bucket_info
.website_conf
);
344 s
->formatter
->open_array_section_with_attrs("container", FormatterAttrs("name", s
->bucket
.name
.c_str(), NULL
));
346 while (iter
!= objs
.end() || pref_iter
!= common_prefixes
.end()) {
347 bool do_pref
= false;
348 bool do_objs
= false;
350 if (iter
!= objs
.end()) {
353 if (pref_iter
== common_prefixes
.end())
355 else if (iter
== objs
.end())
357 else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) == 0) {
360 } else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) <= 0)
365 if (do_objs
&& (marker
.empty() || marker
< key
)) {
366 if (key
.name
.compare(path
) == 0)
369 s
->formatter
->open_object_section("object");
370 s
->formatter
->dump_string("name", key
.name
);
371 s
->formatter
->dump_string("hash", iter
->meta
.etag
);
372 s
->formatter
->dump_int("bytes", iter
->meta
.accounted_size
);
373 if (!iter
->meta
.user_data
.empty())
374 s
->formatter
->dump_string("user_custom_data", iter
->meta
.user_data
);
375 string single_content_type
= iter
->meta
.content_type
;
376 if (iter
->meta
.content_type
.size()) {
377 // content type might hold multiple values, just dump the last one
378 ssize_t pos
= iter
->meta
.content_type
.rfind(',');
381 while (single_content_type
[pos
] == ' ')
383 single_content_type
= single_content_type
.substr(pos
);
385 s
->formatter
->dump_string("content_type", single_content_type
);
387 dump_time(s
, "last_modified", &iter
->meta
.mtime
);
388 s
->formatter
->close_section();
391 if (do_pref
&& (marker
.empty() || pref_iter
->first
.compare(marker
.name
) > 0)) {
392 const string
& name
= pref_iter
->first
;
393 if (name
.compare(delimiter
) == 0)
396 s
->formatter
->open_object_section_with_attrs("subdir", FormatterAttrs("name", name
.c_str(), NULL
));
398 /* swift is a bit inconsistent here */
401 s
->formatter
->dump_string("name", name
);
404 s
->formatter
->dump_string("subdir", name
);
406 s
->formatter
->close_section();
415 s
->formatter
->close_section();
417 int64_t content_len
= 0;
419 content_len
= s
->formatter
->get_len();
420 if (content_len
== 0) {
421 op_ret
= STATUS_NO_CONTENT
;
423 } else if (op_ret
> 0) {
427 set_req_state_err(s
, op_ret
);
429 end_header(s
, this, NULL
, content_len
);
434 rgw_flush_formatter_and_reset(s
, s
->formatter
);
437 static void dump_container_metadata(struct req_state
*s
,
438 const RGWBucketEnt
& bucket
,
439 const RGWQuotaInfo
& quota
,
440 const RGWBucketWebsiteConf
& ws_conf
)
442 /* Adding X-Timestamp to keep align with Swift API */
443 dump_header(s
, "X-Timestamp", utime_t(s
->bucket_info
.creation_time
));
445 dump_header(s
, "X-Container-Object-Count", bucket
.count
);
446 dump_header(s
, "X-Container-Bytes-Used", bucket
.size
);
447 dump_header(s
, "X-Container-Bytes-Used-Actual", bucket
.size_rounded
);
449 if (s
->object
.empty()) {
450 auto swift_policy
= \
451 static_cast<RGWAccessControlPolicy_SWIFT
*>(s
->bucket_acl
.get());
452 std::string read_acl
, write_acl
;
453 swift_policy
->to_str(read_acl
, write_acl
);
455 if (read_acl
.size()) {
456 dump_header(s
, "X-Container-Read", read_acl
);
458 if (write_acl
.size()) {
459 dump_header(s
, "X-Container-Write", write_acl
);
461 if (!s
->bucket_info
.placement_rule
.empty()) {
462 dump_header(s
, "X-Storage-Policy", s
->bucket_info
.placement_rule
);
465 /* Dump user-defined metadata items and generic attrs. */
466 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
467 map
<string
, bufferlist
>::iterator iter
;
468 for (iter
= s
->bucket_attrs
.lower_bound(RGW_ATTR_PREFIX
);
469 iter
!= s
->bucket_attrs
.end();
471 const char *name
= iter
->first
.c_str();
472 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
474 if (geniter
!= rgw_to_http_attrs
.end()) {
475 dump_header(s
, geniter
->second
, iter
->second
);
476 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
477 dump_header_prefixed(s
, "X-Container-Meta-",
478 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
484 /* Dump container versioning info. */
485 if (! s
->bucket_info
.swift_ver_location
.empty()) {
486 dump_header(s
, "X-Versions-Location",
487 url_encode(s
->bucket_info
.swift_ver_location
));
490 /* Dump quota headers. */
492 if (quota
.max_size
>= 0) {
493 dump_header(s
, "X-Container-Meta-Quota-Bytes", quota
.max_size
);
496 if (quota
.max_objects
>= 0) {
497 dump_header(s
, "X-Container-Meta-Quota-Count", quota
.max_objects
);
501 /* Dump Static Website headers. */
502 if (! ws_conf
.index_doc_suffix
.empty()) {
503 dump_header(s
, "X-Container-Meta-Web-Index", ws_conf
.index_doc_suffix
);
506 if (! ws_conf
.error_doc
.empty()) {
507 dump_header(s
, "X-Container-Meta-Web-Error", ws_conf
.error_doc
);
510 if (! ws_conf
.subdir_marker
.empty()) {
511 dump_header(s
, "X-Container-Meta-Web-Directory-Type",
512 ws_conf
.subdir_marker
);
515 if (! ws_conf
.listing_css_doc
.empty()) {
516 dump_header(s
, "X-Container-Meta-Web-Listings-CSS",
517 ws_conf
.listing_css_doc
);
520 if (ws_conf
.listing_enabled
) {
521 dump_header(s
, "X-Container-Meta-Web-Listings", "true");
525 void RGWStatAccount_ObjStore_SWIFT::execute()
527 RGWStatAccount_ObjStore::execute();
528 op_ret
= rgw_get_user_attrs_by_uid(store
, s
->user
->user_id
, attrs
);
531 void RGWStatAccount_ObjStore_SWIFT::send_response()
534 op_ret
= STATUS_NO_CONTENT
;
535 dump_account_metadata(s
,
540 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
543 set_req_state_err(s
, op_ret
);
546 end_header(s
, NULL
, NULL
, 0, true);
551 void RGWStatBucket_ObjStore_SWIFT::send_response()
554 op_ret
= STATUS_NO_CONTENT
;
555 dump_container_metadata(s
, bucket
, bucket_quota
,
556 s
->bucket_info
.website_conf
);
559 set_req_state_err(s
, op_ret
);
562 end_header(s
, this, NULL
, 0, true);
566 static int get_swift_container_settings(req_state
* const s
,
567 RGWRados
* const store
,
568 RGWAccessControlPolicy
* const policy
,
569 bool * const has_policy
,
571 RGWCORSConfiguration
* const cors_config
,
572 bool * const has_cors
)
574 const char * const read_list
= s
->info
.env
->get("HTTP_X_CONTAINER_READ");
575 const char * const write_list
= s
->info
.env
->get("HTTP_X_CONTAINER_WRITE");
579 if (read_list
|| write_list
) {
580 RGWAccessControlPolicy_SWIFT
swift_policy(s
->cct
);
581 const auto r
= swift_policy
.create(store
,
583 s
->user
->display_name
,
591 *policy
= swift_policy
;
597 /*Check and update CORS configuration*/
598 const char *allow_origins
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
599 const char *allow_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
600 const char *expose_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
601 const char *max_age
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
603 RGWCORSConfiguration_SWIFT
*swift_cors
= new RGWCORSConfiguration_SWIFT
;
604 int r
= swift_cors
->create_update(allow_origins
, allow_headers
, expose_headers
, max_age
);
606 dout(0) << "Error creating/updating the cors configuration" << dendl
;
611 *cors_config
= *swift_cors
;
619 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
620 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
621 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
622 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
624 static void get_rmattrs_from_headers(const req_state
* const s
,
625 const char * const put_prefix
,
626 const char * const del_prefix
,
627 set
<string
>& rmattr_names
)
629 const size_t put_prefix_len
= strlen(put_prefix
);
630 const size_t del_prefix_len
= strlen(del_prefix
);
632 for (const auto& kv
: s
->info
.env
->get_map()) {
633 size_t prefix_len
= 0;
634 const char * const p
= kv
.first
.c_str();
636 if (strncasecmp(p
, del_prefix
, del_prefix_len
) == 0) {
637 /* Explicitly requested removal. */
638 prefix_len
= del_prefix_len
;
639 } else if ((strncasecmp(p
, put_prefix
, put_prefix_len
) == 0)
640 && kv
.second
.empty()) {
641 /* Removal requested by putting an empty value. */
642 prefix_len
= put_prefix_len
;
645 if (prefix_len
> 0) {
646 string
name(RGW_ATTR_META_PREFIX
);
647 name
.append(lowercase_dash_http_attr(p
+ prefix_len
));
648 rmattr_names
.insert(name
);
653 static int get_swift_versioning_settings(
655 boost::optional
<std::string
>& swift_ver_location
)
657 /* Removing the Swift's versions location has lower priority than setting
658 * a new one. That's the reason why we're handling it first. */
659 const std::string vlocdel
=
660 s
->info
.env
->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
661 if (vlocdel
.size()) {
662 swift_ver_location
= boost::in_place(std::string());
665 if (s
->info
.env
->exists("HTTP_X_VERSIONS_LOCATION")) {
666 /* If the Swift's versioning is globally disabled but someone wants to
667 * enable it for a given container, new version of Swift will generate
668 * the precondition failed error. */
669 if (! s
->cct
->_conf
->rgw_swift_versioning_enabled
) {
670 return -ERR_PRECONDITION_FAILED
;
673 swift_ver_location
= s
->info
.env
->get("HTTP_X_VERSIONS_LOCATION", "");
679 int RGWCreateBucket_ObjStore_SWIFT::get_params()
682 uint32_t policy_rw_mask
= 0;
684 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
685 &policy_rw_mask
, &cors_config
, &has_cors
);
691 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
694 location_constraint
= store
->get_zonegroup().api_name
;
695 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
,
696 CONT_REMOVE_ATTR_PREFIX
, rmattr_names
);
697 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
699 return get_swift_versioning_settings(s
, swift_ver_location
);
702 static inline int handle_metadata_errors(req_state
* const s
, const int op_ret
)
704 if (op_ret
== -EFBIG
) {
705 /* Handle the custom error message of exceeding maximum custom attribute
706 * (stored as xattr) size. */
707 const auto error_message
= boost::str(
708 boost::format("Metadata value longer than %lld")
709 % s
->cct
->_conf
->get_val
<size_t>("rgw_max_attr_size"));
710 set_req_state_err(s
, EINVAL
, error_message
);
712 } else if (op_ret
== -E2BIG
) {
713 const auto error_message
= boost::str(
714 boost::format("Too many metadata items; max %lld")
715 % s
->cct
->_conf
->get_val
<size_t>("rgw_max_attrs_num_in_req"));
716 set_req_state_err(s
, EINVAL
, error_message
);
723 void RGWCreateBucket_ObjStore_SWIFT::send_response()
725 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
726 if (meta_ret
!= op_ret
) {
730 op_ret
= STATUS_CREATED
;
731 } else if (op_ret
== -ERR_BUCKET_EXISTS
) {
732 op_ret
= STATUS_ACCEPTED
;
734 set_req_state_err(s
, op_ret
);
738 /* Propose ending HTTP header with 0 Content-Length header. */
739 end_header(s
, NULL
, NULL
, 0);
740 rgw_flush_formatter_and_reset(s
, s
->formatter
);
743 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
747 r
= STATUS_NO_CONTENT
;
749 set_req_state_err(s
, r
);
751 end_header(s
, this, NULL
, 0);
752 rgw_flush_formatter_and_reset(s
, s
->formatter
);
755 static int get_delete_at_param(req_state
*s
, boost::optional
<real_time
> &delete_at
)
757 /* Handle Swift object expiration. */
758 real_time delat_proposal
;
759 string x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AFTER", "");
761 if (x_delete
.empty()) {
762 x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AT", "");
764 /* X-Delete-After HTTP is present. It means we need add its value
765 * to the current time. */
766 delat_proposal
= real_clock::now();
769 if (x_delete
.empty()) {
770 delete_at
= boost::none
;
771 if (s
->info
.env
->exists("HTTP_X_REMOVE_DELETE_AT")) {
772 delete_at
= boost::in_place(real_time());
777 long ts
= strict_strtoll(x_delete
.c_str(), 10, &err
);
783 delat_proposal
+= make_timespan(ts
);
784 if (delat_proposal
< real_clock::now()) {
788 delete_at
= delat_proposal
;
793 int RGWPutObj_ObjStore_SWIFT::verify_permission()
795 op_ret
= RGWPutObj_ObjStore::verify_permission();
797 /* We have to differentiate error codes depending on whether user is
798 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
799 * (403 Forbidden). */
800 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
807 int RGWPutObj_ObjStore_SWIFT::get_params()
809 if (s
->has_bad_meta
) {
814 const char *encoding
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
815 if (!encoding
|| strcmp(encoding
, "chunked") != 0) {
816 ldout(s
->cct
, 20) << "neither length nor chunked encoding" << dendl
;
817 return -ERR_LENGTH_REQUIRED
;
820 chunked_upload
= true;
823 supplied_etag
= s
->info
.env
->get("HTTP_ETAG");
825 if (!s
->generic_attrs
.count(RGW_ATTR_CONTENT_TYPE
)) {
826 ldout(s
->cct
, 5) << "content type wasn't provided, trying to guess" << dendl
;
827 const char *suffix
= strrchr(s
->object
.name
.c_str(), '.');
831 string
suffix_str(suffix
);
832 const char *mime
= rgw_find_mime_by_ext(suffix_str
);
834 s
->generic_attrs
[RGW_ATTR_CONTENT_TYPE
] = mime
;
840 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
842 int r
= get_delete_at_param(s
, delete_at
);
844 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
848 if (!s
->cct
->_conf
->rgw_swift_custom_header
.empty()) {
849 string custom_header
= s
->cct
->_conf
->rgw_swift_custom_header
;
850 if (s
->info
.env
->exists(custom_header
.c_str())) {
851 user_data
= s
->info
.env
->get(custom_header
.c_str());
855 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
857 string multipart_manifest
= s
->info
.args
.get("multipart-manifest", &exists
);
859 if (multipart_manifest
!= "put") {
860 ldout(s
->cct
, 5) << "invalid multipart-manifest http param: " << multipart_manifest
<< dendl
;
864 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
865 uint64_t max_len
= s
->cct
->_conf
->rgw_max_slo_entries
* MAX_SLO_ENTRY_SIZE
;
867 slo_info
= new RGWSLOInfo
;
869 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
);
871 ldout(s
->cct
, 5) << "failed to read input for slo r=" << r
<< dendl
;
875 if ((int64_t)slo_info
->entries
.size() > s
->cct
->_conf
->rgw_max_slo_entries
) {
876 ldout(s
->cct
, 5) << "too many entries in slo request: " << slo_info
->entries
.size() << dendl
;
881 uint64_t total_size
= 0;
882 for (const auto& entry
: slo_info
->entries
) {
883 etag_sum
.Update((const byte
*)entry
.etag
.c_str(),
884 entry
.etag
.length());
885 total_size
+= entry
.size_bytes
;
887 ldout(s
->cct
, 20) << "slo_part: " << entry
.path
888 << " size=" << entry
.size_bytes
889 << " etag=" << entry
.etag
892 complete_etag(etag_sum
, &lo_etag
);
893 slo_info
->total_size
= total_size
;
895 ofs
= slo_info
->raw_data_len
;
898 return RGWPutObj_ObjStore::get_params();
901 void RGWPutObj_ObjStore_SWIFT::send_response()
903 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
908 op_ret
= STATUS_CREATED
;
910 set_req_state_err(s
, op_ret
);
913 if (! lo_etag
.empty()) {
914 /* Static Large Object of Swift API has two etags represented by
916 * - etag - for the manifest itself (it will be stored in xattrs),
917 * - lo_etag - for the content composited from SLO's segments.
918 * The value is calculated basing on segments' etags.
919 * In response for PUT request we have to expose the second one.
920 * The first one may be obtained by GET with "multipart-manifest=get"
921 * in query string on a given SLO. */
922 dump_etag(s
, lo_etag
, true /* quoted */);
927 dump_last_modified(s
, mtime
);
928 set_req_state_err(s
, op_ret
);
931 rgw_flush_formatter_and_reset(s
, s
->formatter
);
934 static int get_swift_account_settings(req_state
* const s
,
935 RGWRados
* const store
,
936 RGWAccessControlPolicy_SWIFTAcct
* const policy
,
937 bool * const has_policy
)
941 const char * const acl_attr
= s
->info
.env
->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
943 RGWAccessControlPolicy_SWIFTAcct
swift_acct_policy(s
->cct
);
944 const bool r
= swift_acct_policy
.create(store
,
946 s
->user
->display_name
,
952 *policy
= swift_acct_policy
;
959 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
961 if (s
->has_bad_meta
) {
965 int ret
= get_swift_account_settings(s
,
967 // FIXME: we need to carry unique_ptr in generic class
968 // and allocate appropriate ACL class in the ctor
969 static_cast<RGWAccessControlPolicy_SWIFTAcct
*>(&policy
),
975 get_rmattrs_from_headers(s
, ACCT_PUT_ATTR_PREFIX
, ACCT_REMOVE_ATTR_PREFIX
,
980 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
982 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
983 if (meta_ret
!= op_ret
) {
987 op_ret
= STATUS_NO_CONTENT
;
989 set_req_state_err(s
, op_ret
);
994 rgw_flush_formatter_and_reset(s
, s
->formatter
);
997 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
999 if (s
->has_bad_meta
) {
1003 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
1004 &policy_rw_mask
, &cors_config
, &has_cors
);
1009 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
, CONT_REMOVE_ATTR_PREFIX
,
1011 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
1013 return get_swift_versioning_settings(s
, swift_ver_location
);
1016 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
1018 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
1019 if (meta_ret
!= op_ret
) {
1022 if (!op_ret
&& (op_ret
!= -EINVAL
)) {
1023 op_ret
= STATUS_NO_CONTENT
;
1025 set_req_state_err(s
, op_ret
);
1029 end_header(s
, this);
1030 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1033 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
1035 if (s
->has_bad_meta
) {
1039 /* Handle Swift object expiration. */
1040 int r
= get_delete_at_param(s
, delete_at
);
1042 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1046 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
1047 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
1052 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
1054 const auto meta_ret
= handle_metadata_errors(s
, op_ret
);
1055 if (meta_ret
!= op_ret
) {
1059 op_ret
= STATUS_ACCEPTED
;
1061 set_req_state_err(s
, op_ret
);
1065 dump_content_length(s
, 0);
1069 end_header(s
, this);
1070 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1073 static void bulkdelete_respond(const unsigned num_deleted
,
1074 const unsigned int num_unfound
,
1075 const std::list
<RGWBulkDelete::fail_desc_t
>& failures
,
1076 const int prot_flags
, /* in */
1077 ceph::Formatter
& formatter
) /* out */
1079 formatter
.open_object_section("delete");
1084 if (!failures
.empty()) {
1085 int reason
= ERR_INVALID_REQUEST
;
1086 for (const auto fail_desc
: failures
) {
1087 if (-ENOENT
!= fail_desc
.err
&& -EACCES
!= fail_desc
.err
) {
1088 reason
= fail_desc
.err
;
1092 set_req_state_err(err
, reason
, prot_flags
);
1093 dump_errno(err
, resp_status
);
1094 } else if (0 == num_deleted
&& 0 == num_unfound
) {
1095 /* 400 Bad Request */
1096 dump_errno(400, resp_status
);
1097 resp_body
= "Invalid bulk delete.";
1100 dump_errno(200, resp_status
);
1103 encode_json("Number Deleted", num_deleted
, &formatter
);
1104 encode_json("Number Not Found", num_unfound
, &formatter
);
1105 encode_json("Response Body", resp_body
, &formatter
);
1106 encode_json("Response Status", resp_status
, &formatter
);
1108 formatter
.open_array_section("Errors");
1109 for (const auto fail_desc
: failures
) {
1110 formatter
.open_array_section("object");
1112 stringstream ss_name
;
1113 ss_name
<< fail_desc
.path
;
1114 encode_json("Name", ss_name
.str(), &formatter
);
1117 set_req_state_err(err
, fail_desc
.err
, prot_flags
);
1119 dump_errno(err
, status
);
1120 encode_json("Status", status
, &formatter
);
1121 formatter
.close_section();
1123 formatter
.close_section();
1125 formatter
.close_section();
1128 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1130 op_ret
= RGWDeleteObj_ObjStore::verify_permission();
1132 /* We have to differentiate error codes depending on whether user is
1133 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1134 * (403 Forbidden). */
1135 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1142 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1144 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1145 multipart_delete
= (mm
.compare("delete") == 0);
1147 return RGWDeleteObj_ObjStore::get_params();
1150 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1154 if (multipart_delete
) {
1157 r
= STATUS_NO_CONTENT
;
1160 set_req_state_err(s
, r
);
1163 if (multipart_delete
) {
1164 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1165 CHUNKED_TRANSFER_ENCODING
);
1168 bulkdelete_respond(deleter
->get_num_deleted(),
1169 deleter
->get_num_unfound(),
1170 deleter
->get_failures(),
1173 } else if (-ENOENT
== op_ret
) {
1174 bulkdelete_respond(0, 1, {}, s
->prot_flags
, *s
->formatter
);
1176 RGWBulkDelete::acct_path_t path
;
1177 path
.bucket_name
= s
->bucket_name
;
1178 path
.obj_key
= s
->object
;
1180 RGWBulkDelete::fail_desc_t fail_desc
;
1181 fail_desc
.err
= op_ret
;
1182 fail_desc
.path
= path
;
1184 bulkdelete_respond(0, 0, { fail_desc
}, s
->prot_flags
, *s
->formatter
);
1187 end_header(s
, this);
1190 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1194 static void get_contype_from_attrs(map
<string
, bufferlist
>& attrs
,
1195 string
& content_type
)
1197 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_CONTENT_TYPE
);
1198 if (iter
!= attrs
.end()) {
1199 content_type
= iter
->second
.c_str();
1203 static void dump_object_metadata(struct req_state
* const s
,
1204 map
<string
, bufferlist
> attrs
)
1206 map
<string
, string
> response_attrs
;
1208 for (auto kv
: attrs
) {
1209 const char * name
= kv
.first
.c_str();
1210 const auto aiter
= rgw_to_http_attrs
.find(name
);
1212 if (aiter
!= std::end(rgw_to_http_attrs
)) {
1213 response_attrs
[aiter
->second
] = kv
.second
.c_str();
1214 } else if (strcmp(name
, RGW_ATTR_SLO_UINDICATOR
) == 0) {
1215 // this attr has an extra length prefix from ::encode() in prior versions
1216 dump_header(s
, "X-Object-Meta-Static-Large-Object", "True");
1217 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
,
1218 sizeof(RGW_ATTR_META_PREFIX
)-1) == 0) {
1219 name
+= sizeof(RGW_ATTR_META_PREFIX
) - 1;
1220 dump_header_prefixed(s
, "X-Object-Meta-",
1221 camelcase_dash_http_attr(name
), kv
.second
);
1225 /* Handle override and fallback for Content-Disposition HTTP header.
1226 * At the moment this will be used only by TempURL of the Swift API. */
1227 const auto cditer
= rgw_to_http_attrs
.find(RGW_ATTR_CONTENT_DISP
);
1228 if (cditer
!= std::end(rgw_to_http_attrs
)) {
1229 const auto& name
= cditer
->second
;
1231 if (!s
->content_disp
.override
.empty()) {
1232 response_attrs
[name
] = s
->content_disp
.override
;
1233 } else if (!s
->content_disp
.fallback
.empty()
1234 && response_attrs
.find(name
) == std::end(response_attrs
)) {
1235 response_attrs
[name
] = s
->content_disp
.fallback
;
1239 for (const auto kv
: response_attrs
) {
1240 dump_header(s
, kv
.first
, kv
.second
);
1243 const auto iter
= attrs
.find(RGW_ATTR_DELETE_AT
);
1244 if (iter
!= std::end(attrs
)) {
1247 ::decode(delete_at
, iter
->second
);
1248 if (!delete_at
.is_zero()) {
1249 dump_header(s
, "X-Delete-At", delete_at
.sec());
1251 } catch (buffer::error
& err
) {
1252 ldout(s
->cct
, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1259 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1261 dest_policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1266 int RGWCopyObj_ObjStore_SWIFT::get_params()
1268 if_mod
= s
->info
.env
->get("HTTP_IF_MODIFIED_SINCE");
1269 if_unmod
= s
->info
.env
->get("HTTP_IF_UNMODIFIED_SINCE");
1270 if_match
= s
->info
.env
->get("HTTP_COPY_IF_MATCH");
1271 if_nomatch
= s
->info
.env
->get("HTTP_COPY_IF_NONE_MATCH");
1273 src_tenant_name
= s
->src_tenant_name
;
1274 src_bucket_name
= s
->src_bucket_name
;
1275 src_object
= s
->src_object
;
1276 dest_tenant_name
= s
->bucket_tenant
;
1277 dest_bucket_name
= s
->bucket_name
;
1278 dest_object
= s
->object
.name
;
1280 const char * const fresh_meta
= s
->info
.env
->get("HTTP_X_FRESH_METADATA");
1281 if (fresh_meta
&& strcasecmp(fresh_meta
, "TRUE") == 0) {
1282 attrs_mod
= RGWRados::ATTRSMOD_REPLACE
;
1284 attrs_mod
= RGWRados::ATTRSMOD_MERGE
;
1287 int r
= get_delete_at_param(s
, delete_at
);
1289 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1296 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs
)
1298 if (! sent_header
) {
1300 op_ret
= STATUS_CREATED
;
1301 set_req_state_err(s
, op_ret
);
1303 end_header(s
, this);
1305 /* Send progress information. Note that this diverge from the original swift
1306 * spec. We do this in order to keep connection alive.
1309 s
->formatter
->open_array_section("progress");
1313 s
->formatter
->dump_int("ofs", (uint64_t)ofs
);
1315 rgw_flush_formatter(s
, s
->formatter
);
1318 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1320 /* Dump X-Copied-From. */
1321 dump_header(s
, "X-Copied-From", url_encode(src_bucket
.name
) +
1322 "/" + url_encode(src_object
.name
));
1324 /* Dump X-Copied-From-Account. */
1326 dump_header(s
, "X-Copied-From-Account", url_encode(s
->user
->user_id
.id
));
1328 /* Dump X-Copied-From-Last-Modified. */
1329 dump_time_header(s
, "X-Copied-From-Last-Modified", src_mtime
);
1332 void RGWCopyObj_ObjStore_SWIFT::send_response()
1334 if (! sent_header
) {
1335 string content_type
;
1337 op_ret
= STATUS_CREATED
;
1338 set_req_state_err(s
, op_ret
);
1341 dump_last_modified(s
, mtime
);
1343 get_contype_from_attrs(attrs
, content_type
);
1344 dump_object_metadata(s
, attrs
);
1345 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1346 : "binary/octet-stream");
1348 s
->formatter
->close_section();
1349 rgw_flush_formatter(s
, s
->formatter
);
1353 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1355 op_ret
= RGWGetObj_ObjStore::verify_permission();
1357 /* We have to differentiate error codes depending on whether user is
1358 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1359 * (403 Forbidden). */
1360 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1367 int RGWGetObj_ObjStore_SWIFT::get_params()
1369 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1370 skip_manifest
= (mm
.compare("get") == 0);
1372 return RGWGetObj_ObjStore::get_params();
1375 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1377 std::string error_content
;
1378 op_ret
= error_handler(op_ret
, &error_content
);
1380 /* The error handler has taken care of the error. */
1384 bufferlist error_bl
;
1385 error_bl
.append(error_content
);
1386 return send_response_data(error_bl
, 0, error_bl
.length());
1389 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist
& bl
,
1393 string content_type
;
1399 if (custom_http_ret
) {
1400 set_req_state_err(s
, 0);
1401 dump_errno(s
, custom_http_ret
);
1403 set_req_state_err(s
, (partial_content
&& !op_ret
) ? STATUS_PARTIAL_CONTENT
1408 end_header(s
, NULL
);
1414 dump_range(s
, ofs
, end
, s
->obj_size
);
1418 end_header(s
, NULL
);
1422 dump_content_length(s
, total_len
);
1423 dump_last_modified(s
, lastmod
);
1424 dump_header(s
, "X-Timestamp", utime_t(lastmod
));
1426 dump_header(s
, "X-Static-Large-Object", "True");
1430 if (! lo_etag
.empty()) {
1431 dump_etag(s
, lo_etag
, true /* quoted */);
1433 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
1434 if (iter
!= attrs
.end()) {
1435 dump_etag(s
, iter
->second
);
1439 get_contype_from_attrs(attrs
, content_type
);
1440 dump_object_metadata(s
, attrs
);
1443 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1444 : "binary/octet-stream");
1449 if (get_data
&& !op_ret
) {
1450 const auto r
= dump_body(s
, bl
.c_str() + bl_ofs
, bl_len
);
1455 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1460 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1462 string hdrs
, exp_hdrs
;
1463 uint32_t max_age
= CORS_MAX_AGE_INVALID
;
1464 /*EACCES means, there is no CORS registered yet for the bucket
1465 *ENOENT means, there is no match of the Origin in the list of CORSRule
1467 if (op_ret
== -ENOENT
)
1470 set_req_state_err(s
, op_ret
);
1472 end_header(s
, NULL
);
1475 get_response_params(hdrs
, exp_hdrs
, &max_age
);
1477 dump_access_control(s
, origin
, req_meth
, hdrs
.c_str(), exp_hdrs
.c_str(),
1479 end_header(s
, NULL
);
1482 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1483 list
<RGWBulkDelete::acct_path_t
>& items
, bool * const is_truncated
)
1485 constexpr size_t MAX_LINE_SIZE
= 2048;
1487 RGWClientIOStreamBuf
ciosb(static_cast<RGWRestfulIO
&>(*(s
->cio
)),
1488 size_t(s
->cct
->_conf
->rgw_max_chunk_size
));
1489 istream
cioin(&ciosb
);
1491 char buf
[MAX_LINE_SIZE
];
1492 while (cioin
.getline(buf
, sizeof(buf
))) {
1493 string
path_str(buf
);
1495 ldout(s
->cct
, 20) << "extracted Bulk Delete entry: " << path_str
<< dendl
;
1497 RGWBulkDelete::acct_path_t path
;
1499 /* We need to skip all slashes at the beginning in order to preserve
1500 * compliance with Swift. */
1501 const size_t start_pos
= path_str
.find_first_not_of('/');
1503 if (string::npos
!= start_pos
) {
1504 /* Seperator is the first slash after the leading ones. */
1505 const size_t sep_pos
= path_str
.find('/', start_pos
);
1507 if (string::npos
!= sep_pos
) {
1508 path
.bucket_name
= url_decode(path_str
.substr(start_pos
,
1509 sep_pos
- start_pos
));
1510 path
.obj_key
= url_decode(path_str
.substr(sep_pos
+ 1));
1512 /* It's guaranteed here that bucket name is at least one character
1513 * long and is different than slash. */
1514 path
.bucket_name
= url_decode(path_str
.substr(start_pos
));
1517 items
.push_back(path
);
1520 if (items
.size() == MAX_CHUNK_ENTRIES
) {
1521 *is_truncated
= true;
1526 *is_truncated
= false;
1530 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1532 set_req_state_err(s
, op_ret
);
1534 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1535 CHUNKED_TRANSFER_ENCODING
);
1537 bulkdelete_respond(deleter
->get_num_deleted(),
1538 deleter
->get_num_unfound(),
1539 deleter
->get_failures(),
1542 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1546 std::unique_ptr
<RGWBulkUploadOp::StreamGetter
>
1547 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1549 class SwiftStreamGetter
: public StreamGetter
{
1550 const size_t conlen
;
1555 SwiftStreamGetter(req_state
* const s
, const size_t conlen
)
1561 ssize_t
get_at_most(size_t want
, ceph::bufferlist
& dst
) override
{
1562 /* maximum requested by a caller */
1563 /* data provided by client */
1564 /* RadosGW's limit. */
1565 const size_t max_chunk_size
= \
1566 static_cast<size_t>(s
->cct
->_conf
->rgw_max_chunk_size
);
1567 const size_t max_to_read
= std::min({ want
, conlen
- curpos
, max_chunk_size
});
1569 ldout(s
->cct
, 20) << "bulk_upload: get_at_most max_to_read="
1571 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst
.c_str()) << dendl
;
1573 bufferptr
bp(max_to_read
);
1574 const auto read_len
= recv_body(s
, bp
.c_str(), max_to_read
);
1575 dst
.append(bp
, 0, read_len
);
1576 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1582 return curpos
> s
->cct
->_conf
->rgw_max_put_size
? -ERR_TOO_LARGE
1586 ssize_t
get_exactly(size_t want
, ceph::bufferlist
& dst
) override
{
1587 ldout(s
->cct
, 20) << "bulk_upload: get_exactly want=" << want
<< dendl
;
1589 /* FIXME: do this in a loop. */
1590 const auto ret
= get_at_most(want
, dst
);
1591 ldout(s
->cct
, 20) << "bulk_upload: get_exactly ret=" << ret
<< dendl
;
1594 } else if (static_cast<size_t>(ret
) != want
) {
1606 ldout(s
->cct
, 20) << "bulk upload: create_stream for length="
1607 << s
->length
<< dendl
;
1609 const size_t conlen
= atoll(s
->length
);
1610 return std::unique_ptr
<SwiftStreamGetter
>(new SwiftStreamGetter(s
, conlen
));
1614 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1616 set_req_state_err(s
, op_ret
);
1618 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1619 CHUNKED_TRANSFER_ENCODING
);
1620 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1622 s
->formatter
->open_object_section("delete");
1624 std::string resp_status
;
1625 std::string resp_body
;
1627 if (! failures
.empty()) {
1630 const auto last_err
= { failures
.back().err
};
1631 if (boost::algorithm::contains(last_err
, terminal_errors
)) {
1632 /* The terminal errors are affecting the status of the whole upload. */
1633 set_req_state_err(err
, failures
.back().err
, s
->prot_flags
);
1635 set_req_state_err(err
, ERR_INVALID_REQUEST
, s
->prot_flags
);
1638 dump_errno(err
, resp_status
);
1639 } else if (0 == num_created
&& failures
.empty()) {
1640 /* Nothing created, nothing failed. This means the archive contained no
1641 * entity we could understand (regular file or directory). We need to
1642 * send 400 Bad Request to an HTTP client in the internal status field. */
1643 dump_errno(400, resp_status
);
1644 resp_body
= "Invalid Tar File: No Valid Files";
1647 dump_errno(201, resp_status
);
1650 encode_json("Number Files Created", num_created
, s
->formatter
);
1651 encode_json("Response Body", resp_body
, s
->formatter
);
1652 encode_json("Response Status", resp_status
, s
->formatter
);
1654 s
->formatter
->open_array_section("Errors");
1655 for (const auto& fail_desc
: failures
) {
1656 s
->formatter
->open_array_section("object");
1658 encode_json("Name", fail_desc
.path
, s
->formatter
);
1661 set_req_state_err(err
, fail_desc
.err
, s
->prot_flags
);
1663 dump_errno(err
, status
);
1664 encode_json("Status", status
, s
->formatter
);
1666 s
->formatter
->close_section();
1668 s
->formatter
->close_section();
1670 s
->formatter
->close_section();
1671 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1675 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1677 set_req_state_err(s
, op_ret
);
1679 end_header(s
, this, "application/xml");
1681 std::stringstream ss
;
1683 ss
<< R
"(<?xml version="1.0"?>)" << "\n"
1684 << R
"(<!DOCTYPE cross-domain-policy SYSTEM )"
1685 << R
"("http
://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1686 << R
"(<cross-domain-policy>)" << "\n"
1687 << g_conf
->rgw_cross_domain_policy
<< "\n"
1688 << R
"(</cross-domain-policy>)";
1690 dump_body(s
, ss
.str());
1693 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1695 set_req_state_err(s
, op_ret
);
1697 end_header(s
, this, "application/xml");
1700 static constexpr char DISABLED
[] = "DISABLED BY FILE";
1701 dump_body(s
, DISABLED
, strlen(DISABLED
));
1705 const vector
<pair
<string
, RGWInfo_ObjStore_SWIFT::info
>> RGWInfo_ObjStore_SWIFT::swift_info
=
1707 {"bulk_delete", {false, nullptr}},
1708 {"container_quotas", {false, nullptr}},
1709 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data
}},
1710 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data
}},
1711 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data
}},
1712 {"account_quotas", {false, nullptr}},
1713 {"staticweb", {false, nullptr}},
1714 {"tempauth", {false, RGWInfo_ObjStore_SWIFT::list_tempauth_data
}},
1717 void RGWInfo_ObjStore_SWIFT::execute()
1719 bool is_admin_info_enabled
= false;
1721 const string
& swiftinfo_sig
= s
->info
.args
.get("swiftinfo_sig");
1722 const string
& swiftinfo_expires
= s
->info
.args
.get("swiftinfo_expires");
1724 if (!swiftinfo_sig
.empty() &&
1725 !swiftinfo_expires
.empty() &&
1726 !is_expired(swiftinfo_expires
, s
->cct
)) {
1727 is_admin_info_enabled
= true;
1730 s
->formatter
->open_object_section("info");
1732 for (const auto& pair
: swift_info
) {
1733 if(!is_admin_info_enabled
&& pair
.second
.is_admin_info
)
1736 if (!pair
.second
.list_data
) {
1737 s
->formatter
->open_object_section((pair
.first
).c_str());
1738 s
->formatter
->close_section();
1741 pair
.second
.list_data(*(s
->formatter
), *(s
->cct
->_conf
), *store
);
1745 s
->formatter
->close_section();
1748 void RGWInfo_ObjStore_SWIFT::send_response()
1751 op_ret
= STATUS_NO_CONTENT
;
1753 set_req_state_err(s
, op_ret
);
1755 end_header(s
, this);
1756 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1759 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter
& formatter
,
1760 const md_config_t
& config
,
1763 formatter
.open_object_section("swift");
1764 formatter
.dump_int("max_file_size", config
.rgw_max_put_size
);
1765 formatter
.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX
);
1767 string
ceph_version(CEPH_GIT_NICE_VER
);
1768 formatter
.dump_string("version", ceph_version
);
1770 const size_t max_attr_name_len
= \
1771 g_conf
->get_val
<size_t>("rgw_max_attr_name_len");
1772 if (max_attr_name_len
) {
1773 const size_t meta_name_limit
= \
1774 max_attr_name_len
- strlen(RGW_ATTR_PREFIX RGW_AMZ_META_PREFIX
);
1775 formatter
.dump_int("max_meta_name_length", meta_name_limit
);
1778 const size_t meta_value_limit
= g_conf
->get_val
<size_t>("rgw_max_attr_size");
1779 if (meta_value_limit
) {
1780 formatter
.dump_int("max_meta_value_length", meta_value_limit
);
1783 const size_t meta_num_limit
= \
1784 g_conf
->get_val
<size_t>("rgw_max_attrs_num_in_req");
1785 if (meta_num_limit
) {
1786 formatter
.dump_int("max_meta_count", meta_num_limit
);
1789 formatter
.open_array_section("policies");
1790 RGWZoneGroup
& zonegroup
= store
.get_zonegroup();
1792 for (const auto& placement_targets
: zonegroup
.placement_targets
) {
1793 formatter
.open_object_section("policy");
1794 if (placement_targets
.second
.name
.compare(zonegroup
.default_placement
) == 0)
1795 formatter
.dump_bool("default", true);
1796 formatter
.dump_string("name", placement_targets
.second
.name
.c_str());
1797 formatter
.close_section();
1799 formatter
.close_section();
1801 formatter
.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN
);
1802 formatter
.dump_bool("strict_cors_mode", true);
1803 formatter
.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN
);
1804 formatter
.close_section();
1807 void RGWInfo_ObjStore_SWIFT::list_tempauth_data(Formatter
& formatter
,
1808 const md_config_t
& config
,
1811 formatter
.open_object_section("tempauth");
1812 formatter
.dump_bool("account_acls", true);
1813 formatter
.close_section();
1815 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter
& formatter
,
1816 const md_config_t
& config
,
1819 formatter
.open_object_section("tempurl");
1820 formatter
.open_array_section("methods");
1821 formatter
.dump_string("methodname", "GET");
1822 formatter
.dump_string("methodname", "HEAD");
1823 formatter
.dump_string("methodname", "PUT");
1824 formatter
.dump_string("methodname", "POST");
1825 formatter
.dump_string("methodname", "DELETE");
1826 formatter
.close_section();
1827 formatter
.close_section();
1830 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter
& formatter
,
1831 const md_config_t
& config
,
1834 formatter
.open_object_section("slo");
1835 formatter
.dump_int("max_manifest_segments", config
.rgw_max_slo_entries
);
1836 formatter
.close_section();
1839 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string
& expires
, CephContext
* cct
)
1842 const utime_t now
= ceph_clock_now();
1843 const uint64_t expiration
= (uint64_t)strict_strtoll(expires
.c_str(),
1846 ldout(cct
, 5) << "failed to parse siginfo_expires: " << err
<< dendl
;
1850 if (expiration
<= (uint64_t)now
.sec()) {
1851 ldout(cct
, 5) << "siginfo expired: " << expiration
<< " <= " << now
.sec() << dendl
;
1859 void RGWFormPost::init(RGWRados
* const store
,
1861 RGWHandler
* const dialect_handler
)
1863 prefix
= std::move(s
->object
.name
);
1864 s
->object
= rgw_obj_key();
1866 return RGWPostObj_ObjStore::init(store
, s
, dialect_handler
);
1869 std::size_t RGWFormPost::get_max_file_size() /*const*/
1871 std::string max_str
= get_part_str(ctrl_parts
, "max_file_size", "0");
1874 const std::size_t max_file_size
=
1875 static_cast<uint64_t>(strict_strtoll(max_str
.c_str(), 10, &err
));
1877 if (! err
.empty()) {
1878 ldout(s
->cct
, 5) << "failed to parse FormPost's max_file_size: " << err
1883 return max_file_size
;
1886 bool RGWFormPost::is_non_expired()
1888 std::string expires
= get_part_str(ctrl_parts
, "expires", "0");
1891 const uint64_t expires_timestamp
=
1892 static_cast<uint64_t>(strict_strtoll(expires
.c_str(), 10, &err
));
1894 if (! err
.empty()) {
1895 dout(5) << "failed to parse FormPost's expires: " << err
<< dendl
;
1899 const utime_t now
= ceph_clock_now();
1900 if (expires_timestamp
<= static_cast<uint64_t>(now
.sec())) {
1901 dout(5) << "FormPost form expired: "
1902 << expires_timestamp
<< " <= " << now
.sec() << dendl
;
1909 bool RGWFormPost::is_integral()
1911 const std::string form_signature
= get_part_str(ctrl_parts
, "signature");
1914 get_owner_info(s
, *s
->user
);
1915 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
1917 ldout(s
->cct
, 5) << "cannot get user_info of account's owner" << dendl
;
1921 for (const auto& kv
: s
->user
->temp_url_keys
) {
1922 const int temp_url_key_num
= kv
.first
;
1923 const string
& temp_url_key
= kv
.second
;
1925 if (temp_url_key
.empty()) {
1929 SignatureHelper sig_helper
;
1930 sig_helper
.calc(temp_url_key
,
1931 s
->info
.request_uri
,
1932 get_part_str(ctrl_parts
, "redirect"),
1933 get_part_str(ctrl_parts
, "max_file_size", "0"),
1934 get_part_str(ctrl_parts
, "max_file_count", "0"),
1935 get_part_str(ctrl_parts
, "expires", "0"));
1937 const auto local_sig
= sig_helper
.get_signature();
1939 ldout(s
->cct
, 20) << "FormPost signature [" << temp_url_key_num
<< "]"
1940 << " (calculated): " << local_sig
<< dendl
;
1942 if (sig_helper
.is_equal_to(form_signature
)) {
1945 ldout(s
->cct
, 5) << "FormPost's signature mismatch: "
1946 << local_sig
<< " != " << form_signature
<< dendl
;
1953 void RGWFormPost::get_owner_info(const req_state
* const s
,
1954 RGWUserInfo
& owner_info
) const
1956 /* We cannot use req_state::bucket_name because it isn't available
1957 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
1958 const string
& bucket_name
= s
->init_state
.url_bucket
;
1960 /* TempURL in Formpost only requires that bucket name is specified. */
1961 if (bucket_name
.empty()) {
1965 string bucket_tenant
;
1966 if (!s
->account_name
.empty()) {
1970 const rgw_user
uid(s
->account_name
);
1971 if (uid
.tenant
.empty()) {
1972 const rgw_user
tenanted_uid(uid
.id
, uid
.id
);
1974 if (rgw_get_user_info_by_uid(store
, tenanted_uid
, uinfo
) >= 0) {
1976 bucket_tenant
= uinfo
.user_id
.tenant
;
1981 if (!found
&& rgw_get_user_info_by_uid(store
, uid
, uinfo
) < 0) {
1984 bucket_tenant
= uinfo
.user_id
.tenant
;
1988 /* Need to get user info of bucket owner. */
1989 RGWBucketInfo bucket_info
;
1990 int ret
= store
->get_bucket_info(*static_cast<RGWObjectCtx
*>(s
->obj_ctx
),
1991 bucket_tenant
, bucket_name
,
1992 bucket_info
, nullptr);
1997 ldout(s
->cct
, 20) << "temp url user (bucket owner): " << bucket_info
.owner
2000 if (rgw_get_user_info_by_uid(store
, bucket_info
.owner
, owner_info
) < 0) {
2005 int RGWFormPost::get_params()
2007 /* The parentt class extracts boundary info from the Content-Type. */
2008 int ret
= RGWPostObj_ObjStore::get_params();
2013 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
2015 /* Let's start parsing the HTTP body by parsing each form part step-
2016 * by-step till encountering the first part with file data. */
2018 struct post_form_part part
;
2019 ret
= read_form_part_header(&part
, stream_done
);
2024 if (s
->cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
2025 ldout(s
->cct
, 20) << "read part header -- part.name="
2026 << part
.name
<< dendl
;
2028 for (const auto& pair
: part
.fields
) {
2029 ldout(s
->cct
, 20) << "field.name=" << pair
.first
<< dendl
;
2030 ldout(s
->cct
, 20) << "field.val=" << pair
.second
.val
<< dendl
;
2031 ldout(s
->cct
, 20) << "field.params:" << dendl
;
2033 for (const auto& param_pair
: pair
.second
.params
) {
2034 ldout(s
->cct
, 20) << " " << param_pair
.first
2035 << " -> " << param_pair
.second
<< dendl
;
2041 /* Unexpected here. */
2042 err_msg
= "Malformed request";
2046 const auto field_iter
= part
.fields
.find("Content-Disposition");
2047 if (std::end(part
.fields
) != field_iter
&&
2048 std::end(field_iter
->second
.params
) != field_iter
->second
.params
.find("filename")) {
2049 /* First data part ahead. */
2050 current_data_part
= std::move(part
);
2052 /* Stop the iteration. We can assume that all control parts have been
2053 * already parsed. The rest of HTTP body should contain data parts
2054 * only. They will be picked up by ::get_data(). */
2057 /* Control part ahead. Receive, parse and store for later usage. */
2059 ret
= read_data(part
.data
, s
->cct
->_conf
->rgw_max_chunk_size
,
2060 boundary
, stream_done
);
2063 } else if (! boundary
) {
2064 err_msg
= "Couldn't find boundary";
2068 ctrl_parts
[part
.name
] = std::move(part
);
2070 } while (! stream_done
);
2073 max_len
= get_max_file_size();
2075 if (! current_data_part
) {
2076 err_msg
= "FormPost: no files to process";
2080 if (! is_non_expired()) {
2081 err_msg
= "FormPost: Form Expired";
2085 if (! is_integral()) {
2086 err_msg
= "FormPost: Invalid Signature";
2093 std::string
RGWFormPost::get_current_filename() const
2096 const auto& field
= current_data_part
->fields
.at("Content-Disposition");
2097 const auto iter
= field
.params
.find("filename");
2099 if (std::end(field
.params
) != iter
) {
2100 return prefix
+ iter
->second
;
2102 } catch (std::out_of_range
&) {
2109 std::string
RGWFormPost::get_current_content_type() const
2112 const auto& field
= current_data_part
->fields
.at("Content-Type");
2114 } catch (std::out_of_range
&) {
2118 return std::string();
2121 bool RGWFormPost::is_next_file_to_upload()
2123 if (! stream_done
) {
2124 /* We have at least one additional part in the body. */
2125 struct post_form_part part
;
2126 int r
= read_form_part_header(&part
, stream_done
);
2131 const auto field_iter
= part
.fields
.find("Content-Disposition");
2132 if (std::end(part
.fields
) != field_iter
) {
2133 const auto& params
= field_iter
->second
.params
;
2134 const auto& filename_iter
= params
.find("filename");
2136 if (std::end(params
) != filename_iter
&& ! filename_iter
->second
.empty()) {
2137 current_data_part
= std::move(part
);
2146 int RGWFormPost::get_data(ceph::bufferlist
& bl
, bool& again
)
2150 int r
= read_data(bl
, s
->cct
->_conf
->rgw_max_chunk_size
,
2151 boundary
, stream_done
);
2156 /* Tell RGWPostObj::execute() that it has some data to put. */
2162 void RGWFormPost::send_response()
2164 std::string redirect
= get_part_str(ctrl_parts
, "redirect");
2165 if (! redirect
.empty()) {
2166 op_ret
= STATUS_REDIRECT
;
2169 set_req_state_err(s
, op_ret
);
2170 s
->err
.err_code
= err_msg
;
2172 if (! redirect
.empty()) {
2173 dump_redirect(s
, redirect
);
2175 end_header(s
, this);
2178 bool RGWFormPost::is_formpost_req(req_state
* const s
)
2180 std::string content_type
;
2181 std::map
<std::string
, std::string
> params
;
2183 parse_boundary_params(s
->info
.env
->get("CONTENT_TYPE", ""),
2184 content_type
, params
);
2186 return boost::algorithm::iequals(content_type
, "multipart/form-data") &&
2187 params
.count("boundary") > 0;
2191 RGWOp
*RGWHandler_REST_Service_SWIFT::op_get()
2193 return new RGWListBuckets_ObjStore_SWIFT
;
2196 RGWOp
*RGWHandler_REST_Service_SWIFT::op_head()
2198 return new RGWStatAccount_ObjStore_SWIFT
;
2201 RGWOp
*RGWHandler_REST_Service_SWIFT::op_put()
2203 if (s
->info
.args
.exists("extract-archive")) {
2204 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2209 RGWOp
*RGWHandler_REST_Service_SWIFT::op_post()
2211 if (s
->info
.args
.exists("bulk-delete")) {
2212 return new RGWBulkDelete_ObjStore_SWIFT
;
2214 return new RGWPutMetadataAccount_ObjStore_SWIFT
;
2217 RGWOp
*RGWHandler_REST_Service_SWIFT::op_delete()
2219 if (s
->info
.args
.exists("bulk-delete")) {
2220 return new RGWBulkDelete_ObjStore_SWIFT
;
2225 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret
,
2226 const std::string error_doc
)
2228 /* Try to throw it all away. */
2229 s
->formatter
->reset();
2231 class RGWGetErrorPage
: public RGWGetObj_ObjStore_SWIFT
{
2233 RGWGetErrorPage(RGWRados
* const store
,
2234 RGWHandler_REST
* const handler
,
2236 const int http_ret
) {
2237 /* Calling a virtual from the base class is safe as the subobject should
2238 * be properly initialized and we haven't overridden the init method. */
2239 init(store
, s
, handler
);
2241 set_custom_http_response(http_ret
);
2244 int error_handler(const int err_no
,
2245 std::string
* const error_content
) override
{
2246 /* Enforce that any error generated while getting the error page will
2247 * not be send to a client. This allows us to recover from the double
2248 * fault situation by sending the original message. */
2251 } get_errpage_op(store
, handler
, s
, http_ret
);
2253 s
->object
= std::to_string(http_ret
) + error_doc
;
2255 RGWOp
* newop
= &get_errpage_op
;
2257 return rgw_process_authenticated(handler
, newop
, &req
, s
, true);
2260 int RGWSwiftWebsiteHandler::error_handler(const int err_no
,
2261 std::string
* const error_content
)
2263 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2265 if (can_be_website_req() && ! ws_conf
.error_doc
.empty()) {
2266 set_req_state_err(s
, err_no
);
2267 return serve_errordoc(s
->err
.http_ret
, ws_conf
.error_doc
);
2270 /* Let's go to the default, no-op handler. */
2274 bool RGWSwiftWebsiteHandler::is_web_mode() const
2276 const boost::string_ref webmode
= s
->info
.env
->get("HTTP_X_WEB_MODE", "");
2277 return boost::algorithm::iequals(webmode
, "true");
2280 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2282 /* Static website works only with the GET or HEAD method. Nothing more. */
2283 static const std::set
<boost::string_ref
> ws_methods
= { "GET", "HEAD" };
2284 if (ws_methods
.count(s
->info
.method
) == 0) {
2288 /* We also need to handle early failures from the auth system. In such cases
2289 * req_state::auth.identity may be empty. Let's treat that the same way as
2290 * the anonymous access. */
2291 if (! s
->auth
.identity
) {
2295 /* Swift serves websites only for anonymous requests unless client explicitly
2296 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2297 if (s
->auth
.identity
->is_anonymous() || is_web_mode()) {
2304 RGWOp
* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2306 class RGWMovedPermanently
: public RGWOp
{
2307 const std::string location
;
2309 RGWMovedPermanently(const std::string
& location
)
2310 : location(location
) {
2313 int verify_permission() override
{
2317 void execute() override
{
2318 op_ret
= -ERR_PERMANENT_REDIRECT
;
2322 void send_response() override
{
2323 set_req_state_err(s
, op_ret
);
2325 dump_content_length(s
, 0);
2326 dump_redirect(s
, location
);
2327 end_header(s
, this);
2330 const string
name() override
{
2331 return "RGWMovedPermanently";
2335 return new RGWMovedPermanently(s
->info
.request_uri
+ '/');
2338 RGWOp
* RGWSwiftWebsiteHandler::get_ws_index_op()
2340 /* Retarget to get obj on requested index file. */
2341 if (! s
->object
.empty()) {
2342 s
->object
= s
->object
.name
+
2343 s
->bucket_info
.website_conf
.get_index_doc();
2345 s
->object
= s
->bucket_info
.website_conf
.get_index_doc();
2348 auto getop
= new RGWGetObj_ObjStore_SWIFT
;
2349 getop
->set_get_data(boost::algorithm::equals("GET", s
->info
.method
));
2354 RGWOp
* RGWSwiftWebsiteHandler::get_ws_listing_op()
2356 class RGWWebsiteListing
: public RGWListBucket_ObjStore_SWIFT
{
2357 const std::string prefix_override
;
2359 int get_params() override
{
2360 prefix
= prefix_override
;
2366 void send_response() override
{
2367 /* Generate the header now. */
2368 set_req_state_err(s
, op_ret
);
2370 dump_container_metadata(s
, bucket
, bucket_quota
,
2371 s
->bucket_info
.website_conf
);
2372 end_header(s
, this, "text/html");
2377 /* Now it's the time to start generating HTML bucket listing.
2378 * All the crazy stuff with crafting tags will be delegated to
2379 * RGWSwiftWebsiteListingFormatter. */
2380 std::stringstream ss
;
2381 RGWSwiftWebsiteListingFormatter
htmler(ss
, prefix
);
2383 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2384 htmler
.generate_header(s
->decoded_uri
,
2385 ws_conf
.listing_css_doc
);
2387 for (const auto& pair
: common_prefixes
) {
2388 std::string subdir_name
= pair
.first
;
2389 if (! subdir_name
.empty()) {
2390 /* To be compliant with Swift we need to remove the trailing
2392 subdir_name
.pop_back();
2395 htmler
.dump_subdir(subdir_name
);
2398 for (const rgw_bucket_dir_entry
& obj
: objs
) {
2399 if (! common_prefixes
.count(obj
.key
.name
+ '/')) {
2400 htmler
.dump_object(obj
);
2404 htmler
.generate_footer();
2405 dump_body(s
, ss
.str());
2408 /* Taking prefix_override by value to leverage std::string r-value ref
2409 * ctor and thus avoid extra memory copying/increasing ref counter. */
2410 RGWWebsiteListing(std::string prefix_override
)
2411 : prefix_override(std::move(prefix_override
)) {
2415 std::string prefix
= std::move(s
->object
.name
);
2416 s
->object
= rgw_obj_key();
2418 return new RGWWebsiteListing(std::move(prefix
));
2421 bool RGWSwiftWebsiteHandler::is_web_dir() const
2423 std::string subdir_name
= url_decode(s
->object
.name
);
2425 /* Remove character from the subdir name if it is "/". */
2426 if (subdir_name
.empty()) {
2428 } else if (subdir_name
.back() == '/') {
2429 subdir_name
.pop_back();
2432 rgw_obj
obj(s
->bucket
, std::move(subdir_name
));
2434 /* First, get attrset of the object we'll try to retrieve. */
2435 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2436 obj_ctx
.obj
.set_atomic(obj
);
2437 obj_ctx
.obj
.set_prefetch_data(obj
);
2439 RGWObjState
* state
= nullptr;
2440 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2444 /* A nonexistent object cannot be a considered as a marker representing
2445 * the emulation of catalog in FS hierarchy. */
2446 if (! state
->exists
) {
2450 /* Decode the content type. */
2451 std::string content_type
;
2452 get_contype_from_attrs(state
->attrset
, content_type
);
2454 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2455 const std::string subdir_marker
= ws_conf
.subdir_marker
.empty()
2456 ? "application/directory"
2457 : ws_conf
.subdir_marker
;
2458 return subdir_marker
== content_type
&& state
->size
<= 1;
2461 bool RGWSwiftWebsiteHandler::is_index_present(const std::string
& index
)
2463 rgw_obj
obj(s
->bucket
, index
);
2465 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2466 obj_ctx
.obj
.set_atomic(obj
);
2467 obj_ctx
.obj
.set_prefetch_data(obj
);
2469 RGWObjState
* state
= nullptr;
2470 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2474 /* A nonexistent object cannot be a considered as a viable index. We will
2475 * try to list the bucket or - if this is impossible - return an error. */
2476 return state
->exists
;
2479 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp
* op
, RGWOp
** new_op
)
2481 ldout(s
->cct
, 10) << "Starting retarget" << dendl
;
2482 RGWOp
* op_override
= nullptr;
2484 /* In Swift static web content is served if the request is anonymous or
2485 * has X-Web-Mode HTTP header specified to true. */
2486 if (can_be_website_req()) {
2487 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2488 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2490 if (s
->decoded_uri
.back() != '/') {
2491 op_override
= get_ws_redirect_op();
2492 } else if (! index
.empty() && is_index_present(index
)) {
2493 op_override
= get_ws_index_op();
2494 } else if (ws_conf
.listing_enabled
) {
2495 op_override
= get_ws_listing_op();
2500 handler
->put_op(op
);
2501 op_override
->init(store
, s
, handler
);
2503 *new_op
= op_override
;
2508 /* Return 404 Not Found is the request has web mode enforced but we static web
2509 * wasn't able to serve it accordingly. */
2510 return ! op_override
&& is_web_mode() ? -ENOENT
: 0;
2513 int RGWSwiftWebsiteHandler::retarget_object(RGWOp
* op
, RGWOp
** new_op
)
2515 ldout(s
->cct
, 10) << "Starting object retarget" << dendl
;
2516 RGWOp
* op_override
= nullptr;
2518 /* In Swift static web content is served if the request is anonymous or
2519 * has X-Web-Mode HTTP header specified to true. */
2520 if (can_be_website_req() && is_web_dir()) {
2521 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2522 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2524 if (s
->decoded_uri
.back() != '/') {
2525 op_override
= get_ws_redirect_op();
2526 } else if (! index
.empty() && is_index_present(index
)) {
2527 op_override
= get_ws_index_op();
2528 } else if (ws_conf
.listing_enabled
) {
2529 op_override
= get_ws_listing_op();
2532 /* A regular request or the specified object isn't a subdirectory marker.
2533 * We don't need any re-targeting. Error handling (like sending a custom
2534 * error page) will be performed by error_handler of the actual RGWOp. */
2539 handler
->put_op(op
);
2540 op_override
->init(store
, s
, handler
);
2542 *new_op
= op_override
;
2547 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2548 return ! op_override
? -ENOENT
: 0;
2552 RGWOp
*RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data
)
2555 return new RGWGetACLs_ObjStore_SWIFT
;
2559 return new RGWListBucket_ObjStore_SWIFT
;
2561 return new RGWStatBucket_ObjStore_SWIFT
;
2564 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_get()
2566 return get_obj_op(true);
2569 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_head()
2571 return get_obj_op(false);
2574 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_put()
2577 return new RGWPutACLs_ObjStore_SWIFT
;
2579 if(s
->info
.args
.exists("extract-archive")) {
2580 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2582 return new RGWCreateBucket_ObjStore_SWIFT
;
2585 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_delete()
2587 return new RGWDeleteBucket_ObjStore_SWIFT
;
2590 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_post()
2592 if (RGWFormPost::is_formpost_req(s
)) {
2593 return new RGWFormPost
;
2595 return new RGWPutMetadataBucket_ObjStore_SWIFT
;
2599 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_options()
2601 return new RGWOptionsCORS_ObjStore_SWIFT
;
2605 RGWOp
*RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data
)
2608 return new RGWGetACLs_ObjStore_SWIFT
;
2611 RGWGetObj_ObjStore_SWIFT
*get_obj_op
= new RGWGetObj_ObjStore_SWIFT
;
2612 get_obj_op
->set_get_data(get_data
);
2616 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_get()
2618 return get_obj_op(true);
2621 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_head()
2623 return get_obj_op(false);
2626 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_put()
2629 return new RGWPutACLs_ObjStore_SWIFT
;
2631 if(s
->info
.args
.exists("extract-archive")) {
2632 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2634 if (s
->init_state
.src_bucket
.empty())
2635 return new RGWPutObj_ObjStore_SWIFT
;
2637 return new RGWCopyObj_ObjStore_SWIFT
;
2640 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_delete()
2642 return new RGWDeleteObj_ObjStore_SWIFT
;
2645 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_post()
2647 if (RGWFormPost::is_formpost_req(s
)) {
2648 return new RGWFormPost
;
2650 return new RGWPutMetadataObject_ObjStore_SWIFT
;
2654 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_copy()
2656 return new RGWCopyObj_ObjStore_SWIFT
;
2659 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_options()
2661 return new RGWOptionsCORS_ObjStore_SWIFT
;
2665 int RGWHandler_REST_SWIFT::authorize()
2667 return rgw::auth::Strategy::apply(auth_strategy
, s
);
2670 int RGWHandler_REST_SWIFT::postauth_init()
2672 struct req_init_state
* t
= &s
->init_state
;
2674 /* XXX Stub this until Swift Auth sets account into URL. */
2675 s
->bucket_tenant
= s
->user
->user_id
.tenant
;
2676 s
->bucket_name
= t
->url_bucket
;
2678 dout(10) << "s->object=" <<
2679 (!s
->object
.empty() ? s
->object
: rgw_obj_key("<NULL>"))
2681 << rgw_make_bucket_entry_name(s
->bucket_tenant
, s
->bucket_name
)
2685 ret
= rgw_validate_tenant_name(s
->bucket_tenant
);
2688 ret
= validate_bucket_name(s
->bucket_name
);
2691 ret
= validate_object_name(s
->object
.name
);
2695 if (!t
->src_bucket
.empty()) {
2697 * We don't allow cross-tenant copy at present. It requires account
2698 * names in the URL for Swift.
2700 s
->src_tenant_name
= s
->user
->user_id
.tenant
;
2701 s
->src_bucket_name
= t
->src_bucket
;
2703 ret
= validate_bucket_name(s
->src_bucket_name
);
2707 ret
= validate_object_name(s
->src_object
.name
);
2716 int RGWHandler_REST_SWIFT::validate_bucket_name(const string
& bucket
)
2718 const size_t len
= bucket
.size();
2720 if (len
> MAX_BUCKET_NAME_LEN
) {
2721 /* Bucket Name too long. Generate custom error message and bind it
2722 * to an R-value reference. */
2723 const auto msg
= boost::str(
2724 boost::format("Container name length of %lld longer than %lld")
2725 % len
% int(MAX_BUCKET_NAME_LEN
));
2726 set_req_state_err(s
, ERR_INVALID_BUCKET_NAME
, msg
);
2727 return -ERR_INVALID_BUCKET_NAME
;
2730 const auto ret
= RGWHandler_REST::validate_bucket_name(bucket
);
2738 if (bucket
[0] == '.')
2739 return -ERR_INVALID_BUCKET_NAME
;
2741 if (check_utf8(bucket
.c_str(), len
))
2742 return -ERR_INVALID_UTF8
;
2744 const char *s
= bucket
.c_str();
2746 for (size_t i
= 0; i
< len
; ++i
, ++s
) {
2747 if (*(unsigned char *)s
== 0xff)
2748 return -ERR_INVALID_BUCKET_NAME
;
2754 static void next_tok(string
& str
, string
& tok
, char delim
)
2756 if (str
.size() == 0) {
2761 int pos
= str
.find(delim
);
2763 tok
= str
.substr(0, pos
);
2764 str
= str
.substr(pos
+ 1);
2770 int RGWHandler_REST_SWIFT::init_from_header(struct req_state
* const s
,
2771 const std::string
& frontend_prefix
)
2776 s
->prot_flags
|= RGW_REST_SWIFT
;
2778 char reqbuf
[frontend_prefix
.length() + s
->decoded_uri
.length() + 1];
2779 sprintf(reqbuf
, "%s%s", frontend_prefix
.c_str(), s
->decoded_uri
.c_str());
2780 const char *req_name
= reqbuf
;
2784 if (*req_name
== '?') {
2787 p
= s
->info
.request_params
.c_str();
2790 s
->info
.args
.set(p
);
2791 s
->info
.args
.parse();
2793 /* Skip the leading slash of URL hierarchy. */
2794 if (req_name
[0] != '/') {
2800 if ('\0' == req_name
[0]) {
2801 return g_conf
->rgw_swift_url_prefix
== "/" ? -ERR_BAD_URL
: 0;
2806 size_t pos
= req
.find('/');
2807 if (std::string::npos
!= pos
&& g_conf
->rgw_swift_url_prefix
!= "/") {
2808 bool cut_url
= g_conf
->rgw_swift_url_prefix
.length();
2809 first
= req
.substr(0, pos
);
2811 if (first
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2813 /* Rewind to the "v1/..." part. */
2814 next_tok(req
, first
, '/');
2817 } else if (req
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2818 s
->formatter
= new RGWFormatter_Plain
;
2819 return -ERR_BAD_URL
;
2824 std::string tenant_path
;
2825 if (! g_conf
->rgw_swift_tenant_name
.empty()) {
2826 tenant_path
= "/AUTH_";
2827 tenant_path
.append(g_conf
->rgw_swift_tenant_name
);
2830 /* verify that the request_uri conforms with what's expected */
2831 char buf
[g_conf
->rgw_swift_url_prefix
.length() + 16 + tenant_path
.length()];
2833 if (g_conf
->rgw_swift_url_prefix
== "/") {
2834 blen
= sprintf(buf
, "/v1%s", tenant_path
.c_str());
2836 blen
= sprintf(buf
, "/%s/v1%s",
2837 g_conf
->rgw_swift_url_prefix
.c_str(), tenant_path
.c_str());
2840 if (strncmp(reqbuf
, buf
, blen
) != 0) {
2844 int ret
= allocate_formatter(s
, RGW_FORMAT_PLAIN
, true);
2850 next_tok(req
, ver
, '/');
2852 if (!tenant_path
.empty() || g_conf
->rgw_swift_account_in_url
) {
2853 string account_name
;
2854 next_tok(req
, account_name
, '/');
2856 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2857 const vector
<string
> skipped_prefixes
= { "AUTH_", "KEY_" };
2859 for (const auto pfx
: skipped_prefixes
) {
2860 const size_t comp_len
= min(account_name
.length(), pfx
.length());
2861 if (account_name
.compare(0, comp_len
, pfx
) == 0) {
2862 /* Prefix is present. Drop it. */
2863 account_name
= account_name
.substr(comp_len
);
2868 if (account_name
.empty()) {
2869 return -ERR_PRECONDITION_FAILED
;
2871 s
->account_name
= account_name
;
2875 next_tok(req
, first
, '/');
2877 dout(10) << "ver=" << ver
<< " first=" << first
<< " req=" << req
<< dendl
;
2878 if (first
.size() == 0)
2881 s
->info
.effective_uri
= "/" + first
;
2883 // Save bucket to tide us over until token is parsed.
2884 s
->init_state
.url_bucket
= first
;
2888 rgw_obj_key(req
, s
->info
.env
->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2889 s
->info
.effective_uri
.append("/" + s
->object
.name
);
2895 int RGWHandler_REST_SWIFT::init(RGWRados
* store
, struct req_state
* s
,
2896 rgw::io::BasicClient
*cio
)
2898 struct req_init_state
*t
= &s
->init_state
;
2900 s
->dialect
= "swift";
2902 std::string copy_source
=
2903 url_decode(s
->info
.env
->get("HTTP_X_COPY_FROM", ""));
2904 if (! copy_source
.empty()) {
2905 bool result
= RGWCopyObj::parse_copy_location(copy_source
, t
->src_bucket
,
2908 return -ERR_BAD_URL
;
2911 if (s
->op
== OP_COPY
) {
2912 std::string req_dest
=
2913 url_decode(s
->info
.env
->get("HTTP_DESTINATION", ""));
2914 if (req_dest
.empty())
2915 return -ERR_BAD_URL
;
2917 std::string dest_bucket_name
;
2918 rgw_obj_key dest_obj_key
;
2920 RGWCopyObj::parse_copy_location(req_dest
, dest_bucket_name
,
2923 return -ERR_BAD_URL
;
2925 std::string dest_object
= dest_obj_key
.name
;
2927 /* convert COPY operation into PUT */
2928 t
->src_bucket
= t
->url_bucket
;
2929 s
->src_object
= s
->object
;
2930 t
->url_bucket
= dest_bucket_name
;
2931 s
->object
= rgw_obj_key(dest_object
);
2935 return RGWHandler_REST::init(store
, s
, cio
);
2939 RGWRESTMgr_SWIFT::get_handler(struct req_state
* const s
,
2940 const rgw::auth::StrategyRegistry
& auth_registry
,
2941 const std::string
& frontend_prefix
)
2943 int ret
= RGWHandler_REST_SWIFT::init_from_header(s
, frontend_prefix
);
2945 ldout(s
->cct
, 10) << "init_from_header returned err=" << ret
<< dendl
;
2949 const auto& auth_strategy
= auth_registry
.get_swift();
2951 if (s
->init_state
.url_bucket
.empty()) {
2952 return new RGWHandler_REST_Service_SWIFT(auth_strategy
);
2955 if (s
->object
.empty()) {
2956 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy
);
2959 return new RGWHandler_REST_Obj_SWIFT(auth_strategy
);
2962 RGWHandler_REST
* RGWRESTMgr_SWIFT_Info::get_handler(
2963 struct req_state
* const s
,
2964 const rgw::auth::StrategyRegistry
& auth_registry
,
2965 const std::string
& frontend_prefix
)
2967 s
->prot_flags
|= RGW_REST_SWIFT
;
2968 const auto& auth_strategy
= auth_registry
.get_swift();
2969 return new RGWHandler_REST_SWIFT_Info(auth_strategy
);