summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_cr_rest.h
blob: 914eebee02de2769a3ae4f35f59d900a6dc50d47 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

#pragma once

#include <boost/intrusive_ptr.hpp>
#include <mutex>
#include "include/ceph_assert.h" // boost header clobbers our assert.h

#include "rgw_coroutine.h"
#include "rgw_rest_conn.h"


struct rgw_rest_obj {
  rgw_obj_key key;
  uint64_t content_len;
  std::map<string, string> attrs;
  std::map<string, string> custom_attrs;
  RGWAccessControlPolicy acls;

  void init(const rgw_obj_key& _key) {
    key = _key;
  }
};

class RGWReadRawRESTResourceCR : public RGWSimpleCoroutine {
  bufferlist *result;
 protected:
  RGWRESTConn *conn;
  RGWHTTPManager *http_manager;
  string path;
  param_vec_t params;
  param_vec_t extra_headers;
public:
  boost::intrusive_ptr<RGWRESTReadResource> http_op;
  RGWReadRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                           RGWHTTPManager *_http_manager, const string& _path,
                           rgw_http_param_pair *params, bufferlist *_result)
    : RGWSimpleCoroutine(_cct), result(_result), conn(_conn), http_manager(_http_manager),
    path(_path), params(make_param_list(params))
  {}

 RGWReadRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                          RGWHTTPManager *_http_manager, const string& _path,
                          rgw_http_param_pair *params)
   : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
    path(_path), params(make_param_list(params))
  {}

  RGWReadRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                           RGWHTTPManager *_http_manager, const string& _path,
                           rgw_http_param_pair *params, param_vec_t &hdrs)
    : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
      path(_path), params(make_param_list(params)),
      extra_headers(hdrs)
  {}

 RGWReadRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                          RGWHTTPManager *_http_manager, const string& _path,
                          rgw_http_param_pair *params,
                          std::map <std::string, std::string> *hdrs)
   : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
    path(_path), params(make_param_list(params)),
    extra_headers(make_param_list(hdrs))
    {}


  ~RGWReadRawRESTResourceCR() override {
    request_cleanup();
  }

  int send_request(const DoutPrefixProvider *dpp) override {
    auto op = boost::intrusive_ptr<RGWRESTReadResource>(
        new RGWRESTReadResource(conn, path, params, &extra_headers, http_manager));

    init_new_io(op.get());

    int ret = op->aio_read(dpp);
    if (ret < 0) {
      log_error() << "failed to send http operation: " << op->to_str()
          << " ret=" << ret << std::endl;
      op->put();
      return ret;
    }
    std::swap(http_op, op); // store reference in http_op on success
    return 0;
  }



  virtual int wait_result() {
    return http_op->wait(result, null_yield);
  }

  int request_complete() override {
    int ret;

    ret = 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;
      op->put();
      return ret;
    }
    op->put();
    return 0;
  }

  void request_cleanup() override {
    if (http_op) {
      http_op->put();
      http_op = NULL;
    }
  }

};


template <class T>
class RGWReadRESTResourceCR : public RGWReadRawRESTResourceCR {
  T *result;
 public:
 RGWReadRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                       RGWHTTPManager *_http_manager, const string& _path,
                       rgw_http_param_pair *params, T *_result)
   : RGWReadRawRESTResourceCR(_cct, _conn, _http_manager, _path, params), result(_result)
  {}

  RGWReadRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                        RGWHTTPManager *_http_manager, const string& _path,
                        rgw_http_param_pair *params,
                        std::map <std::string, std::string> *hdrs,
                        T *_result)
    : RGWReadRawRESTResourceCR(_cct, _conn, _http_manager, _path, params, hdrs), result(_result)
  {}

  int wait_result() override {
    return http_op->wait(result, null_yield);
  }

};

template <class T, class E = int>
class RGWSendRawRESTResourceCR: public RGWSimpleCoroutine {
 protected:
  RGWRESTConn *conn;
  RGWHTTPManager *http_manager;
  string method;
  string path;
  param_vec_t params;
  param_vec_t headers;
  map<string, string> *attrs;
  T *result;
  E *err_result;
  bufferlist input_bl;
  bool send_content_length=false;
  boost::intrusive_ptr<RGWRESTSendResource> http_op;

