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

#ifndef CEPH_RGW_CLIENT_IO_H
#define CEPH_RGW_CLIENT_IO_H

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

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


class RGWRestfulIO;

namespace rgw {
namespace io {

using Exception = std::system_error;

/* The minimal and simplest subset of methods that a client of RadosGW can be
 * interacted with. */
class BasicClient {
protected:
  virtual int init_env(CephContext *cct) = 0;

public:
  virtual ~BasicClient() = default;

  /* Initialize the BasicClient and inject CephContext. */
  int init(CephContext *cct);

  /* Return the RGWEnv describing the environment that a given request lives in.
   * The method does not throw exceptions. */
  virtual RGWEnv& get_env() noexcept = 0;

  /* Complete request.
   * On success returns number of bytes generated for a direct client of RadosGW.
   * On failure throws rgw::io::Exception containing errno. */
  virtual size_t complete_request() = 0;
}; /* rgw::io::Client */


class Accounter {
public:
  virtual ~Accounter() = default;

  /* Enable or disable the accounting of both sent and received data. Changing
   * the state does not affect the counters. */
  virtual void set_account(bool enabled) = 0;

  /* Return number of bytes sent to a direct client of RadosGW (direct means
   * eg. a web server instance in the case of using FastCGI front-end) when
   * the accounting was enabled. */
  virtual uint64_t get_bytes_sent() const = 0;

  /* Return number of bytes received from a direct client of RadosGW (direct
   * means eg. a web server instance in the case of using FastCGI front-end)
   * when the accounting was enabled. */
  virtual uint64_t get_bytes_received() const = 0;
}; /* rgw::io::Accounter */


/* Interface abstracting restful interactions with clients, usually through
 * the HTTP protocol. The methods participating in the response generation
 * process should be called in the specific order:
 *   1. send_100_continue() - at most once,
 *   2. send_status() - exactly once,
 *   3. Any of:
 *      a. send_header(),
 *      b. send_content_length() XOR send_chunked_transfer_encoding()
 *         Please note that only one of those two methods must be called
           at most once.
 *   4. complete_header() - exactly once,
 *   5. send_body()
 *   6. complete_request() - exactly once.
 * There are no restrictions on flush() - it may be called in any moment.
 *
 * Receiving data from a client isn't a subject to any further call order
 * restrictions besides those imposed by BasicClient. That is, get_env()
 * and recv_body can be mixed. */
class RestfulClient : public BasicClient {
  template<typename T> friend class DecoratedRestfulClient;

public:
  /* Generate the 100 Continue message.
   * On success returns number of bytes generated for a direct client of RadosGW.
   * On failure throws rgw::io::Exception containing errno. */
  virtual size_t send_100_continue() = 0;

  /* Generate the response's status part taking the HTTP status code as @status
   * and its name pointed in @status_name.
   * On success returns number of bytes generated for a direct client of RadosGW.
   * On failure throws rgw::io::Exception containing errno. */
  virtual size_t send_status(int status, const char *status_name) = 0;

  /* Generate header. On success returns number of bytes generated for a direct
   * client of RadosGW. On failure throws rgw::io::Exception containing errno.
   *
   * std::string_view is being used because of length it internally carries. */
  virtual size_t send_header(const std::string_view& name,
                             const std::string_view& value) = 0;

  /* Inform a client about a content length. Takes number of bytes as @len.
   * On success returns number of bytes generated for a direct client of RadosGW.
   * On failure throws rgw::io::Exception containing errno.
   *
   * CALL LIMITATIONS:
   *  - The method must be called EXACTLY ONCE.
   *  - The method is interchangeable with send_chunked_transfer_encoding(). */
  virtual size_t send_content_length(uint64_t len) = 0;

  /* Inform a client that the chunked transfer encoding will be used.
   * On success returns number of bytes generated for a direct client of RadosGW.
   * On failure throws rgw::io::Exception containing errno.
   *
   * CALL LIMITATIONS:
   *  - The method must be called EXACTLY ONCE.
   *  - The method is interchangeable with send_content_length(). */
  virtual 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;
  }

  /* Generate completion (the CRLF sequence separating headers and body in
   * the case of HTTP) of headers. On success returns number of generated bytes
   * for a direct client of RadosGW. On failure throws rgw::io::Exception with
   * errno. */
  virtual size_t complete_header() = 0;

