/
rgw_rest_swift.cc
1861 lines (1555 loc) · 51.8 KB
/
rgw_rest_swift.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <boost/optional.hpp>
#include <boost/utility/in_place_factory.hpp>
#include "include/assert.h"
#include "common/Formatter.h"
#include "common/utf8.h"
#include "common/ceph_json.h"
#include "rgw_rest_swift.h"
#include "rgw_acl_swift.h"
#include "rgw_cors_swift.h"
#include "rgw_formats.h"
#include "rgw_client_io.h"
#include "rgw_auth.h"
#include "rgw_auth_decoimpl.h"
#include "rgw_swift_auth.h"
#include <array>
#include <sstream>
#include <memory>
#define dout_subsys ceph_subsys_rgw
int RGWListBuckets_ObjStore_SWIFT::get_params()
{
marker = s->info.args.get("marker");
end_marker = s->info.args.get("end_marker");
string limit_str = s->info.args.get("limit");
if (!limit_str.empty()) {
string err;
long l = strict_strtol(limit_str.c_str(), 10, &err);
if (!err.empty()) {
return -EINVAL;
}
if (l > (long)limit_max || l < 0) {
return -ERR_PRECONDITION_FAILED;
}
limit = (uint64_t)l;
}
if (need_stats) {
bool stats, exists;
int r = s->info.args.get_bool("stats", &stats, &exists);
if (r < 0) {
return r;
}
if (exists) {
need_stats = stats;
}
}
return 0;
}
static void dump_account_metadata(struct req_state * const s,
const uint32_t buckets_count,
const uint64_t buckets_object_count,
const uint64_t buckets_size,
const uint64_t buckets_size_rounded,
/* const */map<string, bufferlist>& attrs,
const RGWQuotaInfo& quota,
const RGWAccessControlPolicy_SWIFTAcct &policy)
{
char buf[32];
utime_t now = ceph_clock_now(g_ceph_context);
snprintf(buf, sizeof(buf), "%0.5f", (double)now);
/* Adding X-Timestamp to keep align with Swift API */
STREAM_IO(s)->print("X-Timestamp: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)buckets_count);
STREAM_IO(s)->print("X-Account-Container-Count: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)buckets_object_count);
STREAM_IO(s)->print("X-Account-Object-Count: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)buckets_size);
STREAM_IO(s)->print("X-Account-Bytes-Used: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)buckets_size_rounded);
STREAM_IO(s)->print("X-Account-Bytes-Used-Actual: %s\r\n", buf);
/* Dump TempURL-related stuff */
if (s->perm_mask == RGW_PERM_FULL_CONTROL) {
auto iter = s->user->temp_url_keys.find(0);
if (iter != std::end(s->user->temp_url_keys) && !iter->second.empty()) {
STREAM_IO(s)->print("X-Account-Meta-Temp-Url-Key: %s\r\n",
iter->second.c_str());
}
iter = s->user->temp_url_keys.find(1);
if (iter != std::end(s->user->temp_url_keys) && !iter->second.empty()) {
STREAM_IO(s)->print("X-Account-Meta-Temp-Url-Key-2: %s\r\n",
iter->second.c_str());
}
}
/* Dump quota headers. */
if (quota.enabled) {
if (quota.max_size >= 0) {
STREAM_IO(s)->print("X-Account-Meta-Quota-Bytes: %lld\r\n",
(long long)quota.max_size);
}
/* Limit on the number of objects in a given account is a RadosGW's
* extension. Swift's account quota WSGI filter doesn't support it. */
if (quota.max_objects >= 0) {
STREAM_IO(s)->print("X-Account-Meta-Quota-Count: %lld\r\n",
(long long)quota.max_objects);
}
}
/* Dump user-defined metadata items and generic attrs. */
const size_t PREFIX_LEN = sizeof(RGW_ATTR_META_PREFIX) - 1;
map<string, bufferlist>::iterator iter;
for (iter = attrs.lower_bound(RGW_ATTR_PREFIX); iter != attrs.end(); ++iter) {
const char *name = iter->first.c_str();
map<string, string>::const_iterator geniter = rgw_to_http_attrs.find(name);
if (geniter != rgw_to_http_attrs.end()) {
STREAM_IO(s)->print("%s: %s\r\n", geniter->second.c_str(),
iter->second.c_str());
} else if (strncmp(name, RGW_ATTR_META_PREFIX, PREFIX_LEN) == 0) {
STREAM_IO(s)->print("X-Account-Meta-%s: %s\r\n",
camelcase_dash_http_attr(name + PREFIX_LEN).c_str(),
iter->second.c_str());
}
}
/* Dump account ACLs */
string acct_acl;
policy.to_str(acct_acl);
if (acct_acl.size()) {
STREAM_IO(s)->print("X-Account-Access-Control: %s\r\n", acct_acl.c_str());
}
}
void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets)
{
if (op_ret) {
set_req_state_err(s, op_ret);
} else if (!has_buckets && s->format == RGW_FORMAT_PLAIN) {
op_ret = STATUS_NO_CONTENT;
set_req_state_err(s, op_ret);
}
if (!g_conf->rgw_swift_enforce_content_length) {
/* Adding account stats in the header to keep align with Swift API */
dump_account_metadata(s,
buckets_count,
buckets_objcount,
buckets_size,
buckets_size_rounded,
attrs,
user_quota,
static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
dump_errno(s);
end_header(s, NULL, NULL, NO_CONTENT_LENGTH, true);
}
if (! op_ret) {
dump_start(s);
s->formatter->open_array_section_with_attrs("account",
FormatterAttrs("name", s->user->display_name.c_str(), NULL));
sent_data = true;
}
}
void RGWListBuckets_ObjStore_SWIFT::send_response_data(RGWUserBuckets& buckets)
{
map<string, RGWBucketEnt>& m = buckets.get_buckets();
map<string, RGWBucketEnt>::iterator iter;
if (!sent_data)
return;
for (iter = m.begin(); iter != m.end(); ++iter) {
RGWBucketEnt obj = iter->second;
s->formatter->open_object_section("container");
s->formatter->dump_string("name", obj.bucket.name);
if (need_stats) {
s->formatter->dump_int("count", obj.count);
s->formatter->dump_int("bytes", obj.size);
}
s->formatter->close_section();
if (!g_conf->rgw_swift_enforce_content_length) {
rgw_flush_formatter(s, s->formatter);
}
}
}
void RGWListBuckets_ObjStore_SWIFT::send_response_end()
{
if (sent_data) {
s->formatter->close_section();
}
if (g_conf->rgw_swift_enforce_content_length) {
/* Adding account stats in the header to keep align with Swift API */
dump_account_metadata(s,
buckets_count,
buckets_objcount,
buckets_size,
buckets_size_rounded,
attrs,
user_quota,
static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
dump_errno(s);
end_header(s, NULL, NULL, s->formatter->get_len(), true);
}
if (sent_data || g_conf->rgw_swift_enforce_content_length) {
rgw_flush_formatter_and_reset(s, s->formatter);
}
}
int RGWListBucket_ObjStore_SWIFT::get_params()
{
prefix = s->info.args.get("prefix");
marker = s->info.args.get("marker");
end_marker = s->info.args.get("end_marker");
max_keys = s->info.args.get("limit");
op_ret = parse_max_keys();
if (op_ret < 0) {
return op_ret;
}
if (max > default_max)
return -ERR_PRECONDITION_FAILED;
delimiter = s->info.args.get("delimiter");
string path_args;
if (s->info.args.exists("path")) { // should handle empty path
path_args = s->info.args.get("path");
if (!delimiter.empty() || !prefix.empty()) {
return -EINVAL;
}
prefix = path_args;
delimiter="/";
path = prefix;
if (path.size() && path[path.size() - 1] != '/')
path.append("/");
int len = prefix.size();
int delim_size = delimiter.size();
if (len >= delim_size) {
if (prefix.substr(len - delim_size).compare(delimiter) != 0)
prefix.append(delimiter);
}
}
return 0;
}
static void dump_container_metadata(struct req_state *,
const RGWBucketEnt&,
const RGWQuotaInfo&);
void RGWListBucket_ObjStore_SWIFT::send_response()
{
vector<RGWObjEnt>::iterator iter = objs.begin();
map<string, bool>::iterator pref_iter = common_prefixes.begin();
dump_start(s);
dump_container_metadata(s, bucket, bucket_quota);
s->formatter->open_array_section_with_attrs("container", FormatterAttrs("name", s->bucket.name.c_str(), NULL));
while (iter != objs.end() || pref_iter != common_prefixes.end()) {
bool do_pref = false;
bool do_objs = false;
rgw_obj_key& key = iter->key;
if (pref_iter == common_prefixes.end())
do_objs = true;
else if (iter == objs.end())
do_pref = true;
else if (key.name.compare(pref_iter->first) == 0) {
do_objs = true;
++pref_iter;
} else if (key.name.compare(pref_iter->first) <= 0)
do_objs = true;
else
do_pref = true;
if (do_objs && (marker.empty() || marker < key)) {
if (key.name.compare(path) == 0)
goto next;
s->formatter->open_object_section("object");
s->formatter->dump_string("name", key.name);
s->formatter->dump_string("hash", iter->etag);
s->formatter->dump_int("bytes", iter->size);
string single_content_type = iter->content_type;
if (iter->content_type.size()) {
// content type might hold multiple values, just dump the last one
ssize_t pos = iter->content_type.rfind(',');
if (pos > 0) {
++pos;
while (single_content_type[pos] == ' ')
++pos;
single_content_type = single_content_type.substr(pos);
}
s->formatter->dump_string("content_type", single_content_type);
}
dump_time(s, "last_modified", &iter->mtime);
s->formatter->close_section();
}
if (do_pref && (marker.empty() || pref_iter->first.compare(marker.name) > 0)) {
const string& name = pref_iter->first;
if (name.compare(delimiter) == 0)
goto next;
s->formatter->open_object_section_with_attrs("subdir", FormatterAttrs("name", name.c_str(), NULL));
/* swift is a bit inconsistent here */
switch (s->format) {
case RGW_FORMAT_XML:
s->formatter->dump_string("name", name);
break;
default:
s->formatter->dump_string("subdir", name);
}
s->formatter->close_section();
}
next:
if (do_objs)
++iter;
else
++pref_iter;
}
s->formatter->close_section();
int64_t content_len = 0;
if (! op_ret) {
content_len = s->formatter->get_len();
if (content_len == 0) {
op_ret = STATUS_NO_CONTENT;
}
} else if (op_ret > 0) {
op_ret = 0;
}
set_req_state_err(s, op_ret);
dump_errno(s);
end_header(s, this, NULL, content_len);
if (op_ret < 0) {
return;
}
rgw_flush_formatter_and_reset(s, s->formatter);
}
static void dump_container_metadata(struct req_state *s,
const RGWBucketEnt& bucket,
const RGWQuotaInfo& quota)
{
char buf[32];
/* Adding X-Timestamp to keep align with Swift API */
utime_t ut(s->bucket_info.creation_time);
snprintf(buf, sizeof(buf), "%lld.%05d",
(long long)ut.sec(), (int)(ut.usec() / 10));
STREAM_IO(s)->print("X-Timestamp: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)bucket.count);
STREAM_IO(s)->print("X-Container-Object-Count: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)bucket.size);
STREAM_IO(s)->print("X-Container-Bytes-Used: %s\r\n", buf);
snprintf(buf, sizeof(buf), "%lld", (long long)bucket.size_rounded);
STREAM_IO(s)->print("X-Container-Bytes-Used-Actual: %s\r\n", buf);
if (s->object.empty()) {
RGWAccessControlPolicy_SWIFT *swift_policy
= static_cast<RGWAccessControlPolicy_SWIFT *>(s->bucket_acl);
string read_acl, write_acl;
swift_policy->to_str(read_acl, write_acl);
if (read_acl.size()) {
STREAM_IO(s)->print("X-Container-Read: %s\r\n", read_acl.c_str());
}
if (write_acl.size()) {
STREAM_IO(s)->print("X-Container-Write: %s\r\n", write_acl.c_str());
}
if (!s->bucket_info.placement_rule.empty()) {
STREAM_IO(s)->print("X-Storage-Policy: %s\r\n",
s->bucket_info.placement_rule.c_str());
}
/* Dump user-defined metadata items and generic attrs. */
const size_t PREFIX_LEN = sizeof(RGW_ATTR_META_PREFIX) - 1;
map<string, bufferlist>::iterator iter;
for (iter = s->bucket_attrs.lower_bound(RGW_ATTR_PREFIX);
iter != s->bucket_attrs.end();
++iter) {
const char *name = iter->first.c_str();
map<string, string>::const_iterator geniter = rgw_to_http_attrs.find(name);
if (geniter != rgw_to_http_attrs.end()) {
STREAM_IO(s)->print("%s: %s\r\n", geniter->second.c_str(),
iter->second.c_str());
} else if (strncmp(name, RGW_ATTR_META_PREFIX, PREFIX_LEN) == 0) {
STREAM_IO(s)->print("X-Container-Meta-%s: %s\r\n",
camelcase_dash_http_attr(name + PREFIX_LEN).c_str(),
iter->second.c_str());
}
}
}
/* Dump container versioning info. */
if (!s->bucket_info.swift_ver_location.empty()) {
string encoded_loc;
url_encode(s->bucket_info.swift_ver_location, encoded_loc);
STREAM_IO(s)->print("X-Versions-Location: %s\r\n", encoded_loc.c_str());
}
/* Dump quota headers. */
if (quota.enabled) {
if (quota.max_size >= 0) {
STREAM_IO(s)->print("X-Container-Meta-Quota-Bytes: %lld\r\n",
(long long)quota.max_size);
}
if (quota.max_objects >= 0) {
STREAM_IO(s)->print("X-Container-Meta-Quota-Count: %lld\r\n",
(long long)quota.max_objects);
}
}
}
void RGWStatAccount_ObjStore_SWIFT::execute()
{
RGWStatAccount_ObjStore::execute();
op_ret = rgw_get_user_attrs_by_uid(store, s->user->user_id, attrs);
}
void RGWStatAccount_ObjStore_SWIFT::send_response()
{
if (op_ret >= 0) {
op_ret = STATUS_NO_CONTENT;
dump_account_metadata(s,
buckets_count,
buckets_objcount,
buckets_size,
buckets_size_rounded,
attrs,
user_quota,
static_cast<RGWAccessControlPolicy_SWIFTAcct&>(*s->user_acl));
}
set_req_state_err(s, op_ret);
dump_errno(s);
end_header(s, NULL, NULL, 0, true);
dump_start(s);
}
void RGWStatBucket_ObjStore_SWIFT::send_response()
{
if (op_ret >= 0) {
op_ret = STATUS_NO_CONTENT;
dump_container_metadata(s, bucket, bucket_quota);
}
set_req_state_err(s, op_ret);
dump_errno(s);
end_header(s, this, NULL, 0, true);
dump_start(s);
}
static int get_swift_container_settings(req_state * const s,
RGWRados * const store,
RGWAccessControlPolicy * const policy,
bool * const has_policy,
RGWCORSConfiguration * const cors_config,
bool * const has_cors)
{
string read_list, write_list;
const char * const read_attr = s->info.env->get("HTTP_X_CONTAINER_READ");
if (read_attr) {
read_list = read_attr;
}
const char * const write_attr = s->info.env->get("HTTP_X_CONTAINER_WRITE");
if (write_attr) {
write_list = write_attr;
}
*has_policy = false;
if (read_attr || write_attr) {
RGWAccessControlPolicy_SWIFT swift_policy(s->cct);
int r = swift_policy.create(store,
s->user->user_id,
s->user->display_name,
read_list,
write_list);
if (r < 0) {
return r;
}
*policy = swift_policy;
*has_policy = true;
}
*has_cors = false;
/*Check and update CORS configuration*/
const char *allow_origins = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_ORIGIN");
const char *allow_headers = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_ALLOW_HEADERS");
const char *expose_headers = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_EXPOSE_HEADERS");
const char *max_age = s->info.env->get("HTTP_X_CONTAINER_META_ACCESS_CONTROL_MAX_AGE");
if (allow_origins) {
RGWCORSConfiguration_SWIFT *swift_cors = new RGWCORSConfiguration_SWIFT;
int r = swift_cors->create_update(allow_origins, allow_headers, expose_headers, max_age);
if (r < 0) {
dout(0) << "Error creating/updating the cors configuration" << dendl;
delete swift_cors;
return r;
}
*has_cors = true;
*cors_config = *swift_cors;
cors_config->dump();
delete swift_cors;
}
return 0;
}
#define ACCT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_ACCOUNT_META_"
#define ACCT_PUT_ATTR_PREFIX "HTTP_X_ACCOUNT_META_"
#define CONT_REMOVE_ATTR_PREFIX "HTTP_X_REMOVE_CONTAINER_META_"
#define CONT_PUT_ATTR_PREFIX "HTTP_X_CONTAINER_META_"
static void get_rmattrs_from_headers(const req_state * const s,
const char * const put_prefix,
const char * const del_prefix,
set<string>& rmattr_names)
{
map<string, string, ltstr_nocase>& m = s->info.env->get_map();
map<string, string, ltstr_nocase>::const_iterator iter;
const size_t put_prefix_len = strlen(put_prefix);
const size_t del_prefix_len = strlen(del_prefix);
for (iter = m.begin(); iter != m.end(); ++iter) {
size_t prefix_len = 0;
const char * const p = iter->first.c_str();
if (strncasecmp(p, del_prefix, del_prefix_len) == 0) {
/* Explicitly requested removal. */
prefix_len = del_prefix_len;
} else if ((strncasecmp(p, put_prefix, put_prefix_len) == 0)
&& iter->second.empty()) {
/* Removal requested by putting an empty value. */
prefix_len = put_prefix_len;
}
if (prefix_len > 0) {
string name(RGW_ATTR_META_PREFIX);
name.append(lowercase_dash_http_attr(p + prefix_len));
rmattr_names.insert(name);
}
}
}
static int get_swift_versioning_settings(
req_state * const s,
boost::optional<std::string>& swift_ver_location)
{
/* Removing the Swift's versions location has lower priority than setting
* a new one. That's the reason why we're handling it first. */
const std::string vlocdel =
s->info.env->get("HTTP_X_REMOVE_VERSIONS_LOCATION", "");
if (vlocdel.size()) {
swift_ver_location = boost::in_place(std::string());
}
std::string vloc = s->info.env->get("HTTP_X_VERSIONS_LOCATION", "");
if (vloc.size()) {
/* If the Swift's versioning is globally disabled but someone wants to
* enable it for a given container, new version of Swift will generate
* the precondition failed error. */
if (! s->cct->_conf->rgw_swift_versioning_enabled) {
return -ERR_PRECONDITION_FAILED;
}
swift_ver_location = std::move(vloc);
}
return 0;
}
int RGWCreateBucket_ObjStore_SWIFT::get_params()
{
bool has_policy;
int r = get_swift_container_settings(s, store, &policy, &has_policy, &cors_config, &has_cors);
if (r < 0) {
return r;
}
if (!has_policy) {
policy.create_default(s->user->user_id, s->user->display_name);
}
location_constraint = store->get_zonegroup().api_name;
get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX,
CONT_REMOVE_ATTR_PREFIX, rmattr_names);
placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
return get_swift_versioning_settings(s, swift_ver_location);
}
void RGWCreateBucket_ObjStore_SWIFT::send_response()
{
if (! op_ret)
op_ret = STATUS_CREATED;
else if (op_ret == -ERR_BUCKET_EXISTS)
op_ret = STATUS_ACCEPTED;
set_req_state_err(s, op_ret);
dump_errno(s);
/* Propose ending HTTP header with 0 Content-Length header. */
end_header(s, NULL, NULL, 0);
rgw_flush_formatter_and_reset(s, s->formatter);
}
void RGWDeleteBucket_ObjStore_SWIFT::send_response()
{
int r = op_ret;
if (!r)
r = STATUS_NO_CONTENT;
set_req_state_err(s, r);
dump_errno(s);
end_header(s, this, NULL, 0);
rgw_flush_formatter_and_reset(s, s->formatter);
}
static int get_delete_at_param(req_state *s, real_time *delete_at)
{
/* Handle Swift object expiration. */
real_time delat_proposal;
string x_delete = s->info.env->get("HTTP_X_DELETE_AFTER", "");
if (x_delete.empty()) {
x_delete = s->info.env->get("HTTP_X_DELETE_AT", "");
} else {
/* X-Delete-After HTTP is present. It means we need add its value
* to the current time. */
delat_proposal = real_clock::now();
}
if (x_delete.empty()) {
return 0;
}
string err;
long ts = strict_strtoll(x_delete.c_str(), 10, &err);
if (!err.empty()) {
return -EINVAL;
}
delat_proposal += make_timespan(ts);
if (delat_proposal < real_clock::now()) {
return -EINVAL;
}
*delete_at = delat_proposal;
return 0;
}
int RGWPutObj_ObjStore_SWIFT::get_params()
{
if (s->has_bad_meta) {
return -EINVAL;
}
if (!s->length) {
const char *encoding = s->info.env->get("HTTP_TRANSFER_ENCODING");
if (!encoding || strcmp(encoding, "chunked") != 0) {
ldout(s->cct, 20) << "neither length nor chunked encoding" << dendl;
return -ERR_LENGTH_REQUIRED;
}
chunked_upload = true;
}
supplied_etag = s->info.env->get("HTTP_ETAG");
if (!s->generic_attrs.count(RGW_ATTR_CONTENT_TYPE)) {
ldout(s->cct, 5) << "content type wasn't provided, trying to guess" << dendl;
const char *suffix = strrchr(s->object.name.c_str(), '.');
if (suffix) {
suffix++;
if (*suffix) {
string suffix_str(suffix);
const char *mime = rgw_find_mime_by_ext(suffix_str);
if (mime) {
s->generic_attrs[RGW_ATTR_CONTENT_TYPE] = mime;
}
}
}
}
policy.create_default(s->user->user_id, s->user->display_name);
int r = get_delete_at_param(s, &delete_at);
if (r < 0) {
ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
return r;
}
dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
bool exists;
string multipart_manifest = s->info.args.get("multipart-manifest", &exists);
if (exists) {
if (multipart_manifest != "put") {
ldout(s->cct, 5) << "invalid multipart-manifest http param: " << multipart_manifest << dendl;
return -EINVAL;
}
#define MAX_SLO_ENTRY_SIZE (1024 + 128) // 1024 - max obj name, 128 - enough extra for other info
uint64_t max_len = s->cct->_conf->rgw_max_slo_entries * MAX_SLO_ENTRY_SIZE;
slo_info = new RGWSLOInfo;
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);
if (r < 0) {
ldout(s->cct, 5) << "failed to read input for slo r=" << r << dendl;
return r;
}
if ((int64_t)slo_info->entries.size() > s->cct->_conf->rgw_max_slo_entries) {
ldout(s->cct, 5) << "too many entries in slo request: " << slo_info->entries.size() << dendl;
return -EINVAL;
}
MD5 etag_sum;
uint64_t total_size = 0;
for (const auto& entry : slo_info->entries) {
etag_sum.Update((const byte *)entry.etag.c_str(),
entry.etag.length());
total_size += entry.size_bytes;
ldout(s->cct, 20) << "slo_part: " << entry.path
<< " size=" << entry.size_bytes
<< " etag=" << entry.etag
<< dendl;
}
complete_etag(etag_sum, &lo_etag);
slo_info->total_size = total_size;
ofs = slo_info->raw_data_len;
}
return RGWPutObj_ObjStore::get_params();
}
void RGWPutObj_ObjStore_SWIFT::send_response()
{
if (! op_ret) {
op_ret = STATUS_CREATED;
}
if (!lo_etag.empty()) {
/* Static Large Object of Swift API has two etags represented by
* following members:
* - etag - for the manifest itself (it will be stored in xattrs),
* - lo_etag - for the content composited from SLO's segments.
* The value is calculated basing on segments' etags.
* In response for PUT request we have to expose the second one.
* The first one may be obtained by GET with "multipart-manifest=get"
* in query string on a given SLO. */
dump_etag(s, ("\"" + lo_etag + "\"").c_str());
} else {
dump_etag(s, etag.c_str());
}
dump_last_modified(s, mtime);
set_req_state_err(s, op_ret);
dump_errno(s);
end_header(s, this);
rgw_flush_formatter_and_reset(s, s->formatter);
}
static int get_swift_account_settings(req_state * const s,
RGWRados * const store,
RGWAccessControlPolicy_SWIFTAcct * const policy,
bool * const has_policy)
{
*has_policy = false;
const char * const acl_attr = s->info.env->get("HTTP_X_ACCOUNT_ACCESS_CONTROL");
if (acl_attr) {
RGWAccessControlPolicy_SWIFTAcct swift_acct_policy(s->cct);
int r = swift_acct_policy.create(store,
s->user->user_id,
s->user->display_name,
string(acl_attr));
if (r < 0) {
return r;
}
*policy = swift_acct_policy;
*has_policy = true;
}
return 0;
}
int RGWPutMetadataAccount_ObjStore_SWIFT::get_params()
{
if (s->has_bad_meta) {
return -EINVAL;
}
int ret = get_swift_account_settings(s,
store,
// FIXME: we need to carry unique_ptr in generic class
// and allocate appropriate ACL class in the ctor
static_cast<RGWAccessControlPolicy_SWIFTAcct *>(&policy),
&has_policy);
if (ret < 0) {
return ret;
}
get_rmattrs_from_headers(s, ACCT_PUT_ATTR_PREFIX, ACCT_REMOVE_ATTR_PREFIX,
rmattr_names);
return 0;
}
void RGWPutMetadataAccount_ObjStore_SWIFT::send_response()
{
if (! op_ret) {
op_ret = STATUS_NO_CONTENT;
}
set_req_state_err(s, op_ret);
dump_errno(s);
end_header(s, this);
rgw_flush_formatter_and_reset(s, s->formatter);
}
int RGWPutMetadataBucket_ObjStore_SWIFT::get_params()
{
if (s->has_bad_meta) {
return -EINVAL;
}
int r = get_swift_container_settings(s, store, &policy, &has_policy,
&cors_config, &has_cors);
if (r < 0) {
return r;
}
get_rmattrs_from_headers(s, CONT_PUT_ATTR_PREFIX, CONT_REMOVE_ATTR_PREFIX,
rmattr_names);
placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
return get_swift_versioning_settings(s, swift_ver_location);
}
void RGWPutMetadataBucket_ObjStore_SWIFT::send_response()
{
if (! op_ret) {
op_ret = STATUS_NO_CONTENT;
}
set_req_state_err(s, op_ret);
dump_errno(s);
end_header(s, this);
rgw_flush_formatter_and_reset(s, s->formatter);
}
int RGWPutMetadataObject_ObjStore_SWIFT::get_params()
{
if (s->has_bad_meta) {
return -EINVAL;
}
/* Handle Swift object expiration. */
int r = get_delete_at_param(s, &delete_at);
if (r < 0) {
ldout(s->cct, 5) << "ERROR: failed to get Delete-At param" << dendl;
return r;
}
placement_rule = s->info.env->get("HTTP_X_STORAGE_POLICY", "");
dlo_manifest = s->info.env->get("HTTP_X_OBJECT_MANIFEST");
return 0;
}
void RGWPutMetadataObject_ObjStore_SWIFT::send_response()
{
if (! op_ret) {
op_ret = STATUS_ACCEPTED;
}
set_req_state_err(s, op_ret);
if (!s->err.is_err()) {
dump_content_length(s, 0);
}
dump_errno(s);
end_header(s, this);
rgw_flush_formatter_and_reset(s, s->formatter);
}
static void bulkdelete_respond(const unsigned num_deleted,
const unsigned int num_unfound,
const std::list<RGWBulkDelete::fail_desc_t>& failures,
const int prot_flags, /* in */
ceph::Formatter& formatter) /* out */
{
formatter.open_object_section("delete");
string resp_status;
string resp_body;
if (!failures.empty()) {
int reason = ERR_INVALID_REQUEST;
for (const auto fail_desc : failures) {
if (-ENOENT != fail_desc.err && -EACCES != fail_desc.err) {
reason = fail_desc.err;
}
}
rgw_err err;
set_req_state_err(err, reason, prot_flags);
dump_errno(err, resp_status);
} else if (0 == num_deleted && 0 == num_unfound) {
/* 400 Bad Request */
dump_errno(400, resp_status);
resp_body = "Invalid bulk delete.";
} else {
/* 200 OK */
dump_errno(200, resp_status);
}
encode_json("Number Deleted", num_deleted, &formatter);
encode_json("Number Not Found", num_unfound, &formatter);
encode_json("Response Body", resp_body, &formatter);
encode_json("Response Status", resp_status, &formatter);
formatter.open_array_section("Errors");
for (const auto fail_desc : failures) {
formatter.open_array_section("object");
stringstream ss_name;
ss_name << fail_desc.path;
encode_json("Name", ss_name.str(), &formatter);
rgw_err err;
set_req_state_err(err, fail_desc.err, prot_flags);
string status;
dump_errno(err, status);
encode_json("Status", status, &formatter);
formatter.close_section();
}
formatter.close_section();
formatter.close_section();
}
int RGWDeleteObj_ObjStore_SWIFT::get_params()
{
const string& mm = s->info.args.get("multipart-manifest");
multipart_delete = (mm.compare("delete") == 0);
return RGWDeleteObj_ObjStore::get_params();
}
void RGWDeleteObj_ObjStore_SWIFT::send_response()
{
int r = op_ret;
if (multipart_delete) {
r = 0;
} else if(!r) {
r = STATUS_NO_CONTENT;
}
set_req_state_err(s, r);
dump_errno(s);
if (multipart_delete) {
end_header(s, this /* RGWOp */, nullptr /* contype */,
NO_CONTENT_LENGTH);
if (deleter) {
bulkdelete_respond(deleter->get_num_deleted(),
deleter->get_num_unfound(),
deleter->get_failures(),
s->prot_flags,