 public:
 RGWSendRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                          RGWHTTPManager *_http_manager,
                          const string& _method, const string& _path,
                          rgw_http_param_pair *_params,
                          map<string, string> *_attrs,
                          bufferlist& _input, T *_result,
                          bool _send_content_length,
                          E *_err_result = nullptr)
   : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
     method(_method), path(_path), params(make_param_list(_params)),
     headers(make_param_list(_attrs)), attrs(_attrs),
     result(_result), err_result(_err_result),
     input_bl(_input), send_content_length(_send_content_length) {}

  RGWSendRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                          RGWHTTPManager *_http_manager,
                          const string& _method, const string& _path,
                          rgw_http_param_pair *_params, map<string, string> *_attrs,
                          T *_result, E *_err_result = nullptr)
   : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
    method(_method), path(_path), params(make_param_list(_params)), headers(make_param_list(_attrs)), attrs(_attrs), result(_result),
    err_result(_err_result) {}

  ~RGWSendRawRESTResourceCR() override {
    request_cleanup();
  }

  int send_request(const DoutPrefixProvider *dpp) override {
    auto op = boost::intrusive_ptr<RGWRESTSendResource>(
        new RGWRESTSendResource(conn, method, path, params, &headers, http_manager));

    init_new_io(op.get());

    int ret = op->aio_send(dpp, input_bl);
    if (ret < 0) {
      ldpp_subdout(dpp, rgw, 0) << "ERROR: failed to send request" << dendl;
      op->put();
      return ret;
    }
    std::swap(http_op, op); // store reference in http_op on success
    return 0;
  }

  int request_complete() override {
    int ret;
    if (result || err_result) {
      ret = http_op->wait(result, null_yield, err_result);
    } else {
      bufferlist bl;
      ret = http_op->wait(&bl, null_yield);
    }
    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, 5) << "failed to wait for op, ret=" << ret
          << ": " << op->to_str() << dendl;
      op->put();
      return ret;
    }
    op->put();
    return 0;
  }

  void request_cleanup() override {
    if (http_op) {
      http_op->put();
      http_op = NULL;
    }
  }
};

template <class S, class T, class E = int>
class RGWSendRESTResourceCR : public RGWSendRawRESTResourceCR<T, E> {
 public:
  RGWSendRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                           RGWHTTPManager *_http_manager,
                           const string& _method, const string& _path,
                        rgw_http_param_pair *_params, map<string, string> *_attrs,
                        S& _input, T *_result, E *_err_result = nullptr)
    : RGWSendRawRESTResourceCR<T, E>(_cct, _conn, _http_manager, _method, _path, _params, _attrs, _result, _err_result) {

    JSONFormatter jf;
    encode_json("data", _input, &jf);
    std::stringstream ss;
    jf.flush(ss);
    //bufferlist bl;
    this->input_bl.append(ss.str());
  }

};

template <class S, class T, class E = int>
class RGWPostRESTResourceCR : public RGWSendRESTResourceCR<S, T, E> {
public:
  RGWPostRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                        RGWHTTPManager *_http_manager,
                        const string& _path,
                        rgw_http_param_pair *_params, S& _input,
                        T *_result, E *_err_result = nullptr)
    : RGWSendRESTResourceCR<S, T, E>(_cct, _conn, _http_manager,
                            "POST", _path,
                            _params, nullptr, _input,
                            _result, _err_result) {}
};

template <class T, class E = int>
class RGWPutRawRESTResourceCR: public RGWSendRawRESTResourceCR <T, E> {
 public:
  RGWPutRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                          RGWHTTPManager *_http_manager,
                          const string& _path,
                          rgw_http_param_pair *_params, bufferlist& _input,
                          T *_result, E *_err_result = nullptr)
    : RGWSendRawRESTResourceCR<T, E>(_cct, _conn, _http_manager, "PUT", _path,
                                  _params, nullptr, _input, _result, true, _err_result) {}

};

template <class T, class E = int>
class RGWPostRawRESTResourceCR: public RGWSendRawRESTResourceCR <T, E> {
 public:
  RGWPostRawRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                          RGWHTTPManager *_http_manager,
                          const string& _path,
                          rgw_http_param_pair *_params,
                          map<string, string> * _attrs,
                          bufferlist& _input,
                          T *_result, E *_err_result = nullptr)
    : RGWSendRawRESTResourceCR<T, E>(_cct, _conn, _http_manager, "POST", _path,
                                  _params, _attrs, _input, _result, true, _err_result) {}

};


