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
;
59 int r
= s
->info
.args
.get_bool("stats", &stats
, &exists
);
73 static void dump_account_metadata(struct req_state
* const s
,
74 const uint32_t buckets_count
,
75 const uint64_t buckets_object_count
,
76 const uint64_t buckets_size
,
77 const uint64_t buckets_size_rounded
,
78 /* const */map
<string
, bufferlist
>& attrs
,
79 const RGWQuotaInfo
& quota
,
80 const RGWAccessControlPolicy_SWIFTAcct
&policy
)
82 /* Adding X-Timestamp to keep align with Swift API */
83 dump_header(s
, "X-Timestamp", ceph_clock_now());
85 dump_header(s
, "X-Account-Container-Count", buckets_count
);
86 dump_header(s
, "X-Account-Object-Count", buckets_object_count
);
87 dump_header(s
, "X-Account-Bytes-Used", buckets_size
);
88 dump_header(s
, "X-Account-Bytes-Used-Actual", buckets_size_rounded
);
90 /* Dump TempURL-related stuff */
91 if (s
->perm_mask
== RGW_PERM_FULL_CONTROL
) {
92 auto iter
= s
->user
->temp_url_keys
.find(0);
93 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
94 dump_header(s
, "X-Account-Meta-Temp-Url-Key", iter
->second
);
97 iter
= s
->user
->temp_url_keys
.find(1);
98 if (iter
!= std::end(s
->user
->temp_url_keys
) && ! iter
->second
.empty()) {
99 dump_header(s
, "X-Account-Meta-Temp-Url-Key-2", iter
->second
);
103 /* Dump quota headers. */
105 if (quota
.max_size
>= 0) {
106 dump_header(s
, "X-Account-Meta-Quota-Bytes", quota
.max_size
);
109 /* Limit on the number of objects in a given account is a RadosGW's
110 * extension. Swift's account quota WSGI filter doesn't support it. */
111 if (quota
.max_objects
>= 0) {
112 dump_header(s
, "X-Account-Meta-Quota-Count", quota
.max_objects
);
116 /* Dump user-defined metadata items and generic attrs. */
117 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
118 map
<string
, bufferlist
>::iterator iter
;
119 for (iter
= attrs
.lower_bound(RGW_ATTR_PREFIX
); iter
!= attrs
.end(); ++iter
) {
120 const char *name
= iter
->first
.c_str();
121 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
123 if (geniter
!= rgw_to_http_attrs
.end()) {
124 dump_header(s
, geniter
->second
, iter
->second
);
125 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
126 dump_header_prefixed(s
, "X-Account-Meta-",
127 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
132 /* Dump account ACLs */
134 policy
.to_str(acct_acl
);
135 if (acct_acl
.size()) {
136 dump_header(s
, "X-Account-Access-Control", std::move(acct_acl
));
140 void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets
)
143 set_req_state_err(s
, op_ret
);
144 } else if (!has_buckets
&& s
->format
== RGW_FORMAT_PLAIN
) {
145 op_ret
= STATUS_NO_CONTENT
;
146 set_req_state_err(s
, op_ret
);
149 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
150 /* Adding account stats in the header to keep align with Swift API */
151 dump_account_metadata(s
,
155 buckets_size_rounded
,
158 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
160 end_header(s
, NULL
, NULL
, NO_CONTENT_LENGTH
, true);
165 s
->formatter
->open_array_section_with_attrs("account",
166 FormatterAttrs("name", s
->user
->display_name
.c_str(), NULL
));
172 void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets
& buckets
)
178 /* Take care of the prefix parameter of Swift API. There is no business
179 * in applying the filter earlier as we really need to go through all
180 * entries regardless of it (the headers like X-Account-Container-Count
181 * aren't affected by specifying prefix). */
182 const std::map
<std::string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
183 for (auto iter
= m
.lower_bound(prefix
);
184 iter
!= m
.end() && boost::algorithm::starts_with(iter
->first
, prefix
);
186 const RGWBucketEnt
& obj
= iter
->second
;
188 s
->formatter
->open_object_section("container");
189 s
->formatter
->dump_string("name", obj
.bucket
.name
);
191 s
->formatter
->dump_int("count", obj
.count
);
192 s
->formatter
->dump_int("bytes", obj
.size
);
194 s
->formatter
->close_section();
195 if (! s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
196 rgw_flush_formatter(s
, s
->formatter
);
201 void RGWListBuckets_ObjStore_SWIFT::send_response_end()
204 s
->formatter
->close_section();
207 if (s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
208 /* Adding account stats in the header to keep align with Swift API */
209 dump_account_metadata(s
,
213 buckets_size_rounded
,
216 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
218 end_header(s
, NULL
, NULL
, s
->formatter
->get_len(), true);
221 if (sent_data
|| s
->cct
->_conf
->rgw_swift_enforce_content_length
) {
222 rgw_flush_formatter_and_reset(s
, s
->formatter
);
226 int RGWListBucket_ObjStore_SWIFT::get_params()
228 prefix
= s
->info
.args
.get("prefix");
229 marker
= s
->info
.args
.get("marker");
230 end_marker
= s
->info
.args
.get("end_marker");
231 max_keys
= s
->info
.args
.get("limit");
232 op_ret
= parse_max_keys();
236 if (max
> default_max
)
237 return -ERR_PRECONDITION_FAILED
;
239 delimiter
= s
->info
.args
.get("delimiter");
242 if (s
->info
.args
.exists("path")) { // should handle empty path
243 path_args
= s
->info
.args
.get("path");
244 if (!delimiter
.empty() || !prefix
.empty()) {
251 if (path
.size() && path
[path
.size() - 1] != '/')
254 int len
= prefix
.size();
255 int delim_size
= delimiter
.size();
257 if (len
>= delim_size
) {
258 if (prefix
.substr(len
- delim_size
).compare(delimiter
) != 0)
259 prefix
.append(delimiter
);
266 static void dump_container_metadata(struct req_state
*,
269 const RGWBucketWebsiteConf
&);
271 void RGWListBucket_ObjStore_SWIFT::send_response()
273 vector
<rgw_bucket_dir_entry
>::iterator iter
= objs
.begin();
274 map
<string
, bool>::iterator pref_iter
= common_prefixes
.begin();
277 dump_container_metadata(s
, bucket
, bucket_quota
,
278 s
->bucket_info
.website_conf
);
280 s
->formatter
->open_array_section_with_attrs("container", FormatterAttrs("name", s
->bucket
.name
.c_str(), NULL
));
282 while (iter
!= objs
.end() || pref_iter
!= common_prefixes
.end()) {
283 bool do_pref
= false;
284 bool do_objs
= false;
286 if (iter
!= objs
.end()) {
289 if (pref_iter
== common_prefixes
.end())
291 else if (iter
== objs
.end())
293 else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) == 0) {
296 } else if (!key
.empty() && key
.name
.compare(pref_iter
->first
) <= 0)
301 if (do_objs
&& (marker
.empty() || marker
< key
)) {
302 if (key
.name
.compare(path
) == 0)
305 s
->formatter
->open_object_section("object");
306 s
->formatter
->dump_string("name", key
.name
);
307 s
->formatter
->dump_string("hash", iter
->meta
.etag
);
308 s
->formatter
->dump_int("bytes", iter
->meta
.accounted_size
);
309 if (!iter
->meta
.user_data
.empty())
310 s
->formatter
->dump_string("user_custom_data", iter
->meta
.user_data
);
311 string single_content_type
= iter
->meta
.content_type
;
312 if (iter
->meta
.content_type
.size()) {
313 // content type might hold multiple values, just dump the last one
314 ssize_t pos
= iter
->meta
.content_type
.rfind(',');
317 while (single_content_type
[pos
] == ' ')
319 single_content_type
= single_content_type
.substr(pos
);
321 s
->formatter
->dump_string("content_type", single_content_type
);
323 dump_time(s
, "last_modified", &iter
->meta
.mtime
);
324 s
->formatter
->close_section();
327 if (do_pref
&& (marker
.empty() || pref_iter
->first
.compare(marker
.name
) > 0)) {
328 const string
& name
= pref_iter
->first
;
329 if (name
.compare(delimiter
) == 0)
332 s
->formatter
->open_object_section_with_attrs("subdir", FormatterAttrs("name", name
.c_str(), NULL
));
334 /* swift is a bit inconsistent here */
337 s
->formatter
->dump_string("name", name
);
340 s
->formatter
->dump_string("subdir", name
);
342 s
->formatter
->close_section();
351 s
->formatter
->close_section();
353 int64_t content_len
= 0;
355 content_len
= s
->formatter
->get_len();
356 if (content_len
== 0) {
357 op_ret
= STATUS_NO_CONTENT
;
359 } else if (op_ret
> 0) {
363 set_req_state_err(s
, op_ret
);
365 end_header(s
, this, NULL
, content_len
);
370 rgw_flush_formatter_and_reset(s
, s
->formatter
);
373 static void dump_container_metadata(struct req_state
*s
,
374 const RGWBucketEnt
& bucket
,
375 const RGWQuotaInfo
& quota
,
376 const RGWBucketWebsiteConf
& ws_conf
)
378 /* Adding X-Timestamp to keep align with Swift API */
379 dump_header(s
, "X-Timestamp", utime_t(s
->bucket_info
.creation_time
));
381 dump_header(s
, "X-Container-Object-Count", bucket
.count
);
382 dump_header(s
, "X-Container-Bytes-Used", bucket
.size
);
383 dump_header(s
, "X-Container-Bytes-Used-Actual", bucket
.size_rounded
);
385 if (s
->object
.empty()) {
386 auto swift_policy
= static_cast<RGWAccessControlPolicy_SWIFT
*>(s
->bucket_acl
);
387 std::string read_acl
, write_acl
;
388 swift_policy
->to_str(read_acl
, write_acl
);
390 if (read_acl
.size()) {
391 dump_header(s
, "X-Container-Read", read_acl
);
393 if (write_acl
.size()) {
394 dump_header(s
, "X-Container-Write", write_acl
);
396 if (!s
->bucket_info
.placement_rule
.empty()) {
397 dump_header(s
, "X-Storage-Policy", s
->bucket_info
.placement_rule
);
400 /* Dump user-defined metadata items and generic attrs. */
401 const size_t PREFIX_LEN
= sizeof(RGW_ATTR_META_PREFIX
) - 1;
402 map
<string
, bufferlist
>::iterator iter
;
403 for (iter
= s
->bucket_attrs
.lower_bound(RGW_ATTR_PREFIX
);
404 iter
!= s
->bucket_attrs
.end();
406 const char *name
= iter
->first
.c_str();
407 map
<string
, string
>::const_iterator geniter
= rgw_to_http_attrs
.find(name
);
409 if (geniter
!= rgw_to_http_attrs
.end()) {
410 dump_header(s
, geniter
->second
, iter
->second
);
411 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
, PREFIX_LEN
) == 0) {
412 dump_header_prefixed(s
, "X-Container-Meta-",
413 camelcase_dash_http_attr(name
+ PREFIX_LEN
),
419 /* Dump container versioning info. */
420 if (! s
->bucket_info
.swift_ver_location
.empty()) {
421 dump_header(s
, "X-Versions-Location",
422 url_encode(s
->bucket_info
.swift_ver_location
));
425 /* Dump quota headers. */
427 if (quota
.max_size
>= 0) {
428 dump_header(s
, "X-Container-Meta-Quota-Bytes", quota
.max_size
);
431 if (quota
.max_objects
>= 0) {
432 dump_header(s
, "X-Container-Meta-Quota-Count", quota
.max_objects
);
436 /* Dump Static Website headers. */
437 if (! ws_conf
.index_doc_suffix
.empty()) {
438 dump_header(s
, "X-Container-Meta-Web-Index", ws_conf
.index_doc_suffix
);
441 if (! ws_conf
.error_doc
.empty()) {
442 dump_header(s
, "X-Container-Meta-Web-Error", ws_conf
.error_doc
);
445 if (! ws_conf
.subdir_marker
.empty()) {
446 dump_header(s
, "X-Container-Meta-Web-Directory-Type",
447 ws_conf
.subdir_marker
);
450 if (! ws_conf
.listing_css_doc
.empty()) {
451 dump_header(s
, "X-Container-Meta-Web-Listings-CSS",
452 ws_conf
.listing_css_doc
);
455 if (ws_conf
.listing_enabled
) {
456 dump_header(s
, "X-Container-Meta-Web-Listings", "true");
460 void RGWStatAccount_ObjStore_SWIFT::execute()
462 RGWStatAccount_ObjStore::execute();
463 op_ret
= rgw_get_user_attrs_by_uid(store
, s
->user
->user_id
, attrs
);
466 void RGWStatAccount_ObjStore_SWIFT::send_response()
469 op_ret
= STATUS_NO_CONTENT
;
470 dump_account_metadata(s
,
474 buckets_size_rounded
,
477 static_cast<RGWAccessControlPolicy_SWIFTAcct
&>(*s
->user_acl
));
480 set_req_state_err(s
, op_ret
);
483 end_header(s
, NULL
, NULL
, 0, true);
488 void RGWStatBucket_ObjStore_SWIFT::send_response()
491 op_ret
= STATUS_NO_CONTENT
;
492 dump_container_metadata(s
, bucket
, bucket_quota
,
493 s
->bucket_info
.website_conf
);
496 set_req_state_err(s
, op_ret
);
499 end_header(s
, this, NULL
, 0, true);
503 static int get_swift_container_settings(req_state
* const s
,
504 RGWRados
* const store
,
505 RGWAccessControlPolicy
* const policy
,
506 bool * const has_policy
,
508 RGWCORSConfiguration
* const cors_config
,
509 bool * const has_cors
)
511 string read_list
, write_list
;
513 const char * const read_attr
= s
->info
.env
->get("HTTP_X_CONTAINER_READ");
515 read_list
= read_attr
;
517 const char * const write_attr
= s
->info
.env
->get("HTTP_X_CONTAINER_WRITE");
519 write_list
= write_attr
;
524 if (read_attr
|| write_attr
) {
525 RGWAccessControlPolicy_SWIFT
swift_policy(s
->cct
);
526 const auto r
= swift_policy
.create(store
,
528 s
->user
->display_name
,
536 *policy
= swift_policy
;
542 /*Check and update CORS configuration*/
543 const char *allow_origins
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
544 const char *allow_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
545 const char *expose_headers
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
546 const char *max_age
= s
->info
.env
->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
548 RGWCORSConfiguration_SWIFT
*swift_cors
= new RGWCORSConfiguration_SWIFT
;
549 int r
= swift_cors
->create_update(allow_origins
, allow_headers
, expose_headers
, max_age
);
551 dout(0) << "Error creating/updating the cors configuration" << dendl
;
556 *cors_config
= *swift_cors
;
564 #define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
565 #define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
566 #define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
567 #define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
569 static void get_rmattrs_from_headers(const req_state
* const s
,
570 const char * const put_prefix
,
571 const char * const del_prefix
,
572 set
<string
>& rmattr_names
)
574 const size_t put_prefix_len
= strlen(put_prefix
);
575 const size_t del_prefix_len
= strlen(del_prefix
);
577 for (const auto& kv
: s
->info
.env
->get_map()) {
578 size_t prefix_len
= 0;
579 const char * const p
= kv
.first
.c_str();
581 if (strncasecmp(p
, del_prefix
, del_prefix_len
) == 0) {
582 /* Explicitly requested removal. */
583 prefix_len
= del_prefix_len
;
584 } else if ((strncasecmp(p
, put_prefix
, put_prefix_len
) == 0)
585 && kv
.second
.empty()) {
586 /* Removal requested by putting an empty value. */
587 prefix_len
= put_prefix_len
;
590 if (prefix_len
> 0) {
591 string
name(RGW_ATTR_META_PREFIX
);
592 name
.append(lowercase_dash_http_attr(p
+ prefix_len
));
593 rmattr_names
.insert(name
);
598 static int get_swift_versioning_settings(
600 boost::optional
<std::string
>& swift_ver_location
)
602 /* Removing the Swift's versions location has lower priority than setting
603 * a new one. That's the reason why we're handling it first. */
604 const std::string vlocdel
=
605 s
->info
.env
->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
606 if (vlocdel
.size()) {
607 swift_ver_location
= boost::in_place(std::string());
610 if (s
->info
.env
->exists("HTTP_X_VERSIONS_LOCATION")) {
611 /* If the Swift's versioning is globally disabled but someone wants to
612 * enable it for a given container, new version of Swift will generate
613 * the precondition failed error. */
614 if (! s
->cct
->_conf
->rgw_swift_versioning_enabled
) {
615 return -ERR_PRECONDITION_FAILED
;
618 swift_ver_location
= s
->info
.env
->get("HTTP_X_VERSIONS_LOCATION", "");
624 int RGWCreateBucket_ObjStore_SWIFT::get_params()
627 uint32_t policy_rw_mask
= 0;
629 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
630 &policy_rw_mask
, &cors_config
, &has_cors
);
636 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
639 location_constraint
= store
->get_zonegroup().api_name
;
640 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
,
641 CONT_REMOVE_ATTR_PREFIX
, rmattr_names
);
642 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
644 return get_swift_versioning_settings(s
, swift_ver_location
);
647 void RGWCreateBucket_ObjStore_SWIFT::send_response()
650 op_ret
= STATUS_CREATED
;
651 else if (op_ret
== -ERR_BUCKET_EXISTS
)
652 op_ret
= STATUS_ACCEPTED
;
653 set_req_state_err(s
, op_ret
);
655 /* Propose ending HTTP header with 0 Content-Length header. */
656 end_header(s
, NULL
, NULL
, 0);
657 rgw_flush_formatter_and_reset(s
, s
->formatter
);
660 void RGWDeleteBucket_ObjStore_SWIFT::send_response()
664 r
= STATUS_NO_CONTENT
;
666 set_req_state_err(s
, r
);
668 end_header(s
, this, NULL
, 0);
669 rgw_flush_formatter_and_reset(s
, s
->formatter
);
672 static int get_delete_at_param(req_state
*s
, boost::optional
<real_time
> &delete_at
)
674 /* Handle Swift object expiration. */
675 real_time delat_proposal
;
676 string x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AFTER", "");
678 if (x_delete
.empty()) {
679 x_delete
= s
->info
.env
->get("HTTP_X_DELETE_AT", "");
681 /* X-Delete-After HTTP is present. It means we need add its value
682 * to the current time. */
683 delat_proposal
= real_clock::now();
686 if (x_delete
.empty()) {
687 delete_at
= boost::none
;
688 if (s
->info
.env
->exists("HTTP_X_REMOVE_DELETE_AT")) {
689 delete_at
= boost::in_place(real_time());
694 long ts
= strict_strtoll(x_delete
.c_str(), 10, &err
);
700 delat_proposal
+= make_timespan(ts
);
701 if (delat_proposal
< real_clock::now()) {
705 delete_at
= delat_proposal
;
710 int RGWPutObj_ObjStore_SWIFT::verify_permission()
712 op_ret
= RGWPutObj_ObjStore::verify_permission();
714 /* We have to differentiate error codes depending on whether user is
715 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
716 * (403 Forbidden). */
717 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
724 int RGWPutObj_ObjStore_SWIFT::get_params()
726 if (s
->has_bad_meta
) {
731 const char *encoding
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
732 if (!encoding
|| strcmp(encoding
, "chunked") != 0) {
733 ldout(s
->cct
, 20) << "neither length nor chunked encoding" << dendl
;
734 return -ERR_LENGTH_REQUIRED
;
737 chunked_upload
= true;
740 supplied_etag
= s
->info
.env
->get("HTTP_ETAG");
742 if (!s
->generic_attrs
.count(RGW_ATTR_CONTENT_TYPE
)) {
743 ldout(s
->cct
, 5) << "content type wasn't provided, trying to guess" << dendl
;
744 const char *suffix
= strrchr(s
->object
.name
.c_str(), '.');
748 string
suffix_str(suffix
);
749 const char *mime
= rgw_find_mime_by_ext(suffix_str
);
751 s
->generic_attrs
[RGW_ATTR_CONTENT_TYPE
] = mime
;
757 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
759 int r
= get_delete_at_param(s
, delete_at
);
761 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
765 if (!s
->cct
->_conf
->rgw_swift_custom_header
.empty()) {
766 string custom_header
= s
->cct
->_conf
->rgw_swift_custom_header
;
767 if (s
->info
.env
->exists(custom_header
.c_str())) {
768 user_data
= s
->info
.env
->get(custom_header
.c_str());
772 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
774 string multipart_manifest
= s
->info
.args
.get("multipart-manifest", &exists
);
776 if (multipart_manifest
!= "put") {
777 ldout(s
->cct
, 5) << "invalid multipart-manifest http param: " << multipart_manifest
<< dendl
;
781 #define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
782 uint64_t max_len
= s
->cct
->_conf
->rgw_max_slo_entries
* MAX_SLO_ENTRY_SIZE
;
784 slo_info
= new RGWSLOInfo
;
786 int r
= rgw_rest_get_json_input_keep_data(s
->cct
, s
, slo_info
->entries
, max_len
, &slo_info
->raw_data
, &slo_info
->raw_data_len
);
788 ldout(s
->cct
, 5) << "failed to read input for slo r=" << r
<< dendl
;
792 if ((int64_t)slo_info
->entries
.size() > s
->cct
->_conf
->rgw_max_slo_entries
) {
793 ldout(s
->cct
, 5) << "too many entries in slo request: " << slo_info
->entries
.size() << dendl
;
798 uint64_t total_size
= 0;
799 for (const auto& entry
: slo_info
->entries
) {
800 etag_sum
.Update((const byte
*)entry
.etag
.c_str(),
801 entry
.etag
.length());
802 total_size
+= entry
.size_bytes
;
804 ldout(s
->cct
, 20) << "slo_part: " << entry
.path
805 << " size=" << entry
.size_bytes
806 << " etag=" << entry
.etag
809 complete_etag(etag_sum
, &lo_etag
);
810 slo_info
->total_size
= total_size
;
812 ofs
= slo_info
->raw_data_len
;
815 return RGWPutObj_ObjStore::get_params();
818 void RGWPutObj_ObjStore_SWIFT::send_response()
821 op_ret
= STATUS_CREATED
;
824 if (! lo_etag
.empty()) {
825 /* Static Large Object of Swift API has two etags represented by
827 * - etag - for the manifest itself (it will be stored in xattrs),
828 * - lo_etag - for the content composited from SLO's segments.
829 * The value is calculated basing on segments' etags.
830 * In response for PUT request we have to expose the second one.
831 * The first one may be obtained by GET with "multipart-manifest=get"
832 * in query string on a given SLO. */
833 dump_etag(s
, lo_etag
, true /* quoted */);
838 dump_last_modified(s
, mtime
);
839 set_req_state_err(s
, op_ret
);
842 rgw_flush_formatter_and_reset(s
, s
->formatter
);
845 static int get_swift_account_settings(req_state
* const s
,
846 RGWRados
* const store
,
847 RGWAccessControlPolicy_SWIFTAcct
* const policy
,
848 bool * const has_policy
)
852 const char * const acl_attr
= s
->info
.env
->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
854 RGWAccessControlPolicy_SWIFTAcct
swift_acct_policy(s
->cct
);
855 const bool r
= swift_acct_policy
.create(store
,
857 s
->user
->display_name
,
863 *policy
= swift_acct_policy
;
870 int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
872 if (s
->has_bad_meta
) {
876 int ret
= get_swift_account_settings(s
,
878 // FIXME: we need to carry unique_ptr in generic class
879 // and allocate appropriate ACL class in the ctor
880 static_cast<RGWAccessControlPolicy_SWIFTAcct
*>(&policy
),
886 get_rmattrs_from_headers(s
, ACCT_PUT_ATTR_PREFIX
, ACCT_REMOVE_ATTR_PREFIX
,
891 void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
894 op_ret
= STATUS_NO_CONTENT
;
896 set_req_state_err(s
, op_ret
);
899 rgw_flush_formatter_and_reset(s
, s
->formatter
);
902 int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
904 if (s
->has_bad_meta
) {
908 int r
= get_swift_container_settings(s
, store
, &policy
, &has_policy
,
909 &policy_rw_mask
, &cors_config
, &has_cors
);
914 get_rmattrs_from_headers(s
, CONT_PUT_ATTR_PREFIX
, CONT_REMOVE_ATTR_PREFIX
,
916 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
918 return get_swift_versioning_settings(s
, swift_ver_location
);
921 void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
923 if (!op_ret
&& (op_ret
!= -EINVAL
)) {
924 op_ret
= STATUS_NO_CONTENT
;
926 set_req_state_err(s
, op_ret
);
929 rgw_flush_formatter_and_reset(s
, s
->formatter
);
932 int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
934 if (s
->has_bad_meta
) {
938 /* Handle Swift object expiration. */
939 int r
= get_delete_at_param(s
, delete_at
);
941 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
945 placement_rule
= s
->info
.env
->get("HTTP_X_STORAGE_POLICY", "");
946 dlo_manifest
= s
->info
.env
->get("HTTP_X_OBJECT_MANIFEST");
951 void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
954 op_ret
= STATUS_ACCEPTED
;
956 set_req_state_err(s
, op_ret
);
958 dump_content_length(s
, 0);
962 rgw_flush_formatter_and_reset(s
, s
->formatter
);
965 static void bulkdelete_respond(const unsigned num_deleted
,
966 const unsigned int num_unfound
,
967 const std::list
<RGWBulkDelete::fail_desc_t
>& failures
,
968 const int prot_flags
, /* in */
969 ceph::Formatter
& formatter
) /* out */
971 formatter
.open_object_section("delete");
976 if (!failures
.empty()) {
977 int reason
= ERR_INVALID_REQUEST
;
978 for (const auto fail_desc
: failures
) {
979 if (-ENOENT
!= fail_desc
.err
&& -EACCES
!= fail_desc
.err
) {
980 reason
= fail_desc
.err
;
984 set_req_state_err(err
, reason
, prot_flags
);
985 dump_errno(err
, resp_status
);
986 } else if (0 == num_deleted
&& 0 == num_unfound
) {
987 /* 400 Bad Request */
988 dump_errno(400, resp_status
);
989 resp_body
= "Invalid bulk delete.";
992 dump_errno(200, resp_status
);
995 encode_json("Number Deleted", num_deleted
, &formatter
);
996 encode_json("Number Not Found", num_unfound
, &formatter
);
997 encode_json("Response Body", resp_body
, &formatter
);
998 encode_json("Response Status", resp_status
, &formatter
);
1000 formatter
.open_array_section("Errors");
1001 for (const auto fail_desc
: failures
) {
1002 formatter
.open_array_section("object");
1004 stringstream ss_name
;
1005 ss_name
<< fail_desc
.path
;
1006 encode_json("Name", ss_name
.str(), &formatter
);
1009 set_req_state_err(err
, fail_desc
.err
, prot_flags
);
1011 dump_errno(err
, status
);
1012 encode_json("Status", status
, &formatter
);
1013 formatter
.close_section();
1015 formatter
.close_section();
1017 formatter
.close_section();
1020 int RGWDeleteObj_ObjStore_SWIFT::verify_permission()
1022 op_ret
= RGWDeleteObj_ObjStore::verify_permission();
1024 /* We have to differentiate error codes depending on whether user is
1025 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1026 * (403 Forbidden). */
1027 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1034 int RGWDeleteObj_ObjStore_SWIFT::get_params()
1036 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1037 multipart_delete
= (mm
.compare("delete") == 0);
1039 return RGWDeleteObj_ObjStore::get_params();
1042 void RGWDeleteObj_ObjStore_SWIFT::send_response()
1046 if (multipart_delete
) {
1049 r
= STATUS_NO_CONTENT
;
1052 set_req_state_err(s
, r
);
1055 if (multipart_delete
) {
1056 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1057 CHUNKED_TRANSFER_ENCODING
);
1060 bulkdelete_respond(deleter
->get_num_deleted(),
1061 deleter
->get_num_unfound(),
1062 deleter
->get_failures(),
1065 } else if (-ENOENT
== op_ret
) {
1066 bulkdelete_respond(0, 1, {}, s
->prot_flags
, *s
->formatter
);
1068 RGWBulkDelete::acct_path_t path
;
1069 path
.bucket_name
= s
->bucket_name
;
1070 path
.obj_key
= s
->object
;
1072 RGWBulkDelete::fail_desc_t fail_desc
;
1073 fail_desc
.err
= op_ret
;
1074 fail_desc
.path
= path
;
1076 bulkdelete_respond(0, 0, { fail_desc
}, s
->prot_flags
, *s
->formatter
);
1079 end_header(s
, this);
1082 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1086 static void get_contype_from_attrs(map
<string
, bufferlist
>& attrs
,
1087 string
& content_type
)
1089 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_CONTENT_TYPE
);
1090 if (iter
!= attrs
.end()) {
1091 content_type
= iter
->second
.c_str();
1095 static void dump_object_metadata(struct req_state
* const s
,
1096 map
<string
, bufferlist
> attrs
)
1098 map
<string
, string
> response_attrs
;
1100 for (auto kv
: attrs
) {
1101 const char * name
= kv
.first
.c_str();
1102 const auto aiter
= rgw_to_http_attrs
.find(name
);
1104 if (aiter
!= std::end(rgw_to_http_attrs
)) {
1105 response_attrs
[aiter
->second
] = kv
.second
.c_str();
1106 } else if (strcmp(name
, RGW_ATTR_SLO_UINDICATOR
) == 0) {
1107 // this attr has an extra length prefix from ::encode() in prior versions
1108 dump_header(s
, "X-Object-Meta-Static-Large-Object", "True");
1109 } else if (strncmp(name
, RGW_ATTR_META_PREFIX
,
1110 sizeof(RGW_ATTR_META_PREFIX
)-1) == 0) {
1111 name
+= sizeof(RGW_ATTR_META_PREFIX
) - 1;
1112 dump_header_prefixed(s
, "X-Object-Meta-",
1113 camelcase_dash_http_attr(name
), kv
.second
);
1117 /* Handle override and fallback for Content-Disposition HTTP header.
1118 * At the moment this will be used only by TempURL of the Swift API. */
1119 const auto cditer
= rgw_to_http_attrs
.find(RGW_ATTR_CONTENT_DISP
);
1120 if (cditer
!= std::end(rgw_to_http_attrs
)) {
1121 const auto& name
= cditer
->second
;
1123 if (!s
->content_disp
.override
.empty()) {
1124 response_attrs
[name
] = s
->content_disp
.override
;
1125 } else if (!s
->content_disp
.fallback
.empty()
1126 && response_attrs
.find(name
) == std::end(response_attrs
)) {
1127 response_attrs
[name
] = s
->content_disp
.fallback
;
1131 for (const auto kv
: response_attrs
) {
1132 dump_header(s
, kv
.first
, kv
.second
);
1135 const auto iter
= attrs
.find(RGW_ATTR_DELETE_AT
);
1136 if (iter
!= std::end(attrs
)) {
1139 ::decode(delete_at
, iter
->second
);
1140 if (!delete_at
.is_zero()) {
1141 dump_header(s
, "X-Delete-At", delete_at
.sec());
1143 } catch (buffer::error
& err
) {
1144 ldout(s
->cct
, 0) << "ERROR: cannot decode object's " RGW_ATTR_DELETE_AT
1151 int RGWCopyObj_ObjStore_SWIFT::init_dest_policy()
1153 dest_policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1158 int RGWCopyObj_ObjStore_SWIFT::get_params()
1160 if_mod
= s
->info
.env
->get("HTTP_IF_MODIFIED_SINCE");
1161 if_unmod
= s
->info
.env
->get("HTTP_IF_UNMODIFIED_SINCE");
1162 if_match
= s
->info
.env
->get("HTTP_COPY_IF_MATCH");
1163 if_nomatch
= s
->info
.env
->get("HTTP_COPY_IF_NONE_MATCH");
1165 src_tenant_name
= s
->src_tenant_name
;
1166 src_bucket_name
= s
->src_bucket_name
;
1167 src_object
= s
->src_object
;
1168 dest_tenant_name
= s
->bucket_tenant
;
1169 dest_bucket_name
= s
->bucket_name
;
1170 dest_object
= s
->object
.name
;
1172 const char * const fresh_meta
= s
->info
.env
->get("HTTP_X_FRESH_METADATA");
1173 if (fresh_meta
&& strcasecmp(fresh_meta
, "TRUE") == 0) {
1174 attrs_mod
= RGWRados::ATTRSMOD_REPLACE
;
1176 attrs_mod
= RGWRados::ATTRSMOD_MERGE
;
1179 int r
= get_delete_at_param(s
, delete_at
);
1181 ldout(s
->cct
, 5) << "ERROR: failed to get Delete-At param" << dendl
;
1188 void RGWCopyObj_ObjStore_SWIFT::send_partial_response(off_t ofs
)
1190 if (! sent_header
) {
1192 op_ret
= STATUS_CREATED
;
1193 set_req_state_err(s
, op_ret
);
1195 end_header(s
, this);
1197 /* Send progress information. Note that this diverge from the original swift
1198 * spec. We do this in order to keep connection alive.
1201 s
->formatter
->open_array_section("progress");
1205 s
->formatter
->dump_int("ofs", (uint64_t)ofs
);
1207 rgw_flush_formatter(s
, s
->formatter
);
1210 void RGWCopyObj_ObjStore_SWIFT::dump_copy_info()
1212 /* Dump X-Copied-From. */
1213 dump_header(s
, "X-Copied-From", url_encode(src_bucket
.name
) +
1214 "/" + url_encode(src_object
.name
));
1216 /* Dump X-Copied-From-Account. */
1218 dump_header(s
, "X-Copied-From-Account", url_encode(s
->user
->user_id
.id
));
1220 /* Dump X-Copied-From-Last-Modified. */
1221 dump_time_header(s
, "X-Copied-From-Last-Modified", src_mtime
);
1224 void RGWCopyObj_ObjStore_SWIFT::send_response()
1226 if (! sent_header
) {
1227 string content_type
;
1229 op_ret
= STATUS_CREATED
;
1230 set_req_state_err(s
, op_ret
);
1233 dump_last_modified(s
, mtime
);
1235 get_contype_from_attrs(attrs
, content_type
);
1236 dump_object_metadata(s
, attrs
);
1237 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1238 : "binary/octet-stream");
1240 s
->formatter
->close_section();
1241 rgw_flush_formatter(s
, s
->formatter
);
1245 int RGWGetObj_ObjStore_SWIFT::verify_permission()
1247 op_ret
= RGWGetObj_ObjStore::verify_permission();
1249 /* We have to differentiate error codes depending on whether user is
1250 * anonymous (401 Unauthorized) or he doesn't have necessary permissions
1251 * (403 Forbidden). */
1252 if (s
->auth
.identity
->is_anonymous() && op_ret
== -EACCES
) {
1259 int RGWGetObj_ObjStore_SWIFT::get_params()
1261 const string
& mm
= s
->info
.args
.get("multipart-manifest");
1262 skip_manifest
= (mm
.compare("get") == 0);
1264 return RGWGetObj_ObjStore::get_params();
1267 int RGWGetObj_ObjStore_SWIFT::send_response_data_error()
1269 std::string error_content
;
1270 op_ret
= error_handler(op_ret
, &error_content
);
1272 /* The error handler has taken care of the error. */
1276 bufferlist error_bl
;
1277 error_bl
.append(error_content
);
1278 return send_response_data(error_bl
, 0, error_bl
.length());
1281 int RGWGetObj_ObjStore_SWIFT::send_response_data(bufferlist
& bl
,
1285 string content_type
;
1291 if (custom_http_ret
) {
1292 set_req_state_err(s
, 0);
1293 dump_errno(s
, custom_http_ret
);
1295 set_req_state_err(s
, (partial_content
&& !op_ret
) ? STATUS_PARTIAL_CONTENT
1300 end_header(s
, NULL
);
1306 dump_range(s
, ofs
, end
, s
->obj_size
);
1310 end_header(s
, NULL
);
1314 dump_content_length(s
, total_len
);
1315 dump_last_modified(s
, lastmod
);
1316 dump_header(s
, "X-Timestamp", utime_t(lastmod
));
1318 dump_header(s
, "X-Static-Large-Object", "True");
1322 if (! lo_etag
.empty()) {
1323 dump_etag(s
, lo_etag
, true /* quoted */);
1325 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
1326 if (iter
!= attrs
.end()) {
1327 dump_etag(s
, iter
->second
);
1331 get_contype_from_attrs(attrs
, content_type
);
1332 dump_object_metadata(s
, attrs
);
1335 end_header(s
, this, !content_type
.empty() ? content_type
.c_str()
1336 : "binary/octet-stream");
1341 if (get_data
&& !op_ret
) {
1342 const auto r
= dump_body(s
, bl
.c_str() + bl_ofs
, bl_len
);
1347 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1352 void RGWOptionsCORS_ObjStore_SWIFT::send_response()
1354 string hdrs
, exp_hdrs
;
1355 uint32_t max_age
= CORS_MAX_AGE_INVALID
;
1356 /*EACCES means, there is no CORS registered yet for the bucket
1357 *ENOENT means, there is no match of the Origin in the list of CORSRule
1359 if (op_ret
== -ENOENT
)
1362 set_req_state_err(s
, op_ret
);
1364 end_header(s
, NULL
);
1367 get_response_params(hdrs
, exp_hdrs
, &max_age
);
1369 dump_access_control(s
, origin
, req_meth
, hdrs
.c_str(), exp_hdrs
.c_str(),
1371 end_header(s
, NULL
);
1374 int RGWBulkDelete_ObjStore_SWIFT::get_data(
1375 list
<RGWBulkDelete::acct_path_t
>& items
, bool * const is_truncated
)
1377 constexpr size_t MAX_LINE_SIZE
= 2048;
1379 RGWClientIOStreamBuf
ciosb(static_cast<RGWRestfulIO
&>(*(s
->cio
)),
1380 size_t(s
->cct
->_conf
->rgw_max_chunk_size
));
1381 istream
cioin(&ciosb
);
1383 char buf
[MAX_LINE_SIZE
];
1384 while (cioin
.getline(buf
, sizeof(buf
))) {
1385 string
path_str(buf
);
1387 ldout(s
->cct
, 20) << "extracted Bulk Delete entry: " << path_str
<< dendl
;
1389 RGWBulkDelete::acct_path_t path
;
1391 /* We need to skip all slashes at the beginning in order to preserve
1392 * compliance with Swift. */
1393 const size_t start_pos
= path_str
.find_first_not_of('/');
1395 if (string::npos
!= start_pos
) {
1396 /* Seperator is the first slash after the leading ones. */
1397 const size_t sep_pos
= path_str
.find('/', start_pos
);
1399 if (string::npos
!= sep_pos
) {
1400 path
.bucket_name
= url_decode(path_str
.substr(start_pos
,
1401 sep_pos
- start_pos
));
1402 path
.obj_key
= url_decode(path_str
.substr(sep_pos
+ 1));
1404 /* It's guaranteed here that bucket name is at least one character
1405 * long and is different than slash. */
1406 path
.bucket_name
= url_decode(path_str
.substr(start_pos
));
1409 items
.push_back(path
);
1412 if (items
.size() == MAX_CHUNK_ENTRIES
) {
1413 *is_truncated
= true;
1418 *is_truncated
= false;
1422 void RGWBulkDelete_ObjStore_SWIFT::send_response()
1424 set_req_state_err(s
, op_ret
);
1426 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1427 CHUNKED_TRANSFER_ENCODING
);
1429 bulkdelete_respond(deleter
->get_num_deleted(),
1430 deleter
->get_num_unfound(),
1431 deleter
->get_failures(),
1434 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1438 std::unique_ptr
<RGWBulkUploadOp::StreamGetter
>
1439 RGWBulkUploadOp_ObjStore_SWIFT::create_stream()
1441 class SwiftStreamGetter
: public StreamGetter
{
1442 const size_t conlen
;
1447 SwiftStreamGetter(req_state
* const s
, const size_t conlen
)
1453 ssize_t
get_at_most(size_t want
, ceph::bufferlist
& dst
) override
{
1454 /* maximum requested by a caller */
1455 /* data provided by client */
1456 /* RadosGW's limit. */
1457 const size_t max_chunk_size
= \
1458 static_cast<size_t>(s
->cct
->_conf
->rgw_max_chunk_size
);
1459 const size_t max_to_read
= std::min({ want
, conlen
- curpos
, max_chunk_size
});
1461 ldout(s
->cct
, 20) << "bulk_upload: get_at_most max_to_read="
1463 << ", dst.c_str()=" << reinterpret_cast<intptr_t>(dst
.c_str()) << dendl
;
1465 bufferptr
bp(max_to_read
);
1466 const auto read_len
= recv_body(s
, bp
.c_str(), max_to_read
);
1467 dst
.append(bp
, 0, read_len
);
1468 //const auto read_len = recv_body(s, dst.c_str(), max_to_read);
1474 return curpos
> s
->cct
->_conf
->rgw_max_put_size
? -ERR_TOO_LARGE
1478 ssize_t
get_exactly(size_t want
, ceph::bufferlist
& dst
) override
{
1479 ldout(s
->cct
, 20) << "bulk_upload: get_exactly want=" << want
<< dendl
;
1481 /* FIXME: do this in a loop. */
1482 const auto ret
= get_at_most(want
, dst
);
1483 ldout(s
->cct
, 20) << "bulk_upload: get_exactly ret=" << ret
<< dendl
;
1486 } else if (static_cast<size_t>(ret
) != want
) {
1498 ldout(s
->cct
, 20) << "bulk upload: create_stream for length="
1499 << s
->length
<< dendl
;
1501 const size_t conlen
= atoll(s
->length
);
1502 return std::unique_ptr
<SwiftStreamGetter
>(new SwiftStreamGetter(s
, conlen
));
1506 void RGWBulkUploadOp_ObjStore_SWIFT::send_response()
1508 set_req_state_err(s
, op_ret
);
1510 end_header(s
, this /* RGWOp */, nullptr /* contype */,
1511 CHUNKED_TRANSFER_ENCODING
);
1512 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1514 s
->formatter
->open_object_section("delete");
1516 std::string resp_status
;
1517 std::string resp_body
;
1519 if (! failures
.empty()) {
1522 const auto last_err
= { failures
.back().err
};
1523 if (boost::algorithm::contains(last_err
, terminal_errors
)) {
1524 /* The terminal errors are affecting the status of the whole upload. */
1525 set_req_state_err(err
, failures
.back().err
, s
->prot_flags
);
1527 set_req_state_err(err
, ERR_INVALID_REQUEST
, s
->prot_flags
);
1530 dump_errno(err
, resp_status
);
1531 } else if (0 == num_created
&& failures
.empty()) {
1532 /* Nothing created, nothing failed. This means the archive contained no
1533 * entity we could understand (regular file or directory). We need to
1534 * send 400 Bad Request to an HTTP client in the internal status field. */
1535 dump_errno(400, resp_status
);
1536 resp_body
= "Invalid Tar File: No Valid Files";
1539 dump_errno(201, resp_status
);
1542 encode_json("Number Files Created", num_created
, s
->formatter
);
1543 encode_json("Response Body", resp_body
, s
->formatter
);
1544 encode_json("Response Status", resp_status
, s
->formatter
);
1546 s
->formatter
->open_array_section("Errors");
1547 for (const auto& fail_desc
: failures
) {
1548 s
->formatter
->open_array_section("object");
1550 encode_json("Name", fail_desc
.path
, s
->formatter
);
1553 set_req_state_err(err
, fail_desc
.err
, s
->prot_flags
);
1555 dump_errno(err
, status
);
1556 encode_json("Status", status
, s
->formatter
);
1558 s
->formatter
->close_section();
1560 s
->formatter
->close_section();
1562 s
->formatter
->close_section();
1563 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1567 void RGWGetCrossDomainPolicy_ObjStore_SWIFT::send_response()
1569 set_req_state_err(s
, op_ret
);
1571 end_header(s
, this, "application/xml");
1573 std::stringstream ss
;
1575 ss
<< R
"(<?xml version="1.0"?>)" << "\n"
1576 << R
"(<!DOCTYPE cross-domain-policy SYSTEM )"
1577 << R
"("http
://www.adobe.com/xml/dtds/cross-domain-policy.dtd" >)" << "\n"
1578 << R
"(<cross-domain-policy>)" << "\n"
1579 << g_conf
->rgw_cross_domain_policy
<< "\n"
1580 << R
"(</cross-domain-policy>)";
1582 dump_body(s
, ss
.str());
1585 void RGWGetHealthCheck_ObjStore_SWIFT::send_response()
1587 set_req_state_err(s
, op_ret
);
1589 end_header(s
, this, "application/xml");
1592 static constexpr char DISABLED
[] = "DISABLED BY FILE";
1593 dump_body(s
, DISABLED
, strlen(DISABLED
));
1597 const vector
<pair
<string
, RGWInfo_ObjStore_SWIFT::info
>> RGWInfo_ObjStore_SWIFT::swift_info
=
1599 {"bulk_delete", {false, nullptr}},
1600 {"container_quotas", {false, nullptr}},
1601 {"swift", {false, RGWInfo_ObjStore_SWIFT::list_swift_data
}},
1602 {"tempurl", { false, RGWInfo_ObjStore_SWIFT::list_tempurl_data
}},
1603 {"slo", {false, RGWInfo_ObjStore_SWIFT::list_slo_data
}},
1604 {"account_quotas", {false, nullptr}},
1605 {"staticweb", {false, nullptr}},
1606 {"tempauth", {false, nullptr}},
1609 void RGWInfo_ObjStore_SWIFT::execute()
1611 bool is_admin_info_enabled
= false;
1613 const string
& swiftinfo_sig
= s
->info
.args
.get("swiftinfo_sig");
1614 const string
& swiftinfo_expires
= s
->info
.args
.get("swiftinfo_expires");
1616 if (!swiftinfo_sig
.empty() &&
1617 !swiftinfo_expires
.empty() &&
1618 !is_expired(swiftinfo_expires
, s
->cct
)) {
1619 is_admin_info_enabled
= true;
1622 s
->formatter
->open_object_section("info");
1624 for (const auto& pair
: swift_info
) {
1625 if(!is_admin_info_enabled
&& pair
.second
.is_admin_info
)
1628 if (!pair
.second
.list_data
) {
1629 s
->formatter
->open_object_section((pair
.first
).c_str());
1630 s
->formatter
->close_section();
1633 pair
.second
.list_data(*(s
->formatter
), *(s
->cct
->_conf
), *store
);
1637 s
->formatter
->close_section();
1640 void RGWInfo_ObjStore_SWIFT::send_response()
1643 op_ret
= STATUS_NO_CONTENT
;
1645 set_req_state_err(s
, op_ret
);
1647 end_header(s
, this);
1648 rgw_flush_formatter_and_reset(s
, s
->formatter
);
1651 void RGWInfo_ObjStore_SWIFT::list_swift_data(Formatter
& formatter
,
1652 const md_config_t
& config
,
1655 formatter
.open_object_section("swift");
1656 formatter
.dump_int("max_file_size", config
.rgw_max_put_size
);
1657 formatter
.dump_int("container_listing_limit", RGW_LIST_BUCKETS_LIMIT_MAX
);
1659 string
ceph_version(CEPH_GIT_NICE_VER
);
1660 formatter
.dump_string("version", ceph_version
);
1661 formatter
.dump_int("max_meta_name_length", 81);
1663 formatter
.open_array_section("policies");
1664 RGWZoneGroup
& zonegroup
= store
.get_zonegroup();
1666 for (const auto& placement_targets
: zonegroup
.placement_targets
) {
1667 formatter
.open_object_section("policy");
1668 if (placement_targets
.second
.name
.compare(zonegroup
.default_placement
) == 0)
1669 formatter
.dump_bool("default", true);
1670 formatter
.dump_string("name", placement_targets
.second
.name
.c_str());
1671 formatter
.close_section();
1673 formatter
.close_section();
1675 formatter
.dump_int("max_object_name_size", RGWHandler_REST::MAX_OBJ_NAME_LEN
);
1676 formatter
.dump_bool("strict_cors_mode", true);
1677 formatter
.dump_int("max_container_name_length", RGWHandler_REST::MAX_BUCKET_NAME_LEN
);
1678 formatter
.close_section();
1681 void RGWInfo_ObjStore_SWIFT::list_tempurl_data(Formatter
& formatter
,
1682 const md_config_t
& config
,
1685 formatter
.open_object_section("tempurl");
1686 formatter
.open_array_section("methods");
1687 formatter
.dump_string("methodname", "GET");
1688 formatter
.dump_string("methodname", "HEAD");
1689 formatter
.dump_string("methodname", "PUT");
1690 formatter
.dump_string("methodname", "POST");
1691 formatter
.dump_string("methodname", "DELETE");
1692 formatter
.close_section();
1693 formatter
.close_section();
1696 void RGWInfo_ObjStore_SWIFT::list_slo_data(Formatter
& formatter
,
1697 const md_config_t
& config
,
1700 formatter
.open_object_section("slo");
1701 formatter
.dump_int("max_manifest_segments", config
.rgw_max_slo_entries
);
1702 formatter
.close_section();
1705 bool RGWInfo_ObjStore_SWIFT::is_expired(const std::string
& expires
, CephContext
* cct
)
1708 const utime_t now
= ceph_clock_now();
1709 const uint64_t expiration
= (uint64_t)strict_strtoll(expires
.c_str(),
1712 ldout(cct
, 5) << "failed to parse siginfo_expires: " << err
<< dendl
;
1716 if (expiration
<= (uint64_t)now
.sec()) {
1717 ldout(cct
, 5) << "siginfo expired: " << expiration
<< " <= " << now
.sec() << dendl
;
1725 void RGWFormPost::init(RGWRados
* const store
,
1727 RGWHandler
* const dialect_handler
)
1729 prefix
= std::move(s
->object
.name
);
1730 s
->object
= rgw_obj_key();
1732 return RGWPostObj_ObjStore::init(store
, s
, dialect_handler
);
1735 std::size_t RGWFormPost::get_max_file_size() /*const*/
1737 std::string max_str
= get_part_str(ctrl_parts
, "max_file_size", "0");
1740 const std::size_t max_file_size
=
1741 static_cast<uint64_t>(strict_strtoll(max_str
.c_str(), 10, &err
));
1743 if (! err
.empty()) {
1744 ldout(s
->cct
, 5) << "failed to parse FormPost's max_file_size: " << err
1749 return max_file_size
;
1752 bool RGWFormPost::is_non_expired()
1754 std::string expires
= get_part_str(ctrl_parts
, "expires", "0");
1757 const uint64_t expires_timestamp
=
1758 static_cast<uint64_t>(strict_strtoll(expires
.c_str(), 10, &err
));
1760 if (! err
.empty()) {
1761 dout(5) << "failed to parse FormPost's expires: " << err
<< dendl
;
1765 const utime_t now
= ceph_clock_now();
1766 if (expires_timestamp
<= static_cast<uint64_t>(now
.sec())) {
1767 dout(5) << "FormPost form expired: "
1768 << expires_timestamp
<< " <= " << now
.sec() << dendl
;
1775 bool RGWFormPost::is_integral()
1777 const std::string form_signature
= get_part_str(ctrl_parts
, "signature");
1780 get_owner_info(s
, *s
->user
);
1781 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
1783 ldout(s
->cct
, 5) << "cannot get user_info of account's owner" << dendl
;
1787 for (const auto& kv
: s
->user
->temp_url_keys
) {
1788 const int temp_url_key_num
= kv
.first
;
1789 const string
& temp_url_key
= kv
.second
;
1791 if (temp_url_key
.empty()) {
1795 SignatureHelper sig_helper
;
1796 sig_helper
.calc(temp_url_key
,
1797 s
->info
.request_uri
,
1798 get_part_str(ctrl_parts
, "redirect"),
1799 get_part_str(ctrl_parts
, "max_file_size", "0"),
1800 get_part_str(ctrl_parts
, "max_file_count", "0"),
1801 get_part_str(ctrl_parts
, "expires", "0"));
1803 const auto local_sig
= sig_helper
.get_signature();
1805 ldout(s
->cct
, 20) << "FormPost signature [" << temp_url_key_num
<< "]"
1806 << " (calculated): " << local_sig
<< dendl
;
1808 if (sig_helper
.is_equal_to(form_signature
)) {
1811 ldout(s
->cct
, 5) << "FormPost's signature mismatch: "
1812 << local_sig
<< " != " << form_signature
<< dendl
;
1819 void RGWFormPost::get_owner_info(const req_state
* const s
,
1820 RGWUserInfo
& owner_info
) const
1822 /* We cannot use req_state::bucket_name because it isn't available
1823 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
1824 const string
& bucket_name
= s
->init_state
.url_bucket
;
1826 /* TempURL in Formpost only requires that bucket name is specified. */
1827 if (bucket_name
.empty()) {
1831 string bucket_tenant
;
1832 if (!s
->account_name
.empty()) {
1836 const rgw_user
uid(s
->account_name
);
1837 if (uid
.tenant
.empty()) {
1838 const rgw_user
tenanted_uid(uid
.id
, uid
.id
);
1840 if (rgw_get_user_info_by_uid(store
, tenanted_uid
, uinfo
) >= 0) {
1842 bucket_tenant
= uinfo
.user_id
.tenant
;
1847 if (!found
&& rgw_get_user_info_by_uid(store
, uid
, uinfo
) < 0) {
1850 bucket_tenant
= uinfo
.user_id
.tenant
;
1854 /* Need to get user info of bucket owner. */
1855 RGWBucketInfo bucket_info
;
1856 int ret
= store
->get_bucket_info(*static_cast<RGWObjectCtx
*>(s
->obj_ctx
),
1857 bucket_tenant
, bucket_name
,
1858 bucket_info
, nullptr);
1863 ldout(s
->cct
, 20) << "temp url user (bucket owner): " << bucket_info
.owner
1866 if (rgw_get_user_info_by_uid(store
, bucket_info
.owner
, owner_info
) < 0) {
1871 int RGWFormPost::get_params()
1873 /* The parentt class extracts boundary info from the Content-Type. */
1874 int ret
= RGWPostObj_ObjStore::get_params();
1879 policy
.create_default(s
->user
->user_id
, s
->user
->display_name
);
1881 /* Let's start parsing the HTTP body by parsing each form part step-
1882 * by-step till encountering the first part with file data. */
1884 struct post_form_part part
;
1885 ret
= read_form_part_header(&part
, stream_done
);
1890 if (s
->cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
1891 ldout(s
->cct
, 20) << "read part header -- part.name="
1892 << part
.name
<< dendl
;
1894 for (const auto& pair
: part
.fields
) {
1895 ldout(s
->cct
, 20) << "field.name=" << pair
.first
<< dendl
;
1896 ldout(s
->cct
, 20) << "field.val=" << pair
.second
.val
<< dendl
;
1897 ldout(s
->cct
, 20) << "field.params:" << dendl
;
1899 for (const auto& param_pair
: pair
.second
.params
) {
1900 ldout(s
->cct
, 20) << " " << param_pair
.first
1901 << " -> " << param_pair
.second
<< dendl
;
1907 /* Unexpected here. */
1908 err_msg
= "Malformed request";
1912 const auto field_iter
= part
.fields
.find("Content-Disposition");
1913 if (std::end(part
.fields
) != field_iter
&&
1914 std::end(field_iter
->second
.params
) != field_iter
->second
.params
.find("filename")) {
1915 /* First data part ahead. */
1916 current_data_part
= std::move(part
);
1918 /* Stop the iteration. We can assume that all control parts have been
1919 * already parsed. The rest of HTTP body should contain data parts
1920 * only. They will be picked up by ::get_data(). */
1923 /* Control part ahead. Receive, parse and store for later usage. */
1925 ret
= read_data(part
.data
, s
->cct
->_conf
->rgw_max_chunk_size
,
1926 boundary
, stream_done
);
1929 } else if (! boundary
) {
1930 err_msg
= "Couldn't find boundary";
1934 ctrl_parts
[part
.name
] = std::move(part
);
1936 } while (! stream_done
);
1939 max_len
= get_max_file_size();
1941 if (! current_data_part
) {
1942 err_msg
= "FormPost: no files to process";
1946 if (! is_non_expired()) {
1947 err_msg
= "FormPost: Form Expired";
1951 if (! is_integral()) {
1952 err_msg
= "FormPost: Invalid Signature";
1959 std::string
RGWFormPost::get_current_filename() const
1962 const auto& field
= current_data_part
->fields
.at("Content-Disposition");
1963 const auto iter
= field
.params
.find("filename");
1965 if (std::end(field
.params
) != iter
) {
1966 return prefix
+ iter
->second
;
1968 } catch (std::out_of_range
&) {
1975 std::string
RGWFormPost::get_current_content_type() const
1978 const auto& field
= current_data_part
->fields
.at("Content-Type");
1980 } catch (std::out_of_range
&) {
1984 return std::string();
1987 bool RGWFormPost::is_next_file_to_upload()
1989 if (! stream_done
) {
1990 /* We have at least one additional part in the body. */
1991 struct post_form_part part
;
1992 int r
= read_form_part_header(&part
, stream_done
);
1997 const auto field_iter
= part
.fields
.find("Content-Disposition");
1998 if (std::end(part
.fields
) != field_iter
) {
1999 const auto& params
= field_iter
->second
.params
;
2000 const auto& filename_iter
= params
.find("filename");
2002 if (std::end(params
) != filename_iter
&& ! filename_iter
->second
.empty()) {
2003 current_data_part
= std::move(part
);
2012 int RGWFormPost::get_data(ceph::bufferlist
& bl
, bool& again
)
2016 int r
= read_data(bl
, s
->cct
->_conf
->rgw_max_chunk_size
,
2017 boundary
, stream_done
);
2022 /* Tell RGWPostObj::execute() that it has some data to put. */
2028 void RGWFormPost::send_response()
2030 std::string redirect
= get_part_str(ctrl_parts
, "redirect");
2031 if (! redirect
.empty()) {
2032 op_ret
= STATUS_REDIRECT
;
2035 set_req_state_err(s
, op_ret
);
2036 s
->err
.err_code
= err_msg
;
2038 if (! redirect
.empty()) {
2039 dump_redirect(s
, redirect
);
2041 end_header(s
, this);
2044 bool RGWFormPost::is_formpost_req(req_state
* const s
)
2046 std::string content_type
;
2047 std::map
<std::string
, std::string
> params
;
2049 parse_boundary_params(s
->info
.env
->get("CONTENT_TYPE", ""),
2050 content_type
, params
);
2052 return boost::algorithm::iequals(content_type
, "multipart/form-data") &&
2053 params
.count("boundary") > 0;
2057 RGWOp
*RGWHandler_REST_Service_SWIFT::op_get()
2059 return new RGWListBuckets_ObjStore_SWIFT
;
2062 RGWOp
*RGWHandler_REST_Service_SWIFT::op_head()
2064 return new RGWStatAccount_ObjStore_SWIFT
;
2067 RGWOp
*RGWHandler_REST_Service_SWIFT::op_put()
2069 if (s
->info
.args
.exists("extract-archive")) {
2070 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2075 RGWOp
*RGWHandler_REST_Service_SWIFT::op_post()
2077 if (s
->info
.args
.exists("bulk-delete")) {
2078 return new RGWBulkDelete_ObjStore_SWIFT
;
2080 return new RGWPutMetadataAccount_ObjStore_SWIFT
;
2083 RGWOp
*RGWHandler_REST_Service_SWIFT::op_delete()
2085 if (s
->info
.args
.exists("bulk-delete")) {
2086 return new RGWBulkDelete_ObjStore_SWIFT
;
2091 int RGWSwiftWebsiteHandler::serve_errordoc(const int http_ret
,
2092 const std::string error_doc
)
2094 /* Try to throw it all away. */
2095 s
->formatter
->reset();
2097 class RGWGetErrorPage
: public RGWGetObj_ObjStore_SWIFT
{
2099 RGWGetErrorPage(RGWRados
* const store
,
2100 RGWHandler_REST
* const handler
,
2102 const int http_ret
) {
2103 /* Calling a virtual from the base class is safe as the subobject should
2104 * be properly initialized and we haven't overridden the init method. */
2105 init(store
, s
, handler
);
2107 set_custom_http_response(http_ret
);
2110 int error_handler(const int err_no
,
2111 std::string
* const error_content
) override
{
2112 /* Enforce that any error generated while getting the error page will
2113 * not be send to a client. This allows us to recover from the double
2114 * fault situation by sending the original message. */
2117 } get_errpage_op(store
, handler
, s
, http_ret
);
2119 s
->object
= std::to_string(http_ret
) + error_doc
;
2121 RGWOp
* newop
= &get_errpage_op
;
2123 return rgw_process_authenticated(handler
, newop
, &req
, s
, true);
2126 int RGWSwiftWebsiteHandler::error_handler(const int err_no
,
2127 std::string
* const error_content
)
2129 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2131 if (can_be_website_req() && ! ws_conf
.error_doc
.empty()) {
2132 set_req_state_err(s
, err_no
);
2133 return serve_errordoc(s
->err
.http_ret
, ws_conf
.error_doc
);
2136 /* Let's go to the default, no-op handler. */
2140 bool RGWSwiftWebsiteHandler::is_web_mode() const
2142 const boost::string_ref webmode
= s
->info
.env
->get("HTTP_X_WEB_MODE", "");
2143 return boost::algorithm::iequals(webmode
, "true");
2146 bool RGWSwiftWebsiteHandler::can_be_website_req() const
2148 /* Static website works only with the GET or HEAD method. Nothing more. */
2149 static const std::set
<boost::string_ref
> ws_methods
= { "GET", "HEAD" };
2150 if (ws_methods
.count(s
->info
.method
) == 0) {
2154 /* We also need to handle early failures from the auth system. In such cases
2155 * req_state::auth.identity may be empty. Let's treat that the same way as
2156 * the anonymous access. */
2157 if (! s
->auth
.identity
) {
2161 /* Swift serves websites only for anonymous requests unless client explicitly
2162 * requested this behaviour by supplying X-Web-Mode HTTP header set to true. */
2163 if (s
->auth
.identity
->is_anonymous() || is_web_mode()) {
2170 RGWOp
* RGWSwiftWebsiteHandler::get_ws_redirect_op()
2172 class RGWMovedPermanently
: public RGWOp
{
2173 const std::string location
;
2175 RGWMovedPermanently(const std::string
& location
)
2176 : location(location
) {
2179 int verify_permission() override
{
2183 void execute() override
{
2184 op_ret
= -ERR_PERMANENT_REDIRECT
;
2188 void send_response() override
{
2189 set_req_state_err(s
, op_ret
);
2191 dump_content_length(s
, 0);
2192 dump_redirect(s
, location
);
2193 end_header(s
, this);
2196 const string
name() override
{
2197 return "RGWMovedPermanently";
2201 return new RGWMovedPermanently(s
->info
.request_uri
+ '/');
2204 RGWOp
* RGWSwiftWebsiteHandler::get_ws_index_op()
2206 /* Retarget to get obj on requested index file. */
2207 if (! s
->object
.empty()) {
2208 s
->object
= s
->object
.name
+
2209 s
->bucket_info
.website_conf
.get_index_doc();
2211 s
->object
= s
->bucket_info
.website_conf
.get_index_doc();
2214 auto getop
= new RGWGetObj_ObjStore_SWIFT
;
2215 getop
->set_get_data(boost::algorithm::equals("GET", s
->info
.method
));
2220 RGWOp
* RGWSwiftWebsiteHandler::get_ws_listing_op()
2222 class RGWWebsiteListing
: public RGWListBucket_ObjStore_SWIFT
{
2223 const std::string prefix_override
;
2225 int get_params() override
{
2226 prefix
= prefix_override
;
2232 void send_response() override
{
2233 /* Generate the header now. */
2234 set_req_state_err(s
, op_ret
);
2236 dump_container_metadata(s
, bucket
, bucket_quota
,
2237 s
->bucket_info
.website_conf
);
2238 end_header(s
, this, "text/html");
2243 /* Now it's the time to start generating HTML bucket listing.
2244 * All the crazy stuff with crafting tags will be delegated to
2245 * RGWSwiftWebsiteListingFormatter. */
2246 std::stringstream ss
;
2247 RGWSwiftWebsiteListingFormatter
htmler(ss
, prefix
);
2249 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2250 htmler
.generate_header(s
->decoded_uri
,
2251 ws_conf
.listing_css_doc
);
2253 for (const auto& pair
: common_prefixes
) {
2254 std::string subdir_name
= pair
.first
;
2255 if (! subdir_name
.empty()) {
2256 /* To be compliant with Swift we need to remove the trailing
2258 subdir_name
.pop_back();
2261 htmler
.dump_subdir(subdir_name
);
2264 for (const rgw_bucket_dir_entry
& obj
: objs
) {
2265 if (! common_prefixes
.count(obj
.key
.name
+ '/')) {
2266 htmler
.dump_object(obj
);
2270 htmler
.generate_footer();
2271 dump_body(s
, ss
.str());
2274 /* Taking prefix_override by value to leverage std::string r-value ref
2275 * ctor and thus avoid extra memory copying/increasing ref counter. */
2276 RGWWebsiteListing(std::string prefix_override
)
2277 : prefix_override(std::move(prefix_override
)) {
2281 std::string prefix
= std::move(s
->object
.name
);
2282 s
->object
= rgw_obj_key();
2284 return new RGWWebsiteListing(std::move(prefix
));
2287 bool RGWSwiftWebsiteHandler::is_web_dir() const
2289 std::string subdir_name
= url_decode(s
->object
.name
);
2291 /* Remove character from the subdir name if it is "/". */
2292 if (subdir_name
.empty()) {
2294 } else if (subdir_name
.back() == '/') {
2295 subdir_name
.pop_back();
2298 rgw_obj
obj(s
->bucket
, std::move(subdir_name
));
2300 /* First, get attrset of the object we'll try to retrieve. */
2301 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2302 obj_ctx
.obj
.set_atomic(obj
);
2303 obj_ctx
.obj
.set_prefetch_data(obj
);
2305 RGWObjState
* state
= nullptr;
2306 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2310 /* A nonexistent object cannot be a considered as a marker representing
2311 * the emulation of catalog in FS hierarchy. */
2312 if (! state
->exists
) {
2316 /* Decode the content type. */
2317 std::string content_type
;
2318 get_contype_from_attrs(state
->attrset
, content_type
);
2320 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2321 const std::string subdir_marker
= ws_conf
.subdir_marker
.empty()
2322 ? "application/directory"
2323 : ws_conf
.subdir_marker
;
2324 return subdir_marker
== content_type
&& state
->size
<= 1;
2327 bool RGWSwiftWebsiteHandler::is_index_present(const std::string
& index
)
2329 rgw_obj
obj(s
->bucket
, index
);
2331 RGWObjectCtx
& obj_ctx
= *static_cast<RGWObjectCtx
*>(s
->obj_ctx
);
2332 obj_ctx
.obj
.set_atomic(obj
);
2333 obj_ctx
.obj
.set_prefetch_data(obj
);
2335 RGWObjState
* state
= nullptr;
2336 if (store
->get_obj_state(&obj_ctx
, s
->bucket_info
, obj
, &state
, false) < 0) {
2340 /* A nonexistent object cannot be a considered as a viable index. We will
2341 * try to list the bucket or - if this is impossible - return an error. */
2342 return state
->exists
;
2345 int RGWSwiftWebsiteHandler::retarget_bucket(RGWOp
* op
, RGWOp
** new_op
)
2347 ldout(s
->cct
, 10) << "Starting retarget" << dendl
;
2348 RGWOp
* op_override
= nullptr;
2350 /* In Swift static web content is served if the request is anonymous or
2351 * has X-Web-Mode HTTP header specified to true. */
2352 if (can_be_website_req()) {
2353 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2354 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2356 if (s
->decoded_uri
.back() != '/') {
2357 op_override
= get_ws_redirect_op();
2358 } else if (! index
.empty() && is_index_present(index
)) {
2359 op_override
= get_ws_index_op();
2360 } else if (ws_conf
.listing_enabled
) {
2361 op_override
= get_ws_listing_op();
2366 handler
->put_op(op
);
2367 op_override
->init(store
, s
, handler
);
2369 *new_op
= op_override
;
2374 /* Return 404 Not Found is the request has web mode enforced but we static web
2375 * wasn't able to serve it accordingly. */
2376 return ! op_override
&& is_web_mode() ? -ENOENT
: 0;
2379 int RGWSwiftWebsiteHandler::retarget_object(RGWOp
* op
, RGWOp
** new_op
)
2381 ldout(s
->cct
, 10) << "Starting object retarget" << dendl
;
2382 RGWOp
* op_override
= nullptr;
2384 /* In Swift static web content is served if the request is anonymous or
2385 * has X-Web-Mode HTTP header specified to true. */
2386 if (can_be_website_req() && is_web_dir()) {
2387 const auto& ws_conf
= s
->bucket_info
.website_conf
;
2388 const auto& index
= s
->bucket_info
.website_conf
.get_index_doc();
2390 if (s
->decoded_uri
.back() != '/') {
2391 op_override
= get_ws_redirect_op();
2392 } else if (! index
.empty() && is_index_present(index
)) {
2393 op_override
= get_ws_index_op();
2394 } else if (ws_conf
.listing_enabled
) {
2395 op_override
= get_ws_listing_op();
2398 /* A regular request or the specified object isn't a subdirectory marker.
2399 * We don't need any re-targeting. Error handling (like sending a custom
2400 * error page) will be performed by error_handler of the actual RGWOp. */
2405 handler
->put_op(op
);
2406 op_override
->init(store
, s
, handler
);
2408 *new_op
= op_override
;
2413 /* Return 404 Not Found if we aren't able to re-target for subdir marker. */
2414 return ! op_override
? -ENOENT
: 0;
2418 RGWOp
*RGWHandler_REST_Bucket_SWIFT::get_obj_op(bool get_data
)
2421 return new RGWGetACLs_ObjStore_SWIFT
;
2425 return new RGWListBucket_ObjStore_SWIFT
;
2427 return new RGWStatBucket_ObjStore_SWIFT
;
2430 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_get()
2432 return get_obj_op(true);
2435 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_head()
2437 return get_obj_op(false);
2440 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_put()
2443 return new RGWPutACLs_ObjStore_SWIFT
;
2445 if(s
->info
.args
.exists("extract-archive")) {
2446 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2448 return new RGWCreateBucket_ObjStore_SWIFT
;
2451 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_delete()
2453 return new RGWDeleteBucket_ObjStore_SWIFT
;
2456 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_post()
2458 if (RGWFormPost::is_formpost_req(s
)) {
2459 return new RGWFormPost
;
2461 return new RGWPutMetadataBucket_ObjStore_SWIFT
;
2465 RGWOp
*RGWHandler_REST_Bucket_SWIFT::op_options()
2467 return new RGWOptionsCORS_ObjStore_SWIFT
;
2471 RGWOp
*RGWHandler_REST_Obj_SWIFT::get_obj_op(bool get_data
)
2474 return new RGWGetACLs_ObjStore_SWIFT
;
2477 RGWGetObj_ObjStore_SWIFT
*get_obj_op
= new RGWGetObj_ObjStore_SWIFT
;
2478 get_obj_op
->set_get_data(get_data
);
2482 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_get()
2484 return get_obj_op(true);
2487 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_head()
2489 return get_obj_op(false);
2492 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_put()
2495 return new RGWPutACLs_ObjStore_SWIFT
;
2497 if(s
->info
.args
.exists("extract-archive")) {
2498 return new RGWBulkUploadOp_ObjStore_SWIFT
;
2500 if (s
->init_state
.src_bucket
.empty())
2501 return new RGWPutObj_ObjStore_SWIFT
;
2503 return new RGWCopyObj_ObjStore_SWIFT
;
2506 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_delete()
2508 return new RGWDeleteObj_ObjStore_SWIFT
;
2511 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_post()
2513 if (RGWFormPost::is_formpost_req(s
)) {
2514 return new RGWFormPost
;
2516 return new RGWPutMetadataObject_ObjStore_SWIFT
;
2520 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_copy()
2522 return new RGWCopyObj_ObjStore_SWIFT
;
2525 RGWOp
*RGWHandler_REST_Obj_SWIFT::op_options()
2527 return new RGWOptionsCORS_ObjStore_SWIFT
;
2531 int RGWHandler_REST_SWIFT::authorize()
2533 return rgw::auth::Strategy::apply(auth_strategy
, s
);
2536 int RGWHandler_REST_SWIFT::postauth_init()
2538 struct req_init_state
* t
= &s
->init_state
;
2540 /* XXX Stub this until Swift Auth sets account into URL. */
2541 s
->bucket_tenant
= s
->user
->user_id
.tenant
;
2542 s
->bucket_name
= t
->url_bucket
;
2544 dout(10) << "s->object=" <<
2545 (!s
->object
.empty() ? s
->object
: rgw_obj_key("<NULL>"))
2547 << rgw_make_bucket_entry_name(s
->bucket_tenant
, s
->bucket_name
)
2551 ret
= validate_tenant_name(s
->bucket_tenant
);
2554 ret
= validate_bucket_name(s
->bucket_name
);
2557 ret
= validate_object_name(s
->object
.name
);
2561 if (!t
->src_bucket
.empty()) {
2563 * We don't allow cross-tenant copy at present. It requires account
2564 * names in the URL for Swift.
2566 s
->src_tenant_name
= s
->user
->user_id
.tenant
;
2567 s
->src_bucket_name
= t
->src_bucket
;
2569 ret
= validate_bucket_name(s
->src_bucket_name
);
2573 ret
= validate_object_name(s
->src_object
.name
);
2582 int RGWHandler_REST_SWIFT::validate_bucket_name(const string
& bucket
)
2584 int ret
= RGWHandler_REST::validate_bucket_name(bucket
);
2588 int len
= bucket
.size();
2593 if (bucket
[0] == '.')
2594 return -ERR_INVALID_BUCKET_NAME
;
2596 if (check_utf8(bucket
.c_str(), len
))
2597 return -ERR_INVALID_UTF8
;
2599 const char *s
= bucket
.c_str();
2601 for (int i
= 0; i
< len
; ++i
, ++s
) {
2602 if (*(unsigned char *)s
== 0xff)
2603 return -ERR_INVALID_BUCKET_NAME
;
2609 static void next_tok(string
& str
, string
& tok
, char delim
)
2611 if (str
.size() == 0) {
2616 int pos
= str
.find(delim
);
2618 tok
= str
.substr(0, pos
);
2619 str
= str
.substr(pos
+ 1);
2625 int RGWHandler_REST_SWIFT::init_from_header(struct req_state
* const s
,
2626 const std::string
& frontend_prefix
)
2631 s
->prot_flags
|= RGW_REST_SWIFT
;
2633 char reqbuf
[frontend_prefix
.length() + s
->decoded_uri
.length() + 1];
2634 sprintf(reqbuf
, "%s%s", frontend_prefix
.c_str(), s
->decoded_uri
.c_str());
2635 const char *req_name
= reqbuf
;
2639 if (*req_name
== '?') {
2642 p
= s
->info
.request_params
.c_str();
2645 s
->info
.args
.set(p
);
2646 s
->info
.args
.parse();
2648 /* Skip the leading slash of URL hierarchy. */
2649 if (req_name
[0] != '/') {
2655 if ('\0' == req_name
[0]) {
2656 return g_conf
->rgw_swift_url_prefix
== "/" ? -ERR_BAD_URL
: 0;
2661 size_t pos
= req
.find('/');
2662 if (std::string::npos
!= pos
&& g_conf
->rgw_swift_url_prefix
!= "/") {
2663 bool cut_url
= g_conf
->rgw_swift_url_prefix
.length();
2664 first
= req
.substr(0, pos
);
2666 if (first
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2668 /* Rewind to the "v1/..." part. */
2669 next_tok(req
, first
, '/');
2672 } else if (req
.compare(g_conf
->rgw_swift_url_prefix
) == 0) {
2673 s
->formatter
= new RGWFormatter_Plain
;
2674 return -ERR_BAD_URL
;
2679 std::string tenant_path
;
2680 if (! g_conf
->rgw_swift_tenant_name
.empty()) {
2681 tenant_path
= "/AUTH_";
2682 tenant_path
.append(g_conf
->rgw_swift_tenant_name
);
2685 /* verify that the request_uri conforms with what's expected */
2686 char buf
[g_conf
->rgw_swift_url_prefix
.length() + 16 + tenant_path
.length()];
2688 if (g_conf
->rgw_swift_url_prefix
== "/") {
2689 blen
= sprintf(buf
, "/v1%s", tenant_path
.c_str());
2691 blen
= sprintf(buf
, "/%s/v1%s",
2692 g_conf
->rgw_swift_url_prefix
.c_str(), tenant_path
.c_str());
2695 if (strncmp(reqbuf
, buf
, blen
) != 0) {
2699 int ret
= allocate_formatter(s
, RGW_FORMAT_PLAIN
, true);
2705 next_tok(req
, ver
, '/');
2707 if (!tenant_path
.empty() || g_conf
->rgw_swift_account_in_url
) {
2708 string account_name
;
2709 next_tok(req
, account_name
, '/');
2711 /* Erase all pre-defined prefixes like "AUTH_" or "KEY_". */
2712 const vector
<string
> skipped_prefixes
= { "AUTH_", "KEY_" };
2714 for (const auto pfx
: skipped_prefixes
) {
2715 const size_t comp_len
= min(account_name
.length(), pfx
.length());
2716 if (account_name
.compare(0, comp_len
, pfx
) == 0) {
2717 /* Prefix is present. Drop it. */
2718 account_name
= account_name
.substr(comp_len
);
2723 if (account_name
.empty()) {
2724 return -ERR_PRECONDITION_FAILED
;
2726 s
->account_name
= account_name
;
2730 next_tok(req
, first
, '/');
2732 dout(10) << "ver=" << ver
<< " first=" << first
<< " req=" << req
<< dendl
;
2733 if (first
.size() == 0)
2736 s
->info
.effective_uri
= "/" + first
;
2738 // Save bucket to tide us over until token is parsed.
2739 s
->init_state
.url_bucket
= first
;
2743 rgw_obj_key(req
, s
->info
.env
->get("HTTP_X_OBJECT_VERSION_ID", "")); /* rgw swift extension */
2744 s
->info
.effective_uri
.append("/" + s
->object
.name
);
2750 int RGWHandler_REST_SWIFT::init(RGWRados
* store
, struct req_state
* s
,
2751 rgw::io::BasicClient
*cio
)
2753 struct req_init_state
*t
= &s
->init_state
;
2755 s
->dialect
= "swift";
2757 const char *copy_source
= s
->info
.env
->get("HTTP_X_COPY_FROM");
2759 bool result
= RGWCopyObj::parse_copy_location(copy_source
, t
->src_bucket
,
2762 return -ERR_BAD_URL
;
2765 if (s
->op
== OP_COPY
) {
2766 const char *req_dest
= s
->info
.env
->get("HTTP_DESTINATION");
2768 return -ERR_BAD_URL
;
2770 string dest_bucket_name
;
2771 rgw_obj_key dest_obj_key
;
2773 RGWCopyObj::parse_copy_location(req_dest
, dest_bucket_name
,
2776 return -ERR_BAD_URL
;
2778 string dest_object
= dest_obj_key
.name
;
2780 /* convert COPY operation into PUT */
2781 t
->src_bucket
= t
->url_bucket
;
2782 s
->src_object
= s
->object
;
2783 t
->url_bucket
= dest_bucket_name
;
2784 s
->object
= rgw_obj_key(dest_object
);
2788 return RGWHandler_REST::init(store
, s
, cio
);
2792 RGWRESTMgr_SWIFT::get_handler(struct req_state
* const s
,
2793 const rgw::auth::StrategyRegistry
& auth_registry
,
2794 const std::string
& frontend_prefix
)
2796 int ret
= RGWHandler_REST_SWIFT::init_from_header(s
, frontend_prefix
);
2798 ldout(s
->cct
, 10) << "init_from_header returned err=" << ret
<< dendl
;
2802 const auto& auth_strategy
= auth_registry
.get_swift();
2804 if (s
->init_state
.url_bucket
.empty()) {
2805 return new RGWHandler_REST_Service_SWIFT(auth_strategy
);
2808 if (s
->object
.empty()) {
2809 return new RGWHandler_REST_Bucket_SWIFT(auth_strategy
);
2812 return new RGWHandler_REST_Obj_SWIFT(auth_strategy
);
2815 RGWHandler_REST
* RGWRESTMgr_SWIFT_Info::get_handler(
2816 struct req_state
* const s
,
2817 const rgw::auth::StrategyRegistry
& auth_registry
,
2818 const std::string
& frontend_prefix
)
2820 s
->prot_flags
|= RGW_REST_SWIFT
;
2821 const auto& auth_strategy
= auth_registry
.get_swift();
2822 return new RGWHandler_REST_SWIFT_Info(auth_strategy
);