]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_cr_rest.h
import ceph 12.2.12
[ceph.git] / ceph / src / rgw / rgw_cr_rest.h
CommitLineData
7c673cae
FG
1#ifndef CEPH_RGW_CR_REST_H
2#define CEPH_RGW_CR_REST_H
3
4#include <boost/intrusive_ptr.hpp>
5#include "include/assert.h" // boost header clobbers our assert.h
6
7#include "rgw_coroutine.h"
8#include "rgw_rest_conn.h"
9
10template <class T>
11class RGWReadRESTResourceCR : public RGWSimpleCoroutine {
12 RGWRESTConn *conn;
13 RGWHTTPManager *http_manager;
14 string path;
15 param_vec_t params;
16 T *result;
17
a8e16298
TL
18 param_vec_t extra_headers;
19public:
7c673cae
FG
20 boost::intrusive_ptr<RGWRESTReadResource> http_op;
21
22public:
23 RGWReadRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
24 RGWHTTPManager *_http_manager, const string& _path,
25 rgw_http_param_pair *params, T *_result)
26 : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
27 path(_path), params(make_param_list(params)), result(_result)
28 {}
29
a8e16298
TL
30 RGWReadRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
31 RGWHTTPManager *_http_manager, const string& _path,
32 rgw_http_param_pair *params,
33 std::map <std::string, std::string> *hdrs,
34 T *_result)
35 : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
36 path(_path), params(make_param_list(params)),
37 result(_result), extra_headers(make_param_list(hdrs))
38 {}
39
40
7c673cae
FG
41 ~RGWReadRESTResourceCR() override {
42 request_cleanup();
43 }
44
45 int send_request() override {
46 auto op = boost::intrusive_ptr<RGWRESTReadResource>(
a8e16298 47 new RGWRESTReadResource(conn, path, params, &extra_headers, http_manager));
7c673cae
FG
48
49 op->set_user_info((void *)stack);
50
51 int ret = op->aio_read();
52 if (ret < 0) {
53 log_error() << "failed to send http operation: " << op->to_str()
54 << " ret=" << ret << std::endl;
55 op->put();
56 return ret;
57 }
58 std::swap(http_op, op); // store reference in http_op on success
59 return 0;
60 }
61
62 int request_complete() override {
63 int ret = http_op->wait(result);
64 auto op = std::move(http_op); // release ref on return
65 if (ret < 0) {
66 error_stream << "http operation failed: " << op->to_str()
67 << " status=" << op->get_http_status() << std::endl;
68 op->put();
69 return ret;
70 }
71 op->put();
72 return 0;
73 }
74
75 void request_cleanup() override {
76 if (http_op) {
77 http_op->put();
78 http_op = NULL;
79 }
80 }
81};
82
a8e16298 83template <class S, class T, class E = int>
7c673cae
FG
84class RGWSendRESTResourceCR : public RGWSimpleCoroutine {
85 RGWRESTConn *conn;
86 RGWHTTPManager *http_manager;
87 string method;
88 string path;
89 param_vec_t params;
a8e16298 90 param_vec_t headers;
7c673cae 91 T *result;
a8e16298
TL
92 E *err_result;
93 bufferlist input_bl;
7c673cae
FG
94
95 boost::intrusive_ptr<RGWRESTSendResource> http_op;
96
97public:
98 RGWSendRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
99 RGWHTTPManager *_http_manager,
100 const string& _method, const string& _path,
a8e16298
TL
101 rgw_http_param_pair *_params, map<string, string> *_attrs,
102 S& _input, T *_result, E *_err_result = nullptr)
7c673cae 103 : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
a8e16298
TL
104 method(_method), path(_path), params(make_param_list(_params)), headers(make_param_list(_attrs)),
105 result(_result), err_result(_err_result) {
106 JSONFormatter jf;
107 encode_json("data", _input, &jf);
108 std::stringstream ss;
109 jf.flush(ss);
110 //bufferlist bl;
111 this->input_bl.append(ss.str());
112 }
7c673cae
FG
113
114 ~RGWSendRESTResourceCR() override {
115 request_cleanup();
116 }
117
118 int send_request() override {
119 auto op = boost::intrusive_ptr<RGWRESTSendResource>(
a8e16298 120 new RGWRESTSendResource(conn, method, path, params, &headers, http_manager));
7c673cae
FG
121
122 op->set_user_info((void *)stack);
123
a8e16298 124 int ret = op->aio_send(input_bl);
7c673cae
FG
125 if (ret < 0) {
126 lsubdout(cct, rgw, 0) << "ERROR: failed to send request" << dendl;
127 op->put();
128 return ret;
129 }
130 std::swap(http_op, op); // store reference in http_op on success
131 return 0;
132 }
133
134 int request_complete() override {
135 int ret;
a8e16298
TL
136 if (result || err_result) {
137 ret = http_op->wait(result, err_result);
7c673cae
FG
138 } else {
139 bufferlist bl;
140 ret = http_op->wait_bl(&bl);
141 }
142 auto op = std::move(http_op); // release ref on return
143 if (ret < 0) {
144 error_stream << "http operation failed: " << op->to_str()
145 << " status=" << op->get_http_status() << std::endl;
146 lsubdout(cct, rgw, 5) << "failed to wait for op, ret=" << ret
147 << ": " << op->to_str() << dendl;
148 op->put();
149 return ret;
150 }
151 op->put();
152 return 0;
153 }
154
155 void request_cleanup() override {
156 if (http_op) {
157 http_op->put();
158 http_op = NULL;
159 }
160 }
161};
162
a8e16298
TL
163template <class S, class T, class E = int>
164class RGWPostRESTResourceCR : public RGWSendRESTResourceCR<S, T, E> {
7c673cae
FG
165public:
166 RGWPostRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
167 RGWHTTPManager *_http_manager,
168 const string& _path,
a8e16298
TL
169 rgw_http_param_pair *_params, S& _input,
170 T *_result, E *_err_result = nullptr)
171 : RGWSendRESTResourceCR<S, T, E>(_cct, _conn, _http_manager,
7c673cae 172 "POST", _path,
a8e16298 173 _params, nullptr, _input, _result, _err_result) {}
7c673cae
FG
174};
175
a8e16298
TL
176template <class S, class T, class E = int>
177class RGWPutRESTResourceCR : public RGWSendRESTResourceCR<S, T, E> {
7c673cae
FG
178public:
179 RGWPutRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
180 RGWHTTPManager *_http_manager,
181 const string& _path,
a8e16298
TL
182 rgw_http_param_pair *_params, S& _input,
183 T *_result, E *_err_result = nullptr)
184 : RGWSendRESTResourceCR<S, T, E>(_cct, _conn, _http_manager,
185 "PUT", _path,
186 _params, nullptr, _input,
187 _result, _err_result) {}
188
189 RGWPutRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
190 RGWHTTPManager *_http_manager,
191 const string& _path,
192 rgw_http_param_pair *_params,
193 map <string, string> *_attrs,
194 S& _input, T *_result, E *_err_result = nullptr)
195 : RGWSendRESTResourceCR<S, T, E>(_cct, _conn, _http_manager,
196 "PUT", _path,
197 _params, _attrs, _input,
198 _result, _err_result) {}
7c673cae
FG
199};
200
201class RGWDeleteRESTResourceCR : public RGWSimpleCoroutine {
202 RGWRESTConn *conn;
203 RGWHTTPManager *http_manager;
204 string path;
205 param_vec_t params;
206
207 boost::intrusive_ptr<RGWRESTDeleteResource> http_op;
208
209public:
210 RGWDeleteRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
211 RGWHTTPManager *_http_manager,
212 const string& _path,
213 rgw_http_param_pair *_params)
214 : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
215 path(_path), params(make_param_list(_params))
216 {}
217
218 ~RGWDeleteRESTResourceCR() override {
219 request_cleanup();
220 }
221
222 int send_request() override {
223 auto op = boost::intrusive_ptr<RGWRESTDeleteResource>(
224 new RGWRESTDeleteResource(conn, path, params, nullptr, http_manager));
225
226 op->set_user_info((void *)stack);
227
228 bufferlist bl;
229
230 int ret = op->aio_send(bl);
231 if (ret < 0) {
232 lsubdout(cct, rgw, 0) << "ERROR: failed to send DELETE request" << dendl;
233 op->put();
234 return ret;
235 }
236 std::swap(http_op, op); // store reference in http_op on success
237 return 0;
238 }
239
240 int request_complete() override {
241 int ret;
242 bufferlist bl;
243 ret = http_op->wait_bl(&bl);
244 auto op = std::move(http_op); // release ref on return
245 if (ret < 0) {
246 error_stream << "http operation failed: " << op->to_str()
247 << " status=" << op->get_http_status() << std::endl;
248 lsubdout(cct, rgw, 5) << "failed to wait for op, ret=" << ret
249 << ": " << op->to_str() << dendl;
250 op->put();
251 return ret;
252 }
253 op->put();
254 return 0;
255 }
256
257 void request_cleanup() override {
258 if (http_op) {
259 http_op->put();
260 http_op = NULL;
261 }
262 }
263};
264
265#endif