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

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include <boost/format.hpp>

#include "common/escape.h"
#include "common/Formatter.h"
#include "rgw/rgw_common.h"
#include "rgw/rgw_formats.h"
#include "rgw/rgw_rest.h"

#define LARGE_SIZE 8192

#define dout_subsys ceph_subsys_rgw

RGWFormatter_Plain::RGWFormatter_Plain(const bool ukv)
  : use_kv(ukv)
{
}

RGWFormatter_Plain::~RGWFormatter_Plain()
{
  free(buf);
}

void RGWFormatter_Plain::flush(ostream& os)
{
  if (!buf)
    return;

  if (len) {
    os << buf;
    os.flush();
  }

  reset_buf();
}

void RGWFormatter_Plain::reset_buf()
{
  free(buf);
  buf = NULL;
  len = 0;
  max_len = 0;
}

void RGWFormatter_Plain::reset()
{
  reset_buf();
  stack.clear();
  min_stack_level = 0;
}

void RGWFormatter_Plain::open_array_section(std::string_view name)
{
  struct plain_stack_entry new_entry;
  new_entry.is_array = true;
  new_entry.size = 0;

  if (use_kv && min_stack_level > 0 && !stack.empty()) {
    struct plain_stack_entry& entry = stack.back();

    if (!entry.is_array)
      dump_format(name, "");
  }

  stack.push_back(new_entry);
}

void RGWFormatter_Plain::open_array_section_in_ns(std::string_view name, const char *ns)
{
  ostringstream oss;
  oss << name << " " << ns;
  open_array_section(oss.str().c_str());
}

void RGWFormatter_Plain::open_object_section(std::string_view name)
{
  struct plain_stack_entry new_entry;
  new_entry.is_array = false;
  new_entry.size = 0;

  if (use_kv && min_stack_level > 0)
    dump_format(name, "");

  stack.push_back(new_entry);
}

void RGWFormatter_Plain::open_object_section_in_ns(std::string_view name,
						   const char *ns)
{
  ostringstream oss;
  oss << name << " " << ns;
  open_object_section(oss.str().c_str());
}

void RGWFormatter_Plain::close_section()
{
  stack.pop_back();
}

void RGWFormatter_Plain::dump_unsigned(std::string_view name, uint64_t u)
{
  dump_value_int(name, "%" PRIu64, u);
}

void RGWFormatter_Plain::dump_int(std::string_view name, int64_t u)
{
  dump_value_int(name, "%" PRId64, u);
}

void RGWFormatter_Plain::dump_float(std::string_view name, double d)
{
  dump_value_int(name, "%f", d);
}

void RGWFormatter_Plain::dump_string(std::string_view name, std::string_view s)
{
  dump_format(name, "%.*s", s.size(), s.data());
}

std::ostream& RGWFormatter_Plain::dump_stream(std::string_view name)
{
  // TODO: implement this!
  ceph_abort();
}

void RGWFormatter_Plain::dump_format_va(std::string_view name, const char *ns, bool quoted, const char *fmt, va_list ap)
{
  char buf[LARGE_SIZE];

  struct plain_stack_entry& entry = stack.back();

  if (!min_stack_level)
    min_stack_level = stack.size();

  bool should_print = ((stack.size() == min_stack_level && !entry.size) || use_kv);

  entry.size++;

  if (!should_print)
    return;

  vsnprintf(buf, LARGE_SIZE, fmt, ap);

  const char *eol;
  if (wrote_something) {
    if (use_kv && entry.is_array && entry.size > 1)
      eol = ", ";
    else
      eol = "\n";
  } else
    eol = "";
  wrote_something = true;

  if (use_kv && !entry.is_array)
    write_data("%s%.*s: %s", eol, name.size(), name.data(), buf);
  else
    write_data("%s%s", eol, buf);
}

int RGWFormatter_Plain::get_len() const
{
  // don't include null termination in length
  return (len ? len - 1 : 0);
}

void RGWFormatter_Plain::write_raw_data(const char *data)
{
  write_data("%s", data);
}

void RGWFormatter_Plain::write_data(const char *fmt, ...)
{
#define LARGE_ENOUGH_LEN 128
  int n, size = LARGE_ENOUGH_LEN;
  char s[size + 8];
  char *p, *np;
  bool p_on_stack;
  va_list ap;
  int pos;

  p = s;
  p_on_stack = true;

  while (1) {
    va_start(ap, fmt);
    n = vsnprintf(p, size, fmt, ap);
    va_end(ap);

    if (n > -1 && n < size)
      goto done;
    /* Else try again with more space. */
    if (n > -1)    /* glibc 2.1 */
      size = n+1; /* precisely what is needed */
    else           /* glibc 2.0 */
      size *= 2;  /* twice the old size */
    if (p_on_stack)
      np = (char *)malloc(size + 8);
    else
      np = (char *)realloc(p, size + 8);
    if (!np)
      goto done_free;
    p = np;
    p_on_stack = false;
  }
done:
#define LARGE_ENOUGH_BUF 4096
  if (!buf) {
    max_len = std::max(LARGE_ENOUGH_BUF, size);
    buf = (char *)malloc(max_len);
    if (!buf) {
      cerr << "ERROR: RGWFormatter_Plain::write_data: failed allocating " << max_len << " bytes" << std::endl;
      goto done_free;
    }
  }

  if (len + size > max_len) {
    max_len = len + size + LARGE_ENOUGH_BUF;
    void *_realloc = NULL;
    if ((_realloc = realloc(buf, max_len)) == NULL) {
      cerr << "ERROR: RGWFormatter_Plain::write_data: failed allocating " << max_len << " bytes" << std::endl;
      goto done_free;
    } else {
      buf = (char *)_realloc;
    }
  }

  pos = len;
  if (len)
    pos--; // squash null termination
  strcpy(buf + pos, p);
  len = pos + strlen(p) + 1;
done_free:
  if (!p_on_stack)
    free(p);
}

