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/optional.hpp>
6 #include <boost/utility/in_place_factory.hpp>
8 #include "include/assert.h"
11 #include "common/Formatter.h"
12 #include "common/utf8.h"
13 #include "common/ceph_json.h"
15 #include "rgw_rest_swift.h"
16 #include "rgw_acl_swift.h"
17 #include "rgw_cors_swift.h"
18 #include "rgw_formats.h"
19 #include "rgw_client_io.h"
22 #include "rgw_swift_auth.h"
24 #include "rgw_request.h"
25 #include "rgw_process.h"
31 #include <boost/utility/string_ref.hpp>
33 #define dout_context g_ceph_context
34 #define dout_subsys ceph_subsys_rgw
36 int RGWListBuckets_ObjStore_SWIFT::get_params()
38 prefix
= s
->info
.args
.get("prefix");
39 marker
= s
->info
.args
.get("marker");
40 end_marker
= s
->info
.args
.get("end_marker");
42 string limit_str
= s
->info
.args
.get("limit");
43 if (!limit_str
.empty()) {
45 long l
= strict_strtol(limit_str
.c_str(), 10, &err
);
50 if (l
> (long)limit_max
|| l
< 0) {
51 return -ERR_PRECONDITION_FAILED
;
57 if (s
->cct
->_conf
->rgw_swift_need_stats
) {
59 int r
= s
->info
.args
.get_bool("stats", &stats
, &exists
);
75 static void dump_account_metadata(struct req_state
* const s
,
76 const uint32_t buckets_count
,
77 const uint64_t buckets_object_count
,
78 const uint64_t buckets_size
,
79 const uint64_t buckets_size_rounded
,
80 /* const */map
<string
, bufferlist
>& attrs
,
81 const RGWQuotaInfo
& quota
,
82 const RGWAccessControlPolicy_SWIFTAcct
&policy
)
84 /* Adding X-Timestamp to keep align with Swift API */
85 dump_header(s
, "X-Timestamp", ceph_clock_now());
87 dump_header(s
, "X-Account-Container-Count", buckets_count
);
88 dump_header(s
, "X-Account-Object-Count", buckets_object_count
);
89 dump_header(s
, "X-Account-Bytes-Used", buckets_size
);
90 dump_header(s
, "X-Account-Bytes-Used-Actual", buckets_size_rounded
);
92 /* Dump TempURL-related stuff */
93 if (s
->perm_mask
== RGW_PERM_FULL_CONTROL
) {
94 auto iter
= s
->user
->temp_url_keys
.find(0);
95 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
96 dump_header(s
, "X-Account-Meta-Temp-Url-Key", iter
->second
);
99 iter
= s
->user
->temp_url_keys
.find(1);
100 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
101 dump_header(s
, "X-Account-Meta-Temp-Url-Key-2", iter
->second
);
105 /* Dump quota headers. */
107 if (quota
.max_size
>= 0) {
108 dump_header(s
, "X-Account-Meta-Quota-Bytes", quota
.max_size
);
111 /* Limit on the number of objects in a given account is a RadosGW's
112 * extension. Swift's account quota WSGI filter doesn't support it. */
113 if (quota
.max_objects
>= 0) {
114 dump_header(s
, "X-Account-Meta-Quota-Count", quota
.max_objects
);
118 /* Dump user-defined metadata items and generic attrs. */
119 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
120 map
<string
, bufferlist
>::iterator iter
;
121 for (iter
= attrs
.lower_bound(RGW_ATTR_PREFIX
); iter
!= attrs
.end(); ++iter
) {
122 const char *name
= iter
->first
.c_str();
123 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
125 if (geniter
!= rgw_to_http_attrs
.end()) {
126 dump_header(s
, geniter
->second
, iter
->second
);
127 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
128 dump_header_prefixed(s
, "X-Account-Meta-",
129 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
134 /* Dump account ACLs */
135 auto account_acls
= policy
.to_str();
137 dump_header(s
, "X-Account-Access-Control", std::move(*account_acls
));
141 void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets
)
144 set_req_state_err(s
, op_ret
);
145 } else if (!has_buckets
&& s
->format
== RGW_FORMAT_PLAIN
) {
146 op_ret
= STATUS_NO_CONTENT
;
147 set_req_state_err(s
, op_ret
);
150 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
151 /* Adding account stats in the header to keep align with Swift API */
152 dump_account_metadata(s
,
156 buckets_size_rounded
,
159 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
161 end_header(s
, NULL
, NULL
, NO_CONTENT_LENGTH
, true);
166 s
->formatter
->open_array_section_with_attrs("account",
167 FormatterAttrs("name", s
->user
->display_name
.c_str(), NULL
));
173 void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets
& buckets
)
179 /* Take care of the prefix parameter of Swift API. There is no business
180 * in applying the filter earlier as we really need to go through all
181 * entries regardless of it (the headers like X-Account-Container-Count
182 * aren't affected by specifying prefix). */
183 const std::map
<std::string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
184 for (auto iter
= m
.lower_bound(prefix
);
185 iter
!= m
.end() && boost::algorithm::starts_with(iter
->first
, prefix
);
187 const RGWBucketEnt
& obj
= iter
->second
;
189 s
->formatter
->open_object_section("container");
190 s
->formatter
->dump_string("name", obj
.bucket
.name
);
192 s
->formatter
->dump_int("count", obj
.count
);
193 s
->formatter
->dump_int("bytes", obj
.size
);
195 s
->formatter
->close_section();
196 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
197 rgw_flush_formatter(s
, s
->formatter
);
202 void RGWListBuckets_ObjStore_SWIFT::send_response_end()
205 s
->formatter
->close_section();
208 if (s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
209 /* Adding account stats in the header to keep align with Swift API */
210 dump_account_metadata(s
,
214 buckets_size_rounded
,
217 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
219 end_header(s
, NULL
, NULL
, s
->formatter
->get_len(), true);
222 if (sent_data
|| s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
223 rgw_flush_formatter_and_reset(s
, s
->formatter
);
227 int RGWListBucket_ObjStore_SWIFT::get_params()
229 prefix
= s
->info
.args
.get("prefix");
230 marker
= s
->info
.args
.get("marker");
231 end_marker
= s
->info
.args
.get("end_marker");
232 max_keys
= s
->info
.args
.get("limit");
233 op_ret
= parse_max_keys();
237 if (max
> default_max
)
238 return -ERR_PRECONDITION_FAILED
;
240 delimiter
= s
->info
.args
.get("delimiter");
243 if (s
->info
.args
.exists("path")) { // should handle empty path
244 path_args
= s
->info
.args
.get("path");
245 if (!delimiter
.empty() || !prefix
.empty()) {
252 if (path
.size() && path
[path
.size() - 1] != '/')
255 int len
= prefix
.size();
256 int delim_size
= delimiter
.size();
258 if (len
>= delim_size
) {
259 if (prefix
.substr(len
- delim_size
).compare(delimiter
) != 0)
260 prefix
.append(delimiter
);
267 static void dump_container_metadata(struct req_state
*,
270 const RGWBucketWebsiteConf
&);
272 void RGWListBucket_ObjStore_SWIFT::send_response()
274 vector
<rgw_bucket_dir_entry
>::iterator iter
= objs
.begin();
275 map
<string
, bool>::iterator pref_iter
= common_prefixes
.begin();
278 dump_container_metadata(s
, bucket
, bucket_quota
,
279 s
->bucket_info
.website_conf
);
281 s
->formatter
->open_array_section_with_attrs("container", FormatterAttrs("name", s
->bucket
.name
.c_str(), NULL
));
283 while (iter
!= objs
.end() || pref_iter
!= common_prefixes
.end()) {
284 bool do_pref
= false;
285 bool do_objs
= false;
287 if (iter
!= objs
.end()) {
290 if (pref_iter
== common_prefixes
.end())
292 else if (iter
== objs
.end())
294 else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) == 0) {
297 } else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) <= 0)
302 if (do_objs
&& (marker
.empty() || marker
< key
)) {
303 if (key
.name
.compare(path
) == 0)
306 s
->formatter
->open_object_section("object");
307 s
->formatter
->dump_string("name", key
.name
);
308 s
->formatter
->dump_string("hash", iter
->meta
.etag
);
309 s
->formatter
->dump_int("bytes", iter
->meta
.accounted_size
);
310 if (!iter
->meta
.user_data
.empty())
311 s
->formatter
->dump_string("user_custom_data", iter
->meta
.user_data
);
312 string single_content_type
= iter
->meta
.content_type
;
313 if (iter
->meta
.content_type
.size()) {
314 // content type might hold multiple values, just dump the last one
315 ssize_t pos
= iter
->meta
.content_type
.rfind(',');
318 while (single_content_type
[pos
] == ' ')
320 single_content_type
= single_content_type
.substr(pos
);
322 s
->formatter
->dump_string("content_type", single_content_type
);
324 dump_time(s
, "last_modified", &iter
->meta
.mtime
);
325 s
->formatter
->close_section();
328 if (do_pref
&& (marker
.empty() || pref_iter
->first
.compare(marker
.name
) > 0)) {
329 const string
& name
= pref_iter
->first
;
330 if (name
.compare(delimiter
) == 0)
333 s
->formatter
->open_object_section_with_attrs("subdir", FormatterAttrs("name", name
.c_str(), NULL
));
335 /* swift is a bit inconsistent here */
338 s
->formatter
->dump_string("name", name
);
341 s
->formatter
->dump_string("subdir", name
);
343 s
->formatter
->close_section();
352 s
->formatter
->close_section();
354 int64_t content_len
= 0;
356 content_len
= s
->formatter
->get_len();
357 if (content_len
== 0) {
358 op_ret
= STATUS_NO_CONTENT
;
360 } else if (op_ret
> 0) {
364 set_req_state_err(s
, op_ret
);
366 end_header(s
, this, NULL
, content_len
);
371 rgw_flush_formatter_and_reset(s
, s
->formatter
);
374 static void dump_container_metadata(struct req_state
*s
,
375 const RGWBucketEnt
& bucket
,
376 const RGWQuotaInfo
& quota
,
377 const RGWBucketWebsiteConf
& ws_conf
)
379 /* Adding X-Timestamp to keep align with Swift API */
380 dump_header(s
, "X-Timestamp", utime_t(s
->bucket_info
.creation_time
));
382 dump_header(s
, "X-Container-Object-Count", bucket
.count
);
383 dump_header(s
, "X-Container-Bytes-Used", bucket
.size
);
384 dump_header(s
, "X-Container-Bytes-Used-Actual", bucket
.size_rounded
);
386 if (s
->object
.empty()) {
387 auto swift_policy
= \
388 static_cast<RGWAccessControlPolicy_SWIFT
*>(s
->bucket_acl
.get());
389 std::string read_acl
, write_acl
;
390 swift_policy
->to_str(read_acl
, write_acl
);
392 if (read_acl
.size()) {
393 dump_header(s
, "X-Container-Read", read_acl
);
395 if (write_acl
.size()) {
396 dump_header(s
, "X-Container-Write", write_acl
);
398 if (!s
->bucket_info
.placement_rule
.empty()) {
399 dump_header(s
, "X-Storage-Policy", s
->bucket_info
.placement_rule
);
402 /* Dump user-defined metadata items and generic attrs. */
403 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
404 map
<string
, bufferlist
>::iterator iter
;
405 for (iter
= s
->bucket_attrs
.lower_bound(RGW_ATTR_PREFIX
);
406 iter
!= s
->bucket_attrs
.end();
408 const char *name
= iter
->first
.c_str();
409 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
411 if (geniter
!= rgw_to_http_attrs
.end()) {
412 dump_header(s
, geniter
->second
, iter
->second
);
413 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
414 dump_header_prefixed(s
, "X-Container-Meta-",
415 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
421 /* Dump container versioning info. */
422 if (! s
->bucket_info
.swift_ver_location
.empty()) {
423 dump_header(s
, "X-Versions-Location",
424 url_encode(s
->bucket_info
.swift_ver_location
));
427 /* Dump quota headers. */
429 if (quota
.max_size
>= 0) {
430 dump_header(s
, "X-Container-Meta-Quota-Bytes", quota
.max_size
);
433 if (quota
.max_objects
>= 0) {
434 dump_header(s
, "X-Container-Meta-Quota-Count", quota
.max_objects
);
438 /* Dump Static Website headers. */
439 if (! ws_conf
.index_doc_suffix
.empty()) {
440 dump_header(s
, "X-Container-Meta-Web-Index", ws_conf
.index_doc_suffix
);
443 if (! ws_conf
.error_doc
.empty()) {
444 dump_header(s
, "X-Container-Meta-Web-Error", ws_conf
.error_doc
);
447 if (! ws_conf
.subdir_marker
.empty()) {
448 dump_header(s
, "X-Container-Meta-Web-Directory-Type",
449 ws_conf
.subdir_marker
);
452 if (! ws_conf
.listing_css_doc
.empty()) {
453 dump_header(s
, "X-Container-Meta-Web-Listings-CSS",
454 ws_conf
.listing_css_doc
);
457 if (ws_conf
.listing_enabled
) {
458 dump_header(s
, "X-Container-Meta-Web-Listings", "true");
462 void RGWStatAccount_ObjStore_SWIFT::execute()
464 RGWStatAccount_ObjStore::execute();
465 op_ret
= rgw_get_user_attrs_by_uid(store
, s
->user
->user_id
, attrs
);
468 void RGWStatAccount_ObjStore_SWIFT::send_response()
471 op_ret
= STATUS_NO_CONTENT
;
472 dump_account_metadata(s
,
476 buckets_size_rounded
,
479 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
482 set_req_state_err(s
, op_ret
);
485 end_header(s
, NULL
, NULL
, 0, true);
490 void RGWStatBucket_ObjStore_SWIFT::send_response()
493 op_ret
= STATUS_NO_CONTENT
;
494 dump_container_metadata(s
, bucket
, bucket_quota
,
495 s
->bucket_info
.website_conf
);
498 set_req_state_err(s
, op_ret
);
501 end_header(s
, this, NULL
, 0, true);
505 static int get_swift_container_settings(req_state
* const s
,
506 RGWRados
* const store
,
507 RGWAccessControlPolicy
* const policy
,
508 bool * const has_policy
,
510 RGWCORSConfiguration
* const cors_config
,
511 bool * const has_cors
)
513 string read_list
, write_list
;
515 const char * const read_attr
= s
->info
.env
->get("HTTP_X_CONTAINER_READ");
517 read_list
= read_attr
;
519 const char * const write_attr
= s
->info
.env
->get("HTTP_X_CONTAINER_WRITE");
521 write_list
= write_attr
;
526 if (read_attr
|| write_attr
) {
527 RGWAccessControlPolicy_SWIFT
swift_policy(s
->cct
);
528 const auto r
= swift_policy
.create(store
,
530 s
->user
->display_name
,
538 *policy
= swift_policy
;
544 /*Check and update CORS configuration*/
545 const char *allow_origins
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
546 const char *allow_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
547 const char *expose_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
548 const char *max_age
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
550 RGWCORSConfiguration_SWIFT
*swift_cors
= new RGWCORSConfiguration_SWIFT
;
551 int r
= swift_cors
->create_update(allow_origins
, allow_headers
, expose_headers
, max_age
);
553 dout(0) << "Error creating/updating the cors configuration" << dendl
;
558 *cors_config
= *swift_cors
;
566 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
567 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
568 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
569 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
571 static void get_rmattrs_from_headers(const req_state
* const s
,
572 const char * const put_prefix
,
573 const char * const del_prefix
,
574 set
<string
>& rmattr_names
)
576 const size_t put_prefix_len
= strlen(put_prefix
);
577 const size_t del_prefix_len
= strlen(del_prefix
);
579 for (const auto& kv
: s
->info
.env
->get_map()) {
580 size_t prefix_len
= 0;
581 const char * const p
= kv
.first
.c_str();
583 if (strncasecmp(p
, del_prefix
, del_prefix_len
) == 0) {
584 /* Explicitly requested removal. */
585 prefix_len
= del_prefix_len
;
586 } else if ((strncasecmp(p
, put_prefix
, put_prefix_len
) == 0)
587 && kv
.second
.empty()) {
588 /* Removal requested by putting an empty value. */
589 prefix_len
= put_prefix_len
;
592 if (prefix_len
> 0) {
593 string
name(RGW_ATTR_META_PREFIX
);
594 name
.append(lowercase_dash_http_attr(p
+ prefix_len
));
595 rmattr_names
.insert(name
);
600 static int get_swift_versioning_settings(
602 boost::optional
<std::string
>& swift_ver_location
)
604 /* Removing the Swift's versions location has lower priority than setting
605 * a new one. That's the reason why we're handling it first. */
606 const std::string vlocdel
=
607 s
->info
.env
->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
608 if (vlocdel
.size()) {
609 swift_ver_location
= boost::in_place(std::string());
612 if (s
->info
.env
->exists("HTTP_X_VERSIONS_LOCATION")) {
613 /* If the Swift's versioning is globally disabled but someone wants to
614 * enable it for a given container, new version of Swift will generate
615 * the precondition failed error. */
616 if (! s
->cct
->_conf
->rgw_swift_versioning_enabled
) {
617 return -ERR_PRECONDITION_FAILED
;
620 swift_ver_location
= s
->info
.env
->get("HTTP_X_VERSIONS_LOCATION", "");
626 int RGWCreateBucket_ObjStore_SWIFT::get_params()
629 uint32_t policy_rw_mask
= 0;
631 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
632 &policy_rw_mask
, &cors_config
, &has_cors
);
638 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
641 location_constraint
= store
->get_zonegroup().api_name
;
642 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
,
643 CONT_REMOVE_ATTR_PREFIX
, rmattr_names
);
644 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
646 return get_swift_versioning_settings(s
, swift_ver_location
);
649 void RGWCreateBucket_ObjStore_SWIFT::send_response()
652 op_ret
= STATUS_CREATED
;
653 else if (op_ret
== -ERR_BUCKET_EXISTS
)
654 op_ret
= STATUS_ACCEPTED
;
655 set_req_state_err(s
, op_ret
);
657 /* Propose ending HTTP header with 0 Content-Length header. */
658 end_header(s
, NULL
, NULL
, 0);
659 rgw_flush_formatter_and_reset(s
, s
->formatter
);
662 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
666 r
= STATUS_NO_CONTENT
;
668 set_req_state_err(s
, r
);
670 end_header(s
, this, NULL
, 0);
671 rgw_flush_formatter_and_reset(s
, s
->formatter
);
674 static int get_delete_at_param(req_state
*s
, boost::optional
<real_time
> &delete_at
)
676 /* Handle Swift object expiration. */
677 real_time delat_proposal
;
678 string x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AFTER", "");
680 if (x_delete
.empty()) {
681 x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AT", "");
683 /* X-Delete-After HTTP is present. It means we need add its value
684 * to the current time. */
685 delat_proposal
= real_clock::now();
688 if (x_delete
.empty()) {
689 delete_at
= boost::none
;
690 if (s
->info
.env
->exists("HTTP_X_REMOVE_DELETE_AT")) {
691 delete_at
= boost::in_place(real_time());
696 long ts
= strict_strtoll(x_delete
.c_str(), 10, &err
);
702 delat_proposal
+= make_timespan(ts
);
703 if (delat_proposal
< real_clock::now()) {
707 delete_at
= delat_proposal
;
712 int RGWPutObj_ObjStore_SWIFT::verify_permission()
714 op_ret
= RGWPutObj_ObjStore::verify_permission();
716 /* We have to differentiate error codes depending on whether user is
717 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
718 * (403 Forbidden). */
719 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
726 int RGWPutObj_ObjStore_SWIFT::get_params()
728 if (s
->has_bad_meta
) {
733 const char *encoding
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
734 if (!encoding
|| strcmp(encoding
, "chunked") != 0) {
735 ldout(s
->cct
, 20) << "neither length nor chunked encoding" << dendl
;
736 return -ERR_LENGTH_REQUIRED
;
739 chunked_upload
= true;
742 supplied_etag
= s
->info
.env
->get("HTTP_ETAG");
744 if (!s
->generic_attrs
.count(RGW_ATTR_CONTENT_TYPE
)) {
745 ldout(s
->cct
, 5) << "content type wasn't provided, trying to guess" << dendl
;
746 const char *suffix
= strrchr(s
->object
.name
.c_str(), '.');
750 string
suffix_str(suffix
);
751 const char *mime
= rgw_find_mime_by_ext(suffix_str
);
753 s
->generic_attrs
[RGW_ATTR_CONTENT_TYPE
] = mime
;
759 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
761 int r
= get_delete_at_param(s
, delete_at
);
763 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
767 if (!s
->cct
->_conf
->rgw_swift_custom_header
.empty()) {
768 string custom_header
= s
->cct
->_conf
->rgw_swift_custom_header
;
769 if (s
->info
.env
->exists(custom_header
.c_str())) {
770 user_data
= s
->info
.env
->get(custom_header
.c_str());
774 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
776 string multipart_manifest
= s
->info
.args
.get("multipart-manifest", &exists
);
778 if (multipart_manifest
!= "put") {
779 ldout(s
->cct
, 5) << "invalid multipart-manifest http param: " << multipart_manifest
<< dendl
;
783 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
784 uint64_t max_len
= s
->cct
->_conf
->rgw_max_slo_entries
* MAX_SLO_ENTRY_SIZE
;
786 slo_info
= new RGWSLOInfo
;
788 int r
= rgw_rest_get_json_input_keep_data(s
->cct
, s
, slo_info
->entries
, max_len
, &slo_info
->raw_data
, &slo_info
->raw_data_len
);
790 ldout(s
->cct
, 5) << "failed to read input for slo r=" << r
<< dendl
;
794 if ((int64_t)slo_info
->entries
.size() > s
->cct
->_conf
->rgw_max_slo_entries
) {
795 ldout(s
->cct
, 5) << "too many entries in slo request: " << slo_info
->entries
.size() << dendl
;
800 uint64_t total_size
= 0;
801 for (const auto& entry
: slo_info
->entries
) {
802 etag_sum
.Update((const byte
*)entry
.etag
.c_str(),
803 entry
.etag
.length());
804 total_size
+= entry
.size_bytes
;
806 ldout(s
->cct
, 20) << "slo_part: " << entry
.path
807 << " size=" << entry
.size_bytes
808 << " etag=" << entry
.etag
811 complete_etag(etag_sum
, &lo_etag
);
812 slo_info
->total_size
= total_size
;
814 ofs
= slo_info
->raw_data_len
;
817 return RGWPutObj_ObjStore::get_params();
820 void RGWPutObj_ObjStore_SWIFT::send_response()
823 op_ret
= STATUS_CREATED
;
826 if (! lo_etag
.empty()) {
827 /* Static Large Object of Swift API has two etags represented by
829 * - etag - for the manifest itself (it will be stored in xattrs),
830 * - lo_etag - for the content composited from SLO's segments.
831 * The value is calculated basing on segments' etags.
832 * In response for PUT request we have to expose the second one.
833 * The first one may be obtained by GET with "multipart-manifest=get"
834 * in query string on a given SLO. */
835 dump_etag(s
, lo_etag
, true /* quoted */);
840 dump_last_modified(s
, mtime
);
841 set_req_state_err(s
, op_ret
);
844 rgw_flush_formatter_and_reset(s
, s
->formatter
);
847 static int get_swift_account_settings(req_state
* const s
,
848 RGWRados
* const store
,
849 RGWAccessControlPolicy_SWIFTAcct
* const policy
,
850 bool * const has_policy
)
854 const char * const acl_attr
= s
->info
.env
->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
856 RGWAccessControlPolicy_SWIFTAcct
swift_acct_policy(s
->cct
);
857 const bool r
= swift_acct_policy
.create(store
,
859 s
->user
->display_name
,
865 *policy
= swift_acct_policy
;
872 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
874 if (s
->has_bad_meta
) {
878 int ret
= get_swift_account_settings(s
,
880 // FIXME: we need to carry unique_ptr in generic class
881 // and allocate appropriate ACL class in the ctor
882 static_cast<RGWAccessControlPolicy_SWIFTAcct
*>(&policy
),
888 get_rmattrs_from_headers(s
, ACCT_PUT_ATTR_PREFIX
, ACCT_REMOVE_ATTR_PREFIX
,
893 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
896 op_ret
= STATUS_NO_CONTENT
;
898 set_req_state_err(s
, op_ret
);
901 rgw_flush_formatter_and_reset(s
, s
->formatter
);
904 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
906 if (s
->has_bad_meta
) {
910 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
911 &policy_rw_mask
, &cors_config
, &has_cors
);
916 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
, CONT_REMOVE_ATTR_PREFIX
,
918 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
920 return get_swift_versioning_settings(s
, swift_ver_location
);
923 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
925 if (!op_ret
&& (op_ret
!= -EINVAL
)) {
926 op_ret
= STATUS_NO_CONTENT
;
928 set_req_state_err(s
, op_ret
);
931 rgw_flush_formatter_and_reset(s
, s
->formatter
);
934 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
936 if (s
->has_bad_meta
) {
940 /* Handle Swift object expiration. */
941 int r
= get_delete_at_param(s
, delete_at
);
943 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
947 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
948 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
953 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
956 op_ret
= STATUS_ACCEPTED
;
958 set_req_state_err(s
, op_ret
);
960 dump_content_length(s
, 0);
964 rgw_flush_formatter_and_reset(s
, s
->formatter
);
967 static void bulkdelete_respond(const unsigned num_deleted
,
968 const unsigned int num_unfound
,
969 const std::list
<RGWBulkDelete::fail_desc_t
>& failures
,
970 const int prot_flags
, /* in */
971 ceph::Formatter
& formatter
) /* out */
973 formatter
.open_object_section("delete");
978 if (!failures
.empty()) {
979 int reason
= ERR_INVALID_REQUEST
;
980 for (const auto fail_desc
: failures
) {
981 if (-ENOENT
!= fail_desc
.err
&& -EACCES
!= fail_desc
.err
) {
982 reason
= fail_desc
.err
;
986 set_req_state_err(err
, reason
, prot_flags
);
987 dump_errno(err
, resp_status
);
988 } else if (0 == num_deleted
&& 0 == num_unfound
) {
989 /* 400 Bad Request */
990 dump_errno(400, resp_status
);
991 resp_body
= "Invalid bulk delete.";
994 dump_errno(200, resp_status
);
997 encode_json("Number Deleted", num_deleted
, &formatter
);
998 encode_json("Number Not Found", num_unfound
, &formatter
);
999 encode_json("Response Body", resp_body
, &formatter
);
1000 encode_json("Response Status", resp_status
, &formatter
);
1002 formatter
.open_array_section("Errors");
1003 for (const auto fail_desc
: failures
) {
1004 formatter
.open_array_section("object");
1006 stringstream ss_name
;
1007 ss_name
<< fail_desc
.path
;
1008 encode_json("Name", ss_name
.str(), &formatter
);
1011 set_req_state_err(err
, fail_desc
.err
, prot_flags
);
1013 dump_errno(err
, status
);
1014 encode_json("Status", status
, &formatter
);
1015 formatter
.close_section();
1017 formatter
.close_section();
1019 formatter
.close_section();
1022 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1024 op_ret
= RGWDeleteObj_ObjStore::verify_permission();
1026 /* We have to differentiate error codes depending on whether user is
1027 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1028 * (403 Forbidden). */
1029 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1036 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1038 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1039 multipart_delete
= (mm
.compare("delete") == 0);
1041 return RGWDeleteObj_ObjStore::get_params();
1044 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1048 if (multipart_delete
) {
1051 r
= STATUS_NO_CONTENT
;
1054 set_req_state_err(s
, r
);
1057 if (multipart_delete
) {
1058 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1059 CHUNKED_TRANSFER_ENCODING
);
1062 bulkdelete_respond(deleter
->get_num_deleted(),
1063 deleter
->get_num_unfound(),
1064 deleter
->get_failures(),
1067 } else if (-ENOENT
== op_ret
) {
1068 bulkdelete_respond(0, 1, {}, s
->prot_flags
, *s
->formatter
);
1070 RGWBulkDelete::acct_path_t path
;
1071 path
.bucket_name
= s
->bucket_name
;
1072 path
.obj_key
= s
->object
;
1074 RGWBulkDelete::fail_desc_t fail_desc
;
1075 fail_desc
.err
= op_ret
;
1076 fail_desc
.path
= path
;
1078 bulkdelete_respond(0, 0, { fail_desc
}, s
->prot_flags
, *s
->formatter
);
1081 end_header(s
, this);
1084 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1088 static void get_contype_from_attrs(map
<string
, bufferlist
>& attrs
,
1089 string
& content_type
)
1091 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_CONTENT_TYPE
);
1092 if (iter
!= attrs
.end()) {
1093 content_type
= iter
->second
.c_str();
1097 static void dump_object_metadata(struct req_state
* const s
,
1098 map
<string
, bufferlist
> attrs
)
1100 map
<string
, string
> response_attrs
;
1102 for (auto kv
: attrs
) {
1103 const char * name
= kv
.first
.c_str();
1104 const auto aiter
= rgw_to_http_attrs
.find(name
);
1106 if (aiter
!= std::end(rgw_to_http_attrs
)) {
1107 response_attrs
[aiter
->second
] = kv
.second
.c_str();
1108 } else if (strcmp(name
, RGW_ATTR_SLO_UINDICATOR
) == 0) {
1109 // this attr has an extra length prefix from ::encode() in prior versions
1110 dump_header(s
, "X-Object-Meta-Static-Large-Object", "True");
1111 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
,
1112 sizeof(RGW_ATTR_META_PREFIX
)-1) == 0) {
1113 name
+= sizeof(RGW_ATTR_META_PREFIX
) - 1;
1114 dump_header_prefixed(s
, "X-Object-Meta-",
1115 camelcase_dash_http_attr(name
), kv
.second
);
1119 /* Handle override and fallback for Content-Disposition HTTP header.
1120 * At the moment this will be used only by TempURL of the Swift API. */
1121 const auto cditer
= rgw_to_http_attrs
.find(RGW_ATTR_CONTENT_DISP
);
1122 if (cditer
!= std::end(rgw_to_http_attrs
)) {
1123 const auto& name
= cditer
->second
;
1125 if (!s
->content_disp
.override
.empty()) {
1126 response_attrs
[name
] = s
->content_disp
.override
;
1127 } else if (!s
->content_disp
.fallback
.empty()
1128 && response_attrs
.find(name
) == std::end(response_attrs
)) {
1129 response_attrs
[name
] = s
->content_disp
.fallback
;
1133 for (const auto kv
: response_attrs
) {
1134 dump_header(s
, kv
.first
, kv
.second
);
1137 const auto iter
= attrs
.find(RGW_ATTR_DELETE_AT
);
1138 if (iter
!= std::end(attrs
)) {
1141 ::decode(delete_at
, iter
->second
);
1142 if (!delete_at
.is_zero()) {
1143 dump_header(s
, "X-Delete-At", delete_at
.sec());
1145 } catch (buffer::error
& err
) {
1146 ldout(s
->cct
, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1153 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1155 dest_policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1160 int RGWCopyObj_ObjStore_SWIFT::get_params()
1162 if_mod
= s
->info
.env
->get("HTTP_IF_MODIFIED_SINCE");
1163 if_unmod
= s
->info
.env
->get("HTTP_IF_UNMODIFIED_SINCE");
1164 if_match
= s
->info
.env
->get("HTTP_COPY_IF_MATCH");
1165 if_nomatch
= s
->info
.env
->get("HTTP_COPY_IF_NONE_MATCH");
1167 src_tenant_name
= s
->src_tenant_name
;
1168 src_bucket_name
= s
->src_bucket_name
;
1169 src_object
= s
->src_object
;
1170 dest_tenant_name
= s
->bucket_tenant
;
1171 dest_bucket_name
= s
->bucket_name
;
1172 dest_object
= s
->object
.name
;
1174 const char * const fresh_meta
= s
->info
.env
->get("HTTP_X_FRESH_METADATA");
1175 if (fresh_meta
&& strcasecmp(fresh_meta
, "TRUE") == 0) {
1176 attrs_mod
= RGWRados::ATTRSMOD_REPLACE
;
1178 attrs_mod
= RGWRados::ATTRSMOD_MERGE
;
1181 int r
= get_delete_at_param(s
, delete_at
);
1183 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1190 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs
)
1192 if (! sent_header
) {
1194 op_ret
= STATUS_CREATED
;
1195 set_req_state_err(s
, op_ret
);
1197 end_header(s
, this);
1199 /* Send progress information. Note that this diverge from the original swift
1200 * spec. We do this in order to keep connection alive.
1203 s
->formatter
->open_array_section("progress");
1207 s
->formatter
->dump_int("ofs", (uint64_t)ofs
);
1209 rgw_flush_formatter(s
, s
->formatter
);
1212 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1214 /* Dump X-Copied-From. */
1215 dump_header(s
, "X-Copied-From", url_encode(src_bucket
.name
) +
1216 "/" + url_encode(src_object
.name
));
1218 /* Dump X-Copied-From-Account. */
1220 dump_header(s
, "X-Copied-From-Account", url_encode(s
->user
->user_id
.id
));
1222 /* Dump X-Copied-From-Last-Modified. */
1223 dump_time_header(s
, "X-Copied-From-Last-Modified", src_mtime
);
1226 void RGWCopyObj_ObjStore_SWIFT::send_response()
1228 if (! sent_header
) {
1229 string content_type
;
1231 op_ret
= STATUS_CREATED
;
1232 set_req_state_err(s
, op_ret
);
1235 dump_last_modified(s
, mtime
);
1237 get_contype_from_attrs(attrs
, content_type
);
1238 dump_object_metadata(s
, attrs
);
1239 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1240 : "binary/octet-stream");
1242 s
->formatter
->close_section();
1243 rgw_flush_formatter(s
, s
->formatter
);
1247 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1249 op_ret
= RGWGetObj_ObjStore::verify_permission();
1251 /* We have to differentiate error codes depending on whether user is
1252 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1253 * (403 Forbidden). */
1254 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1261 int RGWGetObj_ObjStore_SWIFT::get_params()
1263 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1264 skip_manifest
= (mm
.compare("get") == 0);
1266 return RGWGetObj_ObjStore::get_params();
1269 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1271 std::string error_content
;
1272 op_ret
= error_handler(op_ret
, &error_content
);
1274 /* The error handler has taken care of the error. */
1278 bufferlist error_bl
;
1279 error_bl
.append(error_content
);
1280 return send_response_data(error_bl
, 0, error_bl
.length());
1283 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist
& bl
,
1287 string content_type
;
1293 if (custom_http_ret
) {
1294 set_req_state_err(s
, 0);
1295 dump_errno(s
, custom_http_ret
);
1297 set_req_state_err(s
, (partial_content
&& !op_ret
) ? STATUS_PARTIAL_CONTENT
1302 end_header(s
, NULL
);
1308 dump_range(s
, ofs
, end
, s
->obj_size
);
1312 end_header(s
, NULL
);
1316 dump_content_length(s
, total_len
);
1317 dump_last_modified(s
, lastmod
);
1318 dump_header(s
, "X-Timestamp", utime_t(lastmod
));
1320 dump_header(s
, "X-Static-Large-Object", "True");
1324 if (! lo_etag
.empty()) {
1325 dump_etag(s
, lo_etag
, true /* quoted */);
1327 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
1328 if (iter
!= attrs
.end()) {
1329 dump_etag(s
, iter
->second
);
1333 get_contype_from_attrs(attrs
, content_type
);
1334 dump_object_metadata(s
, attrs
);
1337 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1338 : "binary/octet-stream");
1343 if (get_data
&& !op_ret
) {
1344 const auto r
= dump_body(s
, bl
.c_str() + bl_ofs
, bl_len
);
1349 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1354 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1356 string hdrs
, exp_hdrs
;
1357 uint32_t max_age
= CORS_MAX_AGE_INVALID
;
1358 /*EACCES means, there is no CORS registered yet for the bucket
1359 *ENOENT means, there is no match of the Origin in the list of CORSRule
1361 if (op_ret
== -ENOENT
)
1364 set_req_state_err(s
, op_ret
);
1366 end_header(s
, NULL
);
1369 get_response_params(hdrs
, exp_hdrs
, &max_age
);
1371 dump_access_control(s
, origin
, req_meth
, hdrs
.c_str(), exp_hdrs
.c_str(),
1373 end_header(s
, NULL
);
1376 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1377 list
<RGWBulkDelete::acct_path_t
>& items
, bool * const is_truncated
)
1379 constexpr size_t MAX_LINE_SIZE
= 2048;
1381 RGWClientIOStreamBuf
ciosb(static_cast<RGWRestfulIO
&>(*(s
->cio
)),
1382 size_t(s
->cct
->_conf
->rgw_max_chunk_size
));
1383 istream
cioin(&ciosb
);
1385 char buf
[MAX_LINE_SIZE
];
1386 while (cioin
.getline(buf
, sizeof(buf
))) {
1387 string
path_str(buf
);
1389 ldout(s
->cct
, 20) << "extracted Bulk Delete entry: " << path_str
<< dendl
;
1391 RGWBulkDelete::acct_path_t path
;
1393 /* We need to skip all slashes at the beginning in order to preserve
1394 * compliance with Swift. */
1395 const size_t start_pos
= path_str
.find_first_not_of('/');
1397 if (string::npos
!= start_pos
) {
1398 /* Seperator is the first slash after the leading ones. */
1399 const size_t sep_pos
= path_str
.find('/', start_pos
);
1401 if (string::npos
!= sep_pos
) {
1402 path
.bucket_name
= url_decode(path_str
.substr(start_pos
,
1403 sep_pos
- start_pos
));
1404 path
.obj_key
= url_decode(path_str
.substr(sep_pos
+ 1));
1406 /* It's guaranteed here that bucket name is at least one character
1407 * long and is different than slash. */
1408 path
.bucket_name
= url_decode(path_str
.substr(start_pos
));
1411 items
.push_back(path
);
1414 if (items
.size() == MAX_CHUNK_ENTRIES
) {
1415 *is_truncated
= true;
1420 *is_truncated
= false;
1424 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1426 set_req_state_err(s
, op_ret
);
1428 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1429 CHUNKED_TRANSFER_ENCODING
);
1431 bulkdelete_respond(deleter
->get_num_deleted(),
1432 deleter
->get_num_unfound(),
1433 deleter
->get_failures(),
1436 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1440 std::unique_ptr
<RGWBulkUploadOp::StreamGetter
>
1441 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1443 class SwiftStreamGetter
: public StreamGetter
{
1444 const size_t conlen
;
1449 SwiftStreamGetter(req_state
* const s
, const size_t conlen
)
1455 ssize_t
get_at_most(size_t want
, ceph::bufferlist
& dst
) override
{
1456 /* maximum requested by a caller */
1457 /* data provided by client */
1458 /* RadosGW's limit. */
1459 const size_t max_chunk_size
= \
1460 static_cast<size_t>(s
->cct
->_conf
->rgw_max_chunk_size
);
1461 const size_t max_to_read
= std::min({ want
, conlen
- curpos
, max_chunk_size
});
1463 ldout(s
->cct
, 20) << "bulk_upload: get_at_most max_to_read="
1465 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst
.c_str()) << dendl
;
1467 bufferptr
bp(max_to_read
);
1468 const auto read_len
= recv_body(s
, bp
.c_str(), max_to_read
);
1469 dst
.append(bp
, 0, read_len
);
1470 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1476 return curpos
> s
->cct
->_conf
->rgw_max_put_size
? -ERR_TOO_LARGE
1480 ssize_t
get_exactly(size_t want
, ceph::bufferlist
& dst
) override
{
1481 ldout(s
->cct
, 20) << "bulk_upload: get_exactly want=" << want
<< dendl
;
1483 /* FIXME: do this in a loop. */
1484 const auto ret
= get_at_most(want
, dst
);
1485 ldout(s
->cct
, 20) << "bulk_upload: get_exactly ret=" << ret
<< dendl
;
1488 } else if (static_cast<size_t>(ret
) != want
) {
1500 ldout(s
->cct
, 20) << "bulk upload: create_stream for length="
1501 << s
->length
<< dendl
;
1503 const size_t conlen
= atoll(s
->length
);
1504 return std::unique_ptr
<SwiftStreamGetter
>(new SwiftStreamGetter(s
, conlen
));
1508 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1510 set_req_state_err(s
, op_ret
);
1512 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1513 CHUNKED_TRANSFER_ENCODING
);
1514 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1516 s
->formatter
->open_object_section("delete");
1518 std::string resp_status
;
1519 std::string resp_body
;
1521 if (! failures
.empty()) {
1524 const auto last_err
= { failures
.back().err
};
1525 if (boost::algorithm::contains(last_err
, terminal_errors
)) {
1526 /* The terminal errors are affecting the status of the whole upload. */
1527 set_req_state_err(err
, failures
.back().err
, s
->prot_flags
);
1529 set_req_state_err(err
, ERR_INVALID_REQUEST
, s
->prot_flags
);
1532 dump_errno(err
, resp_status
);
1533 } else if (0 == num_created
&& failures
.empty()) {
1534 /* Nothing created, nothing failed. This means the archive contained no
1535 * entity we could understand (regular file or directory). We need to
1536 * send 400 Bad Request to an HTTP client in the internal status field. */
1537 dump_errno(400, resp_status
);
1538 resp_body
= "Invalid Tar File: No Valid Files";
1541 dump_errno(201, resp_status
);
1544 encode_json("Number Files Created", num_created
, s
->formatter
);
1545 encode_json("Response Body", resp_body
, s
->formatter
);
1546 encode_json("Response Status", resp_status
, s
->formatter
);
1548 s
->formatter
->open_array_section("Errors");
1549 for (const auto& fail_desc
: failures
) {
1550 s
->formatter
->open_array_section("object");
1552 encode_json("Name", fail_desc
.path
, s
->formatter
);
1555 set_req_state_err(err
, fail_desc
.err
, s
->prot_flags
);
1557 dump_errno(err
, status
);
1558 encode_json("Status", status
, s
->formatter
);
1560 s
->formatter
->close_section();
1562 s
->formatter
->close_section();
1564 s
->formatter
->close_section();
1565 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1569 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1571 set_req_state_err(s
, op_ret
);
1573 end_header(s
, this, "application/xml");
1575 std::stringstream ss
;
1577 ss
<< R
"(<?xml version="1.0"?>)" << "\n"
1578 << R
"(<!DOCTYPE cross-domain-policy SYSTEM )"
1579 << R
"("http
://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1580 << R
"(<cross-domain-policy>)" << "\n"
1581 << g_conf
->rgw_cross_domain_policy
<< "\n"
1582 << R
"(</cross-domain-policy>)";
1584 dump_body(s
, ss
.str());
1587 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1589 set_req_state_err(s
, op_ret
);
1591 end_header(s
, this, "application/xml");
1594 static constexpr char DISABLED
[] = "DISABLED BY FILE";
1595 dump_body(s
, DISABLED
, strlen(DISABLED
));
1599 const vector
<pair
<string
, RGWInfo_ObjStore_SWIFT::info
>> RGWInfo_ObjStore_SWIFT::swift_info
=
1601 {"bulk_delete", {false, nullptr}},
1602 {"container_quotas", {false, nullptr}},
1603 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data
}},
1604 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data
}},
1605 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data
}},
1606 {"account_quotas", {false, nullptr}},
1607 {"staticweb", {false, nullptr}},
1608 {"tempauth", {false, RGWInfo_ObjStore_SWIFT::list_tempauth_data
}},
1611 void RGWInfo_ObjStore_SWIFT::execute()
1613 bool is_admin_info_enabled
= false;
1615 const string
& swiftinfo_sig
= s
->info
.args
.get("swiftinfo_sig");
1616 const string
& swiftinfo_expires
= s
->info
.args
.get("swiftinfo_expires");
1618 if (!swiftinfo_sig
.empty() &&
1619 !swiftinfo_expires
.empty() &&
1620 !is_expired(swiftinfo_expires
, s
->cct
)) {
1621 is_admin_info_enabled
= true;
1624 s
->formatter
->open_object_section("info");
1626 for (const auto& pair
: swift_info
) {
1627 if(!is_admin_info_enabled
&& pair
.second
.is_admin_info
)
1630 if (!pair
.second
.list_data
) {
1631 s
->formatter
->open_object_section((pair
.first
).c_str());
1632 s
->formatter
->close_section();
1635 pair
.second
.list_data(*(s
->formatter
), *(s
->cct
->_conf
), *store
);
1639 s
->formatter
->close_section();
1642 void RGWInfo_ObjStore_SWIFT::send_response()
1645 op_ret
= STATUS_NO_CONTENT
;
1647 set_req_state_err(s
, op_ret
);
1649 end_header(s
, this);
1650 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1653 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter
& formatter
,
1654 const md_config_t
& config
,
1657 formatter
.open_object_section("swift");
1658 formatter
.dump_int("max_file_size", config
.rgw_max_put_size
);
1659 formatter
.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX
);
1661 string
ceph_version(CEPH_GIT_NICE_VER
);
1662 formatter
.dump_string("version", ceph_version
);
1663 formatter
.dump_int("max_meta_name_length", 81);
1665 formatter
.open_array_section("policies");
1666 RGWZoneGroup
& zonegroup
= store
.get_zonegroup();
1668 for (const auto& placement_targets
: zonegroup
.placement_targets
) {
1669 formatter
.open_object_section("policy");
1670 if (placement_targets
.second
.name
.compare(zonegroup
.default_placement
) == 0)
1671 formatter
.dump_bool("default", true);
1672 formatter
.dump_string("name", placement_targets
.second
.name
.c_str());
1673 formatter
.close_section();
1675 formatter
.close_section();
1677 formatter
.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN
);
1678 formatter
.dump_bool("strict_cors_mode", true);
1679 formatter
.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN
);
1680 formatter
.close_section();
1683 void RGWInfo_ObjStore_SWIFT::list_tempauth_data(Formatter
& formatter
,
1684 const md_config_t
& config
,
1687 formatter
.open_object_section("tempauth");
1688 formatter
.dump_bool("account_acls", true);
1689 formatter
.close_section();
1691 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter
& formatter
,
1692 const md_config_t
& config
,
1695 formatter
.open_object_section("tempurl");
1696 formatter
.open_array_section("methods");
1697 formatter
.dump_string("methodname", "GET");
1698 formatter
.dump_string("methodname", "HEAD");
1699 formatter
.dump_string("methodname", "PUT");
1700 formatter
.dump_string("methodname", "POST");
1701 formatter
.dump_string("methodname", "DELETE");
1702 formatter
.close_section();
1703 formatter
.close_section();
1706 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter
& formatter
,
1707 const md_config_t
& config
,
1710 formatter
.open_object_section("slo");
1711 formatter
.dump_int("max_manifest_segments", config
.rgw_max_slo_entries
);
1712 formatter
.close_section();
1715 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string
& expires
, CephContext
* cct
)
1718 const utime_t now
= ceph_clock_now();
1719 const uint64_t expiration
= (uint64_t)strict_strtoll(expires
.c_str(),
1722 ldout(cct
, 5) << "failed to parse siginfo_expires: " << err
<< dendl
;
1726 if (expiration
<= (uint64_t)now
.sec()) {
1727 ldout(cct
, 5) << "siginfo expired: " << expiration
<< " <= " << now
.sec() << dendl
;
1735 void RGWFormPost::init(RGWRados
* const store
,
1737 RGWHandler
* const dialect_handler
)
1739 prefix
= std::move(s
->object
.name
);
1740 s
->object
= rgw_obj_key();
1742 return RGWPostObj_ObjStore::init(store
, s
, dialect_handler
);
1745 std::size_t RGWFormPost::get_max_file_size() /*const*/
1747 std::string max_str
= get_part_str(ctrl_parts
, "max_file_size", "0");
1750 const std::size_t max_file_size
=
1751 static_cast<uint64_t>(strict_strtoll(max_str
.c_str(), 10, &err
));
1753 if (! err
.empty()) {
1754 ldout(s
->cct
, 5) << "failed to parse FormPost's max_file_size: " << err
1759 return max_file_size
;
1762 bool RGWFormPost::is_non_expired()
1764 std::string expires
= get_part_str(ctrl_parts
, "expires", "0");
1767 const uint64_t expires_timestamp
=
1768 static_cast<uint64_t>(strict_strtoll(expires
.c_str(), 10, &err
));
1770 if (! err
.empty()) {
1771 dout(5) << "failed to parse FormPost's expires: " << err
<< dendl
;
1775 const utime_t now
= ceph_clock_now();
1776 if (expires_timestamp
<= static_cast<uint64_t>(now
.sec())) {
1777 dout(5) << "FormPost form expired: "
1778 << expires_timestamp
<< " <= " << now
.sec() << dendl
;
1785 bool RGWFormPost::is_integral()
1787 const std::string form_signature
= get_part_str(ctrl_parts
, "signature");
1790 get_owner_info(s
, *s
->user
);
1791 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
1793 ldout(s
->cct
, 5) << "cannot get user_info of account's owner" << dendl
;
1797 for (const auto& kv
: s
->user
->temp_url_keys
) {
1798 const int temp_url_key_num
= kv
.first
;
1799 const string
& temp_url_key
= kv
.second
;
1801 if (temp_url_key
.empty()) {
1805 SignatureHelper sig_helper
;
1806 sig_helper
.calc(temp_url_key
,
1807 s
->info
.request_uri
,
1808 get_part_str(ctrl_parts
, "redirect"),
1809 get_part_str(ctrl_parts
, "max_file_size", "0"),
1810 get_part_str(ctrl_parts
, "max_file_count", "0"),
1811 get_part_str(ctrl_parts
, "expires", "0"));
1813 const auto local_sig
= sig_helper
.get_signature();
1815 ldout(s
->cct
, 20) << "FormPost signature [" << temp_url_key_num
<< "]"
1816 << " (calculated): " << local_sig
<< dendl
;
1818 if (sig_helper
.is_equal_to(form_signature
)) {
1821 ldout(s
->cct
, 5) << "FormPost's signature mismatch: "
1822 << local_sig
<< " != " << form_signature
<< dendl
;
1829 void RGWFormPost::get_owner_info(const req_state
* const s
,
1830 RGWUserInfo
& owner_info
) const
1832 /* We cannot use req_state::bucket_name because it isn't available
1833 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
1834 const string
& bucket_name
= s
->init_state
.url_bucket
;
1836 /* TempURL in Formpost only requires that bucket name is specified. */
1837 if (bucket_name
.empty()) {
1841 string bucket_tenant
;
1842 if (!s
->account_name
.empty()) {
1846 const rgw_user
uid(s
->account_name
);
1847 if (uid
.tenant
.empty()) {
1848 const rgw_user
tenanted_uid(uid
.id
, uid
.id
);
1850 if (rgw_get_user_info_by_uid(store
, tenanted_uid
, uinfo
) >= 0) {
1852 bucket_tenant
= uinfo
.user_id
.tenant
;
1857 if (!found
&& rgw_get_user_info_by_uid(store
, uid
, uinfo
) < 0) {
1860 bucket_tenant
= uinfo
.user_id
.tenant
;
1864 /* Need to get user info of bucket owner. */
1865 RGWBucketInfo bucket_info
;
1866 int ret
= store
->get_bucket_info(*static_cast<RGWObjectCtx
*>(s
->obj_ctx
),
1867 bucket_tenant
, bucket_name
,
1868 bucket_info
, nullptr);
1873 ldout(s
->cct
, 20) << "temp url user (bucket owner): " << bucket_info
.owner
1876 if (rgw_get_user_info_by_uid(store
, bucket_info
.owner
, owner_info
) < 0) {
1881 int RGWFormPost::get_params()
1883 /* The parentt class extracts boundary info from the Content-Type. */
1884 int ret
= RGWPostObj_ObjStore::get_params();
1889 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1891 /* Let's start parsing the HTTP body by parsing each form part step-
1892 * by-step till encountering the first part with file data. */
1894 struct post_form_part part
;
1895 ret
= read_form_part_header(&part
, stream_done
);
1900 if (s
->cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
1901 ldout(s
->cct
, 20) << "read part header -- part.name="
1902 << part
.name
<< dendl
;
1904 for (const auto& pair
: part
.fields
) {
1905 ldout(s
->cct
, 20) << "field.name=" << pair
.first
<< dendl
;
1906 ldout(s
->cct
, 20) << "field.val=" << pair
.second
.val
<< dendl
;
1907 ldout(s
->cct
, 20) << "field.params:" << dendl
;
1909 for (const auto& param_pair
: pair
.second
.params
) {
1910 ldout(s
->cct
, 20) << " " << param_pair
.first
1911 << " -> " << param_pair
.second
<< dendl
;
1917 /* Unexpected here. */
1918 err_msg
= "Malformed request";
1922 const auto field_iter
= part
.fields
.find("Content-Disposition");
1923 if (std::end(part
.fields
) != field_iter
&&
1924 std::end(field_iter
->second
.params
) != field_iter
->second
.params
.find("filename")) {
1925 /* First data part ahead. */
1926 current_data_part
= std::move(part
);
1928 /* Stop the iteration. We can assume that all control parts have been
1929 * already parsed. The rest of HTTP body should contain data parts
1930 * only. They will be picked up by ::get_data(). */
1933 /* Control part ahead. Receive, parse and store for later usage. */
1935 ret
= read_data(part
.data
, s
->cct
->_conf
->rgw_max_chunk_size
,
1936 boundary
, stream_done
);
1939 } else if (! boundary
) {
1940 err_msg
= "Couldn't find boundary";
1944 ctrl_parts
[part
.name
] = std::move(part
);
1946 } while (! stream_done
);
1949 max_len
= get_max_file_size();
1951 if (! current_data_part
) {
1952 err_msg
= "FormPost: no files to process";
1956 if (! is_non_expired()) {
1957 err_msg
= "FormPost: Form Expired";
1961 if (! is_integral()) {
1962 err_msg
= "FormPost: Invalid Signature";
1969 std::string
RGWFormPost::get_current_filename() const
1972 const auto& field
= current_data_part
->fields
.at("Content-Disposition");
1973 const auto iter
= field
.params
.find("filename");
1975 if (std::end(field
.params
) != iter
) {
1976 return prefix
+ iter
->second
;
1978 } catch (std::out_of_range
&) {
1985 std::string
RGWFormPost::get_current_content_type() const
1988 const auto& field
= current_data_part
->fields
.at("Content-Type");
1990 } catch (std::out_of_range
&) {
1994 return std::string();
1997 bool RGWFormPost::is_next_file_to_upload()
1999 if (! stream_done
) {
2000 /* We have at least one additional part in the body. */
2001 struct post_form_part part
;
2002 int r
= read_form_part_header(&part
, stream_done
);
2007 const auto field_iter
= part
.fields
.find("Content-Disposition");
2008 if (std::end(part
.fields
) != field_iter
) {
2009 const auto& params
= field_iter
->second
.params
;
2010 const auto& filename_iter
= params
.find("filename");
2012 if (std::end(params
) != filename_iter
&& ! filename_iter
->second
.empty()) {
2013 current_data_part
= std::move(part
);
2022 int RGWFormPost::get_data(ceph::bufferlist
& bl
, bool& again
)
2026 int r
= read_data(bl
, s
->cct
->_conf
->rgw_max_chunk_size
,
2027 boundary
, stream_done
);
2032 /* Tell RGWPostObj::execute() that it has some data to put. */
2038 void RGWFormPost::send_response()
2040 std::string redirect
= get_part_str(ctrl_parts
, "redirect");
2041 if (! redirect
.empty()) {
2042 op_ret
= STATUS_REDIRECT
;
2045 set_req_state_err(s
, op_ret
);
2046 s
->err
.err_code
= err_msg
;
2048 if (! redirect
.empty()) {
2049 dump_redirect(s
, redirect
);
2051 end_header(s
, this);
2054 bool RGWFormPost::is_formpost_req(req_state
* const s
)
2056 std::string content_type
;
2057 std::map
<std::string
, std::string
> params
;
2059 parse_boundary_params(s
->info
.env
->get("CONTENT_TYPE", ""),
2060 content_type
, params
);
2062 return boost::algorithm::iequals(content_type
, "multipart/form-data") &&
2063 params
.count("boundary") > 0;
2067 RGWOp
*RGWHandler_REST_Service_SWIFT::op_get()
2069 return new RGWListBuckets_ObjStore_SWIFT
;
2072 RGWOp
*RGWHandler_REST_Service_SWIFT::op_head()
2074 return new RGWStatAccount_ObjStore_SWIFT
;
2077 RGWOp
*RGWHandler_REST_Service_SWIFT::op_put()
2079 if (s
->info
.args
.exists("extract-archive")) {
2080 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2085 RGWOp
*RGWHandler_REST_Service_SWIFT::op_post()
2087 if (s
->info
.args
.exists("bulk-delete")) {
2088 return new RGWBulkDelete_ObjStore_SWIFT
;
2090 return new RGWPutMetadataAccount_ObjStore_SWIFT
;
2093 RGWOp
*RGWHandler_REST_Service_SWIFT::op_delete()
2095 if (s
->info
.args
.exists("bulk-delete")) {
2096 return new RGWBulkDelete_ObjStore_SWIFT
;
2101 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret
,
2102 const std::string error_doc
)
2104 /* Try to throw it all away. */
2105 s
->formatter
->reset();
2107 class RGWGetErrorPage
: public RGWGetObj_ObjStore_SWIFT
{
2109 RGWGetErrorPage(RGWRados
* const store
,
2110 RGWHandler_REST
* const handler
,
2112 const int http_ret
) {
2113 /* Calling a virtual from the base class is safe as the subobject should
2114 * be properly initialized and we haven't overridden the init method. */
2115 init(store
, s
, handler
);
2117 set_custom_http_response(http_ret
);
2120 int error_handler(const int err_no
,
2121 std::string
* const error_content
) override
{
2122 /* Enforce that any error generated while getting the error page will
2123 * not be send to a client. This allows us to recover from the double
2124 * fault situation by sending the original message. */
2127 } get_errpage_op(store
, handler
, s
, http_ret
);
2129 s
->object
= std::to_string(http_ret
) + error_doc
;
2131 RGWOp
* newop
= &get_errpage_op
;
2133 return rgw_process_authenticated(handler
, newop
, &req
, s
, true);
2136 int RGWSwiftWebsiteHandler::error_handler(const int err_no
,
2137 std::string
* const error_content
)
2139 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2141 if (can_be_website_req() && ! ws_conf
.error_doc
.empty()) {
2142 set_req_state_err(s
, err_no
);
2143 return serve_errordoc(s
->err
.http_ret
, ws_conf
.error_doc
);
2146 /* Let's go to the default, no-op handler. */
2150 bool RGWSwiftWebsiteHandler::is_web_mode() const
2152 const boost::string_ref webmode
= s
->info
.env
->get("HTTP_X_WEB_MODE", "");
2153 return boost::algorithm::iequals(webmode
, "true");
2156 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2158 /* Static website works only with the GET or HEAD method. Nothing more. */
2159 static const std::set
<boost::string_ref
> ws_methods
= { "GET", "HEAD" };
2160 if (ws_methods
.count(s
->info
.method
) == 0) {
2164 /* We also need to handle early failures from the auth system. In such cases
2165 * req_state::auth.identity may be empty. Let's treat that the same way as
2166 * the anonymous access. */
2167 if (! s
->auth
.identity
) {
2171 /* Swift serves websites only for anonymous requests unless client explicitly
2172 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2173 if (s
->auth
.identity
->is_anonymous() || is_web_mode()) {
2180 RGWOp
* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2182 class RGWMovedPermanently
: public RGWOp
{
2183 const std::string location
;
2185 RGWMovedPermanently(const std::string
& location
)
2186 : location(location
) {
2189 int verify_permission() override
{
2193 void execute() override
{
2194 op_ret
= -ERR_PERMANENT_REDIRECT
;
2198 void send_response() override
{
2199 set_req_state_err(s
, op_ret
);
2201 dump_content_length(s
, 0);
2202 dump_redirect(s
, location
);
2203 end_header(s
, this);
2206 const string
name() override
{
2207 return "RGWMovedPermanently";
2211 return new RGWMovedPermanently(s
->info
.request_uri
+ '/');
2214 RGWOp
* RGWSwiftWebsiteHandler::get_ws_index_op()
2216 /* Retarget to get obj on requested index file. */
2217 if (! s
->object
.empty()) {
2218 s
->object
= s
->object
.name
+
2219 s
->bucket_info
.website_conf
.get_index_doc();
2221 s
->object
= s
->bucket_info
.website_conf
.get_index_doc();
2224 auto getop
= new RGWGetObj_ObjStore_SWIFT
;
2225 getop
->set_get_data(boost::algorithm::equals("GET", s
->info
.method
));
2230 RGWOp
* RGWSwiftWebsiteHandler::get_ws_listing_op()
2232 class RGWWebsiteListing
: public RGWListBucket_ObjStore_SWIFT
{
2233 const std::string prefix_override
;
2235 int get_params() override
{
2236 prefix
= prefix_override
;
2242 void send_response() override
{
2243 /* Generate the header now. */
2244 set_req_state_err(s
, op_ret
);
2246 dump_container_metadata(s
, bucket
, bucket_quota
,
2247 s
->bucket_info
.website_conf
);
2248 end_header(s
, this, "text/html");
2253 /* Now it's the time to start generating HTML bucket listing.
2254 * All the crazy stuff with crafting tags will be delegated to
2255 * RGWSwiftWebsiteListingFormatter. */
2256 std::stringstream ss
;
2257 RGWSwiftWebsiteListingFormatter
htmler(ss
, prefix
);
2259 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2260 htmler
.generate_header(s
->decoded_uri
,
2261 ws_conf
.listing_css_doc
);
2263 for (const auto& pair
: common_prefixes
) {
2264 std::string subdir_name
= pair
.first
;
2265 if (! subdir_name
.empty()) {
2266 /* To be compliant with Swift we need to remove the trailing
2268 subdir_name
.pop_back();
2271 htmler
.dump_subdir(subdir_name
);
2274 for (const rgw_bucket_dir_entry
& obj
: objs
) {
2275 if (! common_prefixes
.count(obj
.key
.name
+ '/')) {
2276 htmler
.dump_object(obj
);
2280 htmler
.generate_footer();
2281 dump_body(s
, ss
.str());
2284 /* Taking prefix_override by value to leverage std::string r-value ref
2285 * ctor and thus avoid extra memory copying/increasing ref counter. */
2286 RGWWebsiteListing(std::string prefix_override
)
2287 : prefix_override(std::move(prefix_override
)) {
2291 std::string prefix
= std::move(s
->object
.name
);
2292 s
->object
= rgw_obj_key();
2294 return new RGWWebsiteListing(std::move(prefix
));
2297 bool RGWSwiftWebsiteHandler::is_web_dir() const
2299 std::string subdir_name
= url_decode(s
->object
.name
);
2301 /* Remove character from the subdir name if it is "/". */
2302 if (subdir_name
.empty()) {
2304 } else if (subdir_name
.back() == '/') {
2305 subdir_name
.pop_back();
2308 rgw_obj
obj(s
->bucket
, std::move(subdir_name
));
2310 /* First, get attrset of the object we'll try to retrieve. */
2311 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2312 obj_ctx
.obj
.set_atomic(obj
);
2313 obj_ctx
.obj
.set_prefetch_data(obj
);
2315 RGWObjState
* state
= nullptr;
2316 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2320 /* A nonexistent object cannot be a considered as a marker representing
2321 * the emulation of catalog in FS hierarchy. */
2322 if (! state
->exists
) {
2326 /* Decode the content type. */
2327 std::string content_type
;
2328 get_contype_from_attrs(state
->attrset
, content_type
);
2330 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2331 const std::string subdir_marker
= ws_conf
.subdir_marker
.empty()
2332 ? "application/directory"
2333 : ws_conf
.subdir_marker
;
2334 return subdir_marker
== content_type
&& state
->size
<= 1;
2337 bool RGWSwiftWebsiteHandler::is_index_present(const std::string
& index
)
2339 rgw_obj
obj(s
->bucket
, index
);
2341 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2342 obj_ctx
.obj
.set_atomic(obj
);
2343 obj_ctx
.obj
.set_prefetch_data(obj
);
2345 RGWObjState
* state
= nullptr;
2346 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2350 /* A nonexistent object cannot be a considered as a viable index. We will
2351 * try to list the bucket or - if this is impossible - return an error. */
2352 return state
->exists
;
2355 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp
* op
, RGWOp
** new_op
)
2357 ldout(s
->cct
, 10) << "Starting retarget" << dendl
;
2358 RGWOp
* op_override
= nullptr;
2360 /* In Swift static web content is served if the request is anonymous or
2361 * has X-Web-Mode HTTP header specified to true. */
2362 if (can_be_website_req()) {
2363 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2364 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2366 if (s
->decoded_uri
.back() != '/') {
2367 op_override
= get_ws_redirect_op();
2368 } else if (! index
.empty() && is_index_present(index
)) {
2369 op_override
= get_ws_index_op();
2370 } else if (ws_conf
.listing_enabled
) {
2371 op_override
= get_ws_listing_op();
2376 handler
->put_op(op
);
2377 op_override
->init(store
, s
, handler
);
2379 *new_op
= op_override
;
2384 /* Return 404 Not Found is the request has web mode enforced but we static web
2385 * wasn't able to serve it accordingly. */
2386 return ! op_override
&& is_web_mode() ? -ENOENT
: 0;
2389 int RGWSwiftWebsiteHandler::retarget_object(RGWOp
* op
, RGWOp
** new_op
)
2391 ldout(s
->cct
, 10) << "Starting object retarget" << dendl
;
2392 RGWOp
* op_override
= nullptr;
2394 /* In Swift static web content is served if the request is anonymous or
2395 * has X-Web-Mode HTTP header specified to true. */
2396 if (can_be_website_req() && is_web_dir()) {
2397 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2398 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2400 if (s
->decoded_uri
.back() != '/') {
2401 op_override
= get_ws_redirect_op();
2402 } else if (! index
.empty() && is_index_present(index
)) {
2403 op_override
= get_ws_index_op();
2404 } else if (ws_conf
.listing_enabled
) {
2405 op_override
= get_ws_listing_op();
2408 /* A regular request or the specified object isn't a subdirectory marker.
2409 * We don't need any re-targeting. Error handling (like sending a custom
2410 * error page) will be performed by error_handler of the actual RGWOp. */
2415 handler
->put_op(op
);
2416 op_override
->init(store
, s
, handler
);
2418 *new_op
= op_override
;
2423 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2424 return ! op_override
? -ENOENT
: 0;
2428 RGWOp
*RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data
)
2431 return new RGWGetACLs_ObjStore_SWIFT
;
2435 return new RGWListBucket_ObjStore_SWIFT
;
2437 return new RGWStatBucket_ObjStore_SWIFT
;
2440 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_get()
2442 return get_obj_op(true);
2445 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_head()
2447 return get_obj_op(false);
2450 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_put()
2453 return new RGWPutACLs_ObjStore_SWIFT
;
2455 if(s
->info
.args
.exists("extract-archive")) {
2456 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2458 return new RGWCreateBucket_ObjStore_SWIFT
;
2461 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_delete()
2463 return new RGWDeleteBucket_ObjStore_SWIFT
;
2466 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_post()
2468 if (RGWFormPost::is_formpost_req(s
)) {
2469 return new RGWFormPost
;
2471 return new RGWPutMetadataBucket_ObjStore_SWIFT
;
2475 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_options()
2477 return new RGWOptionsCORS_ObjStore_SWIFT
;
2481 RGWOp
*RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data
)
2484 return new RGWGetACLs_ObjStore_SWIFT
;
2487 RGWGetObj_ObjStore_SWIFT
*get_obj_op
= new RGWGetObj_ObjStore_SWIFT
;
2488 get_obj_op
->set_get_data(get_data
);
2492 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_get()
2494 return get_obj_op(true);
2497 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_head()
2499 return get_obj_op(false);
2502 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_put()
2505 return new RGWPutACLs_ObjStore_SWIFT
;
2507 if(s
->info
.args
.exists("extract-archive")) {
2508 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2510 if (s
->init_state
.src_bucket
.empty())
2511 return new RGWPutObj_ObjStore_SWIFT
;
2513 return new RGWCopyObj_ObjStore_SWIFT
;
2516 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_delete()
2518 return new RGWDeleteObj_ObjStore_SWIFT
;
2521 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_post()
2523 if (RGWFormPost::is_formpost_req(s
)) {
2524 return new RGWFormPost
;
2526 return new RGWPutMetadataObject_ObjStore_SWIFT
;
2530 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_copy()
2532 return new RGWCopyObj_ObjStore_SWIFT
;
2535 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_options()
2537 return new RGWOptionsCORS_ObjStore_SWIFT
;
2541 int RGWHandler_REST_SWIFT::authorize()
2543 return rgw::auth::Strategy::apply(auth_strategy
, s
);
2546 int RGWHandler_REST_SWIFT::postauth_init()
2548 struct req_init_state
* t
= &s
->init_state
;
2550 /* XXX Stub this until Swift Auth sets account into URL. */
2551 s
->bucket_tenant
= s
->user
->user_id
.tenant
;
2552 s
->bucket_name
= t
->url_bucket
;
2554 dout(10) << "s->object=" <<
2555 (!s
->object
.empty() ? s
->object
: rgw_obj_key("<NULL>"))
2557 << rgw_make_bucket_entry_name(s
->bucket_tenant
, s
->bucket_name
)
2561 ret
= rgw_validate_tenant_name(s
->bucket_tenant
);
2564 ret
= validate_bucket_name(s
->bucket_name
);
2567 ret
= validate_object_name(s
->object
.name
);
2571 if (!t
->src_bucket
.empty()) {
2573 * We don't allow cross-tenant copy at present. It requires account
2574 * names in the URL for Swift.
2576 s
->src_tenant_name
= s
->user
->user_id
.tenant
;
2577 s
->src_bucket_name
= t
->src_bucket
;
2579 ret
= validate_bucket_name(s
->src_bucket_name
);
2583 ret
= validate_object_name(s
->src_object
.name
);
2592 int RGWHandler_REST_SWIFT::validate_bucket_name(const string
& bucket
)
2594 int ret
= RGWHandler_REST::validate_bucket_name(bucket
);
2598 int len
= bucket
.size();
2603 if (bucket
[0] == '.')
2604 return -ERR_INVALID_BUCKET_NAME
;
2606 if (check_utf8(bucket
.c_str(), len
))
2607 return -ERR_INVALID_UTF8
;
2609 const char *s
= bucket
.c_str();
2611 for (int i
= 0; i
< len
; ++i
, ++s
) {
2612 if (*(unsigned char *)s
== 0xff)
2613 return -ERR_INVALID_BUCKET_NAME
;
2619 static void next_tok(string
& str
, string
& tok
, char delim
)
2621 if (str
.size() == 0) {
2626 int pos
= str
.find(delim
);
2628 tok
= str
.substr(0, pos
);
2629 str
= str
.substr(pos
+ 1);
2635 int RGWHandler_REST_SWIFT::init_from_header(struct req_state
* const s
,
2636 const std::string
& frontend_prefix
)
2641 s
->prot_flags
|= RGW_REST_SWIFT
;
2643 char reqbuf
[frontend_prefix
.length() + s
->decoded_uri
.length() + 1];
2644 sprintf(reqbuf
, "%s%s", frontend_prefix
.c_str(), s
->decoded_uri
.c_str());
2645 const char *req_name
= reqbuf
;
2649 if (*req_name
== '?') {
2652 p
= s
->info
.request_params
.c_str();
2655 s
->info
.args
.set(p
);
2656 s
->info
.args
.parse();
2658 /* Skip the leading slash of URL hierarchy. */
2659 if (req_name
[0] != '/') {
2665 if ('\0' == req_name
[0]) {
2666 return g_conf
->rgw_swift_url_prefix
== "/" ? -ERR_BAD_URL
: 0;
2671 size_t pos
= req
.find('/');
2672 if (std::string::npos
!= pos
&& g_conf
->rgw_swift_url_prefix
!= "/") {
2673 bool cut_url
= g_conf
->rgw_swift_url_prefix
.length();
2674 first
= req
.substr(0, pos
);
2676 if (first
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2678 /* Rewind to the "v1/..." part. */
2679 next_tok(req
, first
, '/');
2682 } else if (req
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2683 s
->formatter
= new RGWFormatter_Plain
;
2684 return -ERR_BAD_URL
;
2689 std::string tenant_path
;
2690 if (! g_conf
->rgw_swift_tenant_name
.empty()) {
2691 tenant_path
= "/AUTH_";
2692 tenant_path
.append(g_conf
->rgw_swift_tenant_name
);
2695 /* verify that the request_uri conforms with what's expected */
2696 char buf
[g_conf
->rgw_swift_url_prefix
.length() + 16 + tenant_path
.length()];
2698 if (g_conf
->rgw_swift_url_prefix
== "/") {
2699 blen
= sprintf(buf
, "/v1%s", tenant_path
.c_str());
2701 blen
= sprintf(buf
, "/%s/v1%s",
2702 g_conf
->rgw_swift_url_prefix
.c_str(), tenant_path
.c_str());
2705 if (strncmp(reqbuf
, buf
, blen
) != 0) {
2709 int ret
= allocate_formatter(s
, RGW_FORMAT_PLAIN
, true);
2715 next_tok(req
, ver
, '/');
2717 if (!tenant_path
.empty() || g_conf
->rgw_swift_account_in_url
) {
2718 string account_name
;
2719 next_tok(req
, account_name
, '/');
2721 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2722 const vector
<string
> skipped_prefixes
= { "AUTH_", "KEY_" };
2724 for (const auto pfx
: skipped_prefixes
) {
2725 const size_t comp_len
= min(account_name
.length(), pfx
.length());
2726 if (account_name
.compare(0, comp_len
, pfx
) == 0) {
2727 /* Prefix is present. Drop it. */
2728 account_name
= account_name
.substr(comp_len
);
2733 if (account_name
.empty()) {
2734 return -ERR_PRECONDITION_FAILED
;
2736 s
->account_name
= account_name
;
2740 next_tok(req
, first
, '/');
2742 dout(10) << "ver=" << ver
<< " first=" << first
<< " req=" << req
<< dendl
;
2743 if (first
.size() == 0)
2746 s
->info
.effective_uri
= "/" + first
;
2748 // Save bucket to tide us over until token is parsed.
2749 s
->init_state
.url_bucket
= first
;
2753 rgw_obj_key(req
, s
->info
.env
->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2754 s
->info
.effective_uri
.append("/" + s
->object
.name
);
2760 int RGWHandler_REST_SWIFT::init(RGWRados
* store
, struct req_state
* s
,
2761 rgw::io::BasicClient
*cio
)
2763 struct req_init_state
*t
= &s
->init_state
;
2765 s
->dialect
= "swift";
2767 const char *copy_source
= s
->info
.env
->get("HTTP_X_COPY_FROM");
2769 bool result
= RGWCopyObj::parse_copy_location(copy_source
, t
->src_bucket
,
2772 return -ERR_BAD_URL
;
2775 if (s
->op
== OP_COPY
) {
2776 const char *req_dest
= s
->info
.env
->get("HTTP_DESTINATION");
2778 return -ERR_BAD_URL
;
2780 string dest_bucket_name
;
2781 rgw_obj_key dest_obj_key
;
2783 RGWCopyObj::parse_copy_location(req_dest
, dest_bucket_name
,
2786 return -ERR_BAD_URL
;
2788 string dest_object
= dest_obj_key
.name
;
2790 /* convert COPY operation into PUT */
2791 t
->src_bucket
= t
->url_bucket
;
2792 s
->src_object
= s
->object
;
2793 t
->url_bucket
= dest_bucket_name
;
2794 s
->object
= rgw_obj_key(dest_object
);
2798 return RGWHandler_REST::init(store
, s
, cio
);
2802 RGWRESTMgr_SWIFT::get_handler(struct req_state
* const s
,
2803 const rgw::auth::StrategyRegistry
& auth_registry
,
2804 const std::string
& frontend_prefix
)
2806 int ret
= RGWHandler_REST_SWIFT::init_from_header(s
, frontend_prefix
);
2808 ldout(s
->cct
, 10) << "init_from_header returned err=" << ret
<< dendl
;
2812 const auto& auth_strategy
= auth_registry
.get_swift();
2814 if (s
->init_state
.url_bucket
.empty()) {
2815 return new RGWHandler_REST_Service_SWIFT(auth_strategy
);
2818 if (s
->object
.empty()) {
2819 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy
);
2822 return new RGWHandler_REST_Obj_SWIFT(auth_strategy
);
2825 RGWHandler_REST
* RGWRESTMgr_SWIFT_Info::get_handler(
2826 struct req_state
* const s
,
2827 const rgw::auth::StrategyRegistry
& auth_registry
,
2828 const std::string
& frontend_prefix
)
2830 s
->prot_flags
|= RGW_REST_SWIFT
;
2831 const auto& auth_strategy
= auth_registry
.get_swift();
2832 return new RGWHandler_REST_SWIFT_Info(auth_strategy
);