  /* Receive no more than @max bytes from a request's body and store it in
   * buffer pointed by @buf. On success returns number of bytes received from
   * a direct client of RadosGW that has been stored in @buf. On failure throws
   * rgw::io::Exception containing errno. */
  virtual size_t recv_body(char* buf, size_t max) = 0;

  /* Generate a part of response's body by taking exactly @len bytes from
   * the buffer pointed by @buf. On success returns number of generated bytes
   * of response's body. On failure throws rgw::io::Exception. */
  virtual size_t send_body(const char* buf, size_t len) = 0;

  /* Flushes all already generated data to a direct client of RadosGW.
   * On failure throws rgw::io::Exception containing errno. */
  virtual void flush() = 0;
} /* rgw::io::RestfulClient */;


/* Abstract decorator over any implementation of rgw::io::RestfulClient
 * which could be provided both as a pointer-to-object or the object itself. */
template <typename DecorateeT>
class DecoratedRestfulClient : public RestfulClient {
  template<typename T> friend class DecoratedRestfulClient;
  friend RGWRestfulIO;

  typedef typename std::remove_pointer<DecorateeT>::type DerefedDecorateeT;

  static_assert(std::is_base_of<RestfulClient, DerefedDecorateeT>::value,
                "DecorateeT must be a subclass of rgw::io::RestfulClient");

  DecorateeT decoratee;

  /* There is an indirection layer over accessing decoratee to share the same
   * code base between dynamic and static decorators. The difference is about
   * what we store internally: pointer to a decorated object versus the whole
   * object itself. */
  template <typename T = void,
            typename std::enable_if<
    ! std::is_pointer<DecorateeT>::value, T>::type* = nullptr>
  DerefedDecorateeT& get_decoratee() {
    return decoratee;
  }

protected:
  template <typename T = void,
            typename std::enable_if<
    std::is_pointer<DecorateeT>::value, T>::type* = nullptr>
  DerefedDecorateeT& get_decoratee() {
    return *decoratee;
  }

  /* Dynamic decorators (those storing a pointer instead of the decorated
   * object itself) can be reconfigured on-the-fly. HOWEVER: there are no
   * facilities for orchestrating such changes. Callers must take care of
   * atomicity and thread-safety. */
  template <typename T = void,
            typename std::enable_if<
    std::is_pointer<DecorateeT>::value, T>::type* = nullptr>
  void set_decoratee(DerefedDecorateeT& new_dec) {
    decoratee = &new_dec;
  }

  int init_env(CephContext *cct) override {
    return get_decoratee().init_env(cct);
  }

public:
  explicit DecoratedRestfulClient(DecorateeT&& decoratee)
    : decoratee(std::forward<DecorateeT>(decoratee)) {
  }

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

  size_t send_100_continue() override {
    return get_decoratee().send_100_continue();
  }

  size_t send_header(const std::string_view& name,
                     const std::string_view& value) override {
    return get_decoratee().send_header(name, value);
  }

  size_t send_content_length(const uint64_t len) override {
    return get_decoratee().send_content_length(len);
  }

  size_t send_chunked_transfer_encoding() override {
    return get_decoratee().send_chunked_transfer_encoding();
  }

  size_t complete_header() override {
    return get_decoratee().complete_header();
  }

  size_t recv_body(char* const buf, const size_t max) override {
    return get_decoratee().recv_body(buf, max);
  }

  size_t send_body(const char* const buf,
                   const size_t len) override {
    return get_decoratee().send_body(buf, len);
  }

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

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

  size_t complete_request() override {
    return get_decoratee().complete_request();
  }
} /* rgw::io::DecoratedRestfulClient */;


/* Interface that should be provided by a front-end class wanting to to use
 * the low-level buffering offered by i.e. StaticOutputBufferer. */
class BuffererSink {
public:
  virtual ~BuffererSink() = default;

  /* Send exactly @len bytes from the memory location pointed by @buf.
   * On success returns @len. On failure throws rgw::io::Exception. */
  virtual size_t write_data(const char *buf, size_t len) = 0;
};

/* Utility class providing RestfulClient's implementations with facilities
 * for low-level buffering without relying on dynamic memory allocations.
 * The buffer is carried entirely on stack. This narrows down applicability
 * to these situations where buffers are relatively small. This perfectly
 * fits the needs of composing an HTTP header. Without that a front-end
 * might need to issue a lot of small IO operations leading to increased
 * overhead on syscalls and fragmentation of a message if the Nagle's
 * algorithm won't be able to form a single TCP segment (usually when
 * running on extremely fast network interfaces like the loopback). */
template <size_t BufferSizeV = 4096>
class StaticOutputBufferer : public std::streambuf {
  static_assert(BufferSizeV >= sizeof(std::streambuf::char_type),
                "Buffer size must be bigger than a single char_type.");

