windows支持gb2312编码

This commit is contained in:
xiongziliang 2017-10-10 00:04:07 +08:00
parent d26d484ecf
commit e1b732e944
4 changed files with 200 additions and 26 deletions

View File

@ -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 服务器名称

View File

@ -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<uint32_t>();
@ -302,13 +308,13 @@ inline bool HttpSession::makeMeun(const string &strFullPath, string &strRet) {
strRet += "<li><a href=\"";
strRet += "/";
strRet += "\">";
strRet += "root directory";
strRet += "根目录";
strRet += "</a></li>\r\n";
strRet += "<li><a href=\"";
strRet += "../";
strRet += "\">";
strRet += "parent directory";
strRet += "上级目录";
strRet += "</a></li>\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<uint32_t>();
bool bClose = (strcasecmp(strConType.data(),"close") == 0) && ( ++m_iReqCnt < reqCnt);

View File

@ -27,40 +27,63 @@
#include <string.h>
#include "strCoding.h"
#if defined(_WIN32)
#include <windows.h>
#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<len; 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]))
{
dd.append("+");
}
else
{
char tempbuff[4];
sprintf(tempbuff, "%%%X%X", (uint8_t)str[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<len) {
if (str[i] == '%') {
tmp[0] = str[i + 1];
tmp[1] = str[i + 2];
output += StrToBin(tmp);
i = i + 3;
}
else if (str[i] == '+') {
output += ' ';
i++;
}
else {
output += str[i];
i++;
}
}
return output;
}
///////////////////////////////windows专用///////////////////////////////////
#if defined(_WIN32)
void UnicodeToGB2312(char* pOut, wchar_t uData)
{
WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(wchar_t), NULL, NULL);
}
void Gb2312ToUnicode(wchar_t* pOut, const char *gbBuffer)
{
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1);
}
string strCoding::UTF8ToGB2312(const string &str) {
auto len = str.size();
auto pText = str.data();
char Ctemp[4] = {0};
char *pOut = new char[len + 1];
memset(pOut, 0, len + 1);
int i = 0, j = 0;
while (i < len)
{
if (pText[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 */

View File

@ -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 */