1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_RGW_REST_CONN_H
5 #define CEPH_RGW_REST_CONN_H
8 #include "rgw_rest_client.h"
9 #include "common/ceph_json.h"
10 #include "common/RefCountedObj.h"
18 static int parse_decode_json(CephContext
*cct
, T
& t
, bufferlist
& bl
)
21 int ret
= p
.parse(bl
.c_str(), bl
.length());
27 decode_json_obj(t
, &p
);
28 } catch (JSONDecoder::err
& e
) {
34 struct rgw_http_param_pair
{
39 // copy a null-terminated rgw_http_param_pair list into a list of string pairs
40 inline param_vec_t
make_param_list(const rgw_http_param_pair
* pp
)
43 while (pp
&& pp
->key
) {
45 string v
= (pp
->val
? pp
->val
: "");
46 params
.emplace_back(make_pair(std::move(k
), std::move(v
)));
55 vector
<string
> endpoints
;
57 string self_zone_group
;
59 std::atomic
<int64_t> counter
= { 0 };
63 RGWRESTConn(CephContext
*_cct
, RGWRados
*store
, const string
& _remote_id
, const list
<string
>& endpoints
);
64 // custom move needed for atomic
65 RGWRESTConn(RGWRESTConn
&& other
);
66 RGWRESTConn
& operator=(RGWRESTConn
&& other
);
68 int get_url(string
& endpoint
);
70 const string
& get_self_zonegroup() {
71 return self_zone_group
;
73 const string
& get_remote_id() {
76 RGWAccessKey
& get_key() {
80 CephContext
*get_ctx() {
83 size_t get_endpoint_count() const { return endpoints
.size(); }
86 int forward(const rgw_user
& uid
, req_info
& info
, obj_version
*objv
, size_t max_response
, bufferlist
*inbl
, bufferlist
*outbl
);
89 int put_obj_init(const rgw_user
& uid
, rgw_obj
& obj
, uint64_t obj_size
,
90 map
<string
, bufferlist
>& attrs
, RGWRESTStreamWriteRequest
**req
);
91 int complete_request(RGWRESTStreamWriteRequest
*req
, string
& etag
, ceph::real_time
*mtime
);
93 int get_obj(const rgw_user
& uid
, req_info
*info
/* optional */, rgw_obj
& obj
,
94 const ceph::real_time
*mod_ptr
, const ceph::real_time
*unmod_ptr
,
95 uint32_t mod_zone_id
, uint64_t mod_pg_ver
,
96 bool prepend_metadata
, bool get_op
, bool rgwx_stat
, bool sync_manifest
,
97 RGWGetDataCB
*cb
, RGWRESTStreamRWRequest
**req
);
98 int complete_request(RGWRESTStreamRWRequest
*req
, string
& etag
, ceph::real_time
*mtime
, uint64_t *psize
, map
<string
, string
>& attrs
);
100 int get_resource(const string
& resource
,
101 param_vec_t
*extra_params
,
102 map
<string
, string
>* extra_headers
,
103 bufferlist
& bl
, RGWHTTPManager
*mgr
= NULL
);
106 int get_json_resource(const string
& resource
, param_vec_t
*params
, T
& t
);
108 int get_json_resource(const string
& resource
, const rgw_http_param_pair
*pp
, T
& t
);
113 int RGWRESTConn::get_json_resource(const string
& resource
, param_vec_t
*params
, T
& t
)
116 int ret
= get_resource(resource
, params
, NULL
, bl
);
121 ret
= parse_decode_json(cct
, t
, bl
);
130 int RGWRESTConn::get_json_resource(const string
& resource
, const rgw_http_param_pair
*pp
, T
& t
)
132 param_vec_t params
= make_param_list(pp
);
133 return get_json_resource(resource
, ¶ms
, t
);
136 class RGWStreamIntoBufferlist
: public RGWGetDataCB
{
139 RGWStreamIntoBufferlist(bufferlist
& _bl
) : bl(_bl
) {}
140 int handle_data(bufferlist
& inbl
, off_t bl_ofs
, off_t bl_len
) override
{
141 bl
.claim_append(inbl
);
146 class RGWRESTReadResource
: public RefCountedObject
{
151 map
<string
, string
> headers
;
153 RGWStreamIntoBufferlist cb
;
156 RGWRESTStreamReadRequest req
;
158 void init_common(param_vec_t
*extra_headers
);
161 RGWRESTReadResource(RGWRESTConn
*_conn
,
162 const string
& _resource
,
163 const rgw_http_param_pair
*pp
,
164 param_vec_t
*extra_headers
,
165 RGWHTTPManager
*_mgr
);
167 RGWRESTReadResource(RGWRESTConn
*_conn
,
168 const string
& _resource
,
169 param_vec_t
& _params
,
170 param_vec_t
*extra_headers
,
171 RGWHTTPManager
*_mgr
);
173 void set_user_info(void *user_info
) {
174 req
.set_user_info(user_info
);
176 void *get_user_info() {
177 return req
.get_user_info();
181 int decode_resource(T
*dest
);
191 int get_http_status() {
192 return req
.get_http_status();
195 int wait_bl(bufferlist
*pbl
) {
196 int ret
= req
.wait();
201 if (req
.get_status() < 0) {
202 return req
.get_status();
217 int RGWRESTReadResource::decode_resource(T
*dest
)
219 int ret
= req
.get_status();
223 ret
= parse_decode_json(cct
, *dest
, bl
);
231 int RGWRESTReadResource::fetch(T
*dest
)
238 ret
= decode_resource(dest
);
246 int RGWRESTReadResource::wait(T
*dest
)
248 int ret
= req
.wait();
253 ret
= decode_resource(dest
);
260 class RGWRESTSendResource
: public RefCountedObject
{
266 map
<string
, string
> headers
;
268 RGWStreamIntoBufferlist cb
;
271 RGWRESTStreamRWRequest req
;
273 void init_common(param_vec_t
*extra_headers
);
276 RGWRESTSendResource(RGWRESTConn
*_conn
,
277 const string
& _method
,
278 const string
& _resource
,
279 const rgw_http_param_pair
*pp
,
280 param_vec_t
*extra_headers
,
281 RGWHTTPManager
*_mgr
);
283 RGWRESTSendResource(RGWRESTConn
*_conn
,
284 const string
& _method
,
285 const string
& _resource
,
287 param_vec_t
*extra_headers
,
288 RGWHTTPManager
*_mgr
);
290 void set_user_info(void *user_info
) {
291 req
.set_user_info(user_info
);
293 void *get_user_info() {
294 return req
.get_user_info();
298 int decode_resource(T
*dest
);
300 int send(bufferlist
& bl
);
302 int aio_send(bufferlist
& bl
);
308 int get_http_status() {
309 return req
.get_http_status();
312 int wait_bl(bufferlist
*pbl
) {
313 int ret
= req
.wait();
318 if (req
.get_status() < 0) {
319 return req
.get_status();
330 int RGWRESTSendResource::decode_resource(T
*dest
)
332 int ret
= req
.get_status();
336 ret
= parse_decode_json(cct
, *dest
, bl
);
344 int RGWRESTSendResource::wait(T
*dest
)
346 int ret
= req
.wait();
351 ret
= decode_resource(dest
);
358 class RGWRESTPostResource
: public RGWRESTSendResource
{
360 RGWRESTPostResource(RGWRESTConn
*_conn
,
361 const string
& _resource
,
362 const rgw_http_param_pair
*pp
,
363 param_vec_t
*extra_headers
,
364 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "POST", _resource
,
365 pp
, extra_headers
, _mgr
) {}
367 RGWRESTPostResource(RGWRESTConn
*_conn
,
368 const string
& _resource
,
370 param_vec_t
*extra_headers
,
371 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "POST", _resource
,
372 params
, extra_headers
, _mgr
) {}
376 class RGWRESTPutResource
: public RGWRESTSendResource
{
378 RGWRESTPutResource(RGWRESTConn
*_conn
,
379 const string
& _resource
,
380 const rgw_http_param_pair
*pp
,
381 param_vec_t
*extra_headers
,
382 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "PUT", _resource
,
383 pp
, extra_headers
, _mgr
) {}
385 RGWRESTPutResource(RGWRESTConn
*_conn
,
386 const string
& _resource
,
388 param_vec_t
*extra_headers
,
389 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "PUT", _resource
,
390 params
, extra_headers
, _mgr
) {}
394 class RGWRESTDeleteResource
: public RGWRESTSendResource
{
396 RGWRESTDeleteResource(RGWRESTConn
*_conn
,
397 const string
& _resource
,
398 const rgw_http_param_pair
*pp
,
399 param_vec_t
*extra_headers
,
400 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "DELETE", _resource
,
401 pp
, extra_headers
, _mgr
) {}
403 RGWRESTDeleteResource(RGWRESTConn
*_conn
,
404 const string
& _resource
,
406 param_vec_t
*extra_headers
,
407 RGWHTTPManager
*_mgr
) : RGWRESTSendResource(_conn
, "DELETE", _resource
,
408 params
, extra_headers
, _mgr
) {}