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

#pragma once

#include <mutex>
#include "rgw_common.h"
#include "rgw_client_io.h"
#include "rgw_rest.h"
#include "rgw_request.h"
#include "rgw_ldap.h"
#include "include/ceph_assert.h"
#include "rgw_main.h"

class OpsLogSink;

namespace rgw {

  class RGWLibFrontend;

  class RGWLib : public DoutPrefixProvider {
    boost::intrusive_ptr<CephContext> cct;
    AppMain main;
    RGWLibFrontend* fe;

  public:
    RGWLib() : main(this), fe(nullptr)
      {}
    ~RGWLib() {}

    rgw::sal::Driver* get_driver() { return main.get_driver(); }

    RGWLibFrontend* get_fe() { return fe; }

    rgw::LDAPHelper* get_ldh() { return main.get_ldh(); }
    CephContext *get_cct() const override { return cct.get(); }
    unsigned get_subsys() const { return ceph_subsys_rgw; }
    std::ostream& gen_prefix(std::ostream& out) const { return out << "lib rgw: "; }

    void set_fe(RGWLibFrontend* fe);

    int init();
    int init(std::vector<const char *>& args);
    int stop();
  };

  extern RGWLib* g_rgwlib;

/* request interface */

  class RGWLibIO : public rgw::io::BasicClient,
                   public rgw::io::Accounter
  {
    RGWUserInfo user_info;
    RGWEnv env;
  public:
    RGWLibIO() {
      get_env().set("HTTP_HOST", "");
    }
    explicit RGWLibIO(const RGWUserInfo &_user_info)
      : user_info(_user_info) {}

    int init_env(CephContext *cct) override {
      env.init(cct);
      return 0;
    }

    const RGWUserInfo& get_user() {
      return user_info;
    }

    int set_uid(rgw::sal::Driver* driver, const rgw_user& uid);

    int write_data(const char *buf, int len);
    int read_data(char *buf, int len);
    int send_status(int status, const char *status_name);
    int send_100_continue();
    int complete_header();
    int send_content_length(uint64_t len);

    RGWEnv& get_env() noexcept override {
      return env;
    }

    size_t complete_request() override { /* XXX */
      return 0;
    };

    void set_account(bool) override {
      return;
    }

    uint64_t get_bytes_sent() const override {
      return 0;
    }

    uint64_t get_bytes_received() const override {
      return 0;
    }

  }; /* RGWLibIO */

  class RGWRESTMgr_Lib : public RGWRESTMgr {
  public:
    RGWRESTMgr_Lib() {}
    ~RGWRESTMgr_Lib() override {}
  }; /* RGWRESTMgr_Lib */

  class RGWHandler_Lib : public RGWHandler {
    friend class RGWRESTMgr_Lib;
  public:

    int authorize(const DoutPrefixProvider *dpp, optional_yield y) override;

    RGWHandler_Lib() {}
    ~RGWHandler_Lib() override {}
    static int init_from_header(rgw::sal::Driver* driver,
				req_state *s);
  }; /* RGWHandler_Lib */

  class RGWLibRequest : public RGWRequest,
			public RGWHandler_Lib {
  private:
    std::unique_ptr<rgw::sal::User> tuser; // Don't use this.  It's empty except during init.
  public:
    CephContext* cct;

    /* unambiguiously return req_state */
    inline req_state* get_state() { return this->RGWRequest::s; }

    RGWLibRequest(CephContext* _cct, std::unique_ptr<rgw::sal::User> _user)
      :  RGWRequest(g_rgwlib->get_driver()->get_new_req_id()),
	 tuser(std::move(_user)), cct(_cct)
      {}

  int postauth_init(optional_yield) override { return 0; }

    /* descendant equivalent of *REST*::init_from_header(...):
     * prepare request for execute()--should mean, fixup URI-alikes
     * and any other expected stat vars in local req_state, for
     * now */
    virtual int header_init() = 0;

    /* descendant initializer responsible to call RGWOp::init()--which
     * descendants are required to inherit */
    virtual int op_init() = 0;

    using RGWHandler::init;

    int init(const RGWEnv& rgw_env, rgw::sal::Driver* _driver,
	     RGWLibIO* io, req_state* _s) {

      RGWRequest::init_state(_s);
      RGWHandler::init(_driver, _s, io);

      get_state()->req_id = driver->zone_unique_id(id);
      get_state()->trans_id = driver->zone_unique_trans_id(id);
      get_state()->bucket_tenant = tuser->get_tenant();
      get_state()->set_user(tuser);

      ldpp_dout(_s, 2) << "initializing for trans_id = "
	  << get_state()->trans_id.c_str() << dendl;

      int ret = header_init();
      if (ret == 0) {
	ret = init_from_header(driver, _s);
      }
      return ret;
    }

    virtual bool only_bucket() = 0;

    int read_permissions(RGWOp *op, optional_yield y) override;

  }; /* RGWLibRequest */

  class RGWLibContinuedReq : public RGWLibRequest {
    RGWLibIO io_ctx;
    req_state rstate;
  public:

    RGWLibContinuedReq(CephContext* _cct, const RGWProcessEnv& penv,
		       std::unique_ptr<rgw::sal::User> _user)
      :  RGWLibRequest(_cct, std::move(_user)), io_ctx(),
	 rstate(_cct, penv, &io_ctx.get_env(), id)
      {
	io_ctx.init(_cct);

	RGWRequest::init_state(&rstate);
	RGWHandler::init(g_rgwlib->get_driver(), &rstate, &io_ctx);

	get_state()->req_id = driver->zone_unique_id(id);
	get_state()->trans_id = driver->zone_unique_trans_id(id);

	ldpp_dout(get_state(), 2) << "initializing for trans_id = "
	    << get_state()->trans_id.c_str() << dendl;
      }

    inline rgw::sal::Driver* get_driver() { return driver; }
    inline RGWLibIO& get_io() { return io_ctx; }

    virtual int execute() final { ceph_abort(); }
    virtual int exec_start() = 0;
    virtual int exec_continue() = 0;
    virtual int exec_finish() = 0;

  }; /* RGWLibContinuedReq */

} /* namespace rgw */