Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_formats.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RGW_FORMATS_H
5 #define CEPH_RGW_FORMATS_H
6
7 #include "common/Formatter.h"
8
9 #include <list>
10 #include <stdint.h>
11 #include <string>
12 #include <ostream>
13
14 struct plain_stack_entry {
15   int size;
16   bool is_array;
17 };
18
19 /* FIXME: this class is mis-named.
20  * FIXME: This was a hack to send certain swift messages.
21  * There is a much better way to do this.
22  */
23 class RGWFormatter_Plain : public Formatter {
24   void reset_buf();
25 public:
26   explicit RGWFormatter_Plain(bool use_kv = false);
27   ~RGWFormatter_Plain() override;
28
29   void set_status(int status, const char* status_name) override {};
30   void output_header() override {};
31   void output_footer() override {};
32   void enable_line_break() override {};
33   void flush(ostream& os) override;
34   void reset() override;
35
36   void open_array_section(const char *name) override;
37   void open_array_section_in_ns(const char *name, const char *ns) override;
38   void open_object_section(const char *name) override;
39   void open_object_section_in_ns(const char *name, const char *ns) override;
40   void close_section() override;
41   void dump_unsigned(const char *name, uint64_t u) override;
42   void dump_int(const char *name, int64_t u) override;
43   void dump_float(const char *name, double d) override;
44   void dump_string(const char *name, const std::string& s) override;
45   std::ostream& dump_stream(const char *name) override;
46   void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
47   int get_len() const override;
48   void write_raw_data(const char *data) override;
49
50 private:
51   void write_data(const char *fmt, ...);
52   void dump_value_int(const char *name, const char *fmt, ...);
53
54   char *buf = nullptr;
55   int len = 0;
56   int max_len = 0;
57
58   std::list<struct plain_stack_entry> stack;
59   size_t min_stack_level = 0;
60   bool use_kv;
61   bool wrote_something = 0;
62 };
63
64
65 /* This is a presentation layer. No logic inside, please. */
66 class RGWSwiftWebsiteListingFormatter {
67   std::ostream& ss;
68   const std::string prefix;
69 protected:
70   std::string format_name(const std::string& item_name) const;
71 public:
72   RGWSwiftWebsiteListingFormatter(std::ostream& ss,
73                                   std::string prefix)
74     : ss(ss),
75       prefix(std::move(prefix)) {
76   }
77
78   /* The supplied css_path can be empty. In such situation a default,
79    * embedded style sheet will be generated. */
80   void generate_header(const std::string& dir_path,
81                        const std::string& css_path);
82   void generate_footer();
83   void dump_object(const rgw_bucket_dir_entry& objent);
84   void dump_subdir(const std::string& name);
85 };
86
87
88 class RGWFormatterFlusher {
89 protected:
90   Formatter *formatter;
91   bool flushed;
92   bool started;
93   virtual void do_flush() = 0;
94   virtual void do_start(int ret) {}
95   void set_formatter(Formatter *f) {
96     formatter = f;
97   }
98 public:
99   explicit RGWFormatterFlusher(Formatter *f) : formatter(f), flushed(false), started(false) {}
100   virtual ~RGWFormatterFlusher() {}
101
102   void flush() {
103     do_flush();
104     flushed = true;
105   }
106
107   virtual void start(int client_ret) {
108     if (!started)
109       do_start(client_ret);
110     started = true;
111   }
112
113   Formatter *get_formatter() { return formatter; }
114   bool did_flush() { return flushed; }
115   bool did_start() { return started; }
116 };
117
118 class RGWStreamFlusher : public RGWFormatterFlusher {
119   ostream& os;
120 protected:
121   void do_flush() override {
122     formatter->flush(os);
123   }
124 public:
125   RGWStreamFlusher(Formatter *f, ostream& _os) : RGWFormatterFlusher(f), os(_os) {}
126 };
127
128 #endif