1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
6 #include "rgw_rest_client.h"
7 #include "common/ceph_json.h"
8 #include "common/RefCountedObj.h"
9 #include "include/common_fwd.h"
16 static int parse_decode_json(T
& t
, bufferlist
& bl
)
19 if (!p
.parse(bl
.c_str(), bl
.length())) {
24 decode_json_obj(t
, &p
);
25 } catch (JSONDecoder::err
& e
) {
31 struct rgw_http_param_pair
{
36 // append a null-terminated rgw_http_param_pair list into a list of string pairs
37 inline void append_param_list(param_vec_t
& params
, const rgw_http_param_pair
* pp
)
39 while (pp
&& pp
->key
) {
41 string v
= (pp
->val
? pp
->val
: "");
42 params
.emplace_back(make_pair(std::move(k
), std::move(v
)));
47 // copy a null-terminated rgw_http_param_pair list into a list of string pairs
48 inline param_vec_t
make_param_list(const rgw_http_param_pair
* pp
)
51 append_param_list(params
, pp
);
55 inline param_vec_t
make_param_list(const map
<string
, string
> *pp
)
61 for (auto iter
: *pp
) {
62 params
.emplace_back(make_pair(iter
.first
, iter
.second
));
70 vector
<string
> endpoints
;
72 string self_zone_group
;
75 std::atomic
<int64_t> counter
= { 0 };
79 RGWRESTConn(CephContext
*_cct
, RGWSI_Zone
*zone_svc
, const string
& _remote_id
, const list
<string
>& endpoints
, HostStyle _host_style
= PathStyle
);
80 RGWRESTConn(CephContext
*_cct
, RGWSI_Zone
*zone_svc
, const string
& _remote_id
, const list
<string
>& endpoints
, RGWAccessKey _cred
, HostStyle _host_style
= PathStyle
);
82 // custom move needed for atomic
83 RGWRESTConn(RGWRESTConn
&& other
);
84 RGWRESTConn
& operator=(RGWRESTConn
&& other
);
85 virtual ~RGWRESTConn() = default;
87 int get_url(string
& endpoint
);
89 const string
& get_self_zonegroup() {
90 return self_zone_group
;
92 const string
& get_remote_id() {
95 RGWAccessKey
& get_key() {
99 HostStyle
get_host_style() {
103 CephContext
*get_ctx() {
106 size_t get_endpoint_count() const { return endpoints
.size(); }
108 virtual void populate_params(param_vec_t
& params
, const rgw_user
*uid
, const string
& zonegroup
);
111 int forward(const rgw_user
& uid
, req_info
& info
, obj_version
*objv
, size_t max_response
, bufferlist
*inbl
, bufferlist
*outbl
, optional_yield y
);
115 int put_obj_send_init(rgw::sal::RGWObject
* obj
, const rgw_http_param_pair
*extra_params
, RGWRESTStreamS3PutObj
**req
);
116 int put_obj_async(const rgw_user
& uid
, rgw::sal::RGWObject
* obj
, uint64_t obj_size
,
117 map
<string
, bufferlist
>& attrs
, bool send
, RGWRESTStreamS3PutObj
**req
);
118 int complete_request(RGWRESTStreamS3PutObj
*req
, string
& etag
,
119 ceph::real_time
*mtime
, optional_yield y
);
121 struct get_obj_params
{
123 req_info
*info
{nullptr};
124 const ceph::real_time
*mod_ptr
{nullptr};
125 const ceph::real_time
*unmod_ptr
{nullptr};
126 bool high_precision_time
{true};
130 uint32_t mod_zone_id
{0};
131 uint64_t mod_pg_ver
{0};
133 bool prepend_metadata
{false};
135 bool rgwx_stat
{false};
136 bool sync_manifest
{false};
138 bool skip_decrypt
{true};
139 RGWHTTPStreamRWRequest::ReceiveCB
*cb
{nullptr};
141 bool range_is_set
{false};
142 uint64_t range_start
{0};
143 uint64_t range_end
{0};
146 int get_obj(const rgw::sal::RGWObject
* obj
, const get_obj_params
& params
, bool send
, RGWRESTStreamRWRequest
**req
);
148 int get_obj(const rgw_user
& uid
, req_info
*info
/* optional */, const rgw::sal::RGWObject
* obj
,
149 const ceph::real_time
*mod_ptr
, const ceph::real_time
*unmod_ptr
,
150 uint32_t mod_zone_id
, uint64_t mod_pg_ver
,
151 bool prepend_metadata
, bool get_op
, bool rgwx_stat
, bool sync_manifest
,
152 bool skip_decrypt
, bool send
, RGWHTTPStreamRWRequest::ReceiveCB
*cb
, RGWRESTStreamRWRequest
**req
);
153 int complete_request(RGWRESTStreamRWRequest
*req
,
155 ceph::real_time
*mtime
,
157 map
<string
, string
> *pattrs
,
158 map
<string
, string
> *pheaders
,
161 int get_resource(const string
& resource
,
162 param_vec_t
*extra_params
,
163 map
<string
, string
>* extra_headers
,
165 bufferlist
*send_data
,
170 int get_json_resource(const string
& resource
, param_vec_t
*params
,
171 bufferlist
*in_data
, optional_yield y
, T
& t
);
173 int get_json_resource(const string
& resource
, param_vec_t
*params
,
174 optional_yield y
, T
& t
);
176 int get_json_resource(const string
& resource
, const rgw_http_param_pair
*pp
,
177 optional_yield y
, T
& t
);
180 void populate_zonegroup(param_vec_t
& params
, const string
& zonegroup
) {
181 if (!zonegroup
.empty()) {
182 params
.push_back(param_pair_t(RGW_SYS_PARAM_PREFIX
"zonegroup", zonegroup
));
185 void populate_uid(param_vec_t
& params
, const rgw_user
*uid
) {
187 string uid_str
= uid
->to_str();
189 params
.push_back(param_pair_t(RGW_SYS_PARAM_PREFIX
"uid", uid_str
));
195 class S3RESTConn
: public RGWRESTConn
{
199 S3RESTConn(CephContext
*_cct
, RGWSI_Zone
*svc_zone
, const string
& _remote_id
, const list
<string
>& endpoints
, HostStyle _host_style
= PathStyle
) :
200 RGWRESTConn(_cct
, svc_zone
, _remote_id
, endpoints
, _host_style
) {}
202 S3RESTConn(CephContext
*_cct
, RGWSI_Zone
*svc_zone
, const string
& _remote_id
, const list
<string
>& endpoints
, RGWAccessKey _cred
, HostStyle _host_style
= PathStyle
):
203 RGWRESTConn(_cct
, svc_zone
, _remote_id
, endpoints
, _cred
, _host_style
) {}
204 ~S3RESTConn() override
= default;
206 void populate_params(param_vec_t
& params
, const rgw_user
*uid
, const string
& zonegroup
) override
{
207 // do not populate any params in S3 REST Connection.
214 int RGWRESTConn::get_json_resource(const string
& resource
, param_vec_t
*params
,
215 bufferlist
*in_data
, optional_yield y
, T
& t
)
218 int ret
= get_resource(resource
, params
, nullptr, bl
, in_data
, nullptr, y
);
223 ret
= parse_decode_json(t
, bl
);
232 int RGWRESTConn::get_json_resource(const string
& resource
, param_vec_t
*params
,
233 optional_yield y
, T
& t
)
235 return get_json_resource(resource
, params
, nullptr, y
, t
);
239 int RGWRESTConn::get_json_resource(const string
& resource
, const rgw_http_param_pair
*pp
,
240 optional_yield y
, T
& t
)
242 param_vec_t params
= make_param_list(pp
);
243 return get_json_resource(resource
, ¶ms
, y
, t
);
246 class RGWStreamIntoBufferlist
: public RGWHTTPStreamRWRequest::ReceiveCB
{
249 explicit RGWStreamIntoBufferlist(bufferlist
& _bl
) : bl(_bl
) {}
250 int handle_data(bufferlist
& inbl
, bool *pause
) override
{
251 bl
.claim_append(inbl
);
252 return inbl
.length();
256 class RGWRESTReadResource
: public RefCountedObject
, public RGWIOProvider
{
261 map
<string
, string
> headers
;
263 RGWStreamIntoBufferlist cb
;
266 RGWRESTStreamReadRequest req
;
268 void init_common(param_vec_t
*extra_headers
);
271 RGWRESTReadResource(RGWRESTConn
*_conn
,
272 const string
& _resource
,
273 const rgw_http_param_pair
*pp
,
274 param_vec_t
*extra_headers
,
275 RGWHTTPManager
*_mgr
);
277 RGWRESTReadResource(RGWRESTConn
*_conn
,
278 const string
& _resource
,
279 param_vec_t
& _params
,
280 param_vec_t
*extra_headers
,
281 RGWHTTPManager
*_mgr
);
282 ~RGWRESTReadResource() = default;
284 rgw_io_id
get_io_id(int io_type
) {
285 return req
.get_io_id(io_type
);
288 void set_io_user_info(void *user_info
) override
{
289 req
.set_io_user_info(user_info
);
292 void *get_io_user_info() override
{
293 return req
.get_io_user_info();
297 int decode_resource(T
*dest
);
299 int read(optional_yield y
);
307 int get_http_status() {
308 return req
.get_http_status();
311 int wait(bufferlist
*pbl
, optional_yield y
) {
312 int ret
= req
.wait(y
);
317 if (req
.get_status() < 0) {
318 return req
.get_status();
325 int wait(T
*dest
, optional_yield y
);
328 int fetch(T
*dest
, optional_yield y
);
333 int RGWRESTReadResource::decode_resource(T
*dest
)
335 int ret
= req
.get_status();
339 ret
= parse_decode_json(*dest
, bl
);
347 int RGWRESTReadResource::fetch(T
*dest
, optional_yield y
)
354 ret
= decode_resource(dest
);
362 int RGWRESTReadResource::wait(T
*dest
, optional_yield y
)
364 int ret
= req
.wait(y
);
369 ret
= decode_resource(dest
);
376 class RGWRESTSendResource
: public RefCountedObject
, public RGWIOProvider
{
382 map
<string
, string
> headers
;
384 RGWStreamIntoBufferlist cb
;
387 RGWRESTStreamRWRequest req
;
389 void init_common(param_vec_t
*extra_headers
);
392 RGWRESTSendResource(RGWRESTConn
*_conn
,
393 const string
& _method
,
394 const string
& _resource
,
395 const rgw_http_param_pair
*pp
,
396 param_vec_t
*extra_headers
,
397 RGWHTTPManager
*_mgr
);
399 RGWRESTSendResource(RGWRESTConn
*_conn
,
400 const string
& _method
,
401 const string
& _resource
,
403 param_vec_t
*extra_headers
,
404 RGWHTTPManager
*_mgr
);
406 ~RGWRESTSendResource() = default;
408 rgw_io_id
get_io_id(int io_type
) {
409 return req
.get_io_id(io_type
);
412 void set_io_user_info(void *user_info
) override
{
413 req
.set_io_user_info(user_info
);
416 void *get_io_user_info() override
{
417 return req
.get_io_user_info();
420 int send(bufferlist
& bl
, optional_yield y
);
422 int aio_send(bufferlist
& bl
);
428 int get_http_status() {
429 return req
.get_http_status();
432 template <class E
= int>
433 int wait(bufferlist
*pbl
, optional_yield y
, E
*err_result
= nullptr) {
434 int ret
= req
.wait(y
);
437 if (ret
< 0 && err_result
) {
438 ret
= parse_decode_json(*err_result
, bl
);
441 return req
.get_status();
444 template <class T
, class E
= int>
445 int wait(T
*dest
, optional_yield y
, E
*err_result
= nullptr);
448 template <class T
, class E
>
449 int RGWRESTSendResource::wait(T
*dest
, optional_yield y
, E
*err_result
)
451 int ret
= req
.wait(y
);
453 ret
= req
.get_status();
456 if (ret
< 0 && err_result
) {
457 ret
= parse_decode_json(*err_result
, bl
);
464 ret
= parse_decode_json(*dest
, bl
);
472 class RGWRESTPostResource
: public RGWRESTSendResource
{
474 RGWRESTPostResource(RGWRESTConn
*_conn
,
475 const string
& _resource
,
476 const rgw_http_param_pair
*pp
,
477 param_vec_t
*extra_headers
,
478 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "POST", _resource
,
479 pp
, extra_headers
, _mgr
) {}
481 RGWRESTPostResource(RGWRESTConn
*_conn
,
482 const string
& _resource
,
484 param_vec_t
*extra_headers
,
485 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "POST", _resource
,
486 params
, extra_headers
, _mgr
) {}
490 class RGWRESTPutResource
: public RGWRESTSendResource
{
492 RGWRESTPutResource(RGWRESTConn
*_conn
,
493 const string
& _resource
,
494 const rgw_http_param_pair
*pp
,
495 param_vec_t
*extra_headers
,
496 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "PUT", _resource
,
497 pp
, extra_headers
, _mgr
) {}
499 RGWRESTPutResource(RGWRESTConn
*_conn
,
500 const string
& _resource
,
502 param_vec_t
*extra_headers
,
503 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "PUT", _resource
,
504 params
, extra_headers
, _mgr
) {}
508 class RGWRESTDeleteResource
: public RGWRESTSendResource
{
510 RGWRESTDeleteResource(RGWRESTConn
*_conn
,
511 const string
& _resource
,
512 const rgw_http_param_pair
*pp
,
513 param_vec_t
*extra_headers
,
514 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "DELETE", _resource
,
515 pp
, extra_headers
, _mgr
) {}
517 RGWRESTDeleteResource(RGWRESTConn
*_conn
,
518 const string
& _resource
,
520 param_vec_t
*extra_headers
,
521 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "DELETE", _resource
,
522 params
, extra_headers
, _mgr
) {}