diff --git a/src/Common/config.cpp b/src/Common/config.cpp index a0ab5168..3d12bb8e 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -95,7 +95,11 @@ const char kKeepAliveSecond[] = HTTP_FIELD"keepAliveSecond"; const char kMaxReqCount[] = HTTP_FIELD"maxReqCount"; //http 字符编码 +#if defined(_WIN32) +#define HTTP_CHAR_SET "gb2312" +#else #define HTTP_CHAR_SET "utf-8" +#endif const char kCharSet[] = HTTP_FIELD"charSet"; //http 服务器名称 diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 67e12cea..13310066 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -175,6 +175,12 @@ void HttpSession::onManager() { inline HttpSession::HttpCode HttpSession::Handle_Req_GET() { string strUrl = strCoding::UrlUTF8Decode(m_parser.Url()); +#ifdef _WIN32 + static bool isGb2312 = !strcasecmp(mINI::Instance()[Config::Http::kCharSet].data(), "gb2312"); + if (isGb2312) { + strUrl = strCoding::UTF8ToGB2312(strUrl); + } +#endif // _WIN32 string strFile = m_strPath + strUrl; string strConType = m_parser["Connection"]; static uint32_t reqCnt = mINI::Instance()[Config::Http::kMaxReqCount].as(); @@ -302,13 +308,13 @@ inline bool HttpSession::makeMeun(const string &strFullPath, string &strRet) { strRet += "
  • "; - strRet += "root directory"; + strRet += "根目录"; strRet += "
  • \r\n"; strRet += "
  • "; - strRet += "parent directory"; + strRet += "上级目录"; strRet += "
  • \r\n"; } @@ -404,6 +410,13 @@ inline HttpSession::HttpCode HttpSession::Handle_Req_POST() { m_strRcvBuf.erase(0, iContentLen); string strUrl = strCoding::UrlUTF8Decode(m_parser.Url()); +#ifdef _WIN32 + static bool isGb2312 = !strcasecmp(mINI::Instance()[Config::Http::kCharSet].data(), "gb2312"); + if (isGb2312) { + strUrl = strCoding::UTF8ToGB2312(strUrl); + } +#endif // _WIN32 + string strConType = m_parser["Connection"]; static uint32_t reqCnt = mINI::Instance()[Config::Http::kMaxReqCount].as(); bool bClose = (strcasecmp(strConType.data(),"close") == 0) && ( ++m_iReqCnt < reqCnt); diff --git a/src/Http/strCoding.cpp b/src/Http/strCoding.cpp index 979a6e94..0f3b093b 100644 --- a/src/Http/strCoding.cpp +++ b/src/Http/strCoding.cpp @@ -27,40 +27,63 @@ #include #include "strCoding.h" +#if defined(_WIN32) +#include +#endif//defined(_WIN32) + namespace ZL { namespace Http { -inline char strCoding::CharToInt(char ch) { - if (ch >= '0' && ch <= '9') - return (char) (ch - '0'); - if (ch >= 'a' && ch <= 'f') - return (char) (ch - 'a' + 10); - if (ch >= 'A' && ch <= 'F') - return (char) (ch - 'A' + 10); +//////////////////////////通用/////////////////////// +void UTF8ToUnicode(WCHAR* pOut, const char *pText) +{ + char* uchar = (char *)pOut; + uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F); + uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F); + return; +} +void UnicodeToUTF8(char* pOut, const WCHAR* pText) +{ + // 注意 WCHAR高低字的顺序,低字节在前,高字节在后 + const char* pchar = (const char *)pText; + pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4)); + pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6); + pOut[2] = (0x80 | (pchar[0] & 0x3F)); + return; +} + +char CharToInt(char ch) +{ + if (ch >= '0' && ch <= '9')return (char)(ch - '0'); + if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10); + if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10); return -1; } -inline char strCoding::StrToBin(const char *str) { +char StrToBin(const char *str) +{ char tempWord[2]; char chn; - tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011 - tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000 - chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000 + tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011 + tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000 + chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000 return chn; } -string strCoding::UrlUTF8Encode(const char * str) { + +string strCoding::UrlUTF8Encode(const string &str) { string dd; - size_t len = strlen(str); + size_t len = str.size(); for (size_t i = 0; i < len; i++) { - if (isalnum((uint8_t) str[i])) { + if (isalnum((uint8_t)str[i])) { char tempbuff[2]; sprintf(tempbuff, "%c", str[i]); dd.append(tempbuff); - } else if (isspace((uint8_t) str[i])) { + } + else if (isspace((uint8_t)str[i])) { dd.append("+"); - } else { + } + else { char tempbuff[4]; - sprintf(tempbuff, "%%%X%X", ((uint8_t*) str)[i] >> 4, - ((uint8_t*) str)[i] % 16); + sprintf(tempbuff, "%%%X%X", (uint8_t)str[i] >> 4,(uint8_t)str[i] % 16); dd.append(tempbuff); } } @@ -69,17 +92,19 @@ string strCoding::UrlUTF8Encode(const char * str) { string strCoding::UrlUTF8Decode(const string &str) { string output = ""; char tmp[2]; - int i = 0, len = str.length(); + int i = 0, len = str.length(); while (i < len) { if (str[i] == '%') { tmp[0] = str[i + 1]; tmp[1] = str[i + 2]; output += StrToBin(tmp); i = i + 3; - } else if (str[i] == '+') { + } + else if (str[i] == '+') { output += ' '; i++; - } else { + } + else { output += str[i]; i++; } @@ -87,5 +112,132 @@ string strCoding::UrlUTF8Decode(const string &str) { return output; } +string strCoding::UrlGB2312Encode(const string &str) +{ + string dd; + size_t len = str.size(); + for (size_t i = 0; i> 4, (uint8_t)str[i] % 16); + dd.append(tempbuff); + } + } + return dd; +} + +string strCoding::UrlGB2312Decode(const string &str) +{ + string output = ""; + char tmp[2]; + int i = 0, idx = 0, len = str.length(); + while (i= 0) + { + pOut[j++] = pText[i++]; + } + else + { + wchar_t Wtemp; + UTF8ToUnicode(&Wtemp, pText + i); + UnicodeToGB2312(Ctemp, Wtemp); + pOut[j] = Ctemp[0]; + pOut[j + 1] = Ctemp[1]; + i += 3; + j += 2; + } + } + string ret = pOut; + delete[] pOut; + return ret; +} + +string strCoding::GB2312ToUTF8(const string &str) { + auto len = str.size(); + auto pText = str.data(); + char buf[4] = { 0 }; + int nLength = len * 3; + char* pOut = new char[nLength]; + memset(pOut, 0, nLength); + int i = 0, j = 0; + while (i < len) + { + //如果是英文直接复制就可以 + if (*(pText + i) >= 0) + { + pOut[j++] = pText[i++]; + } + else + { + wchar_t pbuffer; + Gb2312ToUnicode(&pbuffer, pText + i); + UnicodeToUTF8(buf, &pbuffer); + pOut[j] = buf[0]; + pOut[j + 1] = buf[1]; + pOut[j + 2] = buf[2]; + j += 3; + i += 2; + } + } + string ret = pOut; + delete[] pOut; + return ret; +} +#endif//defined(_WIN32) + + + + } /* namespace Http */ } /* namespace ZL */ diff --git a/src/Http/strCoding.h b/src/Http/strCoding.h index a891c53a..ea1382eb 100644 --- a/src/Http/strCoding.h +++ b/src/Http/strCoding.h @@ -37,13 +37,18 @@ namespace Http { class strCoding { public: - static string UrlUTF8Encode(const char * str); //urlutf8 编码 + static string UrlUTF8Encode(const string &str); //urlutf8 编码 static string UrlUTF8Decode(const string &str); //urlutf8解码 + + static string UrlGB2312Encode(const string &str); //urlgb2312编码 + static string UrlGB2312Decode(const string &str); //urlgb2312解码 +#if defined(_WIN32) + static string UTF8ToGB2312(const string &str);//utf_8转为gb2312 + static string GB2312ToUTF8(const string &str); //gb2312 转utf_8 +#endif//defined(_WIN32) private: strCoding(void); virtual ~strCoding(void); - static inline char CharToInt(char ch); - static inline char StrToBin(const char *str); }; } /* namespace Http */