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

#ifndef CEPH_RGW_CLIENT_IO_H
#define CEPH_RGW_CLIENT_IO_H

#include <exception>
#include <string>
#include <streambuf>
#include <istream>
#include <stdlib.h>

#include <boost/utility/string_ref.hpp>

#include "include/types.h"
#include "rgw_common.h"

class RGWClientIO {
protected:
  virtual void init_env(CephContext *cct) = 0;

public:
  virtual ~RGWClientIO() {}

  void init(CephContext *cct);
  virtual RGWEnv& get_env() noexcept = 0;
  virtual int complete_request() = 0;
}; /* RGWClient IO */


class RGWClientIOAccounter {
public:
  virtual ~RGWClientIOAccounter() {}

  virtual void set_account(bool enabled) = 0;

  virtual uint64_t get_bytes_sent() const = 0;
  virtual uint64_t get_bytes_received() const = 0;
};


class RGWStreamIOEngine : public RGWClientIO {
  friend class RGWStreamIOLegacyWrapper;

public:
  class Exception : public std::exception {
    int err;
  public:
    Exception(const int err)
      : err(err) {
    }

    int value() {
      return err;
    }
  };

  virtual std::size_t send_status(int status, const char *status_name) = 0;
  virtual std::size_t send_100_continue() = 0;

  /* Send header to client. On success returns number of bytes sent to the direct
   * client of RadosGW. On failure throws int containing errno. boost::string_ref
   * is being used because of length it internally carries. */
  virtual std::size_t send_header(const boost::string_ref& name,
                                  const boost::string_ref& value) = 0;

  /* Inform a client about a content length. Takes number of bytes supplied in
   * @len XOR one of the alternative modes for dealing with it passed as @mode.
   * On success returns number of bytes sent to the direct client of RadosGW.
   * On failure throws int containing errno.
   *
   * CALL ORDER:
   *  - The method must be called EXACTLY ONE time.
   *  - The method must be preceeded with a call to send_status().
   *  - The method must not be called after complete_header(). */
  virtual std::size_t send_content_length(uint64_t len) = 0;

  virtual std::size_t send_chunked_transfer_encoding() {
    /* This is a null implementation. We don't send anything here, even the HTTP
     * header. The intended behaviour should be provided through a decorator or
     * directly by a given front-end. */
    return 0;
  }

  virtual std::size_t complete_header() = 0;

  /* Receive body. On success Returns number of bytes sent to the direct
   * client of RadosGW. On failure throws int containing errno. */
  virtual std::size_t recv_body(char* buf, std::size_t max) = 0;
  virtual std::size_t send_body(const char* buf, std::size_t len) = 0;

  virtual void flush() = 0;
};


/* HTTP IO: compatibility layer */
class RGWStreamIO : public RGWClientIO,
                    public RGWClientIOAccounter {
protected:
  bool _account;
  size_t bytes_sent;
  size_t bytes_received;

  SHA256 *sha256_hash;

  bool account() const {
    return _account;
  }

  RGWEnv env;

public:
  virtual int send_status(int status, const char *status_name) = 0;
  virtual int send_100_continue() = 0;
  virtual std::size_t send_header(const boost::string_ref& name,
                                  const boost::string_ref& value) noexcept = 0;
  virtual int send_content_length(uint64_t len) = 0;
  virtual int send_chunked_transfer_encoding() = 0;
  virtual int complete_header() = 0;

  virtual int recv_body(char* buf, std::size_t max) = 0;
  virtual int recv_body(char* buf, std::size_t max, bool calculate_hash) = 0;
  virtual int send_body(const char* buf, std::size_t len) = 0;
  virtual void flush() = 0;

  virtual ~RGWStreamIO() {}

  RGWStreamIO()
    : _account(false),
      bytes_sent(0),
      bytes_received(0),
      sha256_hash(nullptr) {
  }

  std::string grab_aws4_sha256_hash();

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

  void set_account(bool _accnt) override {
    _account = _accnt;
  }

  uint64_t get_bytes_sent() const override {
    return bytes_sent;
  }

  uint64_t get_bytes_received() const override {
    return bytes_received;
  }
}; /* RGWStreamIO */


