]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: different formatters for different services
authorYehuda Sadeh <yehuda@inktank.com>
Mon, 24 Sep 2012 19:30:29 +0000 (12:30 -0700)
committerYehuda Sadeh <yehuda@inktank.com>
Mon, 8 Oct 2012 18:12:28 +0000 (11:12 -0700)
Use json formatter by default for new usage stuff. Also
make it configurable.

We use the JSON formatter for the swift auth.

Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/rgw/rgw_common.h
src/rgw/rgw_rest.cc
src/rgw/rgw_rest.h
src/rgw/rgw_rest_s3.cc
src/rgw/rgw_rest_s3.h
src/rgw/rgw_rest_swift.cc
src/rgw/rgw_swift_auth.cc

index 4f3f14c07feec8f7c095fbab674fe9a4c8ee937c..fcedfba882302dc33c12561e5fee485a2cc3b6f7 100644 (file)
@@ -61,6 +61,7 @@ using ceph::crypto::MD5;
 #define RGW_MAX_PENDING_CHUNKS  16
 #define RGW_MAX_PUT_SIZE        (5ULL*1024*1024*1024)
 
+#define RGW_FORMAT_PLAIN        0
 #define RGW_FORMAT_XML          1
 #define RGW_FORMAT_JSON         2
 
index 8885f72d31252c6eb74d81011b75f18d6cc3e763..ecd4fa3b02712845c5195956763d73b695de4eb5 100644 (file)
@@ -605,6 +605,37 @@ static int init_meta_info(struct req_state *s)
   return 0;
 }
 
+int RGWHandler_ObjStore::allocate_formatter(struct req_state *s, int default_type, bool configurable)
+{
+  s->format = default_type;
+  if (configurable) {
+    string format_str = s->args.get("format");
+    if (format_str.compare("xml") == 0) {
+      s->format = RGW_FORMAT_XML;
+    } else if (format_str.compare("json") == 0) {
+      s->format = RGW_FORMAT_JSON;
+    }
+  }
+
+  switch (s->format) {
+    case RGW_FORMAT_PLAIN:
+      s->formatter = new RGWFormatter_Plain;
+      break;
+    case RGW_FORMAT_XML:
+      s->formatter = new XMLFormatter(false);
+      break;
+    case RGW_FORMAT_JSON:
+      s->formatter = new JSONFormatter(false);
+      break;
+    default:
+      return -EINVAL;
+
+  };
+  s->formatter->reset();
+
+  return 0;
+}
+
 // This function enforces Amazon's spec for bucket names.
 // (The requirements, not the recommendations.)
 int RGWHandler_ObjStore::validate_bucket_name(const string& bucket)
index a4ffa2fca692c01ceccb9e3df1ccd56a58602325..ed09976028038c3352fcb0592510ae11a05992a3 100644 (file)
@@ -195,6 +195,8 @@ protected:
 
   virtual int validate_bucket_name(const string& bucket);
   virtual int validate_object_name(const string& object);
+
+  static int allocate_formatter(struct req_state *s, int default_formatter, bool configurable);
 public:
   RGWHandler_ObjStore() {}
   virtual ~RGWHandler_ObjStore() {}
index 27745ff4a492a56d09c1ed6555297ce98abb47d8..331b97deaeaca9b0cc4e427cfb0a0bba6286636a 100644 (file)
@@ -766,16 +766,11 @@ RGWOp *RGWHandler_ObjStore_Obj_S3::op_post()
     return new RGWInitMultipart_ObjStore_S3;
 }
 
-int RGWHandler_ObjStore_S3::init_from_header(struct req_state *s)
+int RGWHandler_ObjStore_S3::init_from_header(struct req_state *s, int default_formatter, bool configurable_format)
 {
   string req;
   string first;
 
-  /* this is the default, might change in a few lines */
-  s->format = RGW_FORMAT_XML;
-  s->formatter = new XMLFormatter(false);
-  s->formatter->reset();
-
   int pos;
   if (g_conf->rgw_dns_name.length() && s->host) {
     string h(s->host);
@@ -806,6 +801,11 @@ int RGWHandler_ObjStore_S3::init_from_header(struct req_state *s)
   s->args.set(p);
   s->args.parse();
 
+  /* must be called after the args parsing */
+  int ret = allocate_formatter(s, default_formatter, configurable_format);
+  if (ret < 0)
+    return ret;
+
   if (*req_name != '/')
     return 0;
 
@@ -1146,7 +1146,7 @@ int RGW_Auth_S3::authorize(struct req_state *s)
 
 int RGWHandler_Auth_S3::init(struct req_state *state, RGWClientIO *cio)
 {
-  int ret = RGWHandler_ObjStore_S3::init_from_header(state);
+  int ret = RGWHandler_ObjStore_S3::init_from_header(state, RGW_FORMAT_JSON, true);
   if (ret < 0)
     return ret;
 
@@ -1155,7 +1155,7 @@ int RGWHandler_Auth_S3::init(struct req_state *state, RGWClientIO *cio)
 
 RGWHandler *RGWRESTMgr_S3::get_handler(struct req_state *s)
 {
-  int ret = RGWHandler_ObjStore_S3::init_from_header(s);
+  int ret = RGWHandler_ObjStore_S3::init_from_header(s, RGW_FORMAT_XML, false);
   if (ret < 0)
     return NULL;
 
index 42a77a08c9460ac78cca72fa118859341bfdf187..8545e8df730829671cd34a41a4c657d00237b24d 100644 (file)
@@ -186,7 +186,7 @@ public:
 class RGWHandler_ObjStore_S3 : public RGWHandler_ObjStore {
   friend class RGWRESTMgr_S3;
 public:
-  static int init_from_header(struct req_state *s);
+  static int init_from_header(struct req_state *s, int default_formatter, bool configurable_format);
 
   RGWHandler_ObjStore_S3() : RGWHandler_ObjStore() {}
   virtual ~RGWHandler_ObjStore_S3() {}
index fc7595f38c364419e01e26db2a46a43b6e1a2e54..5ea1e5a7af6fc91d9027719147d603b104e92daf 100644 (file)
@@ -720,18 +720,9 @@ int RGWHandler_ObjStore_SWIFT::init_from_header(struct req_state *s)
     return -ENOENT;
   }
 
-  s->formatter = new RGWFormatter_Plain;
-  string format_str = s->args.get("format");
-  if (format_str.compare("xml") == 0) {
-    s->format = RGW_FORMAT_XML;
-    delete s->formatter;
-    s->formatter = new XMLFormatter(false);
-  } else if (format_str.compare("json") == 0) {
-    s->format = RGW_FORMAT_JSON;
-    delete s->formatter;
-    s->formatter = new JSONFormatter(false);
-  }
-  s->formatter->reset();
+  int ret = allocate_formatter(s, RGW_FORMAT_PLAIN, true);
+  if (ret < 0)
+    return ret;
 
   string ver;
 
index 656a3314351f2be939f685006eb2958c6604cedc..64be3318824991a20cb912c55e9e9602f88932ae 100644 (file)
@@ -221,6 +221,8 @@ done:
 int RGWHandler_SWIFT_Auth::init(struct req_state *state, RGWClientIO *cio)
 {
   state->dialect = "swift-auth";
+  state->formatter = new JSONFormatter;
+  state->format = RGW_FORMAT_JSON;
 
   return RGWHandler::init(state, cio);
 }