浏览代码

HttpMessage GetBool GetInt GetFloat GetString

hewei 5 年之前
父节点
当前提交
47998aa44d
共有 5 个文件被更改,包括 46 次插入29 次删除
  1. 11 0
      base/hstring.h
  2. 7 4
      examples/httpd/http_api_test.h
  3. 21 11
      http/HttpMessage.cpp
  4. 6 8
      http/HttpMessage.h
  5. 1 6
      http/http_content.h

+ 11 - 0
base/hstring.h

@@ -4,6 +4,7 @@
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
 #include <map>
 #include <map>
+#include <sstream>
 
 
 #include "hbase.h"
 #include "hbase.h"
 
 
@@ -60,6 +61,16 @@ public:
     }
     }
 };
 };
 
 
+namespace hv {
+// NOTE: low-version NDK not provide std::to_string
+template<typename T>
+static inline std::string to_string(const T& num) {
+    std::ostringstream os;
+    os << num;
+    return os.str();
+}
+}
+
 #define SPACE_CHARS     " \t\r\n"
 #define SPACE_CHARS     " \t\r\n"
 #define PAIR_CHARS      "{}[]()<>\"\"\'\'``"
 #define PAIR_CHARS      "{}[]()<>\"\"\'\'``"
 
 

+ 7 - 4
examples/httpd/http_api_test.h

@@ -95,10 +95,13 @@ inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
 }
 }
 
 
 inline int http_api_test(HttpRequest* req, HttpResponse* res) {
 inline int http_api_test(HttpRequest* req, HttpResponse* res) {
-    string str = req->GetValue("string");
-    int64_t n = req->Get<int64_t>("int");
-    double f = req->Get<double>("float");
-    bool b = req->Get<bool>("bool");
+    string str = req->GetString("string");
+    //int64_t n = req->Get<int64_t>("int");
+    //double f = req->Get<double>("float");
+    //bool b = req->Get<bool>("bool");
+    int64_t n = req->GetInt("int");
+    double f = req->GetFloat("float");
+    bool b = req->GetBool("bool");
 
 
     res->content_type = req->content_type;
     res->content_type = req->content_type;
     res->Set("string", str);
     res->Set("string", str);

+ 21 - 11
http/HttpMessage.cpp

@@ -9,7 +9,7 @@
 // NOTE: json ignore number/string, 123/"123"
 // NOTE: json ignore number/string, 123/"123"
 using nlohmann::detail::value_t;
 using nlohmann::detail::value_t;
 
 
-std::string HttpMessage::GetValue(const char* key, const std::string& defvalue) {
+std::string HttpMessage::GetString(const char* key, const std::string& defvalue) {
     switch (content_type) {
     switch (content_type) {
     case APPLICATION_JSON:
     case APPLICATION_JSON:
     {
     {
@@ -28,17 +28,17 @@ std::string HttpMessage::GetValue(const char* key, const std::string& defvalue)
             case value_t::number_integer:
             case value_t::number_integer:
             {
             {
                 std::int64_t n = *iter;
                 std::int64_t n = *iter;
-                return std::to_string(n);
+                return hv::to_string(n);
             }
             }
             case value_t::number_unsigned:
             case value_t::number_unsigned:
             {
             {
                 std::uint64_t n = *iter;
                 std::uint64_t n = *iter;
-                return std::to_string(n);
+                return hv::to_string(n);
             }
             }
             case value_t::number_float:
             case value_t::number_float:
             {
             {
                 double f = *iter;
                 double f = *iter;
-                return std::to_string(f);
+                return hv::to_string(f);
             }
             }
             default:
             default:
                 return defvalue;
                 return defvalue;
@@ -107,8 +107,8 @@ int64_t HttpMessage::Get(const char* key, int64_t defvalue) {
         return defvalue;
         return defvalue;
     }
     }
     else {
     else {
-        std::string str = GetValue(key);
-        return str.empty() ? defvalue : atoi(str.c_str());
+        std::string str = GetString(key);
+        return str.empty() ? defvalue : atoll(str.c_str());
     }
     }
 }
 }
 
 
@@ -151,8 +151,8 @@ double HttpMessage::Get(const char* key, double defvalue) {
         return defvalue;
         return defvalue;
     }
     }
     else {
     else {
-        std::string str = GetValue(key);
-        return str.empty() ? defvalue : atoi(str.c_str());
+        std::string str = GetString(key);
+        return str.empty() ? defvalue : atof(str.c_str());
     }
     }
 }
 }
 
 
@@ -186,7 +186,7 @@ bool HttpMessage::Get(const char* key, bool defvalue) {
             case value_t::string:
             case value_t::string:
             {
             {
                 std::string str = *iter;
                 std::string str = *iter;
-                return atof(str.c_str());
+                return getboolean(str.c_str());
             }
             }
             default:
             default:
                 return defvalue;
                 return defvalue;
@@ -195,10 +195,20 @@ bool HttpMessage::Get(const char* key, bool defvalue) {
         return defvalue;
         return defvalue;
     }
     }
     else {
     else {
-        std::string str = GetValue(key);
-        return str.empty() ? defvalue : atoi(str.c_str());
+        std::string str = GetString(key);
+        return str.empty() ? defvalue : getboolean(str.c_str());
     }
     }
 }
 }