template <class S, class T, class E = int>
class RGWPutRESTResourceCR : public RGWSendRESTResourceCR<S, T, E> {
public:
  RGWPutRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                        RGWHTTPManager *_http_manager,
                        const string& _path,
                        rgw_http_param_pair *_params, S& _input,
                        T *_result, E *_err_result = nullptr)
    : RGWSendRESTResourceCR<S, T, E>(_cct, _conn, _http_manager,
                                  "PUT", _path,
                                  _params, nullptr, _input,
                                  _result, _err_result) {}

  RGWPutRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                       RGWHTTPManager *_http_manager,
                       const string& _path,
                       rgw_http_param_pair *_params,
                       map <string, string> *_attrs,
                       S& _input, T *_result, E *_err_result = nullptr)
    : RGWSendRESTResourceCR<S, T, E>(_cct, _conn, _http_manager,
                                  "PUT", _path,
                                  _params, _attrs, _input,
                                  _result, _err_result) {}

};

class RGWDeleteRESTResourceCR : public RGWSimpleCoroutine {
  RGWRESTConn *conn;
  RGWHTTPManager *http_manager;
  string path;
  param_vec_t params;

  boost::intrusive_ptr<RGWRESTDeleteResource> http_op;

public:
  RGWDeleteRESTResourceCR(CephContext *_cct, RGWRESTConn *_conn,
                        RGWHTTPManager *_http_manager,
                        const string& _path,
                        rgw_http_param_pair *_params)
    : RGWSimpleCoroutine(_cct), conn(_conn), http_manager(_http_manager),
      path(_path), params(make_param_list(_params))
  {}

  ~RGWDeleteRESTResourceCR() override {
    request_cleanup();
  }

  int send_request(const DoutPrefixProvider *dpp) override {
    auto op = boost::intrusive_ptr<RGWRESTDeleteResource>(
        new RGWRESTDeleteResource(conn, path, params, nullptr, http_manager));

    init_new_io(op.get());

    bufferlist bl;

    int ret = op->aio_send(dpp, bl);
    if (ret < 0) {
      ldpp_subdout(dpp, rgw, 0) << "ERROR: failed to send DELETE request" << dendl;
      op->put();
      return ret;
    }
    std::swap(http_op, op); // store reference in http_op on success
    return 0;
  }

  int request_complete() override {
    int ret;
    bufferlist bl;
    ret = http_op->wait(&bl, null_yield);
    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, 5) << "failed to wait for op, ret=" << ret
          << ": " << op->to_str() << dendl;
      op->put();
      return ret;
    }
    op->put();
    return 0;
  }

  void request_cleanup() override {
    if (http_op) {
      http_op->put();
      http_op = NULL;
    }
  }
};

class RGWCRHTTPGetDataCB : public RGWHTTPStreamRWRequest::ReceiveCB {
  ceph::mutex lock = ceph::make_mutex("RGWCRHTTPGetDataCB");
  RGWCoroutinesEnv *env;
  RGWCoroutine *cr;
  RGWHTTPStreamRWRequest *req;
  rgw_io_id io_id;
  bufferlist data;
  bufferlist extra_data;
  bool got_all_extra_data{false};
  bool paused{false};
  bool notified{false};
public:
  RGWCRHTTPGetDataCB(RGWCoroutinesEnv *_env, RGWCoroutine *_cr, RGWHTTPStreamRWRequest *_req);

  int handle_data(bufferlist& bl, bool *pause) override;

  void claim_data(bufferlist *dest, uint64_t max);

  bufferlist& get_extra_data() {
    return extra_data;
  }

  bool has_data() {
    return (data.length() > 0);
  }

  bool has_all_extra_data() {
    return got_all_extra_data;
  }
};


class RGWStreamReadResourceCRF {
protected:
  boost::asio::coroutine read_state;

public:
  virtual int init(const DoutPrefixProvider *dpp) = 0;
  virtual int read(bufferlist *data, uint64_t max, bool *need_retry) = 0; /* reentrant */
  virtual int decode_rest_obj(map<string, string>& headers, bufferlist& extra_data) = 0;
  virtual bool has_attrs() = 0;
  virtual void get_attrs(std::map<string, string> *attrs) = 0;
  virtual ~RGWStreamReadResourceCRF() = default;
};

class RGWStreamWriteResourceCRF {
protected:
  boost::asio::coroutine write_state;
  boost::asio::coroutine drain_state;

public:
  virtual int init() = 0;
  virtual void send_ready(const DoutPrefixProvider *dpp, const rgw_rest_obj& rest_obj) = 0;
  virtual int send() = 0;
  virtual int write(bufferlist& data, bool *need_retry) = 0; /* reentrant */
  virtual int drain_writes(bool *need_retry) = 0; /* reentrant */
  
  virtual ~RGWStreamWriteResourceCRF() = default;
};

class RGWStreamReadHTTPResourceCRF : public RGWStreamReadResourceCRF {
  CephContext *cct;
  RGWCoroutinesEnv *env;
  RGWCoroutine *caller;
  RGWHTTPManager *http_manager;

