From b40f76a7a0cf460f2a851b001885a4831a6f6503 Mon Sep 17 00:00:00 2001 From: fang yuxiang Date: Tue, 9 Jan 2018 10:47:54 +0800 Subject: [PATCH] rgw: rework json/xml escape usage follow #19806 Signed-off-by: fang yuxiang fang.yuxiang@eisoo.com --- src/cls/rgw/cls_rgw.cc | 8 ++++---- src/common/Formatter.cc | 21 +++++---------------- src/common/Formatter.h | 1 - src/common/HTMLFormatter.cc | 10 ++++++---- src/rgw/rgw_formats.cc | 9 ++++++--- src/rgw/rgw_rest_s3.cc | 2 +- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/cls/rgw/cls_rgw.cc b/src/cls/rgw/cls_rgw.cc index 9f004c51973..919fec1fbe8 100644 --- a/src/cls/rgw/cls_rgw.cc +++ b/src/cls/rgw/cls_rgw.cc @@ -994,10 +994,10 @@ static void update_olh_log(struct rgw_bucket_olh_entry& olh_data_entry, OLHLogOp static string escape_str(const string& s) { - int len = escape_json_attr_len(s.c_str(), s.size()); - char escaped[len]; - escape_json_attr(s.c_str(), s.size(), escaped); - return string(escaped); + int len = escape_json_attr_len(s.c_str(), s.size()); + std::string escaped(len, 0); + escape_json_attr(s.c_str(), s.size(), escaped.data()); + return escaped; } static int write_obj_instance_entry(cls_method_context_t hctx, struct rgw_bucket_dir_entry& instance_entry, const string& instance_idx) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 3375cb792c3..d0cc2d2aeda 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -163,10 +163,7 @@ void JSONFormatter::print_comma(json_formatter_stack_entry_d& entry) void JSONFormatter::print_quoted_string(std::string_view s) { - int len = escape_json_attr_len(s.data(), s.size()); - char escaped[len]; - escape_json_attr(s.data(), s.size(), escaped); - m_ss << '\"' << escaped << '\"'; + m_ss << '\"' << json_stream_escaper(s.data()) << '\"'; } void JSONFormatter::print_name(const char *name) @@ -450,7 +447,7 @@ void XMLFormatter::dump_string(const char *name, std::string_view s) [this](char c) { return this->to_lower_underscore(c); }); print_spaces(); - m_ss << "<" << e << ">" << escape_xml_str(s.data()) << ""; + m_ss << "<" << e << ">" << xml_stream_escaper(s.data()) << ""; if (m_pretty) m_ss << "\n"; } @@ -464,7 +461,7 @@ void XMLFormatter::dump_string_with_attrs(const char *name, std::string_view s, std::string attrs_str; get_attrs_str(&attrs, attrs_str); print_spaces(); - m_ss << "<" << e << attrs_str << ">" << escape_xml_str(s.data()) << ""; + m_ss << "<" << e << attrs_str << ">" << xml_stream_escaper(s.data()) << ""; if (m_pretty) m_ss << "\n"; } @@ -489,7 +486,7 @@ void XMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted, if (ns) { m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << ""; } else { - m_ss << "<" << e << ">" << escape_xml_str(buf) << ""; + m_ss << "<" << e << ">" << xml_stream_escaper(buf) << ""; } if (m_pretty) @@ -545,7 +542,7 @@ void XMLFormatter::open_section_in_ns(const char *name, const char *ns, const Fo void XMLFormatter::finish_pending_string() { if (!m_pending_string_name.empty()) { - m_ss << escape_xml_str(m_pending_string.str().c_str()) + m_ss << xml_stream_escaper(m_pending_string.str()) << ""; m_pending_string_name.clear(); m_pending_string.str(std::string()); @@ -564,14 +561,6 @@ void XMLFormatter::print_spaces() } } -std::string XMLFormatter::escape_xml_str(std::string_view str) -{ - size_t len = escape_xml_attr_len(str.data()); - std::vector escaped(len, '\0'); - escape_xml_attr(str.data(), &escaped[0]); - return std::string(&escaped[0]); -} - char XMLFormatter::to_lower_underscore(char c) const { if (m_underscored && c == ' ') { diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 6cfe60dac04..8cfcc0b0a77 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -169,7 +169,6 @@ namespace ceph { void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs); void finish_pending_string(); void print_spaces(); - static std::string escape_xml_str(std::string_view str); void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str); char to_lower_underscore(char c) const; diff --git a/src/common/HTMLFormatter.cc b/src/common/HTMLFormatter.cc index c096ec5ceea..5889566d74c 100644 --- a/src/common/HTMLFormatter.cc +++ b/src/common/HTMLFormatter.cc @@ -24,6 +24,8 @@ #include #include // for strdup +#include "common/escape.h" + // ----------------------- namespace ceph { @@ -109,7 +111,7 @@ void HTMLFormatter::dump_float(const char *name, double d) void HTMLFormatter::dump_string(const char *name, std::string_view s) { - dump_template(name, escape_xml_str(s.data())); + dump_template(name, xml_stream_escaper(s.data())); } void HTMLFormatter::dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs) @@ -118,7 +120,7 @@ void HTMLFormatter::dump_string_with_attrs(const char *name, std::string_view s, std::string attrs_str; get_attrs_str(&attrs, attrs_str); print_spaces(); - m_ss << "
  • " << e << ": " << escape_xml_str(s.data()) << attrs_str << "
  • "; + m_ss << "
  • " << e << ": " << xml_stream_escaper(s.data()) << attrs_str << "
  • "; if (m_pretty) m_ss << "\n"; } @@ -139,9 +141,9 @@ void HTMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted std::string e(name); print_spaces(); if (ns) { - m_ss << "
  • " << e << ": " << escape_xml_str(buf) << "
  • "; + m_ss << "
  • " << e << ": " << xml_stream_escaper(buf) << "
  • "; } else { - m_ss << "
  • " << e << ": " << escape_xml_str(buf) << "
  • "; + m_ss << "
  • " << e << ": " << xml_stream_escaper(buf) << "
  • "; } if (m_pretty) diff --git a/src/rgw/rgw_formats.cc b/src/rgw/rgw_formats.cc index 0803a614ff8..4c207078844 100644 --- a/src/rgw/rgw_formats.cc +++ b/src/rgw/rgw_formats.cc @@ -287,7 +287,10 @@ void RGWFormatter_Plain::dump_value_int(const char *name, const char *fmt, ...) class HTMLHelper : public XMLFormatter { public: static std::string escape(const std::string& unescaped_str) { - return escape_xml_str(unescaped_str.c_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; } }; @@ -298,7 +301,7 @@ void RGWSwiftWebsiteListingFormatter::generate_header( ss << R"()"; - ss << "Listing of " << HTMLHelper::escape(dir_path) + ss << "<html><head><title>Listing of " << xml_stream_escaper(dir_path) << ""; if (! css_path.empty()) { @@ -315,7 +318,7 @@ void RGWSwiftWebsiteListingFormatter::generate_header( ss << ""; - ss << R"(

    Listing of )" << HTMLHelper::escape(dir_path) << "

    " + ss << R"(

    Listing of )" << xml_stream_escaper(dir_path) << "

    " << R"()" << R"()" << R"()" diff --git a/src/rgw/rgw_rest_s3.cc b/src/rgw/rgw_rest_s3.cc index fce58c61a66..5c62d9423ef 100644 --- a/src/rgw/rgw_rest_s3.cc +++ b/src/rgw/rgw_rest_s3.cc @@ -2158,7 +2158,7 @@ void RGWCopyObj_ObjStore_S3::send_response() if (op_ret == 0) { dump_time(s, "LastModified", &mtime); - std::string etag_str = etag.to_str(); + std::string etag_str = etag.c_str(); if (! etag_str.empty()) { s->formatter->dump_string("ETag", std::move(etag_str)); } -- 2.39.5
    Name