+
+bool HttpMessage::GetBool(const char* key, bool defvalue) {
+    return Get<bool>(key, defvalue);
+}
+int64_t HttpMessage::GetInt(const char* key, int64_t defvalue) {
+    return Get<int64_t>(key, defvalue);
+}
+double HttpMessage::GetFloat(const char* key, double defvalue) {
+    return Get<double>(key, defvalue);
+}
 #endif
 #endif
 
 
 void HttpMessage::FillContentType() {
 void HttpMessage::FillContentType() {

+ 6 - 8
http/HttpMessage.h

@@ -3,7 +3,6 @@
 
 
 #include <string>
 #include <string>
 #include <map>
 #include <map>
-#include <sstream>
 
 
 #include "hstring.h"
 #include "hstring.h"
 #include "httpdef.h"
 #include "httpdef.h"
@@ -39,12 +38,15 @@ public:
     MultiPart           form;       // MULTIPART_FORM_DATA
     MultiPart           form;       // MULTIPART_FORM_DATA
     KeyValue            kv;         // X_WWW_FORM_URLENCODED
     KeyValue            kv;         // X_WWW_FORM_URLENCODED
 
 
-    std::string GetValue(const char* key, const std::string& = std::string(""));
     // T=[bool, int64_t, double]
     // T=[bool, int64_t, double]
     template<typename T>
     template<typename T>
     T Get(const char* key, T defvalue = 0);
     T Get(const char* key, T defvalue = 0);
 
 
-    // T=[string, bool, int64_t, double]
+    std::string GetString(const char* key, const std::string& = "");
+    bool GetBool(const char* key, bool defvalue = 0);
+    int64_t GetInt(const char* key, int64_t defvalue = 0);
+    double GetFloat(const char* key, double defvalue = 0);
+
     template<typename T>
     template<typename T>
     void Set(const char* key, const T& value) {
     void Set(const char* key, const T& value) {
         switch (content_type) {
         switch (content_type) {
@@ -55,11 +57,7 @@ public:
             form[key] = FormData(value);
             form[key] = FormData(value);
             break;
             break;
         case X_WWW_FORM_URLENCODED:
         case X_WWW_FORM_URLENCODED:
-        {
-            std::ostringstream os;
-            os << value;
-            kv[key] = os.str();
-        }
+            kv[key] = hv::to_string(value);
             break;
             break;
         default:
         default:
             break;
             break;

+ 1 - 6
http/http_content.h

@@ -11,7 +11,6 @@ int         parse_query_params(const char* query_string, QueryParams& query_para
 // NOTE: WITHOUT_HTTP_CONTENT
 // NOTE: WITHOUT_HTTP_CONTENT
 // ndk-r10e no std::to_string and can't compile modern json.hpp
 // ndk-r10e no std::to_string and can't compile modern json.hpp
 #ifndef WITHOUT_HTTP_CONTENT
 #ifndef WITHOUT_HTTP_CONTENT
-#include <sstream>
 
 
 /**************multipart/form-data*************************************
 /**************multipart/form-data*************************************
 --boundary
 --boundary
@@ -40,11 +39,7 @@ struct FormData {
     }
     }
     template<typename T>
     template<typename T>
     FormData(T num) {
     FormData(T num) {
-        // NOTE: low-version NDK not provide std::to_string
-        //content = std::to_string(num);
-        std::ostringstream os;
-        os << num;
-        content = os.str();
+        content = hv::to_string(num);
     }
     }
 };
 };