  RGWHTTPStreamRWRequest *req{nullptr};

  std::optional<RGWCRHTTPGetDataCB> in_cb;

  bufferlist extra_data;

  bool got_attrs{false};
  bool got_extra_data{false};

  rgw_io_id io_read_mask;

protected:
  rgw_rest_obj rest_obj;

  struct range_info {
    bool is_set{false};
    uint64_t ofs;
    uint64_t size;
  } range;

  ceph::real_time mtime;
  string etag;

public:
  RGWStreamReadHTTPResourceCRF(CephContext *_cct,
                               RGWCoroutinesEnv *_env,
                               RGWCoroutine *_caller,
                               RGWHTTPManager *_http_manager,
                               const rgw_obj_key& _src_key) : cct(_cct),
                                                                env(_env),
                                                                caller(_caller),
                                                                http_manager(_http_manager) {
    rest_obj.init(_src_key);
  }
  ~RGWStreamReadHTTPResourceCRF();

  int init(const DoutPrefixProvider *dpp) override;
  int read(bufferlist *data, uint64_t max, bool *need_retry) override; /* reentrant */
  int decode_rest_obj(map<string, string>& headers, bufferlist& extra_data) override;
  bool has_attrs() override;
  void get_attrs(std::map<string, string> *attrs) override;
  bool is_done();
  virtual bool need_extra_data() { return false; }

  void set_req(RGWHTTPStreamRWRequest *r) {
    req = r;
  }

  rgw_rest_obj& get_rest_obj() {
    return rest_obj;
  }

  void set_range(uint64_t ofs, uint64_t size) {
    range.is_set = true;
    range.ofs = ofs;
    range.size = size;
  }
};

class RGWStreamWriteHTTPResourceCRF : public RGWStreamWriteResourceCRF {
protected:
  RGWCoroutinesEnv *env;
  RGWCoroutine *caller;
  RGWHTTPManager *http_manager;

  using lock_guard = std::lock_guard<std::mutex>;

  std::mutex blocked_lock;
  bool is_blocked;

  RGWHTTPStreamRWRequest *req{nullptr};

  struct multipart_info {
    bool is_multipart{false};
    string upload_id;
    int part_num{0};
    uint64_t part_size;
  } multipart;

  class WriteDrainNotify : public RGWWriteDrainCB {
    RGWStreamWriteHTTPResourceCRF *crf;
  public:
    explicit WriteDrainNotify(RGWStreamWriteHTTPResourceCRF *_crf) : crf(_crf) {}
    void notify(uint64_t pending_size) override;
  } write_drain_notify_cb;

public:
  RGWStreamWriteHTTPResourceCRF(CephContext *_cct,
                               RGWCoroutinesEnv *_env,
                               RGWCoroutine *_caller,
                               RGWHTTPManager *_http_manager) : env(_env),
                                                               caller(_caller),
                                                               http_manager(_http_manager),
                                                               write_drain_notify_cb(this) {}
  virtual ~RGWStreamWriteHTTPResourceCRF();

  int init() override {
    return 0;
  }
  void send_ready(const DoutPrefixProvider *dpp, const rgw_rest_obj& rest_obj) override;
  int send() override;
  int write(bufferlist& data, bool *need_retry) override; /* reentrant */
  void write_drain_notify(uint64_t pending_size);
  int drain_writes(bool *need_retry) override; /* reentrant */

  virtual void handle_headers(const std::map<string, string>& headers) {}

  void set_req(RGWHTTPStreamRWRequest *r) {
    req = r;
  }

  void set_multipart(const string& upload_id, int part_num, uint64_t part_size) {
    multipart.is_multipart = true;
    multipart.upload_id = upload_id;
    multipart.part_num = part_num;
    multipart.part_size = part_size;
  }
};

class RGWStreamSpliceCR : public RGWCoroutine {
  CephContext *cct;
  RGWHTTPManager *http_manager;
  string url;
  std::shared_ptr<RGWStreamReadHTTPResourceCRF> in_crf;
  std::shared_ptr<RGWStreamWriteHTTPResourceCRF> out_crf;
  bufferlist bl;
  bool need_retry{false};
  bool sent_attrs{false};
  uint64_t total_read{0};
  int ret{0};
public:
  RGWStreamSpliceCR(CephContext *_cct, RGWHTTPManager *_mgr,
                    std::shared_ptr<RGWStreamReadHTTPResourceCRF>& _in_crf,
                    std::shared_ptr<RGWStreamWriteHTTPResourceCRF>& _out_crf);
  ~RGWStreamSpliceCR();

  int operate(const DoutPrefixProvider *dpp) override;
};