Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
e1b732e9
Commit
e1b732e9
authored
Oct 10, 2017
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
windows支持gb2312编码
parent
d26d484e
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
200 行增加
和
26 行删除
+200
-26
src/Common/config.cpp
+4
-0
src/Http/HttpSession.cpp
+15
-2
src/Http/strCoding.cpp
+173
-21
src/Http/strCoding.h
+8
-3
没有找到文件。
src/Common/config.cpp
查看文件 @
e1b732e9
...
...
@@ -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 服务器名称
...
...
src/Http/HttpSession.cpp
查看文件 @
e1b732e9
...
...
@@ -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
);
...
...
src/Http/strCoding.cpp
查看文件 @
e1b732e9
...
...
@@ -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
=
str
len
(
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 */
src/Http/strCoding.h
查看文件 @
e1b732e9
...
...
@@ -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 */
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论