]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_formats.h
6c7d006282baf2d335d6538239ea542d86319661
[ceph.git] / 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 flush(ostream& os) override;
33 void reset() override;
34
35 void open_array_section(const char *name) override;
36 void open_array_section_in_ns(const char *name, const char *ns) override;
37 void open_object_section(const char *name) override;
38 void open_object_section_in_ns(const char *name, const char *ns) override;
39 void close_section() override;
40 void dump_unsigned(const char *name, uint64_t u) override;
41 void dump_int(const char *name, int64_t u) override;
42 void dump_float(const char *name, double d) override;
43 void dump_string(const char *name, const std::string& s) override;
44 std::ostream& dump_stream(const char *name) override;
45 void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
46 int get_len() const override;
47 void write_raw_data(const char *data) override;
48
49 private:
50 void write_data(const char *fmt, ...);
51 void dump_value_int(const char *name, const char *fmt, ...);
52
53 char *buf = nullptr;
54 int len = 0;
55 int max_len = 0;
56
57 std::list<struct plain_stack_entry> stack;
58 size_t min_stack_level = 0;
59 bool use_kv;
60 bool wrote_something = 0;
61 };
62
63
64 /* This is a presentation layer. No logic inside, please. */
65 class RGWSwiftWebsiteListingFormatter {
66 std::ostream& ss;
67 const std::string prefix;
68 protected:
69 std::string format_name(const std::string& item_name) const;
70 public:
71 RGWSwiftWebsiteListingFormatter(std::ostream& ss,
72 std::string prefix)
73 : ss(ss),
74 prefix(std::move(prefix)) {
75 }
76
77 /* The supplied css_path can be empty. In such situation a default,
78 * embedded style sheet will be generated. */
79 void generate_header(const std::string& dir_path,
80 const std::string& css_path);
81 void generate_footer();
82 void dump_object(const rgw_bucket_dir_entry& objent);
83 void dump_subdir(const std::string& name);
84 };
85
86
87 class RGWFormatterFlusher {
88 protected:
89 Formatter *formatter;
90 bool flushed;
91 bool started;
92 virtual void do_flush() = 0;
93 virtual void do_start(int ret) {}
94 void set_formatter(Formatter *f) {
95 formatter = f;
96 }
97 public:
98 explicit RGWFormatterFlusher(Formatter *f) : formatter(f), flushed(false), started(false) {}
99 virtual ~RGWFormatterFlusher() {}
100
101 void flush() {
102 do_flush();
103 flushed = true;
104 }
105
106 virtual void start(int client_ret) {
107 if (!started)
108 do_start(client_ret);
109 started = true;
110 }
111
112 Formatter *get_formatter() { return formatter; }
113 bool did_flush() { return flushed; }
114 bool did_start() { return started; }
115 };
116
117 class RGWStreamFlusher : public RGWFormatterFlusher {
118 ostream& os;
119 protected:
120 void do_flush() override {
121 formatter->flush(os);
122 }
123 public:
124 RGWStreamFlusher(Formatter *f, ostream& _os) : RGWFormatterFlusher(f), os(_os) {}
125 };
126
127 #endif