From: Justin Cinkelj <justin.cink...@xlab.si>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

httpserver: unify string to bool parameter conversion

boolean parameter type is used in file, network and trace.json.
Corresponding .cc files have diffrent string to bool conversions.
And each implementation has a slightly different behaviour.
trace.cc:
"true" and "1" are true, otherwise false
file.cc:
"false" and "" are false, "true" is true, otherwise throw exception

So "0" and "1" are handled inconsistently.
Fix this by using a common implementation, which can be reused in other
modules too.

Signed-off-by: Justin Cinkelj <justin.cink...@xlab.si>
Message-Id: <20170217100547.11281-1-justin.cink...@xlab.si>

---
diff --git a/modules/httpserver/api/file.cc b/modules/httpserver/api/file.cc
--- a/modules/httpserver/api/file.cc
+++ b/modules/httpserver/api/file.cc
@@ -37,19 +37,6 @@ using namespace json;
 using namespace std;
 using namespace file_json;

-static bool is_true(const http::server::request& req, const string& param)
-{
-    string val = req.get_query_param(param);
-    std::transform(val.begin(), val.end(), val.begin(), ::tolower);
-    if (val == "" || val == "false") {
-        return false;
-    }
-    if (val != "true") {
- throw bad_param_exception(string("Invalid value ") + val + " use true/false");
-    }
-    return true;
-}
-
 /**
  * A helper function to set the op and path param
  * It validate that both exists and if not, throw an exception
@@ -343,7 +330,7 @@ class del_file_handler : public handler_base {
             get_stat(full_path, buffer);
             try {
                 if ((buffer.st_mode & S_IFMT) == S_IFDIR
-                        && is_true(req, "recursive")) {
+                        && str2bool(req.get_query_param("recursive"))) {
                     boost::filesystem::remove_all(full_path);
                 } else {
                     boost::filesystem::remove(full_path);
diff --git a/modules/httpserver/api/trace.cc b/modules/httpserver/api/trace.cc
--- a/modules/httpserver/api/trace.cc
+++ b/modules/httpserver/api/trace.cc
@@ -46,18 +46,6 @@ void httpserver::api::trace::init(routes & routes)
         }
     };

-    struct str2bool {
-        str2bool(std::string s)
-        {
-            std::transform(s.begin(), s.end(), s.begin(), ::tolower);
-            value = s == "true" || s == "1";
-        }
-        operator bool() const {
-            return value;
-        }
-        bool value;
-    };
-
     trace_json_init_path("Trace API");

     trace_json::getTraceEventStatus.set_handler([](const_req req) {
diff --git a/modules/httpserver/common.cc b/modules/httpserver/common.cc
--- a/modules/httpserver/common.cc
+++ b/modules/httpserver/common.cc
@@ -6,6 +6,9 @@
  */
 #include "common.hh"

+#include "request.hh"
+#include "exception.hh"
+
 namespace httpserver {

 operation_type str2type(const std::string& type)
@@ -25,4 +28,21 @@ operation_type str2type(const std::string& type)
     return GET;
 }

+namespace api {
+
+bool str2bool(std::string val)
+{
+    std::transform(val.begin(), val.end(), val.begin(), ::tolower);
+    if (val == "" || val == "false" || val == "0") {
+        return false;
+    }
+    if (val == "true" || val == "1") {
+        return true;
+    }
+ throw bad_param_exception(std::string("Invalid value ") + val + " use true/false or 0/1");
+    return true;
+}
+
+}
+
 }
diff --git a/modules/httpserver/common.hh b/modules/httpserver/common.hh
--- a/modules/httpserver/common.hh
+++ b/modules/httpserver/common.hh
@@ -26,6 +26,17 @@ enum operation_type {
  */
 operation_type str2type(const std::string& type);

+namespace api {
+
+/**
+ * Convert string to bool value. If input is invalid, exception is raised.
+ * @param val  string "true" or "1" -> true, "false", "0" or "" -> false.
+ * @return the boolean value
+ */
+bool str2bool(std::string val);
+
+}
+
 }

 #endif /* COMMON_HH_ */

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to