From f9a7924658914144070ba2334971ec2db9737593 Mon Sep 17 00:00:00 2001 From: hugy <504650082@qq.com> Date: Tue, 25 Apr 2023 08:48:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dutf8=E7=9A=84=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=92=8Cjson=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db.cc | 4 ++-- src/http_handler.cc | 2 +- src/utils.cc | 46 ++++++++++++++++++++++++++++++++++----------- src/utils.h | 2 +- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/db.cc b/src/db.cc index 1e3fe77..4c64ef7 100644 --- a/src/db.cc +++ b/src/db.cc @@ -123,9 +123,9 @@ int DB::Select(DWORD db_hanle, const char *sql, string content(it[i].content); item.push_back(content); } else { - string b64_str = + string base64_str = base64_encode((BYTE *)it[i].content, it[i].content_len); - item.push_back(b64_str); + item.push_back(base64_str); } } else { diff --git a/src/http_handler.cc b/src/http_handler.cc index 91d3e15..8450ca6 100644 --- a/src/http_handler.cc +++ b/src/http_handler.cc @@ -588,7 +588,7 @@ void HttpHandler::HandlerRequest(struct mg_connection *c, void *ev_data) { ret = res.dump(); } if (ret != "") { - mg_http_reply(c, 200, "", ret.c_str(), 0, 0); + mg_http_reply(c, 200, "Content-Type: application/json\r\n", "%s\n", ret.c_str()); } } else { mg_http_reply(c, 500, NULL, "%s", "Invalid URI"); diff --git a/src/utils.cc b/src/utils.cc index 07fd094..d92bd2b 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -172,19 +172,43 @@ std::string Utils::WCharToUTF8(wchar_t *wstr) { return std::string(); } -bool Utils::IsTextUtf8(const char *str, int len) { - int needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0); - wchar_t *wide_str = new wchar_t[needed]; - MultiByteToWideChar(CP_UTF8, 0, str, len, wide_str, needed); +bool Utils::IsTextUtf8(const char *str,int length) { + char endian = 1; + bool littlen_endian = (*(char *)&endian == 1); - char *new_str = new char[len + 1]; - WideCharToMultiByte(CP_ACP, 0, wide_str, -1, new_str, len + 1, NULL, NULL); + size_t i; + int bytes_num; + unsigned char chr; - bool isUtf8 = strcmp(str, new_str) == 0; + i = 0; + bytes_num = 0; + while (i < length) { + if (littlen_endian) { + chr = *(str + i); + } else { // Big Endian + chr = (*(str + i) << 8) | *(str + i + 1); + i++; + } - delete[] wide_str; - delete[] new_str; - - return isUtf8; + if (bytes_num == 0) { + if ((chr & 0x80) != 0) { + while ((chr & 0x80) != 0) { + chr <<= 1; + bytes_num++; + } + if ((bytes_num < 2) || (bytes_num > 6)) { + return false; + } + bytes_num--; + } + } else { + if ((chr & 0xC0) != 0x80) { + return false; + } + bytes_num--; + } + i++; + } + return (bytes_num == 0); } } // namespace wxhelper \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index 331b2fa..284c324 100644 --- a/src/utils.h +++ b/src/utils.h @@ -47,7 +47,7 @@ class Utils { static std::string WCharToUTF8(wchar_t *wstr); - static bool IsTextUtf8(const char *str, int len); + static bool IsTextUtf8(const char * str,int length) ; template static std::vector split(T1 str, T2 letter) {