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
= static_cast<RGWAccessControlPolicy_SWIFT
*>(s
->bucket_acl
);
388 std::string read_acl
, write_acl
;
389 swift_policy
->to_str(read_acl
, write_acl
);
391 if (read_acl
.size()) {
392 dump_header(s
, "X-Container-Read", read_acl
);
394 if (write_acl
.size()) {
395 dump_header(s
, "X-Container-Write", write_acl
);
397 if (!s
->bucket_info
.placement_rule
.empty()) {
398 dump_header(s
, "X-Storage-Policy", s
->bucket_info
.placement_rule
);
401 /* Dump user-defined metadata items and generic attrs. */
402 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
403 map
<string
, bufferlist
>::iterator iter
;
404 for (iter
= s
->bucket_attrs
.lower_bound(RGW_ATTR_PREFIX
);
405 iter
!= s
->bucket_attrs
.end();
407 const char *name
= iter
->first
.c_str();
408 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
410 if (geniter
!= rgw_to_http_attrs
.end()) {
411 dump_header(s
, geniter
->second
, iter
->second
);
412 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
413 dump_header_prefixed(s
, "X-Container-Meta-",
414 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
420 /* Dump container versioning info. */
421 if (! s
->bucket_info
.swift_ver_location
.empty()) {
422 dump_header(s
, "X-Versions-Location",
423 url_encode(s
->bucket_info
.swift_ver_location
));
426 /* Dump quota headers. */
428 if (quota
.max_size
>= 0) {
429 dump_header(s
, "X-Container-Meta-Quota-Bytes", quota
.max_size
);
432 if (quota
.max_objects
>= 0) {
433 dump_header(s
, "X-Container-Meta-Quota-Count", quota
.max_objects
);
437 /* Dump Static Website headers. */
438 if (! ws_conf
.index_doc_suffix
.empty()) {
439 dump_header(s
, "X-Container-Meta-Web-Index", ws_conf
.index_doc_suffix
);
442 if (! ws_conf
.error_doc
.empty()) {
443 dump_header(s
, "X-Container-Meta-Web-Error", ws_conf
.error_doc
);
446 if (! ws_conf
.subdir_marker
.empty()) {
447 dump_header(s
, "X-Container-Meta-Web-Directory-Type",
448 ws_conf
.subdir_marker
);
451 if (! ws_conf
.listing_css_doc
.empty()) {
452 dump_header(s
, "X-Container-Meta-Web-Listings-CSS",
453 ws_conf
.listing_css_doc
);
456 if (ws_conf
.listing_enabled
) {
457 dump_header(s
, "X-Container-Meta-Web-Listings", "true");
461 void RGWStatAccount_ObjStore_SWIFT::execute()
463 RGWStatAccount_ObjStore::execute();
464 op_ret
= rgw_get_user_attrs_by_uid(store
, s
->user
->user_id
, attrs
);
467 void RGWStatAccount_ObjStore_SWIFT::send_response()
470 op_ret
= STATUS_NO_CONTENT
;
471 dump_account_metadata(s
,
475 buckets_size_rounded
,
478 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
481 set_req_state_err(s
, op_ret
);
484 end_header(s
, NULL
, NULL
, 0, true);
489 void RGWStatBucket_ObjStore_SWIFT::send_response()
492 op_ret
= STATUS_NO_CONTENT
;
493 dump_container_metadata(s
, bucket
, bucket_quota
,
494 s
->bucket_info
.website_conf
);
497 set_req_state_err(s
, op_ret
);
500 end_header(s
, this, NULL
, 0, true);
504 static int get_swift_container_settings(req_state
* const s
,
505 RGWRados
* const store
,
506 RGWAccessControlPolicy
* const policy
,
507 bool * const has_policy
,
509 RGWCORSConfiguration
* const cors_config
,
510 bool * const has_cors
)
512 string read_list
, write_list
;
514 const char * const read_attr
= s
->info
.env
->get("HTTP_X_CONTAINER_READ");
516 read_list
= read_attr
;
518 const char * const write_attr
= s
->info
.env
->get("HTTP_X_CONTAINER_WRITE");
520 write_list
= write_attr
;
525 if (read_attr
|| write_attr
) {
526 RGWAccessControlPolicy_SWIFT
swift_policy(s
->cct
);
527 const auto r
= swift_policy
.create(store
,
529 s
->user
->display_name
,
537 *policy
= swift_policy
;
543 /*Check and update CORS configuration*/
544 const char *allow_origins
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
545 const char *allow_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
546 const char *expose_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
547 const char *max_age
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
549 RGWCORSConfiguration_SWIFT
*swift_cors
= new RGWCORSConfiguration_SWIFT
;
550 int r
= swift_cors
->create_update(allow_origins
, allow_headers
, expose_headers
, max_age
);
552 dout(0) << "Error creating/updating the cors configuration" << dendl
;
557 *cors_config
= *swift_cors
;
565 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
566 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
567 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
568 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
570 static void get_rmattrs_from_headers(const req_state
* const s
,
571 const char * const put_prefix
,
572 const char * const del_prefix
,
573 set
<string
>& rmattr_names
)
575 const size_t put_prefix_len
= strlen(put_prefix
);
576 const size_t del_prefix_len
= strlen(del_prefix
);
578 for (const auto& kv
: s
->info
.env
->get_map()) {
579 size_t prefix_len
= 0;
580 const char * const p
= kv
.first
.c_str();
582 if (strncasecmp(p
, del_prefix
, del_prefix_len
) == 0) {
583 /* Explicitly requested removal. */
584 prefix_len
= del_prefix_len
;
585 } else if ((strncasecmp(p
, put_prefix
, put_prefix_len
) == 0)
586 && kv
.second
.empty()) {
587 /* Removal requested by putting an empty value. */
588 prefix_len
= put_prefix_len
;
591 if (prefix_len
> 0) {
592 string
name(RGW_ATTR_META_PREFIX
);
593 name
.append(lowercase_dash_http_attr(p
+ prefix_len
));
594 rmattr_names
.insert(name
);
599 static int get_swift_versioning_settings(
601 boost::optional
<std::string
>& swift_ver_location
)
603 /* Removing the Swift's versions location has lower priority than setting
604 * a new one. That's the reason why we're handling it first. */
605 const std::string vlocdel
=
606 s
->info
.env
->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
607 if (vlocdel
.size()) {
608 swift_ver_location
= boost::in_place(std::string());
611 if (s
->info
.env
->exists("HTTP_X_VERSIONS_LOCATION")) {
612 /* If the Swift's versioning is globally disabled but someone wants to
613 * enable it for a given container, new version of Swift will generate
614 * the precondition failed error. */
615 if (! s
->cct
->_conf
->rgw_swift_versioning_enabled
) {
616 return -ERR_PRECONDITION_FAILED
;
619 swift_ver_location
= s
->info
.env
->get("HTTP_X_VERSIONS_LOCATION", "");
625 int RGWCreateBucket_ObjStore_SWIFT::get_params()
628 uint32_t policy_rw_mask
= 0;
630 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
631 &policy_rw_mask
, &cors_config
, &has_cors
);
637 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
640 location_constraint
= store
->get_zonegroup().api_name
;
641 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
,
642 CONT_REMOVE_ATTR_PREFIX
, rmattr_names
);
643 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
645 return get_swift_versioning_settings(s
, swift_ver_location
);
648 void RGWCreateBucket_ObjStore_SWIFT::send_response()
651 op_ret
= STATUS_CREATED
;
652 else if (op_ret
== -ERR_BUCKET_EXISTS
)
653 op_ret
= STATUS_ACCEPTED
;
654 set_req_state_err(s
, op_ret
);
656 /* Propose ending HTTP header with 0 Content-Length header. */
657 end_header(s
, NULL
, NULL
, 0);
658 rgw_flush_formatter_and_reset(s
, s
->formatter
);
661 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
665 r
= STATUS_NO_CONTENT
;
667 set_req_state_err(s
, r
);
669 end_header(s
, this, NULL
, 0);
670 rgw_flush_formatter_and_reset(s
, s
->formatter
);
673 static int get_delete_at_param(req_state
*s
, boost::optional
<real_time
> &delete_at
)
675 /* Handle Swift object expiration. */
676 real_time delat_proposal
;
677 string x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AFTER", "");
679 if (x_delete
.empty()) {
680 x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AT", "");
682 /* X-Delete-After HTTP is present. It means we need add its value
683 * to the current time. */
684 delat_proposal
= real_clock::now();
687 if (x_delete
.empty()) {
688 delete_at
= boost::none
;
689 if (s
->info
.env
->exists("HTTP_X_REMOVE_DELETE_AT")) {
690 delete_at
= boost::in_place(real_time());
695 long ts
= strict_strtoll(x_delete
.c_str(), 10, &err
);
701 delat_proposal
+= make_timespan(ts
);
702 if (delat_proposal
< real_clock::now()) {
706 delete_at
= delat_proposal
;
711 int RGWPutObj_ObjStore_SWIFT::verify_permission()
713 op_ret
= RGWPutObj_ObjStore::verify_permission();
715 /* We have to differentiate error codes depending on whether user is
716 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
717 * (403 Forbidden). */
718 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
725 int RGWPutObj_ObjStore_SWIFT::get_params()
727 if (s
->has_bad_meta
) {
732 const char *encoding
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
733 if (!encoding
|| strcmp(encoding
, "chunked") != 0) {
734 ldout(s
->cct
, 20) << "neither length nor chunked encoding" << dendl
;
735 return -ERR_LENGTH_REQUIRED
;
738 chunked_upload
= true;
741 supplied_etag
= s
->info
.env
->get("HTTP_ETAG");
743 if (!s
->generic_attrs
.count(RGW_ATTR_CONTENT_TYPE
)) {
744 ldout(s
->cct
, 5) << "content type wasn't provided, trying to guess" << dendl
;
745 const char *suffix
= strrchr(s
->object
.name
.c_str(), '.');
749 string
suffix_str(suffix
);
750 const char *mime
= rgw_find_mime_by_ext(suffix_str
);
752 s
->generic_attrs
[RGW_ATTR_CONTENT_TYPE
] = mime
;
758 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
760 int r
= get_delete_at_param(s
, delete_at
);
762 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
766 if (!s
->cct
->_conf
->rgw_swift_custom_header
.empty()) {
767 string custom_header
= s
->cct
->_conf
->rgw_swift_custom_header
;
768 if (s
->info
.env
->exists(custom_header
.c_str())) {
769 user_data
= s
->info
.env
->get(custom_header
.c_str());
773 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
775 string multipart_manifest
= s
->info
.args
.get("multipart-manifest", &exists
);
777 if (multipart_manifest
!= "put") {
778 ldout(s
->cct
, 5) << "invalid multipart-manifest http param: " << multipart_manifest
<< dendl
;
782 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
783 uint64_t max_len
= s
->cct
->_conf
->rgw_max_slo_entries
* MAX_SLO_ENTRY_SIZE
;
785 slo_info
= new RGWSLOInfo
;
787 int r
= rgw_rest_get_json_input_keep_data(s
->cct
, s
, slo_info
->entries
, max_len
, &slo_info
->raw_data
, &slo_info
->raw_data_len
);
789 ldout(s
->cct
, 5) << "failed to read input for slo r=" << r
<< dendl
;
793 if ((int64_t)slo_info
->entries
.size() > s
->cct
->_conf
->rgw_max_slo_entries
) {
794 ldout(s
->cct
, 5) << "too many entries in slo request: " << slo_info
->entries
.size() << dendl
;
799 uint64_t total_size
= 0;
800 for (const auto& entry
: slo_info
->entries
) {
801 etag_sum
.Update((const byte
*)entry
.etag
.c_str(),
802 entry
.etag
.length());
803 total_size
+= entry
.size_bytes
;
805 ldout(s
->cct
, 20) << "slo_part: " << entry
.path
806 << " size=" << entry
.size_bytes
807 << " etag=" << entry
.etag
810 complete_etag(etag_sum
, &lo_etag
);
811 slo_info
->total_size
= total_size
;
813 ofs
= slo_info
->raw_data_len
;
816 return RGWPutObj_ObjStore::get_params();
819 void RGWPutObj_ObjStore_SWIFT::send_response()
822 op_ret
= STATUS_CREATED
;
825 if (! lo_etag
.empty()) {
826 /* Static Large Object of Swift API has two etags represented by
828 * - etag - for the manifest itself (it will be stored in xattrs),
829 * - lo_etag - for the content composited from SLO's segments.
830 * The value is calculated basing on segments' etags.
831 * In response for PUT request we have to expose the second one.
832 * The first one may be obtained by GET with "multipart-manifest=get"
833 * in query string on a given SLO. */
834 dump_etag(s
, lo_etag
, true /* quoted */);
839 dump_last_modified(s
, mtime
);
840 set_req_state_err(s
, op_ret
);
843 rgw_flush_formatter_and_reset(s
, s
->formatter
);
846 static int get_swift_account_settings(req_state
* const s
,
847 RGWRados
* const store
,
848 RGWAccessControlPolicy_SWIFTAcct
* const policy
,
849 bool * const has_policy
)
853 const char * const acl_attr
= s
->info
.env
->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
855 RGWAccessControlPolicy_SWIFTAcct
swift_acct_policy(s
->cct
);
856 const bool r
= swift_acct_policy
.create(store
,
858 s
->user
->display_name
,
864 *policy
= swift_acct_policy
;
871 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
873 if (s
->has_bad_meta
) {
877 int ret
= get_swift_account_settings(s
,
879 // FIXME: we need to carry unique_ptr in generic class
880 // and allocate appropriate ACL class in the ctor
881 static_cast<RGWAccessControlPolicy_SWIFTAcct
*>(&policy
),
887 get_rmattrs_from_headers(s
, ACCT_PUT_ATTR_PREFIX
, ACCT_REMOVE_ATTR_PREFIX
,
892 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
895 op_ret
= STATUS_NO_CONTENT
;
897 set_req_state_err(s
, op_ret
);
900 rgw_flush_formatter_and_reset(s
, s
->formatter
);
903 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
905 if (s
->has_bad_meta
) {
909 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
910 &policy_rw_mask
, &cors_config
, &has_cors
);
915 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
, CONT_REMOVE_ATTR_PREFIX
,
917 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
919 return get_swift_versioning_settings(s
, swift_ver_location
);
922 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
924 if (!op_ret
&& (op_ret
!= -EINVAL
)) {
925 op_ret
= STATUS_NO_CONTENT
;
927 set_req_state_err(s
, op_ret
);
930 rgw_flush_formatter_and_reset(s
, s
->formatter
);
933 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
935 if (s
->has_bad_meta
) {
939 /* Handle Swift object expiration. */
940 int r
= get_delete_at_param(s
, delete_at
);
942 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
946 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
947 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
952 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
955 op_ret
= STATUS_ACCEPTED
;
957 set_req_state_err(s
, op_ret
);
959 dump_content_length(s
, 0);
963 rgw_flush_formatter_and_reset(s
, s
->formatter
);
966 static void bulkdelete_respond(const unsigned num_deleted
,
967 const unsigned int num_unfound
,
968 const std::list
<RGWBulkDelete::fail_desc_t
>& failures
,
969 const int prot_flags
, /* in */
970 ceph::Formatter
& formatter
) /* out */
972 formatter
.open_object_section("delete");
977 if (!failures
.empty()) {
978 int reason
= ERR_INVALID_REQUEST
;
979 for (const auto fail_desc
: failures
) {
980 if (-ENOENT
!= fail_desc
.err
&& -EACCES
!= fail_desc
.err
) {
981 reason
= fail_desc
.err
;
985 set_req_state_err(err
, reason
, prot_flags
);
986 dump_errno(err
, resp_status
);
987 } else if (0 == num_deleted
&& 0 == num_unfound
) {
988 /* 400 Bad Request */
989 dump_errno(400, resp_status
);
990 resp_body
= "Invalid bulk delete.";
993 dump_errno(200, resp_status
);
996 encode_json("Number Deleted", num_deleted
, &formatter
);
997 encode_json("Number Not Found", num_unfound
, &formatter
);
998 encode_json("Response Body", resp_body
, &formatter
);
999 encode_json("Response Status", resp_status
, &formatter
);
1001 formatter
.open_array_section("Errors");
1002 for (const auto fail_desc
: failures
) {
1003 formatter
.open_array_section("object");
1005 stringstream ss_name
;
1006 ss_name
<< fail_desc
.path
;
1007 encode_json("Name", ss_name
.str(), &formatter
);
1010 set_req_state_err(err
, fail_desc
.err
, prot_flags
);
1012 dump_errno(err
, status
);
1013 encode_json("Status", status
, &formatter
);
1014 formatter
.close_section();
1016 formatter
.close_section();
1018 formatter
.close_section();
1021 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1023 op_ret
= RGWDeleteObj_ObjStore::verify_permission();
1025 /* We have to differentiate error codes depending on whether user is
1026 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1027 * (403 Forbidden). */
1028 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1035 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1037 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1038 multipart_delete
= (mm
.compare("delete") == 0);
1040 return RGWDeleteObj_ObjStore::get_params();
1043 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1047 if (multipart_delete
) {
1050 r
= STATUS_NO_CONTENT
;
1053 set_req_state_err(s
, r
);
1056 if (multipart_delete
) {
1057 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1058 CHUNKED_TRANSFER_ENCODING
);
1061 bulkdelete_respond(deleter
->get_num_deleted(),
1062 deleter
->get_num_unfound(),
1063 deleter
->get_failures(),
1066 } else if (-ENOENT
== op_ret
) {
1067 bulkdelete_respond(0, 1, {}, s
->prot_flags
, *s
->formatter
);
1069 RGWBulkDelete::acct_path_t path
;
1070 path
.bucket_name
= s
->bucket_name
;
1071 path
.obj_key
= s
->object
;
1073 RGWBulkDelete::fail_desc_t fail_desc
;
1074 fail_desc
.err
= op_ret
;
1075 fail_desc
.path
= path
;
1077 bulkdelete_respond(0, 0, { fail_desc
}, s
->prot_flags
, *s
->formatter
);
1080 end_header(s
, this);
1083 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1087 static void get_contype_from_attrs(map
<string
, bufferlist
>& attrs
,
1088 string
& content_type
)
1090 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_CONTENT_TYPE
);
1091 if (iter
!= attrs
.end()) {
1092 content_type
= iter
->second
.c_str();
1096 static void dump_object_metadata(struct req_state
* const s
,
1097 map
<string
, bufferlist
> attrs
)
1099 map
<string
, string
> response_attrs
;
1101 for (auto kv
: attrs
) {
1102 const char * name
= kv
.first
.c_str();
1103 const auto aiter
= rgw_to_http_attrs
.find(name
);
1105 if (aiter
!= std::end(rgw_to_http_attrs
)) {
1106 response_attrs
[aiter
->second
] = kv
.second
.c_str();
1107 } else if (strcmp(name
, RGW_ATTR_SLO_UINDICATOR
) == 0) {
1108 // this attr has an extra length prefix from ::encode() in prior versions
1109 dump_header(s
, "X-Object-Meta-Static-Large-Object", "True");
1110 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
,
1111 sizeof(RGW_ATTR_META_PREFIX
)-1) == 0) {
1112 name
+= sizeof(RGW_ATTR_META_PREFIX
) - 1;
1113 dump_header_prefixed(s
, "X-Object-Meta-",
1114 camelcase_dash_http_attr(name
), kv
.second
);
1118 /* Handle override and fallback for Content-Disposition HTTP header.
1119 * At the moment this will be used only by TempURL of the Swift API. */
1120 const auto cditer
= rgw_to_http_attrs
.find(RGW_ATTR_CONTENT_DISP
);
1121 if (cditer
!= std::end(rgw_to_http_attrs
)) {
1122 const auto& name
= cditer
->second
;
1124 if (!s
->content_disp
.override
.empty()) {
1125 response_attrs
[name
] = s
->content_disp
.override
;
1126 } else if (!s
->content_disp
.fallback
.empty()
1127 && response_attrs
.find(name
) == std::end(response_attrs
)) {
1128 response_attrs
[name
] = s
->content_disp
.fallback
;
1132 for (const auto kv
: response_attrs
) {
1133 dump_header(s
, kv
.first
, kv
.second
);
1136 const auto iter
= attrs
.find(RGW_ATTR_DELETE_AT
);
1137 if (iter
!= std::end(attrs
)) {
1140 ::decode(delete_at
, iter
->second
);
1141 if (!delete_at
.is_zero()) {
1142 dump_header(s
, "X-Delete-At", delete_at
.sec());
1144 } catch (buffer::error
& err
) {
1145 ldout(s
->cct
, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1152 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1154 dest_policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1159 int RGWCopyObj_ObjStore_SWIFT::get_params()
1161 if_mod
= s
->info
.env
->get("HTTP_IF_MODIFIED_SINCE");
1162 if_unmod
= s
->info
.env
->get("HTTP_IF_UNMODIFIED_SINCE");
1163 if_match
= s
->info
.env
->get("HTTP_COPY_IF_MATCH");
1164 if_nomatch
= s
->info
.env
->get("HTTP_COPY_IF_NONE_MATCH");
1166 src_tenant_name
= s
->src_tenant_name
;
1167 src_bucket_name
= s
->src_bucket_name
;
1168 src_object
= s
->src_object
;
1169 dest_tenant_name
= s
->bucket_tenant
;
1170 dest_bucket_name
= s
->bucket_name
;
1171 dest_object
= s
->object
.name
;
1173 const char * const fresh_meta
= s
->info
.env
->get("HTTP_X_FRESH_METADATA");
1174 if (fresh_meta
&& strcasecmp(fresh_meta
, "TRUE") == 0) {
1175 attrs_mod
= RGWRados::ATTRSMOD_REPLACE
;
1177 attrs_mod
= RGWRados::ATTRSMOD_MERGE
;
1180 int r
= get_delete_at_param(s
, delete_at
);
1182 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1189 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs
)
1191 if (! sent_header
) {
1193 op_ret
= STATUS_CREATED
;
1194 set_req_state_err(s
, op_ret
);
1196 end_header(s
, this);
1198 /* Send progress information. Note that this diverge from the original swift
1199 * spec. We do this in order to keep connection alive.
1202 s
->formatter
->open_array_section("progress");
1206 s
->formatter
->dump_int("ofs", (uint64_t)ofs
);
1208 rgw_flush_formatter(s
, s
->formatter
);
1211 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1213 /* Dump X-Copied-From. */
1214 dump_header(s
, "X-Copied-From", url_encode(src_bucket
.name
) +
1215 "/" + url_encode(src_object
.name
));
1217 /* Dump X-Copied-From-Account. */
1219 dump_header(s
, "X-Copied-From-Account", url_encode(s
->user
->user_id
.id
));
1221 /* Dump X-Copied-From-Last-Modified. */
1222 dump_time_header(s
, "X-Copied-From-Last-Modified", src_mtime
);
1225 void RGWCopyObj_ObjStore_SWIFT::send_response()
1227 if (! sent_header
) {
1228 string content_type
;
1230 op_ret
= STATUS_CREATED
;
1231 set_req_state_err(s
, op_ret
);
1234 dump_last_modified(s
, mtime
);
1236 get_contype_from_attrs(attrs
, content_type
);
1237 dump_object_metadata(s
, attrs
);
1238 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1239 : "binary/octet-stream");
1241 s
->formatter
->close_section();
1242 rgw_flush_formatter(s
, s
->formatter
);
1246 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1248 op_ret
= RGWGetObj_ObjStore::verify_permission();
1250 /* We have to differentiate error codes depending on whether user is
1251 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1252 * (403 Forbidden). */
1253 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1260 int RGWGetObj_ObjStore_SWIFT::get_params()
1262 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1263 skip_manifest
= (mm
.compare("get") == 0);
1265 return RGWGetObj_ObjStore::get_params();
1268 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1270 std::string error_content
;
1271 op_ret
= error_handler(op_ret
, &error_content
);
1273 /* The error handler has taken care of the error. */
1277 bufferlist error_bl
;
1278 error_bl
.append(error_content
);
1279 return send_response_data(error_bl
, 0, error_bl
.length());
1282 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist
& bl
,
1286 string content_type
;
1292 if (custom_http_ret
) {
1293 set_req_state_err(s
, 0);
1294 dump_errno(s
, custom_http_ret
);
1296 set_req_state_err(s
, (partial_content
&& !op_ret
) ? STATUS_PARTIAL_CONTENT
1301 end_header(s
, NULL
);
1307 dump_range(s
, ofs
, end
, s
->obj_size
);
1311 end_header(s
, NULL
);
1315 dump_content_length(s
, total_len
);
1316 dump_last_modified(s
, lastmod
);
1317 dump_header(s
, "X-Timestamp", utime_t(lastmod
));
1319 dump_header(s
, "X-Static-Large-Object", "True");
1323 if (! lo_etag
.empty()) {
1324 dump_etag(s
, lo_etag
, true /* quoted */);
1326 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
1327 if (iter
!= attrs
.end()) {
1328 dump_etag(s
, iter
->second
);
1332 get_contype_from_attrs(attrs
, content_type
);
1333 dump_object_metadata(s
, attrs
);
1336 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1337 : "binary/octet-stream");
1342 if (get_data
&& !op_ret
) {
1343 const auto r
= dump_body(s
, bl
.c_str() + bl_ofs
, bl_len
);
1348 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1353 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1355 string hdrs
, exp_hdrs
;
1356 uint32_t max_age
= CORS_MAX_AGE_INVALID
;
1357 /*EACCES means, there is no CORS registered yet for the bucket
1358 *ENOENT means, there is no match of the Origin in the list of CORSRule
1360 if (op_ret
== -ENOENT
)
1363 set_req_state_err(s
, op_ret
);
1365 end_header(s
, NULL
);
1368 get_response_params(hdrs
, exp_hdrs
, &max_age
);
1370 dump_access_control(s
, origin
, req_meth
, hdrs
.c_str(), exp_hdrs
.c_str(),
1372 end_header(s
, NULL
);
1375 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1376 list
<RGWBulkDelete::acct_path_t
>& items
, bool * const is_truncated
)
1378 constexpr size_t MAX_LINE_SIZE
= 2048;
1380 RGWClientIOStreamBuf
ciosb(static_cast<RGWRestfulIO
&>(*(s
->cio
)),
1381 size_t(s
->cct
->_conf
->rgw_max_chunk_size
));
1382 istream
cioin(&ciosb
);
1384 char buf
[MAX_LINE_SIZE
];
1385 while (cioin
.getline(buf
, sizeof(buf
))) {
1386 string
path_str(buf
);
1388 ldout(s
->cct
, 20) << "extracted Bulk Delete entry: " << path_str
<< dendl
;
1390 RGWBulkDelete::acct_path_t path
;
1392 /* We need to skip all slashes at the beginning in order to preserve
1393 * compliance with Swift. */
1394 const size_t start_pos
= path_str
.find_first_not_of('/');
1396 if (string::npos
!= start_pos
) {
1397 /* Seperator is the first slash after the leading ones. */
1398 const size_t sep_pos
= path_str
.find('/', start_pos
);
1400 if (string::npos
!= sep_pos
) {
1401 path
.bucket_name
= url_decode(path_str
.substr(start_pos
,
1402 sep_pos
- start_pos
));
1403 path
.obj_key
= url_decode(path_str
.substr(sep_pos
+ 1));
1405 /* It's guaranteed here that bucket name is at least one character
1406 * long and is different than slash. */
1407 path
.bucket_name
= url_decode(path_str
.substr(start_pos
));
1410 items
.push_back(path
);
1413 if (items
.size() == MAX_CHUNK_ENTRIES
) {
1414 *is_truncated
= true;
1419 *is_truncated
= false;
1423 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1425 set_req_state_err(s
, op_ret
);
1427 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1428 CHUNKED_TRANSFER_ENCODING
);
1430 bulkdelete_respond(deleter
->get_num_deleted(),
1431 deleter
->get_num_unfound(),
1432 deleter
->get_failures(),
1435 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1439 std::unique_ptr
<RGWBulkUploadOp::StreamGetter
>
1440 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1442 class SwiftStreamGetter
: public StreamGetter
{
1443 const size_t conlen
;
1448 SwiftStreamGetter(req_state
* const s
, const size_t conlen
)
1454 ssize_t
get_at_most(size_t want
, ceph::bufferlist
& dst
) override
{
1455 /* maximum requested by a caller */
1456 /* data provided by client */
1457 /* RadosGW's limit. */
1458 const size_t max_chunk_size
= \
1459 static_cast<size_t>(s
->cct
->_conf
->rgw_max_chunk_size
);
1460 const size_t max_to_read
= std::min({ want
, conlen
- curpos
, max_chunk_size
});
1462 ldout(s
->cct
, 20) << "bulk_upload: get_at_most max_to_read="
1464 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst
.c_str()) << dendl
;
1466 bufferptr
bp(max_to_read
);
1467 const auto read_len
= recv_body(s
, bp
.c_str(), max_to_read
);
1468 dst
.append(bp
, 0, read_len
);
1469 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1475 return curpos
> s
->cct
->_conf
->rgw_max_put_size
? -ERR_TOO_LARGE
1479 ssize_t
get_exactly(size_t want
, ceph::bufferlist
& dst
) override
{
1480 ldout(s
->cct
, 20) << "bulk_upload: get_exactly want=" << want
<< dendl
;
1482 /* FIXME: do this in a loop. */
1483 const auto ret
= get_at_most(want
, dst
);
1484 ldout(s
->cct
, 20) << "bulk_upload: get_exactly ret=" << ret
<< dendl
;
1487 } else if (static_cast<size_t>(ret
) != want
) {
1499 ldout(s
->cct
, 20) << "bulk upload: create_stream for length="
1500 << s
->length
<< dendl
;
1502 const size_t conlen
= atoll(s
->length
);
1503 return std::unique_ptr
<SwiftStreamGetter
>(new SwiftStreamGetter(s
, conlen
));
1507 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1509 set_req_state_err(s
, op_ret
);
1511 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1512 CHUNKED_TRANSFER_ENCODING
);
1513 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1515 s
->formatter
->open_object_section("delete");
1517 std::string resp_status
;
1518 std::string resp_body
;
1520 if (! failures
.empty()) {
1523 const auto last_err
= { failures
.back().err
};
1524 if (boost::algorithm::contains(last_err
, terminal_errors
)) {
1525 /* The terminal errors are affecting the status of the whole upload. */
1526 set_req_state_err(err
, failures
.back().err
, s
->prot_flags
);
1528 set_req_state_err(err
, ERR_INVALID_REQUEST
, s
->prot_flags
);
1531 dump_errno(err
, resp_status
);
1532 } else if (0 == num_created
&& failures
.empty()) {
1533 /* Nothing created, nothing failed. This means the archive contained no
1534 * entity we could understand (regular file or directory). We need to
1535 * send 400 Bad Request to an HTTP client in the internal status field. */
1536 dump_errno(400, resp_status
);
1537 resp_body
= "Invalid Tar File: No Valid Files";
1540 dump_errno(201, resp_status
);
1543 encode_json("Number Files Created", num_created
, s
->formatter
);
1544 encode_json("Response Body", resp_body
, s
->formatter
);
1545 encode_json("Response Status", resp_status
, s
->formatter
);
1547 s
->formatter
->open_array_section("Errors");
1548 for (const auto& fail_desc
: failures
) {
1549 s
->formatter
->open_array_section("object");
1551 encode_json("Name", fail_desc
.path
, s
->formatter
);
1554 set_req_state_err(err
, fail_desc
.err
, s
->prot_flags
);
1556 dump_errno(err
, status
);
1557 encode_json("Status", status
, s
->formatter
);
1559 s
->formatter
->close_section();
1561 s
->formatter
->close_section();
1563 s
->formatter
->close_section();
1564 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1568 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1570 set_req_state_err(s
, op_ret
);
1572 end_header(s
, this, "application/xml");
1574 std::stringstream ss
;
1576 ss
<< R
"(<?xml version="1.0"?>)" << "\n"
1577 << R
"(<!DOCTYPE cross-domain-policy SYSTEM )"
1578 << R
"("http
://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1579 << R
"(<cross-domain-policy>)" << "\n"
1580 << g_conf
->rgw_cross_domain_policy
<< "\n"
1581 << R
"(</cross-domain-policy>)";
1583 dump_body(s
, ss
.str());
1586 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1588 set_req_state_err(s
, op_ret
);
1590 end_header(s
, this, "application/xml");
1593 static constexpr char DISABLED
[] = "DISABLED BY FILE";
1594 dump_body(s
, DISABLED
, strlen(DISABLED
));
1598 const vector
<pair
<string
, RGWInfo_ObjStore_SWIFT::info
>> RGWInfo_ObjStore_SWIFT::swift_info
=
1600 {"bulk_delete", {false, nullptr}},
1601 {"container_quotas", {false, nullptr}},
1602 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data
}},
1603 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data
}},
1604 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data
}},
1605 {"account_quotas", {false, nullptr}},
1606 {"staticweb", {false, nullptr}},
1607 {"tempauth", {false, RGWInfo_ObjStore_SWIFT::list_tempauth_data
}},
1610 void RGWInfo_ObjStore_SWIFT::execute()
1612 bool is_admin_info_enabled
= false;
1614 const string
& swiftinfo_sig
= s
->info
.args
.get("swiftinfo_sig");
1615 const string
& swiftinfo_expires
= s
->info
.args
.get("swiftinfo_expires");
1617 if (!swiftinfo_sig
.empty() &&
1618 !swiftinfo_expires
.empty() &&
1619 !is_expired(swiftinfo_expires
, s
->cct
)) {
1620 is_admin_info_enabled
= true;
1623 s
->formatter
->open_object_section("info");
1625 for (const auto& pair
: swift_info
) {
1626 if(!is_admin_info_enabled
&& pair
.second
.is_admin_info
)
1629 if (!pair
.second
.list_data
) {
1630 s
->formatter
->open_object_section((pair
.first
).c_str());
1631 s
->formatter
->close_section();
1634 pair
.second
.list_data(*(s
->formatter
), *(s
->cct
->_conf
), *store
);
1638 s
->formatter
->close_section();
1641 void RGWInfo_ObjStore_SWIFT::send_response()
1644 op_ret
= STATUS_NO_CONTENT
;
1646 set_req_state_err(s
, op_ret
);
1648 end_header(s
, this);
1649 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1652 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter
& formatter
,
1653 const md_config_t
& config
,
1656 formatter
.open_object_section("swift");
1657 formatter
.dump_int("max_file_size", config
.rgw_max_put_size
);
1658 formatter
.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX
);
1660 string
ceph_version(CEPH_GIT_NICE_VER
);
1661 formatter
.dump_string("version", ceph_version
);
1662 formatter
.dump_int("max_meta_name_length", 81);
1664 formatter
.open_array_section("policies");
1665 RGWZoneGroup
& zonegroup
= store
.get_zonegroup();
1667 for (const auto& placement_targets
: zonegroup
.placement_targets
) {
1668 formatter
.open_object_section("policy");
1669 if (placement_targets
.second
.name
.compare(zonegroup
.default_placement
) == 0)
1670 formatter
.dump_bool("default", true);
1671 formatter
.dump_string("name", placement_targets
.second
.name
.c_str());
1672 formatter
.close_section();
1674 formatter
.close_section();
1676 formatter
.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN
);
1677 formatter
.dump_bool("strict_cors_mode", true);
1678 formatter
.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN
);
1679 formatter
.close_section();
1682 void RGWInfo_ObjStore_SWIFT::list_tempauth_data(Formatter
& formatter
,
1683 const md_config_t
& config
,
1686 formatter
.open_object_section("tempauth");
1687 formatter
.dump_bool("account_acls", true);
1688 formatter
.close_section();
1690 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter
& formatter
,
1691 const md_config_t
& config
,
1694 formatter
.open_object_section("tempurl");
1695 formatter
.open_array_section("methods");
1696 formatter
.dump_string("methodname", "GET");
1697 formatter
.dump_string("methodname", "HEAD");
1698 formatter
.dump_string("methodname", "PUT");
1699 formatter
.dump_string("methodname", "POST");
1700 formatter
.dump_string("methodname", "DELETE");
1701 formatter
.close_section();
1702 formatter
.close_section();
1705 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter
& formatter
,
1706 const md_config_t
& config
,
1709 formatter
.open_object_section("slo");
1710 formatter
.dump_int("max_manifest_segments", config
.rgw_max_slo_entries
);
1711 formatter
.close_section();
1714 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string
& expires
, CephContext
* cct
)
1717 const utime_t now
= ceph_clock_now();
1718 const uint64_t expiration
= (uint64_t)strict_strtoll(expires
.c_str(),
1721 ldout(cct
, 5) << "failed to parse siginfo_expires: " << err
<< dendl
;
1725 if (expiration
<= (uint64_t)now
.sec()) {
1726 ldout(cct
, 5) << "siginfo expired: " << expiration
<< " <= " << now
.sec() << dendl
;
1734 void RGWFormPost::init(RGWRados
* const store
,
1736 RGWHandler
* const dialect_handler
)
1738 prefix
= std::move(s
->object
.name
);
1739 s
->object
= rgw_obj_key();
1741 return RGWPostObj_ObjStore::init(store
, s
, dialect_handler
);
1744 std::size_t RGWFormPost::get_max_file_size() /*const*/
1746 std::string max_str
= get_part_str(ctrl_parts
, "max_file_size", "0");
1749 const std::size_t max_file_size
=
1750 static_cast<uint64_t>(strict_strtoll(max_str
.c_str(), 10, &err
));
1752 if (! err
.empty()) {
1753 ldout(s
->cct
, 5) << "failed to parse FormPost's max_file_size: " << err
1758 return max_file_size
;
1761 bool RGWFormPost::is_non_expired()
1763 std::string expires
= get_part_str(ctrl_parts
, "expires", "0");
1766 const uint64_t expires_timestamp
=
1767 static_cast<uint64_t>(strict_strtoll(expires
.c_str(), 10, &err
));
1769 if (! err
.empty()) {
1770 dout(5) << "failed to parse FormPost's expires: " << err
<< dendl
;
1774 const utime_t now
= ceph_clock_now();
1775 if (expires_timestamp
<= static_cast<uint64_t>(now
.sec())) {
1776 dout(5) << "FormPost form expired: "
1777 << expires_timestamp
<< " <= " << now
.sec() << dendl
;
1784 bool RGWFormPost::is_integral()
1786 const std::string form_signature
= get_part_str(ctrl_parts
, "signature");
1789 get_owner_info(s
, *s
->user
);
1790 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
1792 ldout(s
->cct
, 5) << "cannot get user_info of account's owner" << dendl
;
1796 for (const auto& kv
: s
->user
->temp_url_keys
) {
1797 const int temp_url_key_num
= kv
.first
;
1798 const string
& temp_url_key
= kv
.second
;
1800 if (temp_url_key
.empty()) {
1804 SignatureHelper sig_helper
;
1805 sig_helper
.calc(temp_url_key
,
1806 s
->info
.request_uri
,
1807 get_part_str(ctrl_parts
, "redirect"),
1808 get_part_str(ctrl_parts
, "max_file_size", "0"),
1809 get_part_str(ctrl_parts
, "max_file_count", "0"),
1810 get_part_str(ctrl_parts
, "expires", "0"));
1812 const auto local_sig
= sig_helper
.get_signature();
1814 ldout(s
->cct
, 20) << "FormPost signature [" << temp_url_key_num
<< "]"
1815 << " (calculated): " << local_sig
<< dendl
;
1817 if (sig_helper
.is_equal_to(form_signature
)) {
1820 ldout(s
->cct
, 5) << "FormPost's signature mismatch: "
1821 << local_sig
<< " != " << form_signature
<< dendl
;
1828 void RGWFormPost::get_owner_info(const req_state
* const s
,
1829 RGWUserInfo
& owner_info
) const
1831 /* We cannot use req_state::bucket_name because it isn't available
1832 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
1833 const string
& bucket_name
= s
->init_state
.url_bucket
;
1835 /* TempURL in Formpost only requires that bucket name is specified. */
1836 if (bucket_name
.empty()) {
1840 string bucket_tenant
;
1841 if (!s
->account_name
.empty()) {
1845 const rgw_user
uid(s
->account_name
);
1846 if (uid
.tenant
.empty()) {
1847 const rgw_user
tenanted_uid(uid
.id
, uid
.id
);
1849 if (rgw_get_user_info_by_uid(store
, tenanted_uid
, uinfo
) >= 0) {
1851 bucket_tenant
= uinfo
.user_id
.tenant
;
1856 if (!found
&& rgw_get_user_info_by_uid(store
, uid
, uinfo
) < 0) {
1859 bucket_tenant
= uinfo
.user_id
.tenant
;
1863 /* Need to get user info of bucket owner. */
1864 RGWBucketInfo bucket_info
;
1865 int ret
= store
->get_bucket_info(*static_cast<RGWObjectCtx
*>(s
->obj_ctx
),
1866 bucket_tenant
, bucket_name
,
1867 bucket_info
, nullptr);
1872 ldout(s
->cct
, 20) << "temp url user (bucket owner): " << bucket_info
.owner
1875 if (rgw_get_user_info_by_uid(store
, bucket_info
.owner
, owner_info
) < 0) {
1880 int RGWFormPost::get_params()
1882 /* The parentt class extracts boundary info from the Content-Type. */
1883 int ret
= RGWPostObj_ObjStore::get_params();
1888 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1890 /* Let's start parsing the HTTP body by parsing each form part step-
1891 * by-step till encountering the first part with file data. */
1893 struct post_form_part part
;
1894 ret
= read_form_part_header(&part
, stream_done
);
1899 if (s
->cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
1900 ldout(s
->cct
, 20) << "read part header -- part.name="
1901 << part
.name
<< dendl
;
1903 for (const auto& pair
: part
.fields
) {
1904 ldout(s
->cct
, 20) << "field.name=" << pair
.first
<< dendl
;
1905 ldout(s
->cct
, 20) << "field.val=" << pair
.second
.val
<< dendl
;
1906 ldout(s
->cct
, 20) << "field.params:" << dendl
;
1908 for (const auto& param_pair
: pair
.second
.params
) {
1909 ldout(s
->cct
, 20) << " " << param_pair
.first
1910 << " -> " << param_pair
.second
<< dendl
;
1916 /* Unexpected here. */
1917 err_msg
= "Malformed request";
1921 const auto field_iter
= part
.fields
.find("Content-Disposition");
1922 if (std::end(part
.fields
) != field_iter
&&
1923 std::end(field_iter
->second
.params
) != field_iter
->second
.params
.find("filename")) {
1924 /* First data part ahead. */
1925 current_data_part
= std::move(part
);
1927 /* Stop the iteration. We can assume that all control parts have been
1928 * already parsed. The rest of HTTP body should contain data parts
1929 * only. They will be picked up by ::get_data(). */
1932 /* Control part ahead. Receive, parse and store for later usage. */
1934 ret
= read_data(part
.data
, s
->cct
->_conf
->rgw_max_chunk_size
,
1935 boundary
, stream_done
);
1938 } else if (! boundary
) {
1939 err_msg
= "Couldn't find boundary";
1943 ctrl_parts
[part
.name
] = std::move(part
);
1945 } while (! stream_done
);
1948 max_len
= get_max_file_size();
1950 if (! current_data_part
) {
1951 err_msg
= "FormPost: no files to process";
1955 if (! is_non_expired()) {
1956 err_msg
= "FormPost: Form Expired";
1960 if (! is_integral()) {
1961 err_msg
= "FormPost: Invalid Signature";
1968 std::string
RGWFormPost::get_current_filename() const
1971 const auto& field
= current_data_part
->fields
.at("Content-Disposition");
1972 const auto iter
= field
.params
.find("filename");
1974 if (std::end(field
.params
) != iter
) {
1975 return prefix
+ iter
->second
;
1977 } catch (std::out_of_range
&) {
1984 std::string
RGWFormPost::get_current_content_type() const
1987 const auto& field
= current_data_part
->fields
.at("Content-Type");
1989 } catch (std::out_of_range
&) {
1993 return std::string();
1996 bool RGWFormPost::is_next_file_to_upload()
1998 if (! stream_done
) {
1999 /* We have at least one additional part in the body. */
2000 struct post_form_part part
;
2001 int r
= read_form_part_header(&part
, stream_done
);
2006 const auto field_iter
= part
.fields
.find("Content-Disposition");
2007 if (std::end(part
.fields
) != field_iter
) {
2008 const auto& params
= field_iter
->second
.params
;
2009 const auto& filename_iter
= params
.find("filename");
2011 if (std::end(params
) != filename_iter
&& ! filename_iter
->second
.empty()) {
2012 current_data_part
= std::move(part
);
2021 int RGWFormPost::get_data(ceph::bufferlist
& bl
, bool& again
)
2025 int r
= read_data(bl
, s
->cct
->_conf
->rgw_max_chunk_size
,
2026 boundary
, stream_done
);
2031 /* Tell RGWPostObj::execute() that it has some data to put. */
2037 void RGWFormPost::send_response()
2039 std::string redirect
= get_part_str(ctrl_parts
, "redirect");
2040 if (! redirect
.empty()) {
2041 op_ret
= STATUS_REDIRECT
;
2044 set_req_state_err(s
, op_ret
);
2045 s
->err
.err_code
= err_msg
;
2047 if (! redirect
.empty()) {
2048 dump_redirect(s
, redirect
);
2050 end_header(s
, this);
2053 bool RGWFormPost::is_formpost_req(req_state
* const s
)
2055 std::string content_type
;
2056 std::map
<std::string
, std::string
> params
;
2058 parse_boundary_params(s
->info
.env
->get("CONTENT_TYPE", ""),
2059 content_type
, params
);
2061 return boost::algorithm::iequals(content_type
, "multipart/form-data") &&
2062 params
.count("boundary") > 0;
2066 RGWOp
*RGWHandler_REST_Service_SWIFT::op_get()
2068 return new RGWListBuckets_ObjStore_SWIFT
;
2071 RGWOp
*RGWHandler_REST_Service_SWIFT::op_head()
2073 return new RGWStatAccount_ObjStore_SWIFT
;
2076 RGWOp
*RGWHandler_REST_Service_SWIFT::op_put()
2078 if (s
->info
.args
.exists("extract-archive")) {
2079 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2084 RGWOp
*RGWHandler_REST_Service_SWIFT::op_post()
2086 if (s
->info
.args
.exists("bulk-delete")) {
2087 return new RGWBulkDelete_ObjStore_SWIFT
;
2089 return new RGWPutMetadataAccount_ObjStore_SWIFT
;
2092 RGWOp
*RGWHandler_REST_Service_SWIFT::op_delete()
2094 if (s
->info
.args
.exists("bulk-delete")) {
2095 return new RGWBulkDelete_ObjStore_SWIFT
;
2100 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret
,
2101 const std::string error_doc
)
2103 /* Try to throw it all away. */
2104 s
->formatter
->reset();
2106 class RGWGetErrorPage
: public RGWGetObj_ObjStore_SWIFT
{
2108 RGWGetErrorPage(RGWRados
* const store
,
2109 RGWHandler_REST
* const handler
,
2111 const int http_ret
) {
2112 /* Calling a virtual from the base class is safe as the subobject should
2113 * be properly initialized and we haven't overridden the init method. */
2114 init(store
, s
, handler
);
2116 set_custom_http_response(http_ret
);
2119 int error_handler(const int err_no
,
2120 std::string
* const error_content
) override
{
2121 /* Enforce that any error generated while getting the error page will
2122 * not be send to a client. This allows us to recover from the double
2123 * fault situation by sending the original message. */
2126 } get_errpage_op(store
, handler
, s
, http_ret
);
2128 s
->object
= std::to_string(http_ret
) + error_doc
;
2130 RGWOp
* newop
= &get_errpage_op
;
2132 return rgw_process_authenticated(handler
, newop
, &req
, s
, true);
2135 int RGWSwiftWebsiteHandler::error_handler(const int err_no
,
2136 std::string
* const error_content
)
2138 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2140 if (can_be_website_req() && ! ws_conf
.error_doc
.empty()) {
2141 set_req_state_err(s
, err_no
);
2142 return serve_errordoc(s
->err
.http_ret
, ws_conf
.error_doc
);
2145 /* Let's go to the default, no-op handler. */
2149 bool RGWSwiftWebsiteHandler::is_web_mode() const
2151 const boost::string_ref webmode
= s
->info
.env
->get("HTTP_X_WEB_MODE", "");
2152 return boost::algorithm::iequals(webmode
, "true");
2155 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2157 /* Static website works only with the GET or HEAD method. Nothing more. */
2158 static const std::set
<boost::string_ref
> ws_methods
= { "GET", "HEAD" };
2159 if (ws_methods
.count(s
->info
.method
) == 0) {
2163 /* We also need to handle early failures from the auth system. In such cases
2164 * req_state::auth.identity may be empty. Let's treat that the same way as
2165 * the anonymous access. */
2166 if (! s
->auth
.identity
) {
2170 /* Swift serves websites only for anonymous requests unless client explicitly
2171 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2172 if (s
->auth
.identity
->is_anonymous() || is_web_mode()) {
2179 RGWOp
* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2181 class RGWMovedPermanently
: public RGWOp
{
2182 const std::string location
;
2184 RGWMovedPermanently(const std::string
& location
)
2185 : location(location
) {
2188 int verify_permission() override
{
2192 void execute() override
{
2193 op_ret
= -ERR_PERMANENT_REDIRECT
;
2197 void send_response() override
{
2198 set_req_state_err(s
, op_ret
);
2200 dump_content_length(s
, 0);
2201 dump_redirect(s
, location
);
2202 end_header(s
, this);
2205 const string
name() override
{
2206 return "RGWMovedPermanently";
2210 return new RGWMovedPermanently(s
->info
.request_uri
+ '/');
2213 RGWOp
* RGWSwiftWebsiteHandler::get_ws_index_op()
2215 /* Retarget to get obj on requested index file. */
2216 if (! s
->object
.empty()) {
2217 s
->object
= s
->object
.name
+
2218 s
->bucket_info
.website_conf
.get_index_doc();
2220 s
->object
= s
->bucket_info
.website_conf
.get_index_doc();
2223 auto getop
= new RGWGetObj_ObjStore_SWIFT
;
2224 getop
->set_get_data(boost::algorithm::equals("GET", s
->info
.method
));
2229 RGWOp
* RGWSwiftWebsiteHandler::get_ws_listing_op()
2231 class RGWWebsiteListing
: public RGWListBucket_ObjStore_SWIFT
{
2232 const std::string prefix_override
;
2234 int get_params() override
{
2235 prefix
= prefix_override
;
2241 void send_response() override
{
2242 /* Generate the header now. */
2243 set_req_state_err(s
, op_ret
);
2245 dump_container_metadata(s
, bucket
, bucket_quota
,
2246 s
->bucket_info
.website_conf
);
2247 end_header(s
, this, "text/html");
2252 /* Now it's the time to start generating HTML bucket listing.
2253 * All the crazy stuff with crafting tags will be delegated to
2254 * RGWSwiftWebsiteListingFormatter. */
2255 std::stringstream ss
;
2256 RGWSwiftWebsiteListingFormatter
htmler(ss
, prefix
);
2258 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2259 htmler
.generate_header(s
->decoded_uri
,
2260 ws_conf
.listing_css_doc
);
2262 for (const auto& pair
: common_prefixes
) {
2263 std::string subdir_name
= pair
.first
;
2264 if (! subdir_name
.empty()) {
2265 /* To be compliant with Swift we need to remove the trailing
2267 subdir_name
.pop_back();
2270 htmler
.dump_subdir(subdir_name
);
2273 for (const rgw_bucket_dir_entry
& obj
: objs
) {
2274 if (! common_prefixes
.count(obj
.key
.name
+ '/')) {
2275 htmler
.dump_object(obj
);
2279 htmler
.generate_footer();
2280 dump_body(s
, ss
.str());
2283 /* Taking prefix_override by value to leverage std::string r-value ref
2284 * ctor and thus avoid extra memory copying/increasing ref counter. */
2285 RGWWebsiteListing(std::string prefix_override
)
2286 : prefix_override(std::move(prefix_override
)) {
2290 std::string prefix
= std::move(s
->object
.name
);
2291 s
->object
= rgw_obj_key();
2293 return new RGWWebsiteListing(std::move(prefix
));
2296 bool RGWSwiftWebsiteHandler::is_web_dir() const
2298 std::string subdir_name
= url_decode(s
->object
.name
);
2300 /* Remove character from the subdir name if it is "/". */
2301 if (subdir_name
.empty()) {
2303 } else if (subdir_name
.back() == '/') {
2304 subdir_name
.pop_back();
2307 rgw_obj
obj(s
->bucket
, std::move(subdir_name
));
2309 /* First, get attrset of the object we'll try to retrieve. */
2310 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2311 obj_ctx
.obj
.set_atomic(obj
);
2312 obj_ctx
.obj
.set_prefetch_data(obj
);
2314 RGWObjState
* state
= nullptr;
2315 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2319 /* A nonexistent object cannot be a considered as a marker representing
2320 * the emulation of catalog in FS hierarchy. */
2321 if (! state
->exists
) {
2325 /* Decode the content type. */
2326 std::string content_type
;
2327 get_contype_from_attrs(state
->attrset
, content_type
);
2329 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2330 const std::string subdir_marker
= ws_conf
.subdir_marker
.empty()
2331 ? "application/directory"
2332 : ws_conf
.subdir_marker
;
2333 return subdir_marker
== content_type
&& state
->size
<= 1;
2336 bool RGWSwiftWebsiteHandler::is_index_present(const std::string
& index
)
2338 rgw_obj
obj(s
->bucket
, index
);
2340 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2341 obj_ctx
.obj
.set_atomic(obj
);
2342 obj_ctx
.obj
.set_prefetch_data(obj
);
2344 RGWObjState
* state
= nullptr;
2345 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2349 /* A nonexistent object cannot be a considered as a viable index. We will
2350 * try to list the bucket or - if this is impossible - return an error. */
2351 return state
->exists
;
2354 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp
* op
, RGWOp
** new_op
)
2356 ldout(s
->cct
, 10) << "Starting retarget" << dendl
;
2357 RGWOp
* op_override
= nullptr;
2359 /* In Swift static web content is served if the request is anonymous or
2360 * has X-Web-Mode HTTP header specified to true. */
2361 if (can_be_website_req()) {
2362 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2363 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2365 if (s
->decoded_uri
.back() != '/') {
2366 op_override
= get_ws_redirect_op();
2367 } else if (! index
.empty() && is_index_present(index
)) {
2368 op_override
= get_ws_index_op();
2369 } else if (ws_conf
.listing_enabled
) {
2370 op_override
= get_ws_listing_op();
2375 handler
->put_op(op
);
2376 op_override
->init(store
, s
, handler
);
2378 *new_op
= op_override
;
2383 /* Return 404 Not Found is the request has web mode enforced but we static web
2384 * wasn't able to serve it accordingly. */
2385 return ! op_override
&& is_web_mode() ? -ENOENT
: 0;
2388 int RGWSwiftWebsiteHandler::retarget_object(RGWOp
* op
, RGWOp
** new_op
)
2390 ldout(s
->cct
, 10) << "Starting object retarget" << dendl
;
2391 RGWOp
* op_override
= nullptr;
2393 /* In Swift static web content is served if the request is anonymous or
2394 * has X-Web-Mode HTTP header specified to true. */
2395 if (can_be_website_req() && is_web_dir()) {
2396 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2397 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2399 if (s
->decoded_uri
.back() != '/') {
2400 op_override
= get_ws_redirect_op();
2401 } else if (! index
.empty() && is_index_present(index
)) {
2402 op_override
= get_ws_index_op();
2403 } else if (ws_conf
.listing_enabled
) {
2404 op_override
= get_ws_listing_op();
2407 /* A regular request or the specified object isn't a subdirectory marker.
2408 * We don't need any re-targeting. Error handling (like sending a custom
2409 * error page) will be performed by error_handler of the actual RGWOp. */
2414 handler
->put_op(op
);
2415 op_override
->init(store
, s
, handler
);
2417 *new_op
= op_override
;
2422 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2423 return ! op_override
? -ENOENT
: 0;
2427 RGWOp
*RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data
)
2430 return new RGWGetACLs_ObjStore_SWIFT
;
2434 return new RGWListBucket_ObjStore_SWIFT
;
2436 return new RGWStatBucket_ObjStore_SWIFT
;
2439 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_get()
2441 return get_obj_op(true);
2444 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_head()
2446 return get_obj_op(false);
2449 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_put()
2452 return new RGWPutACLs_ObjStore_SWIFT
;
2454 if(s
->info
.args
.exists("extract-archive")) {
2455 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2457 return new RGWCreateBucket_ObjStore_SWIFT
;
2460 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_delete()
2462 return new RGWDeleteBucket_ObjStore_SWIFT
;
2465 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_post()
2467 if (RGWFormPost::is_formpost_req(s
)) {
2468 return new RGWFormPost
;
2470 return new RGWPutMetadataBucket_ObjStore_SWIFT
;
2474 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_options()
2476 return new RGWOptionsCORS_ObjStore_SWIFT
;
2480 RGWOp
*RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data
)
2483 return new RGWGetACLs_ObjStore_SWIFT
;
2486 RGWGetObj_ObjStore_SWIFT
*get_obj_op
= new RGWGetObj_ObjStore_SWIFT
;
2487 get_obj_op
->set_get_data(get_data
);
2491 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_get()
2493 return get_obj_op(true);
2496 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_head()
2498 return get_obj_op(false);
2501 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_put()
2504 return new RGWPutACLs_ObjStore_SWIFT
;
2506 if(s
->info
.args
.exists("extract-archive")) {
2507 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2509 if (s
->init_state
.src_bucket
.empty())
2510 return new RGWPutObj_ObjStore_SWIFT
;
2512 return new RGWCopyObj_ObjStore_SWIFT
;
2515 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_delete()
2517 return new RGWDeleteObj_ObjStore_SWIFT
;
2520 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_post()
2522 if (RGWFormPost::is_formpost_req(s
)) {
2523 return new RGWFormPost
;
2525 return new RGWPutMetadataObject_ObjStore_SWIFT
;
2529 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_copy()
2531 return new RGWCopyObj_ObjStore_SWIFT
;
2534 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_options()
2536 return new RGWOptionsCORS_ObjStore_SWIFT
;
2540 int RGWHandler_REST_SWIFT::authorize()
2542 return rgw::auth::Strategy::apply(auth_strategy
, s
);
2545 int RGWHandler_REST_SWIFT::postauth_init()
2547 struct req_init_state
* t
= &s
->init_state
;
2549 /* XXX Stub this until Swift Auth sets account into URL. */
2550 s
->bucket_tenant
= s
->user
->user_id
.tenant
;
2551 s
->bucket_name
= t
->url_bucket
;
2553 dout(10) << "s->object=" <<
2554 (!s
->object
.empty() ? s
->object
: rgw_obj_key("<NULL>"))
2556 << rgw_make_bucket_entry_name(s
->bucket_tenant
, s
->bucket_name
)
2560 ret
= validate_tenant_name(s
->bucket_tenant
);
2563 ret
= validate_bucket_name(s
->bucket_name
);
2566 ret
= validate_object_name(s
->object
.name
);
2570 if (!t
->src_bucket
.empty()) {
2572 * We don't allow cross-tenant copy at present. It requires account
2573 * names in the URL for Swift.
2575 s
->src_tenant_name
= s
->user
->user_id
.tenant
;
2576 s
->src_bucket_name
= t
->src_bucket
;
2578 ret
= validate_bucket_name(s
->src_bucket_name
);
2582 ret
= validate_object_name(s
->src_object
.name
);
2591 int RGWHandler_REST_SWIFT::validate_bucket_name(const string
& bucket
)
2593 int ret
= RGWHandler_REST::validate_bucket_name(bucket
);
2597 int len
= bucket
.size();
2602 if (bucket
[0] == '.')
2603 return -ERR_INVALID_BUCKET_NAME
;
2605 if (check_utf8(bucket
.c_str(), len
))
2606 return -ERR_INVALID_UTF8
;
2608 const char *s
= bucket
.c_str();
2610 for (int i
= 0; i
< len
; ++i
, ++s
) {
2611 if (*(unsigned char *)s
== 0xff)
2612 return -ERR_INVALID_BUCKET_NAME
;
2618 static void next_tok(string
& str
, string
& tok
, char delim
)
2620 if (str
.size() == 0) {
2625 int pos
= str
.find(delim
);
2627 tok
= str
.substr(0, pos
);
2628 str
= str
.substr(pos
+ 1);
2634 int RGWHandler_REST_SWIFT::init_from_header(struct req_state
* const s
,
2635 const std::string
& frontend_prefix
)
2640 s
->prot_flags
|= RGW_REST_SWIFT
;
2642 char reqbuf
[frontend_prefix
.length() + s
->decoded_uri
.length() + 1];
2643 sprintf(reqbuf
, "%s%s", frontend_prefix
.c_str(), s
->decoded_uri
.c_str());
2644 const char *req_name
= reqbuf
;
2648 if (*req_name
== '?') {
2651 p
= s
->info
.request_params
.c_str();
2654 s
->info
.args
.set(p
);
2655 s
->info
.args
.parse();
2657 /* Skip the leading slash of URL hierarchy. */
2658 if (req_name
[0] != '/') {
2664 if ('\0' == req_name
[0]) {
2665 return g_conf
->rgw_swift_url_prefix
== "/" ? -ERR_BAD_URL
: 0;
2670 size_t pos
= req
.find('/');
2671 if (std::string::npos
!= pos
&& g_conf
->rgw_swift_url_prefix
!= "/") {
2672 bool cut_url
= g_conf
->rgw_swift_url_prefix
.length();
2673 first
= req
.substr(0, pos
);
2675 if (first
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2677 /* Rewind to the "v1/..." part. */
2678 next_tok(req
, first
, '/');
2681 } else if (req
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2682 s
->formatter
= new RGWFormatter_Plain
;
2683 return -ERR_BAD_URL
;
2688 std::string tenant_path
;
2689 if (! g_conf
->rgw_swift_tenant_name
.empty()) {
2690 tenant_path
= "/AUTH_";
2691 tenant_path
.append(g_conf
->rgw_swift_tenant_name
);
2694 /* verify that the request_uri conforms with what's expected */
2695 char buf
[g_conf
->rgw_swift_url_prefix
.length() + 16 + tenant_path
.length()];
2697 if (g_conf
->rgw_swift_url_prefix
== "/") {
2698 blen
= sprintf(buf
, "/v1%s", tenant_path
.c_str());
2700 blen
= sprintf(buf
, "/%s/v1%s",
2701 g_conf
->rgw_swift_url_prefix
.c_str(), tenant_path
.c_str());
2704 if (strncmp(reqbuf
, buf
, blen
) != 0) {
2708 int ret
= allocate_formatter(s
, RGW_FORMAT_PLAIN
, true);
2714 next_tok(req
, ver
, '/');
2716 if (!tenant_path
.empty() || g_conf
->rgw_swift_account_in_url
) {
2717 string account_name
;
2718 next_tok(req
, account_name
, '/');
2720 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2721 const vector
<string
> skipped_prefixes
= { "AUTH_", "KEY_" };
2723 for (const auto pfx
: skipped_prefixes
) {
2724 const size_t comp_len
= min(account_name
.length(), pfx
.length());
2725 if (account_name
.compare(0, comp_len
, pfx
) == 0) {
2726 /* Prefix is present. Drop it. */
2727 account_name
= account_name
.substr(comp_len
);
2732 if (account_name
.empty()) {
2733 return -ERR_PRECONDITION_FAILED
;
2735 s
->account_name
= account_name
;
2739 next_tok(req
, first
, '/');
2741 dout(10) << "ver=" << ver
<< " first=" << first
<< " req=" << req
<< dendl
;
2742 if (first
.size() == 0)
2745 s
->info
.effective_uri
= "/" + first
;
2747 // Save bucket to tide us over until token is parsed.
2748 s
->init_state
.url_bucket
= first
;
2752 rgw_obj_key(req
, s
->info
.env
->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2753 s
->info
.effective_uri
.append("/" + s
->object
.name
);
2759 int RGWHandler_REST_SWIFT::init(RGWRados
* store
, struct req_state
* s
,
2760 rgw::io::BasicClient
*cio
)
2762 struct req_init_state
*t
= &s
->init_state
;
2764 s
->dialect
= "swift";
2766 const char *copy_source
= s
->info
.env
->get("HTTP_X_COPY_FROM");
2768 bool result
= RGWCopyObj::parse_copy_location(copy_source
, t
->src_bucket
,
2771 return -ERR_BAD_URL
;
2774 if (s
->op
== OP_COPY
) {
2775 const char *req_dest
= s
->info
.env
->get("HTTP_DESTINATION");
2777 return -ERR_BAD_URL
;
2779 string dest_bucket_name
;
2780 rgw_obj_key dest_obj_key
;
2782 RGWCopyObj::parse_copy_location(req_dest
, dest_bucket_name
,
2785 return -ERR_BAD_URL
;
2787 string dest_object
= dest_obj_key
.name
;
2789 /* convert COPY operation into PUT */
2790 t
->src_bucket
= t
->url_bucket
;
2791 s
->src_object
= s
->object
;
2792 t
->url_bucket
= dest_bucket_name
;
2793 s
->object
= rgw_obj_key(dest_object
);
2797 return RGWHandler_REST::init(store
, s
, cio
);
2801 RGWRESTMgr_SWIFT::get_handler(struct req_state
* const s
,
2802 const rgw::auth::StrategyRegistry
& auth_registry
,
2803 const std::string
& frontend_prefix
)
2805 int ret
= RGWHandler_REST_SWIFT::init_from_header(s
, frontend_prefix
);
2807 ldout(s
->cct
, 10) << "init_from_header returned err=" << ret
<< dendl
;
2811 const auto& auth_strategy
= auth_registry
.get_swift();
2813 if (s
->init_state
.url_bucket
.empty()) {
2814 return new RGWHandler_REST_Service_SWIFT(auth_strategy
);
2817 if (s
->object
.empty()) {
2818 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy
);
2821 return new RGWHandler_REST_Obj_SWIFT(auth_strategy
);
2824 RGWHandler_REST
* RGWRESTMgr_SWIFT_Info::get_handler(
2825 struct req_state
* const s
,
2826 const rgw::auth::StrategyRegistry
& auth_registry
,
2827 const std::string
& frontend_prefix
)
2829 s
->prot_flags
|= RGW_REST_SWIFT
;
2830 const auto& auth_strategy
= auth_registry
.get_swift();
2831 return new RGWHandler_REST_SWIFT_Info(auth_strategy
);