void RGWFormatter_Plain::dump_value_int(std::string_view name, const char *fmt, ...)
{
  char buf[LARGE_SIZE];
  va_list ap;

  if (!min_stack_level)
    min_stack_level = stack.size();

  struct plain_stack_entry& entry = stack.back();
  bool should_print = ((stack.size() == min_stack_level && !entry.size) || use_kv);

  entry.size++;

  if (!should_print)
    return;

  va_start(ap, fmt);
  vsnprintf(buf, LARGE_SIZE, fmt, ap);
  va_end(ap);

  const char *eol;
  if (wrote_something) {
    eol = "\n";
  } else
    eol = "";
  wrote_something = true;

  if (use_kv && !entry.is_array)
    write_data("%s%.*s: %s", eol, name.size(), name.data(), buf);
  else
    write_data("%s%s", eol, buf);

}


/* An utility class that serves as a mean to access the protected static
 * methods of XMLFormatter. */
class HTMLHelper : public XMLFormatter {
public:
  static std::string escape(const std::string& unescaped_str) {
    int len = escape_xml_attr_len(unescaped_str.c_str());
    std::string escaped(len, 0);
    escape_xml_attr(unescaped_str.c_str(), escaped.data());
    return escaped;
  }
};

void RGWSwiftWebsiteListingFormatter::generate_header(
  const std::string& dir_path,
  const std::string& css_path)
{
  ss << R"(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 )"
     << R"(Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">)";

  ss << "<html><head><title>Listing of " << xml_stream_escaper(dir_path)
     << "</title>";

  if (! css_path.empty()) {
    ss << boost::format(R"(<link rel="stylesheet" type="text/css" href="%s" />)")
                                % url_encode(css_path);
  } else {
    ss << R"(<style type="text/css">)"
       << R"(h1 {font-size: 1em; font-weight: bold;})"
       << R"(th {text-align: left; padding: 0px 1em 0px 1em;})"
       << R"(td {padding: 0px 1em 0px 1em;})"
       << R"(a {text-decoration: none;})"
       << R"(</style>)";
  }

  ss << "</head><body>";

  ss << R"(<h1 id="title">Listing of )" << xml_stream_escaper(dir_path) << "</h1>"
     << R"(<table id="listing">)"
     << R"(<tr id="heading">)"
     << R"(<th class="colname">Name</th>)"
     << R"(<th class="colsize">Size</th>)"
     << R"(<th class="coldate">Date</th>)"
     << R"(</tr>)";

  if (! prefix.empty()) {
    ss << R"(<tr id="parent" class="item">)"
       << R"(<td class="colname"><a href="../">../</a></td>)"
       << R"(<td class="colsize">&nbsp;</td>)"
       << R"(<td class="coldate">&nbsp;</td>)"
       << R"(</tr>)";
  }
}

void RGWSwiftWebsiteListingFormatter::generate_footer()
{
  ss << R"(</table></body></html>)";
}

std::string RGWSwiftWebsiteListingFormatter::format_name(
  const std::string& item_name) const
{
  return item_name.substr(prefix.length());
}

void RGWSwiftWebsiteListingFormatter::dump_object(const rgw_bucket_dir_entry& objent)
{
  const auto name = format_name(objent.key.name);
  ss << boost::format(R"(<tr class="item %s">)")
                                % "default"
     << boost::format(R"(<td class="colname"><a href="%s">%s</a></td>)")
                                % url_encode(name)
                                % HTMLHelper::escape(name)
     << boost::format(R"(<td class="colsize">%lld</td>)") % objent.meta.size
     << boost::format(R"(<td class="coldate">%s</td>)")
                                % dump_time_to_str(objent.meta.mtime)
     << R"(</tr>)";
}

void RGWSwiftWebsiteListingFormatter::dump_subdir(const std::string& name)
{
  const auto fname = format_name(name);
  ss << R"(<tr class="item subdir">)"
     << boost::format(R"(<td class="colname"><a href="%s">%s</a></td>)")
                                % url_encode(fname)
                                % HTMLHelper::escape(fname)
     << R"(<td class="colsize">&nbsp;</td>)"
     << R"(<td class="coldate">&nbsp;</td>)"
     << R"(</tr>)";
}