From 49c03ee707a1ea75ae9102ea3bea70c760c90423 Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 27 Feb 2019 10:36:03 +0800 Subject: [PATCH 1/2] Add htmlTranslate() method --- examples/simple_example/api_v1_ApiTest.cc | 1 + lib/inc/drogon/HttpViewData.h | 9 +++++ lib/src/HttpViewData.cc | 45 +++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/src/HttpViewData.cc diff --git a/examples/simple_example/api_v1_ApiTest.cc b/examples/simple_example/api_v1_ApiTest.cc index 0f2cc349..8ae2d15a 100755 --- a/examples/simple_example/api_v1_ApiTest.cc +++ b/examples/simple_example/api_v1_ApiTest.cc @@ -34,6 +34,7 @@ void ApiTest::your_method_name(const HttpRequestPtr &req, const std::function para; para["p1"] = std::to_string(p1); para["p2"] = std::to_string(p2); + para["p3"] = HttpViewData::htmlTranslate(""); data.insert("parameters", para); auto res = HttpResponse::newHttpViewResponse("ListParaView", data); callback(res); diff --git a/lib/inc/drogon/HttpViewData.h b/lib/inc/drogon/HttpViewData.h index 626e1b67..9467d642 100755 --- a/lib/inc/drogon/HttpViewData.h +++ b/lib/inc/drogon/HttpViewData.h @@ -119,6 +119,15 @@ class HttpViewData return _viewData[key]; } + /// Translate some special characters to HTML format, such as: + /** + * " --> " + * & --> & + * < --> < + * > --> > + */ + static std::string htmlTranslate(const std::string &str); + protected: typedef std::unordered_map ViewDataMap; mutable ViewDataMap _viewData; diff --git a/lib/src/HttpViewData.cc b/lib/src/HttpViewData.cc new file mode 100644 index 00000000..1778e2c5 --- /dev/null +++ b/lib/src/HttpViewData.cc @@ -0,0 +1,45 @@ +/** + * + * HttpViewData.cc + * An Tao + * + * Copyright 2018, An Tao. All rights reserved. + * https://github.com/an-tao/drogon + * Use of this source code is governed by a MIT license + * that can be found in the License file. + * + * Drogon + * + */ + +#include + +using namespace drogon; + +std::string HttpViewData::htmlTranslate(const std::string &str) +{ + std::string ret; + ret.reserve(str.length()); + for (auto &ch : str) + { + switch (ch) + { + case '"': + ret.append("""); + break; + case '<': + ret.append("<"); + break; + case '>': + ret.append(">"); + break; + case '&': + ret.append("&"); + break; + default: + ret.push_back(ch); + break; + } + } + return ret; +} \ No newline at end of file From 769a9a83dd9fc7d738c5c5a479a7890c96532974 Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 27 Feb 2019 10:36:37 +0800 Subject: [PATCH 2/2] Change version to 0.9.26 --- get_version.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/get_version.sh b/get_version.sh index fd531455..bff380e1 100755 --- a/get_version.sh +++ b/get_version.sh @@ -3,7 +3,7 @@ GIT_VER=$(git log|grep ^commit|wc -l|sed -e "s/^ *//") MD5=$(git log|head -1|awk '{printf $2}') TMP_FILE=/tmp/version -echo "#define VERSION \"0.9.25.$GIT_VER\"" > ${TMP_FILE} +echo "#define VERSION \"0.9.26.$GIT_VER\"" > ${TMP_FILE} echo "#define VERSION_MD5 \"$MD5\"" >> ${TMP_FILE} if [ ! -f $1 ];then mv -f ${TMP_FILE} $1