]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw/rest: replace RGW_FORMAT_ macros with an enum class
authorMatt Benjamin <mbenjamin@redhat.com>
Sat, 13 Aug 2022 18:29:10 +0000 (14:29 -0400)
committerMatt Benjamin <mbenjamin@redhat.com>
Tue, 16 Aug 2022 17:52:42 +0000 (13:52 -0400)
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 <mbenjamin@redhat.com>
15 files changed:
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_rest.cc
src/rgw/rgw_rest.h
src/rgw/rgw_rest_iam.cc
src/rgw/rgw_rest_iam.h
src/rgw/rgw_rest_s3.cc
src/rgw/rgw_rest_s3.h
src/rgw/rgw_rest_sts.cc
src/rgw/rgw_rest_sts.h
src/rgw/rgw_rest_swift.cc
src/rgw/rgw_rest_swift.h
src/rgw/rgw_swift_auth.cc
src/rgw/rgw_sync_module_es_rest.cc
src/rgw/rgw_sync_module_pubsub_rest.cc

index 044f83ddee9239517137674f2a39fc1301295a12..1c62a68d512db61e608cbaef938d4c943e4f78e2 100644 (file)
@@ -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();
 }
 
index 884bceeacbb17241779da533770c6744b75a1baf..237b02aef0c4bb878d1b2e196bd5705958c4d6ef 100644 (file)
@@ -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;
index 7279bbcc5ef0d4e3bf57522aa0fe1353434e8000..e2c4ad4557984830d91f041d315c341f7237d276 100644 (file)
@@ -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:
index 833be21ecbb76c520865ed347d34c4dd1363caa1..926756402eab788f7d34fc326d3194bbbbd7c84b 100644 (file)
@@ -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;
index 4b2547903f32fcd8830b0e67500bfc28d4d8ac21..d71b5b3180812091206f4155f32071ec84793fc6 100644 (file)
@@ -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;
index 93bcc58d82f19c771ef729c6df578b9e989376b5..698758e836a3389b529235504a5ac07d27052ef7 100644 (file)
@@ -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)
index 6ebb1fc7d043049e74b60d7625b37717bb302545..49a2465d2323a5ab84bf3b5891bb02c7b6c41f21 100644 (file)
@@ -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;
 
index a4c9c893c1b1d75c2543b473ee715077adf46ec9..adff0e12ecc9bb12e092cdc6696b620b13e65a2a 100644 (file)
@@ -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(),
index bd1b842f8cf991d1c76d063c4ab6f1814068829d..026f9a28934fc308fa37d708bfafa7c72854c334 100644 (file)
@@ -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;
index 3a3abdf26e8b00a80f7b7e5d34e25d02679c5a91..a129074b48e94b12e611293f03e624c6bd8b0472 100644 (file)
@@ -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(),
index b615b38f25812be5e2564b334a6859d951504089..2e4115aa9d1196710b0b8ba285cac5f679d21674 100644 (file)
@@ -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;
 
index a6e0bd90d9ff206c2a8848016d2139c4cb24fec2..c4842f70635f25e7a64433a981853c385ad70e16 100644 (file)
@@ -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);
   }
index 0069d02e6551ef2eda436c417e4c32287cfe35ff..8415a41e3a5db074deb004a72443a667946e300a 100644 (file)
@@ -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);
 }
index d5c202e9a01e7ffb6722a305255169bd367c7823..e0f5d19e77a3702adf37ba8737c31f79747ace5b 100644 (file)
@@ -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;
   }
index 8ae6f1dfe6ee2a9b9598b249656049a430bcb254..2b91dd39cf66d30a18a09c2df4ce731e8998df4c 100644 (file)
@@ -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);
     }