Merge pull request #62 from an-tao/dev

Add htmlTranslate() method
This commit is contained in:
An Tao 2019-02-27 11:13:12 +08:00 committed by GitHub
commit 89cac562cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 1 deletions

View File

@ -34,6 +34,7 @@ void ApiTest::your_method_name(const HttpRequestPtr &req, const std::function<vo
std::map<std::string, std::string> para; std::map<std::string, std::string> para;
para["p1"] = std::to_string(p1); para["p1"] = std::to_string(p1);
para["p2"] = std::to_string(p2); para["p2"] = std::to_string(p2);
para["p3"] = HttpViewData::htmlTranslate("<script>alert(\" This should not be displayed in a browser alert box.\");</script>");
data.insert("parameters", para); data.insert("parameters", para);
auto res = HttpResponse::newHttpViewResponse("ListParaView", data); auto res = HttpResponse::newHttpViewResponse("ListParaView", data);
callback(res); callback(res);

View File

@ -3,7 +3,7 @@
GIT_VER=$(git log|grep ^commit|wc -l|sed -e "s/^ *//") GIT_VER=$(git log|grep ^commit|wc -l|sed -e "s/^ *//")
MD5=$(git log|head -1|awk '{printf $2}') MD5=$(git log|head -1|awk '{printf $2}')
TMP_FILE=/tmp/version 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} echo "#define VERSION_MD5 \"$MD5\"" >> ${TMP_FILE}
if [ ! -f $1 ];then if [ ! -f $1 ];then
mv -f ${TMP_FILE} $1 mv -f ${TMP_FILE} $1

View File

@ -119,6 +119,15 @@ class HttpViewData
return _viewData[key]; return _viewData[key];
} }
/// Translate some special characters to HTML format, such as:
/**
* " --> &quot;
* & --> &amp;
* < --> &lt;
* > --> &gt;
*/
static std::string htmlTranslate(const std::string &str);
protected: protected:
typedef std::unordered_map<std::string, any> ViewDataMap; typedef std::unordered_map<std::string, any> ViewDataMap;
mutable ViewDataMap _viewData; mutable ViewDataMap _viewData;

45
lib/src/HttpViewData.cc Normal file
View File

@ -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 <drogon/HttpViewData.h>
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("&quot;");
break;
case '<':
ret.append("&lt;");
break;
case '>':
ret.append("&gt;");
break;
case '&':
ret.append("&amp;");
break;
default:
ret.push_back(ch);
break;
}
}
return ret;
}