/* A class for preserving interface compatibility with RGWStreamIO clients
 * while allowing front-end migration to the new API. We don't multi-inherit
 * from RGWDecoratedStreamIO<> to avoid using virtual inheritance in engine.
 * Should be removed after converting all clients. */
class RGWStreamIOLegacyWrapper : public RGWStreamIO {
  RGWStreamIOEngine * const engine;

  RGWStreamIOEngine& get_decoratee() {
    return *engine;
  }

#define EXCPT_TO_RC(code)                                       \
  try {                                                         \
    return code;                                                \
  } catch (RGWStreamIOEngine::Exception& e) {                   \
    return e.value();                                           \
  }

#define EXCPT_TO_VOID(code)                                     \
  try {                                                         \
    return code;                                                \
  } catch (RGWStreamIOEngine::Exception& e) {                   \
    return;                                                     \
  }

protected:
  void init_env(CephContext *cct) override {
    EXCPT_TO_VOID(get_decoratee().init_env(cct));
  }

public:
  RGWStreamIOLegacyWrapper(RGWStreamIOEngine * const engine)
    : engine(engine) {
  }

  int send_status(const int status, const char* const status_name) override {
    EXCPT_TO_RC(get_decoratee().send_status(status, status_name));
  }

  std::size_t send_header(const boost::string_ref& name,
                          const boost::string_ref& value) noexcept override {
    EXCPT_TO_RC(get_decoratee().send_header(name, value));
  }

  int send_100_continue() override {
    EXCPT_TO_RC(get_decoratee().send_100_continue());
  }

  int send_content_length(const uint64_t len) override {
    EXCPT_TO_RC(get_decoratee().send_content_length(len));
  }

  int send_chunked_transfer_encoding() override {
    EXCPT_TO_RC(get_decoratee().send_chunked_transfer_encoding());
  }

  int complete_header() override {
    EXCPT_TO_RC(get_decoratee().complete_header());
  }

  int recv_body(char* buf, const std::size_t max) override {
    EXCPT_TO_RC(get_decoratee().recv_body(buf, max));
  }

  int recv_body(char* buf, std::size_t max, bool calculate_hash) override;

  int send_body(const char* const buf, const std::size_t len) override {
    EXCPT_TO_RC(get_decoratee().send_body(buf, len));
  }


  void flush() override {
    EXCPT_TO_VOID(get_decoratee().flush());
  }

  RGWEnv& get_env() noexcept override {
    return get_decoratee().get_env();
  }

  int complete_request() override {
    EXCPT_TO_RC(get_decoratee().complete_request());
  }
};


class RGWClientIOStreamBuf : public std::streambuf {
protected:
  RGWStreamIO &sio;
  std::size_t const window_size;
  std::size_t const putback_size;
  std::vector<char> buffer;

public:
  RGWClientIOStreamBuf(RGWStreamIO &s, std::size_t ws, std::size_t ps = 1)
    : sio(s),
      window_size(ws),
      putback_size(ps),
      buffer(ws + ps)
  {
    setg(nullptr, nullptr, nullptr);
  }

  std::streambuf::int_type underflow() {
    if (gptr() < egptr()) {
      return traits_type::to_int_type(*gptr());
    }

    char * const base = buffer.data();
    char * start;

    if (nullptr != eback()) {
      /* We need to skip moving bytes on first underflow. In such case
       * there is simply no previous data we should preserve for unget()
       * or something similar. */
      std::memmove(base, egptr() - putback_size, putback_size);
      start = base + putback_size;
    } else {
      start = base;
    }

    const int read_len = sio.recv_body(base, window_size, false);
    if (read_len < 0 || 0 == read_len) {
      return traits_type::eof();
    }

    setg(base, start, start + read_len);

    return traits_type::to_int_type(*gptr());
  }
};

class RGWClientIOStream : private RGWClientIOStreamBuf, public std::istream {
/* Inheritance from RGWClientIOStreamBuf is a kind of shadow, undirect
 * form of composition here. We cannot do that explicitly because istream
 * ctor is being called prior to construction of any member of this class. */

public:
  explicit RGWClientIOStream(RGWStreamIO &s)
    : RGWClientIOStreamBuf(s, 1, 2),
      istream(static_cast<RGWClientIOStreamBuf *>(this)) {
  }
};

#endif /* CEPH_RGW_CLIENT_IO_H */