-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
rgw_cr_rest.h
133 lines (113 loc) · 3.49 KB
/
rgw_cr_rest.h
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
#ifndef CEPH_RGW_CR_REST_H
#define CEPH_RGW_CR_REST_H
#include <boost/intrusive_ptr.hpp>
#include "include/assert.h" // boost header clobbers our assert.h
#include "rgw_coroutine.h"
#include "rgw_rest_conn.h"
template <class T>
class RGWReadRESTResourceCR : public RGWSimpleCoroutine {
RGWRESTConn *conn;
RGWHTTPManager *http_manager;
string path;
param_list_t params;
T *result;
boost::intrusive_ptr<RGWRESTReadResource> http_op;
public:
RGWReadRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
RGWHTTPManager *_http_manager, const string& _path,
rgw_http_param_pair *params, T *_result)
: RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
path(_path), params(make_param_list(params)), result(_result)
{}
int send_request() {
auto op = boost::intrusive_ptr<RGWRESTReadResource>(
new RGWRESTReadResource(conn, path, params, NULL, http_manager));
op->set_user_info((void *)stack);
int ret = op->aio_read();
if (ret < 0) {
log_error() << "failed to send http operation: " << op->to_str()
<< " ret=" << ret << std::endl;
return ret;
}
std::swap(http_op, op); // store reference in http_op on success
return 0;
}
int request_complete() {
int ret = http_op->wait(result);
auto op = std::move(http_op); // release ref on return
if (ret < 0) {
error_stream << "http operation failed: " << op->to_str()
<< " status=" << op->get_http_status() << std::endl;
return ret;
}
return 0;
}
void request_cleanup() {
if (http_op) {
http_op->put();
}
}
};
template <class S, class T>
class RGWPostRESTResourceCR : public RGWSimpleCoroutine {
RGWRESTConn *conn;
RGWHTTPManager *http_manager;
string path;
param_list_t params;
T *result;
S input;
boost::intrusive_ptr<RGWRESTPostResource> http_op;
public:
RGWPostRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
RGWHTTPManager *_http_manager, const string& _path,
rgw_http_param_pair *_params, S& _input, T *_result)
: RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
path(_path), params(make_param_list(_params)), result(_result),
input(_input)
{}
int send_request() {
auto op = boost::intrusive_ptr<RGWRESTPostResource>(
new RGWRESTPostResource(conn, path, params, NULL, http_manager));
op->set_user_info((void *)stack);
JSONFormatter jf;
encode_json("data", input, &jf);
std::stringstream ss;
jf.flush(ss);
bufferlist bl;
bl.append(ss.str());
int ret = op->aio_send(bl);
if (ret < 0) {
lsubdout(cct, rgw, 0) << "ERROR: failed to send post request" << dendl;
op->put();
return ret;
}
std::swap(http_op, op); // store reference in http_op on success
return 0;
}
int request_complete() {
int ret;
if (result) {
ret = http_op->wait(result);
} else {
bufferlist bl;
ret = http_op->wait_bl(&bl);
}
auto op = std::move(http_op); // release ref on return
if (ret < 0) {
error_stream << "http operation failed: " << op->to_str()
<< " status=" << op->get_http_status() << std::endl;
lsubdout(cct, rgw, 0) << "ERROR: failed to wait for op, ret=" << ret
<< ": " << op->to_str() << dendl;
op->put();
return ret;
}
op->put();
return 0;
}
void request_cleanup() {
if (http_op) {
http_op->put();
}
}
};
#endif