From 9070c229acb762692258a8299a5ace6b53a4993f Mon Sep 17 00:00:00 2001 From: Matt Benjamin Date: Sat, 13 Aug 2022 14:29:10 -0400 Subject: [PATCH] rgw/rest: replace RGW_FORMAT_ macros with an enum class This is a pure cleanup. The method to print an RGWFormat object as a MIME type is now called to_mime_type(). Signed-off-by: Matt Benjamin --- src/rgw/rgw_common.cc | 4 +-- src/rgw/rgw_common.h | 33 +++++++++++++++++++++---- src/rgw/rgw_rest.cc | 34 +++++++++++++------------- src/rgw/rgw_rest.h | 4 +-- src/rgw/rgw_rest_iam.cc | 4 +-- src/rgw/rgw_rest_iam.h | 2 +- src/rgw/rgw_rest_s3.cc | 8 +++--- src/rgw/rgw_rest_s3.h | 3 ++- src/rgw/rgw_rest_sts.cc | 4 +-- src/rgw/rgw_rest_sts.h | 2 +- src/rgw/rgw_rest_swift.cc | 6 ++--- src/rgw/rgw_rest_swift.h | 6 ++--- src/rgw/rgw_swift_auth.cc | 2 +- src/rgw/rgw_sync_module_es_rest.cc | 6 ++--- src/rgw/rgw_sync_module_pubsub_rest.cc | 4 +-- 15 files changed, 73 insertions(+), 49 deletions(-) diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index 044f83ddee9..1c62a68d512 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -360,7 +360,7 @@ void set_req_state_err(req_state* s, int err_no) void dump(req_state* s) { - if (s->format != RGW_FORMAT_HTML) + if (s->format != RGWFormat::HTML) s->formatter->open_object_section("Error"); if (!s->err.err_code.empty()) s->formatter->dump_string("Code", s->err.err_code); @@ -371,7 +371,7 @@ void dump(req_state* s) if (!s->trans_id.empty()) // TODO: connect to expose_bucket or another toggle s->formatter->dump_string("RequestId", s->trans_id); s->formatter->dump_string("HostId", s->host_id); - if (s->format != RGW_FORMAT_HTML) + if (s->format != RGWFormat::HTML) s->formatter->close_section(); } diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 884bceeacbb..237b02aef0c 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -151,10 +151,33 @@ using ceph::crypto::MD5; #define RGW_ATTR_TRACE RGW_ATTR_PREFIX "trace" -#define RGW_FORMAT_PLAIN 0 -#define RGW_FORMAT_XML 1 -#define RGW_FORMAT_JSON 2 -#define RGW_FORMAT_HTML 3 +enum class RGWFormat : int8_t { + BAD_FORMAT = -1, + PLAIN = 0, + XML, + JSON, + HTML, +}; + +static inline const char* to_mime_type(const RGWFormat f) +{ + switch (f) { + case RGWFormat::XML: + return "application/xml"; + break; + case RGWFormat::JSON: + return "application/json"; + break; + case RGWFormat::HTML: + return "text/html"; + break; + case RGWFormat::PLAIN: + return "text/plain"; + break; + default: + return "invalid format"; + } +} #define RGW_CAP_READ 0x1 #define RGW_CAP_WRITE 0x2 @@ -1558,7 +1581,7 @@ struct req_state : DoutPrefixProvider { std::string ratelimit_bucket_marker; std::string ratelimit_user_name; bool content_started{false}; - int format{0}; + RGWFormat format{RGWFormat::PLAIN}; ceph::Formatter *formatter{nullptr}; std::string decoded_uri; std::string relative_uri; diff --git a/src/rgw/rgw_rest.cc b/src/rgw/rgw_rest.cc index 7279bbcc5ef..e2c4ad45579 100644 --- a/src/rgw/rgw_rest.cc +++ b/src/rgw/rgw_rest.cc @@ -592,13 +592,13 @@ void end_header(req_state* s, RGWOp* op, const char *content_type, if (force_content_type || (!content_type && s->formatter->get_len() != 0) || s->is_err()){ switch (s->format) { - case RGW_FORMAT_XML: + case RGWFormat::XML: ctype = "application/xml"; break; - case RGW_FORMAT_JSON: + case RGWFormat::JSON: ctype = "application/json"; break; - case RGW_FORMAT_HTML: + case RGWFormat::HTML: ctype = "text/html"; break; default: @@ -660,7 +660,7 @@ void abort_early(req_state *s, RGWOp* op, int err_no, string error_content(""); if (!s->formatter) { s->formatter = new JSONFormatter; - s->format = RGW_FORMAT_JSON; + s->format = RGWFormat::JSON; } // op->error_handler is responsible for calling it's handler error_handler @@ -1725,19 +1725,19 @@ void RGWHandler_REST::put_op(RGWOp* op) } /* put_op */ int RGWHandler_REST::allocate_formatter(req_state *s, - int default_type, + RGWFormat default_type, bool configurable) { - s->format = -1; // set to invalid value to allocation happens anyway + s->format = RGWFormat::BAD_FORMAT; // set to invalid value to allocation happens anyway auto type = default_type; if (configurable) { string format_str = s->info.args.get("format"); if (format_str.compare("xml") == 0) { - type = RGW_FORMAT_XML; + type = RGWFormat::XML; } else if (format_str.compare("json") == 0) { - type = RGW_FORMAT_JSON; + type = RGWFormat::JSON; } else if (format_str.compare("html") == 0) { - type = RGW_FORMAT_HTML; + type = RGWFormat::HTML; } else { const char *accept = s->info.env->get("HTTP_ACCEPT"); if (accept) { @@ -1748,11 +1748,11 @@ int RGWHandler_REST::allocate_formatter(req_state *s, } format_buf[i] = 0; if ((strcmp(format_buf, "text/xml") == 0) || (strcmp(format_buf, "application/xml") == 0)) { - type = RGW_FORMAT_XML; + type = RGWFormat::XML; } else if (strcmp(format_buf, "application/json") == 0) { - type = RGW_FORMAT_JSON; + type = RGWFormat::JSON; } else if (strcmp(format_buf, "text/html") == 0) { - type = RGW_FORMAT_HTML; + type = RGWFormat::HTML; } } } @@ -1760,7 +1760,7 @@ int RGWHandler_REST::allocate_formatter(req_state *s, return RGWHandler_REST::reallocate_formatter(s, type); } -int RGWHandler_REST::reallocate_formatter(req_state *s, int type) +int RGWHandler_REST::reallocate_formatter(req_state *s, const RGWFormat type) { if (s->format == type) { // do nothing, just reset @@ -1778,14 +1778,14 @@ int RGWHandler_REST::reallocate_formatter(req_state *s, int type) const bool swift_bulkupload = s->prot_flags & RGW_REST_SWIFT && s->info.args.exists("extract-archive"); switch (s->format) { - case RGW_FORMAT_PLAIN: + case RGWFormat::PLAIN: { const bool use_kv_syntax = s->info.args.exists("bulk-delete") || multipart_delete || swift_bulkupload; s->formatter = new RGWFormatter_Plain(use_kv_syntax); break; } - case RGW_FORMAT_XML: + case RGWFormat::XML: { const bool lowercase_underscore = s->info.args.exists("bulk-delete") || multipart_delete || swift_bulkupload; @@ -1793,10 +1793,10 @@ int RGWHandler_REST::reallocate_formatter(req_state *s, int type) s->formatter = new XMLFormatter(false, lowercase_underscore); break; } - case RGW_FORMAT_JSON: + case RGWFormat::JSON: s->formatter = new JSONFormatter(false); break; - case RGW_FORMAT_HTML: + case RGWFormat::HTML: s->formatter = new HTMLFormatter(s->prot_flags & RGW_REST_WEBSITE); break; default: diff --git a/src/rgw/rgw_rest.h b/src/rgw/rgw_rest.h index 833be21ecbb..926756402ea 100644 --- a/src/rgw/rgw_rest.h +++ b/src/rgw/rgw_rest.h @@ -547,7 +547,7 @@ protected: virtual RGWOp *op_options() { return NULL; } public: - static int allocate_formatter(req_state *s, int default_formatter, + static int allocate_formatter(req_state *s, RGWFormat default_formatter, bool configurable); static constexpr int MAX_BUCKET_NAME_LEN = 255; @@ -558,7 +558,7 @@ public: static int validate_bucket_name(const std::string& bucket); static int validate_object_name(const std::string& object); - static int reallocate_formatter(req_state *s, int type); + static int reallocate_formatter(req_state *s, RGWFormat type); int init_permissions(RGWOp* op, optional_yield y) override; int read_permissions(RGWOp* op, optional_yield y) override; diff --git a/src/rgw/rgw_rest_iam.cc b/src/rgw/rgw_rest_iam.cc index 4b2547903f3..d71b5b31808 100644 --- a/src/rgw/rgw_rest_iam.cc +++ b/src/rgw/rgw_rest_iam.cc @@ -99,7 +99,7 @@ int RGWHandler_REST_IAM::init(rgw::sal::Store* store, { s->dialect = "iam"; - if (int ret = RGWHandler_REST_IAM::init_from_header(s, RGW_FORMAT_XML, true); ret < 0) { + if (int ret = RGWHandler_REST_IAM::init_from_header(s, RGWFormat::XML, true); ret < 0) { ldpp_dout(s, 10) << "init_from_header returned err=" << ret << dendl; return ret; } @@ -113,7 +113,7 @@ int RGWHandler_REST_IAM::authorize(const DoutPrefixProvider* dpp, optional_yield } int RGWHandler_REST_IAM::init_from_header(req_state* s, - int default_formatter, + RGWFormat default_formatter, bool configurable_format) { string req; diff --git a/src/rgw/rgw_rest_iam.h b/src/rgw/rgw_rest_iam.h index 93bcc58d82f..698758e836a 100644 --- a/src/rgw/rgw_rest_iam.h +++ b/src/rgw/rgw_rest_iam.h @@ -14,7 +14,7 @@ class RGWHandler_REST_IAM : public RGWHandler_REST { void rgw_iam_parse_input(); public: - static int init_from_header(req_state *s, int default_formatter, bool configurable_format); + static int init_from_header(req_state *s, RGWFormat default_formatter, bool configurable_format); RGWHandler_REST_IAM(const rgw::auth::StrategyRegistry& auth_registry, bufferlist& bl_post_body) diff --git a/src/rgw/rgw_rest_s3.cc b/src/rgw/rgw_rest_s3.cc index 6ebb1fc7d04..49a2465d232 100644 --- a/src/rgw/rgw_rest_s3.cc +++ b/src/rgw/rgw_rest_s3.cc @@ -4751,7 +4751,7 @@ RGWOp *RGWHandler_REST_Obj_S3::op_options() int RGWHandler_REST_S3::init_from_header(rgw::sal::Store* store, req_state* s, - int default_formatter, + RGWFormat default_formatter, bool configurable_format) { string req; @@ -5039,7 +5039,7 @@ int RGW_Auth_S3::authorize(const DoutPrefixProvider *dpp, int RGWHandler_Auth_S3::init(rgw::sal::Store* store, req_state *state, rgw::io::BasicClient *cio) { - int ret = RGWHandler_REST_S3::init_from_header(store, state, RGW_FORMAT_JSON, true); + int ret = RGWHandler_REST_S3::init_from_header(store, state, RGWFormat::JSON, true); if (ret < 0) return ret; @@ -5054,8 +5054,8 @@ RGWHandler_REST* RGWRESTMgr_S3::get_handler(rgw::sal::Store* store, bool is_s3website = enable_s3website && (s->prot_flags & RGW_REST_WEBSITE); int ret = RGWHandler_REST_S3::init_from_header(store, s, - is_s3website ? RGW_FORMAT_HTML : - RGW_FORMAT_XML, true); + is_s3website ? RGWFormat::HTML : + RGWFormat::XML, true); if (ret < 0) return NULL; diff --git a/src/rgw/rgw_rest_s3.h b/src/rgw/rgw_rest_s3.h index a4c9c893c1b..adff0e12ecc 100644 --- a/src/rgw/rgw_rest_s3.h +++ b/src/rgw/rgw_rest_s3.h @@ -649,7 +649,8 @@ class RGWHandler_REST_S3 : public RGWHandler_REST { protected: const rgw::auth::StrategyRegistry& auth_registry; public: - static int init_from_header(rgw::sal::Store* store, req_state *s, int default_formatter, bool configurable_format); + static int init_from_header(rgw::sal::Store* store, req_state *s, RGWFormat default_formatter, + bool configurable_format); explicit RGWHandler_REST_S3(const rgw::auth::StrategyRegistry& auth_registry) : RGWHandler_REST(), diff --git a/src/rgw/rgw_rest_sts.cc b/src/rgw/rgw_rest_sts.cc index bd1b842f8cf..026f9a28934 100644 --- a/src/rgw/rgw_rest_sts.cc +++ b/src/rgw/rgw_rest_sts.cc @@ -793,7 +793,7 @@ int RGWHandler_REST_STS::init(rgw::sal::Store* store, { s->dialect = "sts"; - if (int ret = RGWHandler_REST_STS::init_from_header(s, RGW_FORMAT_XML, true); ret < 0) { + if (int ret = RGWHandler_REST_STS::init_from_header(s, RGWFormat::XML, true); ret < 0) { ldpp_dout(s, 10) << "init_from_header returned err=" << ret << dendl; return ret; } @@ -810,7 +810,7 @@ int RGWHandler_REST_STS::authorize(const DoutPrefixProvider* dpp, optional_yield } int RGWHandler_REST_STS::init_from_header(req_state* s, - int default_formatter, + RGWFormat default_formatter, bool configurable_format) { string req; diff --git a/src/rgw/rgw_rest_sts.h b/src/rgw/rgw_rest_sts.h index 3a3abdf26e8..a129074b48e 100644 --- a/src/rgw/rgw_rest_sts.h +++ b/src/rgw/rgw_rest_sts.h @@ -204,7 +204,7 @@ class RGWHandler_REST_STS : public RGWHandler_REST { void rgw_sts_parse_input(); public: - static int init_from_header(req_state *s, int default_formatter, bool configurable_format); + static int init_from_header(req_state *s, RGWFormat default_formatter, bool configurable_format); RGWHandler_REST_STS(const rgw::auth::StrategyRegistry& auth_registry, const std::string& post_body="") : RGWHandler_REST(), diff --git a/src/rgw/rgw_rest_swift.cc b/src/rgw/rgw_rest_swift.cc index b615b38f258..2e4115aa9d1 100644 --- a/src/rgw/rgw_rest_swift.cc +++ b/src/rgw/rgw_rest_swift.cc @@ -167,7 +167,7 @@ void RGWListBuckets_ObjStore_SWIFT::send_response_begin(bool has_buckets) { if (op_ret) { set_req_state_err(s, op_ret); - } else if (!has_buckets && s->format == RGW_FORMAT_PLAIN) { + } else if (!has_buckets && s->format == RGWFormat::PLAIN) { op_ret = STATUS_NO_CONTENT; set_req_state_err(s, op_ret); } @@ -414,7 +414,7 @@ void RGWListBucket_ObjStore_SWIFT::send_response() /* swift is a bit inconsistent here */ switch (s->format) { - case RGW_FORMAT_XML: + case RGWFormat::XML: s->formatter->dump_string("name", name); break; default: @@ -2983,7 +2983,7 @@ int RGWHandler_REST_SWIFT::init_from_header(rgw::sal::Store* store, return -ENOENT; } - int ret = allocate_formatter(s, RGW_FORMAT_PLAIN, true); + int ret = allocate_formatter(s, RGWFormat::PLAIN, true); if (ret < 0) return ret; diff --git a/src/rgw/rgw_rest_swift.h b/src/rgw/rgw_rest_swift.h index a6e0bd90d9f..c4842f70635 100644 --- a/src/rgw/rgw_rest_swift.h +++ b/src/rgw/rgw_rest_swift.h @@ -541,7 +541,7 @@ public: rgw::io::BasicClient* const cio) override { state->dialect = "swift"; state->formatter = new JSONFormatter; - state->format = RGW_FORMAT_JSON; + state->format = RGWFormat::JSON; return RGWHandler::init(store, state, cio); } @@ -598,7 +598,7 @@ public: rgw::io::BasicClient* const cio) override { state->dialect = "swift"; state->formatter = new JSONFormatter; - state->format = RGW_FORMAT_JSON; + state->format = RGWFormat::JSON; return RGWHandler::init(store, state, cio); } @@ -655,7 +655,7 @@ public: rgw::io::BasicClient* const cio) override { state->dialect = "swift"; state->formatter = new JSONFormatter; - state->format = RGW_FORMAT_JSON; + state->format = RGWFormat::JSON; return RGWHandler::init(store, state, cio); } diff --git a/src/rgw/rgw_swift_auth.cc b/src/rgw/rgw_swift_auth.cc index 0069d02e655..8415a41e3a5 100644 --- a/src/rgw/rgw_swift_auth.cc +++ b/src/rgw/rgw_swift_auth.cc @@ -756,7 +756,7 @@ int RGWHandler_SWIFT_Auth::init(rgw::sal::Store* store, req_state *state, { state->dialect = "swift-auth"; state->formatter = new JSONFormatter; - state->format = RGW_FORMAT_JSON; + state->format = RGWFormat::JSON; return RGWHandler::init(store, state, cio); } diff --git a/src/rgw/rgw_sync_module_es_rest.cc b/src/rgw/rgw_sync_module_es_rest.cc index d5c202e9a01..e0f5d19e77a 100644 --- a/src/rgw/rgw_sync_module_es_rest.cc +++ b/src/rgw/rgw_sync_module_es_rest.cc @@ -331,7 +331,7 @@ public: if (is_truncated) { s->formatter->dump_string("NextMarker", next_marker); } - if (s->format == RGW_FORMAT_JSON) { + if (s->format == RGWFormat::JSON) { s->formatter->open_array_section("Objects"); } for (auto& i : response.hits.hits) { @@ -371,7 +371,7 @@ public: rgw_flush_formatter(s, s->formatter); s->formatter->close_section(); }; - if (s->format == RGW_FORMAT_JSON) { + if (s->format == RGWFormat::JSON) { s->formatter->close_section(); } s->formatter->close_section(); @@ -410,7 +410,7 @@ RGWHandler_REST* RGWRESTMgr_MDSearch_S3::get_handler(rgw::sal::Store* store, { int ret = RGWHandler_REST_S3::init_from_header(store, s, - RGW_FORMAT_XML, true); + RGWFormat::XML, true); if (ret < 0) { return nullptr; } diff --git a/src/rgw/rgw_sync_module_pubsub_rest.cc b/src/rgw/rgw_sync_module_pubsub_rest.cc index 8ae6f1dfe6e..2b91dd39cf6 100644 --- a/src/rgw/rgw_sync_module_pubsub_rest.cc +++ b/src/rgw/rgw_sync_module_pubsub_rest.cc @@ -504,7 +504,7 @@ RGWHandler_REST* RGWRESTMgr_PubSub::get_handler(rgw::sal::Store* store, const rgw::auth::StrategyRegistry& auth_registry, const std::string& frontend_prefix) { - if (RGWHandler_REST_S3::init_from_header(store, s, RGW_FORMAT_JSON, true) < 0) { + if (RGWHandler_REST_S3::init_from_header(store, s, RGWFormat::JSON, true) < 0) { return nullptr; } @@ -519,7 +519,7 @@ RGWHandler_REST* RGWRESTMgr_PubSub::get_handler(rgw::sal::Store* store, } else if (s->init_state.url_bucket == "notifications") { handler = new RGWHandler_REST_PSNotifs(auth_registry); } else if (s->info.args.exists("notification")) { - const int ret = RGWHandler_REST::allocate_formatter(s, RGW_FORMAT_XML, true); + const int ret = RGWHandler_REST::allocate_formatter(s, RGWFormat::XML, true); if (ret == 0) { handler = new RGWHandler_REST_PSNotifs_S3(auth_registry); } -- 2.39.5