Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sun604645125
Mozi.HttpEmbedded
提交
df6581b7
M
Mozi.HttpEmbedded
项目概览
sun604645125
/
Mozi.HttpEmbedded
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Mozi.HttpEmbedded
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
df6581b7
编写于
9月 11, 2020
作者:
JasonWcx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改模块载入方法,增加服务器用户组
上级
4d6f4aca
变更
26
隐藏空白更改
内联
并排
Showing
26 changed file
with
156 addition
and
144 deletion
+156
-144
Mozi.HttpEmbedded/Auth/Authenticator.cs
Mozi.HttpEmbedded/Auth/Authenticator.cs
+8
-8
Mozi.HttpEmbedded/Auth/AuthorizationType.cs
Mozi.HttpEmbedded/Auth/AuthorizationType.cs
+2
-2
Mozi.HttpEmbedded/Auth/User.cs
Mozi.HttpEmbedded/Auth/User.cs
+11
-2
Mozi.HttpEmbedded/Cookie/HttpCookie.cs
Mozi.HttpEmbedded/Cookie/HttpCookie.cs
+1
-1
Mozi.HttpEmbedded/Encode/Base64.cs
Mozi.HttpEmbedded/Encode/Base64.cs
+2
-2
Mozi.HttpEmbedded/Encode/StringEncoder.cs
Mozi.HttpEmbedded/Encode/StringEncoder.cs
+1
-1
Mozi.HttpEmbedded/Encode/UrlEncoder.cs
Mozi.HttpEmbedded/Encode/UrlEncoder.cs
+8
-8
Mozi.HttpEmbedded/File.cs
Mozi.HttpEmbedded/File.cs
+1
-1
Mozi.HttpEmbedded/Generic/AbsClassEnum.cs
Mozi.HttpEmbedded/Generic/AbsClassEnum.cs
+2
-2
Mozi.HttpEmbedded/HandleError.cs
Mozi.HttpEmbedded/HandleError.cs
+4
-4
Mozi.HttpEmbedded/HeaderProperty.cs
Mozi.HttpEmbedded/HeaderProperty.cs
+6
-6
Mozi.HttpEmbedded/HttpRequest.cs
Mozi.HttpEmbedded/HttpRequest.cs
+26
-26
Mozi.HttpEmbedded/HttpResponse.cs
Mozi.HttpEmbedded/HttpResponse.cs
+6
-6
Mozi.HttpEmbedded/HttpServer.cs
Mozi.HttpEmbedded/HttpServer.cs
+8
-13
Mozi.HttpEmbedded/HttpVersion.cs
Mozi.HttpEmbedded/HttpVersion.cs
+4
-4
Mozi.HttpEmbedded/Page/Router.cs
Mozi.HttpEmbedded/Page/Router.cs
+27
-16
Mozi.HttpEmbedded/Page/TestApi.cs
Mozi.HttpEmbedded/Page/TestApi.cs
+7
-9
Mozi.HttpEmbedded/ProtocolType.cs
Mozi.HttpEmbedded/ProtocolType.cs
+3
-3
Mozi.HttpEmbedded/RequestMethod.cs
Mozi.HttpEmbedded/RequestMethod.cs
+3
-3
Mozi.HttpEmbedded/ServerEvent.cs
Mozi.HttpEmbedded/ServerEvent.cs
+1
-1
Mozi.HttpEmbedded/SocketServer.cs
Mozi.HttpEmbedded/SocketServer.cs
+15
-16
Mozi.HttpEmbedded/Source/Mime.cs
Mozi.HttpEmbedded/Source/Mime.cs
+1
-1
Mozi.HttpEmbedded/Source/StaticFiles.cs
Mozi.HttpEmbedded/Source/StaticFiles.cs
+2
-2
Mozi.HttpEmbedded/StateObject.cs
Mozi.HttpEmbedded/StateObject.cs
+2
-2
Mozi.HttpEmbedded/StatusCode.cs
Mozi.HttpEmbedded/StatusCode.cs
+2
-2
Mozi.HttpEmbedded/TransformHeader.cs
Mozi.HttpEmbedded/TransformHeader.cs
+3
-3
未找到文件。
Mozi.HttpEmbedded/Auth/Authenticator.cs
浏览文件 @
df6581b7
...
...
@@ -25,10 +25,10 @@ namespace Mozi.HttpEmbedded.Auth
{
if
(
authType
.
Equals
(
AuthorizationType
.
Basic
))
{
S
tring
userinfo
=
Base64
.
From
(
authBody
);
s
tring
userinfo
=
Base64
.
From
(
authBody
);
var
indBnd
=
userinfo
.
IndexOf
((
char
)
ASCIICode
.
COLON
);
S
tring
username
=
userinfo
.
Substring
(
0
,
indBnd
);
S
tring
password
=
userinfo
.
Substring
(
indBnd
+
1
);
s
tring
username
=
userinfo
.
Substring
(
0
,
indBnd
);
s
tring
password
=
userinfo
.
Substring
(
indBnd
+
1
);
return
IsValidUser
(
username
,
password
);
}
else
...
...
@@ -46,7 +46,7 @@ namespace Mozi.HttpEmbedded.Auth
/// <returns></returns>
private
bool
IsValidUser
(
string
userName
,
string
userPassword
)
{
return
_users
.
Any
(
x
=>
x
.
UserName
.
Equals
(
userName
)
&&
x
.
Password
.
Equals
(
userPassword
));
return
_users
.
Any
(
x
=>
x
.
UserGroup
==
UserGroup
.
Admin
&&
x
.
UserName
.
Equals
(
userName
)
&&
x
.
Password
.
Equals
(
userPassword
));
}
/// <summary>
/// 设置认证类型
...
...
@@ -69,7 +69,7 @@ namespace Mozi.HttpEmbedded.Auth
var
user
=
_users
.
Find
(
x
=>
x
.
UserName
.
Equals
(
userName
));
if
(
user
==
null
)
{
_users
.
Add
(
new
User
()
{
UserName
=
userName
,
Password
=
userPassword
});
_users
.
Add
(
new
User
()
{
UserName
=
userName
,
Password
=
userPassword
,
UserGroup
=
UserGroup
.
Admin
});
}
else
{
...
...
@@ -117,12 +117,12 @@ namespace Mozi.HttpEmbedded.Auth
public
string
GetChallenge
()
{
List
<
S
tring
>
clgs
=
new
List
<
string
>();
List
<
s
tring
>
clgs
=
new
List
<
string
>();
foreach
(
var
o
in
Challenge
)
{
clgs
.
Add
(
S
tring
.
Format
(
"{0}=\"{1}\""
,
o
.
Key
,
o
.
Value
));
clgs
.
Add
(
s
tring
.
Format
(
"{0}=\"{1}\""
,
o
.
Key
,
o
.
Value
));
}
var
clg
=
String
.
Format
(
"{0} {1}"
,
this
.
GetType
().
Name
,
S
tring
.
Join
(
","
,
clgs
));
var
clg
=
string
.
Format
(
"{0} {1}"
,
this
.
GetType
().
Name
,
s
tring
.
Join
(
","
,
clgs
));
return
clg
;
}
...
...
Mozi.HttpEmbedded/Auth/AuthorizationType.cs
浏览文件 @
df6581b7
...
...
@@ -21,7 +21,7 @@ namespace Mozi.HttpEmbedded.Auth
public
S
tring
Name
public
s
tring
Name
{
get
{
return
_name
;
}
set
{
_name
=
value
;
}
...
...
@@ -32,7 +32,7 @@ namespace Mozi.HttpEmbedded.Auth
get
{
return
Name
;
}
}
private
AuthorizationType
(
S
tring
name
)
private
AuthorizationType
(
s
tring
name
)
{
_name
=
name
;
}
...
...
Mozi.HttpEmbedded/Auth/User.cs
浏览文件 @
df6581b7
namespace
Mozi.HttpEmbedded.Auth
{
/// <summary>
/// 服务器
认证
用户
/// 服务器用户
/// </summary>
class
User
public
class
User
{
public
string
UserName
{
get
;
set
;
}
public
string
Password
{
get
;
set
;
}
public
UserGroup
UserGroup
{
get
;
set
;
}
}
/// <summary>
/// 用户组
/// </summary>
public
enum
UserGroup
{
User
=
0
,
Admin
=
1
//管理组
}
}
Mozi.HttpEmbedded/Cookie/HttpCookie.cs
浏览文件 @
df6581b7
...
...
@@ -124,7 +124,7 @@ namespace Mozi.HttpEmbedded.Cookie
public
static
RequestCookie
Parse
(
string
data
)
{
RequestCookie
hc
=
new
RequestCookie
();
S
tring
[]
kps
=
data
.
Split
(
new
string
[]
{
"; "
},
StringSplitOptions
.
RemoveEmptyEntries
);
s
tring
[]
kps
=
data
.
Split
(
new
string
[]
{
"; "
},
StringSplitOptions
.
RemoveEmptyEntries
);
foreach
(
var
kp
in
kps
)
{
var
startIndex
=
kp
.
IndexOf
((
char
)
ASCIICode
.
EQUAL
);
...
...
Mozi.HttpEmbedded/Encode/Base64.cs
浏览文件 @
df6581b7
...
...
@@ -7,13 +7,13 @@ namespace Mozi.HttpEmbedded.Encode
/// </summary>
public
class
Base64
{
public
static
String
To
(
S
tring
data
)
public
static
string
To
(
s
tring
data
)
{
byte
[]
infos
=
StringEncoder
.
Encode
(
data
);
return
Convert
.
ToBase64String
(
infos
,
Base64FormattingOptions
.
None
);
}
public
static
String
From
(
S
tring
data
)
public
static
string
From
(
s
tring
data
)
{
return
StringEncoder
.
Decode
(
Convert
.
FromBase64String
(
data
));
}
...
...
Mozi.HttpEmbedded/Encode/StringEncoder.cs
浏览文件 @
df6581b7
...
...
@@ -7,7 +7,7 @@ namespace Mozi.HttpEmbedded.Encode
/// </summary>
public
static
class
StringEncoder
{
public
static
byte
[]
Encode
(
S
tring
data
)
public
static
byte
[]
Encode
(
s
tring
data
)
{
return
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
data
);
}
...
...
Mozi.HttpEmbedded/Encode/UrlEncoder.cs
浏览文件 @
df6581b7
...
...
@@ -12,7 +12,7 @@ namespace Mozi.HttpEmbedded.Encode
' '
,
'"'
,
'#'
,
'%'
,
'&'
,
'('
,
')'
,
'+'
,
','
,
'/'
,
':'
,
';'
,
'<'
,
'='
,
'>'
,
'?'
,
'@'
,
'\\'
,
'|'
};
private
static
readonly
S
tring
[]
To
=
{
private
static
readonly
s
tring
[]
To
=
{
"%20"
,
"%22"
,
"%23"
,
"%25"
,
"%26"
,
"%28"
,
"%29"
,
"%2B"
,
"%2C"
,
"%2F"
,
"%3A"
,
"%3B"
,
"%3C"
,
"%3D"
,
"%3E"
,
"%3F"
,
"%40"
,
"%5C"
,
"%7C"
};
/// <summary>
...
...
@@ -20,11 +20,11 @@ namespace Mozi.HttpEmbedded.Encode
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
String
DecodeUrl
(
S
tring
data
)
public
static
string
DecodeUrl
(
s
tring
data
)
{
for
(
int
i
=
0
;
i
<
To
.
Length
;
i
++)
{
S
tring
s
=
To
[
i
];
s
tring
s
=
To
[
i
];
while
(
data
.
Contains
(
s
))
{
data
=
data
.
Replace
(
s
,
To
[
i
]);
...
...
@@ -37,7 +37,7 @@ namespace Mozi.HttpEmbedded.Encode
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
String
EncodeUrl
(
S
tring
data
)
public
static
string
EncodeUrl
(
s
tring
data
)
{
for
(
int
i
=
0
;
i
<
From
.
Length
;
i
++)
{
...
...
@@ -54,14 +54,14 @@ namespace Mozi.HttpEmbedded.Encode
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
Dictionary
<
String
,
String
>
ParseQuery
(
S
tring
data
)
public
static
Dictionary
<
string
,
string
>
ParseQuery
(
s
tring
data
)
{
data
=
DecodeUrl
(
data
);
Dictionary
<
String
,
S
tring
>
res
=
new
Dictionary
<
string
,
string
>();
S
tring
[]
querys
=
data
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
AND
},
StringSplitOptions
.
RemoveEmptyEntries
);
Dictionary
<
string
,
s
tring
>
res
=
new
Dictionary
<
string
,
string
>();
s
tring
[]
querys
=
data
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
AND
},
StringSplitOptions
.
RemoveEmptyEntries
);
foreach
(
var
item
in
querys
)
{
S
tring
[]
kp
=
item
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
EQUAL
},
StringSplitOptions
.
RemoveEmptyEntries
);
s
tring
[]
kp
=
item
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
EQUAL
},
StringSplitOptions
.
RemoveEmptyEntries
);
res
.
Add
(
kp
[
0
],
kp
[
1
]
??
""
);
}
return
res
;
...
...
Mozi.HttpEmbedded/File.cs
浏览文件 @
df6581b7
...
...
@@ -5,7 +5,7 @@ namespace Mozi.HttpEmbedded
{
public
class
File
{
public
S
tring
FileName
{
get
;
set
;
}
public
s
tring
FileName
{
get
;
set
;
}
public
int
FileIndex
{
get
;
set
;
}
public
byte
[]
FileData
{
get
;
set
;
}
}
...
...
Mozi.HttpEmbedded/Generic/AbsClassEnum.cs
浏览文件 @
df6581b7
...
...
@@ -8,7 +8,7 @@ namespace Mozi.HttpEmbedded.Generic
/// </summary>
public
abstract
class
AbsClassEnum
{
protected
abstract
S
tring
Tag
{
get
;
}
protected
abstract
s
tring
Tag
{
get
;
}
/// <summary>
/// 获取方法
/// </summary>
...
...
@@ -20,7 +20,7 @@ namespace Mozi.HttpEmbedded.Generic
FieldInfo
[]
pis
=
typeof
(
T
).
GetFields
(
BindingFlags
.
IgnoreCase
|
BindingFlags
.
Public
|
BindingFlags
.
Static
);
foreach
(
var
info
in
pis
)
{
Object
oc
=
info
.
GetValue
(
null
);
object
oc
=
info
.
GetValue
(
null
);
if
(
oc
!=
null
)
{
if
(((
T
)
oc
).
Tag
.
Equals
(
name
,
StringComparison
.
OrdinalIgnoreCase
))
...
...
Mozi.HttpEmbedded/HandleError.cs
浏览文件 @
df6581b7
...
...
@@ -7,10 +7,10 @@ namespace Mozi.HttpEmbedded
/// </summary>
class
HandleError
{
public
String
Title
{
get
;
set
;
}
public
string
Title
{
get
;
set
;
}
public
DateTime
Time
{
get
;
set
;
}
public
String
Description
{
get
;
set
;
}
public
String
Remark
{
get
;
set
;
}
public
String
Source
{
get
;
set
;
}
public
string
Description
{
get
;
set
;
}
public
string
Remark
{
get
;
set
;
}
public
string
Source
{
get
;
set
;
}
}
}
\ No newline at end of file
Mozi.HttpEmbedded/HeaderProperty.cs
浏览文件 @
df6581b7
...
...
@@ -112,17 +112,17 @@ namespace Mozi.HttpEmbedded
//通常在值为“XMLHttpRequest”时使用
// Not standard
public
S
tring
PropertyTag
{
get
;
set
;
}
public
S
tring
PropertyValue
{
get
;
set
;
}
public
s
tring
PropertyTag
{
get
;
set
;
}
public
s
tring
PropertyValue
{
get
;
set
;
}
protected
override
S
tring
Tag
{
get
{
return
PropertyTag
;
}
}
protected
override
s
tring
Tag
{
get
{
return
PropertyTag
;
}
}
private
HeaderProperty
()
{
}
private
HeaderProperty
(
S
tring
tag
)
private
HeaderProperty
(
s
tring
tag
)
{
PropertyTag
=
tag
;
}
...
...
@@ -132,14 +132,14 @@ namespace Mozi.HttpEmbedded
/// <returns></returns>
public
override
string
ToString
()
{
return
S
tring
.
Format
(
"{0} {1}"
,
PropertyTag
,
PropertyValue
);
return
s
tring
.
Format
(
"{0} {1}"
,
PropertyTag
,
PropertyValue
);
}
/// <summary>
/// 解析
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
HeaderProperty
Parse
(
S
tring
data
)
public
static
HeaderProperty
Parse
(
s
tring
data
)
{
var
tag
=
data
.
Substring
(
0
,
data
.
IndexOf
((
char
)
ASCIICode
.
COLON
));
var
value
=
data
.
Substring
(
data
.
IndexOf
((
char
)
ASCIICode
.
SPACE
)
+
1
);
...
...
Mozi.HttpEmbedded/HttpRequest.cs
浏览文件 @
df6581b7
...
...
@@ -22,32 +22,32 @@ namespace Mozi.HttpEmbedded
/// <summary>
/// 请求路径
/// </summary>
public
S
tring
Path
{
get
;
private
set
;
}
public
s
tring
Path
{
get
;
private
set
;
}
/// <summary>
/// 查询字符串
/// </summary>
public
S
tring
QueryString
{
get
;
private
set
;
}
public
s
tring
QueryString
{
get
;
private
set
;
}
/// <summary>
/// 查询 索引可忽略大小写
/// </summary>
public
Dictionary
<
String
,
S
tring
>
Query
=
new
Dictionary
<
string
,
string
>(
new
StringCompareIgnoreCase
());
public
Dictionary
<
string
,
s
tring
>
Query
=
new
Dictionary
<
string
,
string
>(
new
StringCompareIgnoreCase
());
/// <summary>
/// POST 索引可忽略大小写
/// </summary>
public
Dictionary
<
String
,
S
tring
>
Post
=
new
Dictionary
<
string
,
string
>(
new
StringCompareIgnoreCase
());
public
Dictionary
<
string
,
s
tring
>
Post
=
new
Dictionary
<
string
,
string
>(
new
StringCompareIgnoreCase
());
/// <summary>
/// 可接受压缩算法
/// </summary>
public
S
tring
AcceptEncoding
{
get
;
private
set
;
}
public
s
tring
AcceptEncoding
{
get
;
private
set
;
}
/// <summary>
/// 源地址
/// </summary>
public
S
tring
Host
{
get
;
private
set
;
}
public
s
tring
Host
{
get
;
private
set
;
}
/// <summary>
/// 客户端信息
/// </summary>
public
S
tring
UserAgent
{
get
;
private
set
;
}
public
s
tring
UserAgent
{
get
;
private
set
;
}
/// <summary>
/// 请求方法
/// </summary>
...
...
@@ -55,11 +55,11 @@ namespace Mozi.HttpEmbedded
/// <summary>
/// 内容类型
/// </summary>
public
S
tring
ContentType
{
get
;
private
set
;
}
public
s
tring
ContentType
{
get
;
private
set
;
}
/// <summary>
/// 内容大小
/// </summary>
public
S
tring
ContentLength
{
get
;
private
set
;
}
public
s
tring
ContentLength
{
get
;
private
set
;
}
/// <summary>
/// 请求头
/// </summary>
...
...
@@ -75,7 +75,7 @@ namespace Mozi.HttpEmbedded
/// <summary>
/// 首行字符串
/// </summary>
public
S
tring
FirstLineString
{
get
;
private
set
;
}
public
s
tring
FirstLineString
{
get
;
private
set
;
}
/// <summary>
/// 请求头数据
/// </summary>
...
...
@@ -185,7 +185,7 @@ namespace Mozi.HttpEmbedded
/// <param name="data"></param>
private
static
void
SplitPayload
(
ref
HttpRequest
req
,
byte
[]
data
)
{
S
tring
formType
=
req
.
Headers
.
GetValue
(
HeaderProperty
.
ContentType
.
PropertyTag
);
s
tring
formType
=
req
.
Headers
.
GetValue
(
HeaderProperty
.
ContentType
.
PropertyTag
);
if
(
formType
!=
null
)
{
if
(
formType
.
Contains
(
"application/x-www-form-urlencoded"
))
...
...
@@ -219,9 +219,9 @@ namespace Mozi.HttpEmbedded
/// <param name="data"></param>
private
static
void
SplitPayloadFormData
(
ref
HttpRequest
req
,
byte
[]
data
)
{
String
contentType
=
req
.
Headers
.
GetValue
(
HeaderProperty
.
ContentType
.
PropertyTag
);
S
tring
boundary
=
""
;
S
tring
[]
values
=
contentType
.
Split
(
new
[]{(
char
)
ASCIICode
.
SEMICOLON
},
StringSplitOptions
.
RemoveEmptyEntries
);
string
contentType
=
req
.
Headers
.
GetValue
(
HeaderProperty
.
ContentType
.
PropertyTag
);
s
tring
boundary
=
""
;
s
tring
[]
values
=
contentType
.
Split
(
new
[]{(
char
)
ASCIICode
.
SEMICOLON
},
StringSplitOptions
.
RemoveEmptyEntries
);
//取得分割符号boundary
...
...
@@ -279,8 +279,8 @@ namespace Mozi.HttpEmbedded
File
file
=
new
File
();
bool
isFile
=
false
;
String
fieldName
=
S
tring
.
Empty
;
String
fileName
=
S
tring
.
Empty
;
string
fieldName
=
s
tring
.
Empty
;
string
fileName
=
s
tring
.
Empty
;
while
(
Array
.
IndexOf
(
fragbody
,
ASCIICode
.
CR
,
posCR
+
1
)
>
0
)
{
...
...
@@ -296,9 +296,9 @@ namespace Mozi.HttpEmbedded
{
//内容描述信息
//Content-Disposition: form-data; name="fieldNameHere"; filename="fieldName.ext"
S
tring
disposition
=
StringEncoder
.
Decode
(
fragement
);
S
tring
[]
headers
=
disposition
.
Split
(
new
[]
{(
char
)
ASCIICode
.
SEMICOLON
},
StringSplitOptions
.
RemoveEmptyEntries
);
s
tring
disposition
=
StringEncoder
.
Decode
(
fragement
);
s
tring
[]
headers
=
disposition
.
Split
(
new
[]
{(
char
)
ASCIICode
.
SEMICOLON
},
StringSplitOptions
.
RemoveEmptyEntries
);
fieldName
=
headers
[
1
].
Trim
().
Replace
(
"name="
,
""
).
Trim
((
char
)
ASCIICode
.
QUOTE
);
if
(
headers
.
Length
>
2
)
...
...
@@ -399,15 +399,15 @@ namespace Mozi.HttpEmbedded
//解析起始行
req
.
FirstLineData
=
data
;
req
.
FirstLineString
=
Encoding
.
UTF8
.
GetString
(
data
);
S
tring
[]
sFirst
=
req
.
FirstLineString
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
SPACE
},
StringSplitOptions
.
None
);
s
tring
[]
sFirst
=
req
.
FirstLineString
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
SPACE
},
StringSplitOptions
.
None
);
//方法 查询 协议
S
tring
sMethod
=
sFirst
[
0
];
S
tring
sUrl
=
sFirst
[
1
];
S
tring
sProtocol
=
sFirst
[
2
];
s
tring
sMethod
=
sFirst
[
0
];
s
tring
sUrl
=
sFirst
[
1
];
s
tring
sProtocol
=
sFirst
[
2
];
RequestMethod
rm
=
AbsClassEnum
.
Get
<
RequestMethod
>(
sMethod
);
req
.
Method
=
rm
;
S
tring
[]
urls
=
sUrl
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
QUESTION
},
StringSplitOptions
.
RemoveEmptyEntries
);
s
tring
[]
urls
=
sUrl
.
Split
(
new
[]
{
(
char
)
ASCIICode
.
QUESTION
},
StringSplitOptions
.
RemoveEmptyEntries
);
req
.
Path
=
urls
[
0
];
if
(
urls
.
Length
>
1
)
{
...
...
@@ -415,8 +415,8 @@ namespace Mozi.HttpEmbedded
req
.
Query
=
UrlEncoder
.
ParseQuery
(
urls
[
1
]);
}
S
tring
sProtoType
=
sProtocol
.
Substring
(
0
,
sProtocol
.
IndexOf
((
char
)
ASCIICode
.
DIVIDE
));
S
tring
sProtoVersion
=
sProtocol
.
Substring
(
sProtocol
.
IndexOf
((
char
)
ASCIICode
.
DIVIDE
)
+
1
);
s
tring
sProtoType
=
sProtocol
.
Substring
(
0
,
sProtocol
.
IndexOf
((
char
)
ASCIICode
.
DIVIDE
));
s
tring
sProtoVersion
=
sProtocol
.
Substring
(
sProtocol
.
IndexOf
((
char
)
ASCIICode
.
DIVIDE
)
+
1
);
req
.
Protocol
=
AbsClassEnum
.
Get
<
ProtocolType
>(
sProtoType
);
req
.
ProtocolVersion
=
AbsClassEnum
.
Get
<
HttpVersion
>(
sProtoVersion
);
}
...
...
Mozi.HttpEmbedded/HttpResponse.cs
浏览文件 @
df6581b7
...
...
@@ -22,7 +22,7 @@ namespace Mozi.HttpEmbedded
public
int
ContengLength
=
0
;
public
S
tring
Server
=
""
;
public
s
tring
Server
=
""
;
/// <summary>
/// 请求头
...
...
@@ -31,7 +31,7 @@ namespace Mozi.HttpEmbedded
/// <summary>
/// 压缩类型
/// </summary>
public
S
tring
ContentEncoding
{
get
;
set
;
}
public
s
tring
ContentEncoding
{
get
;
set
;
}
/// <summary>
/// 请求数据体
/// </summary>
...
...
@@ -68,7 +68,7 @@ namespace Mozi.HttpEmbedded
/// </summary>
/// <param name="head"></param>
/// <returns></returns>
public
HttpResponse
AddHeader
(
HeaderProperty
head
,
S
tring
value
)
public
HttpResponse
AddHeader
(
HeaderProperty
head
,
s
tring
value
)
{
Headers
.
Add
(
head
,
value
);
return
this
;
...
...
@@ -79,7 +79,7 @@ namespace Mozi.HttpEmbedded
/// <param name="item"></param>
/// <param name="value"></param>
/// <returns></returns>
public
HttpResponse
AddHeader
(
String
item
,
S
tring
value
)
public
HttpResponse
AddHeader
(
string
item
,
s
tring
value
)
{
Headers
.
Add
(
item
,
value
);
return
this
;
...
...
@@ -106,7 +106,7 @@ namespace Mozi.HttpEmbedded
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
HttpResponse
Write
(
S
tring
data
)
public
HttpResponse
Write
(
s
tring
data
)
{
Write
(
StringEncoder
.
Encode
(
data
));
return
this
;
...
...
@@ -144,7 +144,7 @@ namespace Mozi.HttpEmbedded
/// <returns></returns>
public
byte
[]
GetStatusLine
()
{
return
StringEncoder
.
Encode
(
S
tring
.
Format
(
"HTTP/{0} {1} {2}"
,
ProxyVersion
.
Version
,
Status
.
Code
,
Status
.
Text
));
return
StringEncoder
.
Encode
(
s
tring
.
Format
(
"HTTP/{0} {1} {2}"
,
ProxyVersion
.
Version
,
Status
.
Code
,
Status
.
Text
));
}
/// <summary>
/// 重定向302
...
...
Mozi.HttpEmbedded/HttpServer.cs
浏览文件 @
df6581b7
...
...
@@ -49,24 +49,19 @@ namespace Mozi.HttpEmbedded
/// <summary>
/// 时区
/// </summary>
public
S
tring
Timezone
{
get
;
set
;
}
public
s
tring
Timezone
{
get
;
set
;
}
/// <summary>
/// 编码格式
/// </summary>
public
S
tring
Encoding
{
get
;
set
;
}
public
s
tring
Encoding
{
get
;
set
;
}
/// <summary>
/// 服务器名称
/// </summary>
public
S
tring
ServerName
public
s
tring
ServerName
{
get
{
return
_serverName
;
}
private
set
{
_serverName
=
value
;
}
}
/// <summary>
/// 服务器用户
/// </summary>
private
List
<
User
>
_users
=
new
List
<
User
>();
public
HttpServer
()
{
Auth
=
new
Authenticator
();
...
...
@@ -131,13 +126,13 @@ namespace Mozi.HttpEmbedded
private
StatusCode
HandleAuth
(
ref
HttpContext
context
)
{
var
authorization
=
context
.
Request
.
Headers
.
GetValue
(
HeaderProperty
.
Authorization
.
PropertyTag
);
if
(!
S
tring
.
IsNullOrEmpty
(
authorization
)
&&
Auth
.
Check
(
authorization
))
if
(!
s
tring
.
IsNullOrEmpty
(
authorization
)
&&
Auth
.
Check
(
authorization
))
{
return
HandleRequest
(
ref
context
);
}
else
{
context
.
Response
.
AddHeader
(
HeaderProperty
.
WWWAuthenticate
,
S
tring
.
Format
(
"{0} realm=\"{1}\""
,
Auth
.
AuthType
.
Name
,
AuthorizationType
.
REALM
));
context
.
Response
.
AddHeader
(
HeaderProperty
.
WWWAuthenticate
,
s
tring
.
Format
(
"{0} realm=\"{1}\""
,
Auth
.
AuthType
.
Name
,
AuthorizationType
.
REALM
));
return
StatusCode
.
Unauthorized
;
}
}
...
...
@@ -264,12 +259,12 @@ namespace Mozi.HttpEmbedded
}
/// <summary>
/// 设置服务器认证用户
/// <para>如果<see cref="F:EnableAuth"/>=<see cref="
Boolean
.False"/>,此设置就没有意义</para>
/// <para>如果<see cref="F:EnableAuth"/>=<see cref="
bool
.False"/>,此设置就没有意义</para>
/// </summary>
/// <param name="userName"></param>
/// <param name="userPassword"></param>
/// <returns></returns>
public
HttpServer
SetUser
(
String
userName
,
S
tring
userPassword
)
public
HttpServer
SetUser
(
string
userName
,
s
tring
userPassword
)
{
Auth
.
SetUser
(
userName
,
userName
);
return
this
;
...
...
@@ -300,7 +295,7 @@ namespace Mozi.HttpEmbedded
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public
HttpServer
UseErrorPage
(
S
tring
page
)
public
HttpServer
UseErrorPage
(
s
tring
page
)
{
throw
new
NotImplementedException
();
}
...
...
Mozi.HttpEmbedded/HttpVersion.cs
浏览文件 @
df6581b7
...
...
@@ -14,9 +14,9 @@ namespace Mozi.HttpEmbedded
public
static
readonly
HttpVersion
Version12
=
new
HttpVersion
(
"1.2"
);
public
static
readonly
HttpVersion
Version20
=
new
HttpVersion
(
"2.0"
);
public
S
tring
Version
{
get
{
return
_vervalue
;
}
}
public
s
tring
Version
{
get
{
return
_vervalue
;
}
}
protected
override
S
tring
Tag
{
get
{
return
Version
;
}
}
protected
override
s
tring
Tag
{
get
{
return
Version
;
}
}
private
string
_vervalue
=
""
;
...
...
@@ -28,9 +28,9 @@ namespace Mozi.HttpEmbedded
/// 重写ToString方法
/// </summary>
/// <returns></returns>
public
override
S
tring
ToString
()
public
override
s
tring
ToString
()
{
return
S
tring
.
Format
(
"HTTP/{0}"
,
_vervalue
);
return
s
tring
.
Format
(
"HTTP/{0}"
,
_vervalue
);
}
}
}
\ No newline at end of file
Mozi.HttpEmbedded/Page/Router.cs
浏览文件 @
df6581b7
...
...
@@ -25,26 +25,37 @@ namespace Mozi.HttpEmbedded.Page
private
Router
()
{
LoadInternalApi
();
}
/// <summary>
///
调起
///
从程序集载入接口
/// </summary>
/// <param name="
ctx
"></param>
internal
object
Invoke
(
HttpContext
ctx
)
/// <param name="
ass
"></param>
private
void
LoadApiFromAssembly
(
Assembly
ass
)
{
Assembly
ass
=
Assembly
.
GetExecutingAssembly
();
Type
[]
types
=
ass
.
GetExportedTypes
();
foreach
(
var
type
in
types
)
{
if
(
type
.
IsSubclassOf
(
typeof
(
BaseApi
)))
{
apis
.
Add
(
type
);
apis
.
Add
(
type
);
}
}
ass
.
GetModules
();
}
/// <summary>
/// 载入内部接口
/// </summary>
private
void
LoadInternalApi
()
{
Assembly
ass
=
Assembly
.
GetExecutingAssembly
();
LoadApiFromAssembly
(
ass
);
}
/// <summary>
/// 调起
/// </summary>
/// <param name="ctx"></param>
internal
object
Invoke
(
HttpContext
ctx
)
{
string
path
=
ctx
.
Request
.
Path
;
//确定路径映射关系
AccessPoint
ap
=
Match
(
path
);
...
...
@@ -73,14 +84,15 @@ namespace Mozi.HttpEmbedded.Page
return
method
.
Invoke
(
instance
,
BindingFlags
.
IgnoreCase
,
null
,
args
,
CultureInfo
.
CurrentCulture
);
//调起相关方法
}
/// <summary>
/// 载入模块
/// </summary>
/// <returns></returns>
public
Router
Register
(
string
filePath
)
{
throw
new
NotImplementedException
();
Assembly
ass
=
Assembly
.
LoadFrom
(
filePath
);
LoadApiFromAssembly
(
ass
);
return
this
;
}
/// <summary>
/// 路由注入
...
...
@@ -112,7 +124,6 @@ namespace Mozi.HttpEmbedded.Page
_mappers
.
Add
(
new
RouteMapper
(){
Pattern
=
pattern
});
return
this
;
}
public
AccessPoint
Match
(
string
path
)
{
foreach
(
var
mapper
in
_mappers
)
...
...
@@ -148,7 +159,7 @@ namespace Mozi.HttpEmbedded.Page
private
string
Prefix
{
get
;
set
;
}
private
string
Suffix
{
get
;
set
;
}
private
S
tring
Link
{
get
;
set
;
}
private
s
tring
Link
{
get
;
set
;
}
private
string
IdName
{
get
;
set
;
}
private
Regex
Matcher
{
get
;
set
;
}
...
...
@@ -171,7 +182,7 @@ namespace Mozi.HttpEmbedded.Page
Prefix
=
pattern
.
Substring
(
0
,
indCrl
);
Link
=
pattern
.
Substring
(
indCrl
+
12
,
indID
-
indCrl
-
12
);
Suffix
=
""
;
Matcher
=
new
Regex
(
S
tring
.
Format
(
"{0}[a-zA-Z]\\w+{1}[a-zA-Z]\\w+{2}"
,
Regex
.
Escape
(
Prefix
),
Regex
.
Escape
(
Link
),
Regex
.
Escape
(
Suffix
)),
RegexOptions
.
IgnoreCase
);
Matcher
=
new
Regex
(
s
tring
.
Format
(
"{0}[a-zA-Z]\\w+{1}[a-zA-Z]\\w+{2}"
,
Regex
.
Escape
(
Prefix
),
Regex
.
Escape
(
Link
),
Regex
.
Escape
(
Suffix
)),
RegexOptions
.
IgnoreCase
);
}
/// <summary>
/// 判断是否匹配路由
...
...
@@ -210,8 +221,8 @@ namespace Mozi.HttpEmbedded.Page
public
class
AccessPoint
{
public
S
tring
Domain
{
get
;
set
;
}
public
S
tring
Method
{
get
;
set
;
}
public
s
tring
Domain
{
get
;
set
;
}
public
s
tring
Method
{
get
;
set
;
}
}
}
}
Mozi.HttpEmbedded/Page/TestApi.cs
浏览文件 @
df6581b7
...
...
@@ -15,15 +15,13 @@ namespace Mozi.HttpEmbedded.Page
{
return
"Welcome to Mozi.HttpEnbedded"
;
}
/// <summary>
/// 取时间
/// </summary>
/// <returns></returns>
public
string
GetTime
()
{
//public string Hello(string greet)
//{
// return String.Format("you say {0}", greet);
//}
}
public
class
Test2
:
Test
{
}
}
}
Mozi.HttpEmbedded/ProtocolType.cs
浏览文件 @
df6581b7
...
...
@@ -13,15 +13,15 @@ namespace Mozi.HttpEmbedded
private
string
_proxy
;
public
S
tring
Proxy
public
s
tring
Proxy
{
get
{
return
_proxy
;
}
set
{
_proxy
=
value
;
}
}
protected
override
S
tring
Tag
{
get
{
return
Proxy
;
}
}
protected
override
s
tring
Tag
{
get
{
return
Proxy
;
}
}
private
ProtocolType
(
S
tring
typeName
)
private
ProtocolType
(
s
tring
typeName
)
{
_proxy
=
typeName
;
}
...
...
Mozi.HttpEmbedded/RequestMethod.cs
浏览文件 @
df6581b7
...
...
@@ -83,17 +83,17 @@ namespace Mozi.HttpEmbedded
/// <summary>
/// 方法名
/// </summary>
public
S
tring
Name
public
s
tring
Name
{
get
{
return
_name
;
}
set
{
_name
=
value
;
}
}
protected
override
S
tring
Tag
{
get
{
return
Name
;
}
}
protected
override
s
tring
Tag
{
get
{
return
Name
;
}
}
private
string
_name
;
private
RequestMethod
(
S
tring
name
)
private
RequestMethod
(
s
tring
name
)
{
_name
=
name
;
}
...
...
Mozi.HttpEmbedded/ServerEvent.cs
浏览文件 @
df6581b7
...
...
@@ -36,7 +36,7 @@ namespace Mozi.HttpEmbedded
public
class
DataTransferArgs
:
EventArgs
{
public
byte
[]
Data
{
get
;
set
;
}
public
S
tring
IP
{
get
;
set
;
}
public
s
tring
IP
{
get
;
set
;
}
public
int
Port
{
get
;
set
;
}
public
Socket
Socket
{
get
;
set
;
}
...
...
Mozi.HttpEmbedded/SocketServer.cs
浏览文件 @
df6581b7
...
...
@@ -13,7 +13,7 @@ namespace Mozi.HttpEmbedded
//private static SocketServer _mSocketServer;
private
int
_iPort
=
9000
;
private
Dictionary
<
string
,
Socket
>
_socketDocker
;
private
Socket
sc
;
private
Socket
_
sc
;
/// <summary>
/// 服务器启动事件
/// </summary>
...
...
@@ -22,6 +22,9 @@ namespace Mozi.HttpEmbedded
/// 客户端连接事件
/// </summary>
public
event
ClientConnect
OnClientConnect
;
/// <summary>
/// 客户端断开连接时间
/// </summary>
public
event
ClientDisConnect
AfterClientDisConnect
;
/// <summary>
/// 数据接收开始事件
...
...
@@ -46,7 +49,7 @@ namespace Mozi.HttpEmbedded
public
Socket
SocketMain
{
get
{
return
sc
;
}
get
{
return
_
sc
;
}
}
public
SocketServer
()
...
...
@@ -54,7 +57,6 @@ namespace Mozi.HttpEmbedded
_socketDocker
=
new
Dictionary
<
string
,
Socket
>();
}
//TODO 测试此处是否有BUG
/// <summary>
/// 启动服务器
...
...
@@ -63,23 +65,23 @@ namespace Mozi.HttpEmbedded
public
void
StartServer
(
int
port
)
{
_iPort
=
port
;
if
(
sc
==
null
)
if
(
_
sc
==
null
)
{
sc
=
new
Socket
(
AddressFamily
.
InterNetwork
,
SocketType
.
Stream
,
System
.
Net
.
Sockets
.
ProtocolType
.
Tcp
);
_
sc
=
new
Socket
(
AddressFamily
.
InterNetwork
,
SocketType
.
Stream
,
System
.
Net
.
Sockets
.
ProtocolType
.
Tcp
);
}
else
{
sc
.
Close
();
_
sc
.
Close
();
}
System
.
Net
.
IPEndPoint
endpoint
=
new
System
.
Net
.
IPEndPoint
(
System
.
Net
.
IPAddress
.
Any
,
_iPort
);
sc
.
Bind
(
endpoint
);
sc
.
Listen
(
_iPort
);
_
sc
.
Bind
(
endpoint
);
_
sc
.
Listen
(
_iPort
);
//回调服务器启动事件
if
(
OnServerStart
!=
null
)
{
OnServerStart
(
this
,
new
ServerArgs
()
{
StartTime
=
DateTime
.
Now
,
StopTime
=
DateTime
.
MinValue
});
}
sc
.
BeginAccept
(
new
AsyncCallback
(
CallbackAccept
),
sc
);
_sc
.
BeginAccept
(
new
AsyncCallback
(
CallbackAccept
),
_
sc
);
}
/// <summary>
/// 关闭服务器
...
...
@@ -89,10 +91,10 @@ namespace Mozi.HttpEmbedded
_socketDocker
.
Clear
();
try
{
sc
.
Shutdown
(
SocketShutdown
.
Both
);
_
sc
.
Shutdown
(
SocketShutdown
.
Both
);
if
(
AfterServerStop
!=
null
)
{
AfterServerStop
(
sc
,
null
);
AfterServerStop
(
_
sc
,
null
);
}
}
catch
...
...
@@ -153,13 +155,10 @@ namespace Mozi.HttpEmbedded
{
//置空数据连接
so
.
ResetBuffer
(
iByteRead
);
if
(
client
.
Available
>
0
)
{
if
(
client
.
Available
>
0
){
//Thread.Sleep(10);
client
.
BeginReceive
(
so
.
Buffer
,
0
,
StateObject
.
BufferSize
,
0
,
CallbackReceive
,
so
);
}
else
{
}
else
{
_socketDocker
.
Remove
(
so
.
Id
);
if
(
AfterReceiveEnd
!=
null
)
{
...
...
Mozi.HttpEmbedded/Source/Mime.cs
浏览文件 @
df6581b7
...
...
@@ -451,7 +451,7 @@ namespace Mozi.HttpEmbedded.Source
/// <returns></returns>
public
static
string
GetContentType
(
string
ext
)
{
if
(
S
tring
.
IsNullOrEmpty
(
ext
)||!
Types
.
ContainsKey
(
ext
))
if
(
s
tring
.
IsNullOrEmpty
(
ext
)||!
Types
.
ContainsKey
(
ext
))
{
return
"text/html"
;
}
...
...
Mozi.HttpEmbedded/Source/StaticFiles.cs
浏览文件 @
df6581b7
...
...
@@ -68,13 +68,13 @@ namespace Mozi.HttpEmbedded.Source
/// </summary>
/// <param name="path"></param>
/// <param name="ifModifiedSince"></param>
/// <returns><see cref="
Boolean
:true">Modified</see></returns>
/// <returns><see cref="
bool
:true">Modified</see></returns>
public
bool
CheckIfModified
(
string
path
,
string
ifModifiedSince
)
{
DateTime
dtModified
=
System
.
IO
.
File
.
GetLastWriteTime
(
_root
+
"\\"
+
path
);
try
{
if
(!
S
tring
.
IsNullOrEmpty
(
ifModifiedSince
))
if
(!
s
tring
.
IsNullOrEmpty
(
ifModifiedSince
))
{
DateTime
dtSince
=
DateTime
.
ParseExact
(
ifModifiedSince
,
"ddd, dd MMM yyyy HH:mm:ss GMT"
,
CultureInfo
.
InvariantCulture
,
...
...
Mozi.HttpEmbedded/StateObject.cs
浏览文件 @
df6581b7
...
...
@@ -11,14 +11,14 @@ namespace Mozi.HttpEmbedded
/// </summary>
class
StateObject
{
public
S
tring
Id
{
get
;
set
;}
//连接标识符
public
s
tring
Id
{
get
;
set
;}
//连接标识符
public
Socket
WorkSocket
=
null
;
public
int
RemotePort
=
0
;
public
static
int
BufferSize
=
1024
;
public
List
<
byte
>
Data
=
new
List
<
byte
>();
public
byte
[]
Buffer
=
new
byte
[
BufferSize
*
2
];
public
StringBuilder
sb
=
new
StringBuilder
();
public
S
tring
IP
{
get
;
set
;
}
public
s
tring
IP
{
get
;
set
;
}
//TODO 此处没有完整处理包体,会有多读取的冗余数据
public
void
ResetBuffer
(
int
count
)
...
...
Mozi.HttpEmbedded/StatusCode.cs
浏览文件 @
df6581b7
...
...
@@ -282,9 +282,9 @@ namespace Mozi.HttpEmbedded
/// </summary>
public
int
Code
{
get
{
return
_code
;
}}
public
String
Text
{
get
{
return
_text
;
}}
public
string
Text
{
get
{
return
_text
;
}}
private
StatusCode
(
int
code
,
S
tring
desc
)
private
StatusCode
(
int
code
,
s
tring
desc
)
{
_code
=
code
;
_text
=
desc
;
...
...
Mozi.HttpEmbedded/TransformHeader.cs
浏览文件 @
df6581b7
...
...
@@ -43,7 +43,7 @@ namespace Mozi.HttpEmbedded
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public
TransformHeader
Add
(
String
key
,
S
tring
value
)
public
TransformHeader
Add
(
string
key
,
s
tring
value
)
{
if
(
HeaderData
.
ContainsKey
(
key
))
{
...
...
@@ -60,7 +60,7 @@ namespace Mozi.HttpEmbedded
/// </summary>
/// <param name="header"></param>
/// <returns></returns>
public
TransformHeader
Add
(
HeaderProperty
header
,
S
tring
value
)
public
TransformHeader
Add
(
HeaderProperty
header
,
s
tring
value
)
{
Add
(
header
.
PropertyTag
,
value
);
return
this
;
...
...
@@ -74,7 +74,7 @@ namespace Mozi.HttpEmbedded
List
<
byte
>
buffer
=
new
List
<
byte
>();
foreach
(
var
item
in
HeaderData
)
{
buffer
.
AddRange
(
Encoding
.
UTF8
.
GetBytes
(
S
tring
.
Format
(
"{0}: {1}"
,
item
.
Key
,
item
.
Value
)));
buffer
.
AddRange
(
Encoding
.
UTF8
.
GetBytes
(
s
tring
.
Format
(
"{0}: {1}"
,
item
.
Key
,
item
.
Value
)));
buffer
.
AddRange
(
Carriage
);
}
return
buffer
.
ToArray
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录