  using std::streambuf::int_type;

  int_type overflow(const int_type c) override {
    *pptr() = c;
    pbump(sizeof(std::streambuf::char_type));

    if (! sync()) {
      /* No error, the buffer has been successfully synchronized. */
      return c;
    } else {
      return std::streambuf::traits_type::eof();
    }
  }

  int sync() override {
    const auto len = static_cast<size_t>(std::streambuf::pptr() -
                                         std::streambuf::pbase());
    std::streambuf::pbump(-len);
    sink.write_data(std::streambuf::pbase(), len);
    /* Always return success here. In case of failure write_data() will throw
     * rgw::io::Exception. */
    return 0;
  }

  BuffererSink& sink;
  std::streambuf::char_type buffer[BufferSizeV];

public:
  explicit StaticOutputBufferer(BuffererSink& sink)
    : sink(sink) {
    constexpr size_t len = sizeof(buffer) - sizeof(std::streambuf::char_type);
    std::streambuf::setp(buffer, buffer + len);
  }
};

} /* namespace io */
} /* namespace rgw */


/* We're doing this nasty thing only because of extensive usage of templates
 * to implement the static decorator pattern. C++ templates de facto enforce
 * mixing interfaces with implementation. Additionally, those classes derive
 * from RGWRestfulIO defined here. I believe that including in the middle of
 * file is still better than polluting it directly. */
#include "rgw_client_io_filters.h"


/* RGWRestfulIO: high level interface to interact with RESTful clients. What
 * differentiates it from rgw::io::RestfulClient is providing more specific APIs
 * like rgw::io::Accounter or the AWS Auth v4 stuff implemented by filters
 * while hiding the pipelined architecture from clients.
 *
 * rgw::io::Accounter came in as a part of rgw::io::AccountingFilter. */
class RGWRestfulIO : public rgw::io::AccountingFilter<rgw::io::RestfulClient*> {
  std::vector<std::shared_ptr<DecoratedRestfulClient>> filters;

public:
  ~RGWRestfulIO() override = default;

  RGWRestfulIO(CephContext *_cx, rgw::io::RestfulClient* engine)
    : AccountingFilter<rgw::io::RestfulClient*>(_cx, std::move(engine)) {
  }

  void add_filter(std::shared_ptr<DecoratedRestfulClient> new_filter) {
    new_filter->set_decoratee(this->get_decoratee());
    this->set_decoratee(*new_filter);
    filters.emplace_back(std::move(new_filter));
  }
}; /* RGWRestfulIO */


/* Type conversions to work around lack of req_state type hierarchy matching
 * (e.g.) REST backends (may be replaced w/dynamic typed req_state). */
static inline rgw::io::RestfulClient* RESTFUL_IO(struct req_state* s) {
  ceph_assert(dynamic_cast<rgw::io::RestfulClient*>(s->cio) != nullptr);

  return static_cast<rgw::io::RestfulClient*>(s->cio);
}

static inline rgw::io::Accounter* ACCOUNTING_IO(struct req_state* s) {
  auto ptr = dynamic_cast<rgw::io::Accounter*>(s->cio);
  ceph_assert(ptr != nullptr);

  return ptr;
}

static inline RGWRestfulIO* AWS_AUTHv4_IO(const req_state* const s) {
  ceph_assert(dynamic_cast<RGWRestfulIO*>(s->cio) != nullptr);

  return static_cast<RGWRestfulIO*>(s->cio);
}


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

public:
  RGWClientIOStreamBuf(RGWRestfulIO &rio, size_t ws, size_t ps = 1)
    : rio(rio),
      window_size(ws),
      putback_size(ps),
      buffer(ws + ps)
  {
    setg(nullptr, nullptr, nullptr);
  }

  std::streambuf::int_type underflow() override {
    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;
    }

    size_t read_len = 0;
    try {
      read_len = rio.recv_body(base, window_size);
    } catch (rgw::io::Exception&) {
      return traits_type::eof();
    }
    if (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(RGWRestfulIO &s)
    : RGWClientIOStreamBuf(s, 1, 2),
      istream(static_cast<RGWClientIOStreamBuf *>(this)) {
  }
};

#endif /* CEPH_RGW_CLIENT_IO_H */