瀏覽代碼

Made toXml and toJson non-member function.

Tatsuhiro Tsujikawa 14 年之前
父節點
當前提交
4f0a45abe2
共有 4 個文件被更改,包括 22 次插入18 次删除
  1. 2 2
      src/HttpServerBodyCommand.cc
  2. 7 6
      src/RpcResponse.cc
  3. 8 5
      src/RpcResponse.h
  4. 5 5
      test/RpcResponseTest.cc

+ 2 - 2
src/HttpServerBodyCommand.cc

@@ -113,7 +113,7 @@ void HttpServerBodyCommand::sendJsonRpcResponse
  const std::string& callback)
 {
   bool gzip = httpServer_->supportsGZip();
-  std::string responseData = res.toJson(callback, gzip);
+  std::string responseData = rpc::toJson(res, callback, gzip);
   if(res.code == 0) {
     httpServer_->feedResponse(responseData,
                               getJsonRpcContentType(!callback.empty()));
@@ -220,7 +220,7 @@ bool HttpServerBodyCommand::execute()
           A2_LOG_INFO(fmt("Executing RPC method %s", req.methodName.c_str()));
           rpc::RpcResponse res = method->execute(req, e_);
           bool gzip = httpServer_->supportsGZip();
-          std::string responseData = res.toXml(gzip);
+          std::string responseData = rpc::toXml(res, gzip);
           httpServer_->feedResponse(responseData, "text/xml");
           addHttpServerResponseCommand();
 #endif // ENABLE_XML_RPC

+ 7 - 6
src/RpcResponse.cc

@@ -145,19 +145,19 @@ RpcResponse& RpcResponse::operator=(const RpcResponse& c)
   return *this;
 }
 
-std::string RpcResponse::toXml(bool gzip) const
+std::string toXml(const RpcResponse& res, bool gzip)
 {
   if(gzip) {
 #ifdef HAVE_ZLIB
     GZipEncoder o;
     o.init();
-    return encodeAll(o, code, param);
+    return encodeAll(o, res.code, res.param);
 #else // !HAVE_ZLIB
     abort();
 #endif // !HAVE_ZLIB
   } else {
     std::stringstream o;
-    return encodeAll(o, code, param);
+    return encodeAll(o, res.code, res.param);
   }
 }
 
@@ -189,19 +189,20 @@ OutputStream& encodeJsonAll
 }
 } // namespace
 
-std::string RpcResponse::toJson(const std::string& callback, bool gzip) const
+std::string toJson
+(const RpcResponse& res, const std::string& callback, bool gzip)
 {
   if(gzip) {
 #ifdef HAVE_ZLIB
     GZipEncoder o;
     o.init();
-    return encodeJsonAll(o, code, param, id, callback).str();
+    return encodeJsonAll(o, res.code, res.param, res.id, callback).str();
 #else // !HAVE_ZLIB
     abort();
 #endif // !HAVE_ZLIB
   } else {
     std::stringstream o;
-    return encodeJsonAll(o, code, param, id, callback).str();
+    return encodeJsonAll(o, res.code, res.param, res.id, callback).str();
   }
 }
 

+ 8 - 5
src/RpcResponse.h

@@ -64,13 +64,16 @@ struct RpcResponse {
   ~RpcResponse();
 
   RpcResponse& operator=(const RpcResponse& c);
+};
 
-  std::string toXml(bool gzip = false) const;
+std::string toXml(const RpcResponse& response, bool gzip = false);
 
-  // Encodes RPC response in JSON. If callback is not empty, the
-  // resulting string is JSONP.
-  std::string toJson(const std::string& callback, bool gzip = false) const;
-};
+// Encodes RPC response in JSON. If callback is not empty, the
+// resulting string is JSONP.
+std::string toJson
+(const RpcResponse& response,
+ const std::string& callback,
+ bool gzip = false);
 
 std::string toJsonBatch
 (const std::vector<RpcResponse>& results,

+ 5 - 5
test/RpcResponseTest.cc

@@ -31,13 +31,13 @@ void RpcResponseTest::testToJson()
     SharedHandle<String> id = String::g("9");
     RpcResponse res(0, param, id);
     results.push_back(res);
-    std::string s = res.toJson("", false);
+    std::string s = toJson(res, "", false);
     CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","
                                      "\"jsonrpc\":\"2.0\","
                                      "\"result\":[1]}"),
                          s);
     // with callback
-    s = res.toJson("cb", false);
+    s = toJson(res, "cb", false);
     CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":\"9\","
                                      "\"jsonrpc\":\"2.0\","
                                      "\"result\":[1]})"),
@@ -50,7 +50,7 @@ void RpcResponseTest::testToJson()
     param->put("message", "HELLO ERROR");
     RpcResponse res(1, param, Null::g());
     results.push_back(res);
-    std::string s = res.toJson("", false);
+    std::string s = toJson(res, "", false);
     CPPUNIT_ASSERT_EQUAL(std::string("{\"error\":{\"code\":1,"
                                      "\"message\":\"HELLO ERROR\"},"
                                      "\"id\":null,"
@@ -58,7 +58,7 @@ void RpcResponseTest::testToJson()
                                      "}"),
                          s);
     // with callback
-    s = res.toJson("cb", false);
+    s = toJson(res, "cb", false);
     CPPUNIT_ASSERT_EQUAL(std::string("cb({\"error\":{\"code\":1,"
                                      "\"message\":\"HELLO ERROR\"},"
                                      "\"id\":null,"
@@ -103,7 +103,7 @@ void RpcResponseTest::testToXml()
   param->put("faultCode", Integer::g(1));
   param->put("faultString", "No such method: make.hamburger");
   RpcResponse res(1, param, Null::g());
-  std::string s = res.toXml(false);
+  std::string s = toXml(res, false);
   CPPUNIT_ASSERT_EQUAL
     (std::string("<?xml version=\"1.0\"?>"
                  "<methodResponse>"