Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
NetWork
Cinatra
提交
ec194948
C
Cinatra
项目概览
NetWork
/
Cinatra
通知
5
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cinatra
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ec194948
编写于
1月 30, 2021
作者:
Q
qicosmos
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
format code
上级
414948a6
变更
4
展开全部
显示空白变更内容
内联
并排
Showing
4 changed file
with
3037 addition
and
3118 deletion
+3037
-3118
include/cinatra/connection.hpp
include/cinatra/connection.hpp
+1367
-1352
include/cinatra/http_server.hpp
include/cinatra/http_server.hpp
+520
-525
include/cinatra/request.hpp
include/cinatra/request.hpp
+814
-891
include/cinatra/response.hpp
include/cinatra/response.hpp
+336
-350
未找到文件。
include/cinatra/connection.hpp
浏览文件 @
ec194948
此差异已折叠。
点击以展开。
include/cinatra/http_server.hpp
浏览文件 @
ec194948
此差异已折叠。
点击以展开。
include/cinatra/request.hpp
浏览文件 @
ec194948
此差异已折叠。
点击以展开。
include/cinatra/response.hpp
浏览文件 @
ec194948
...
...
@@ -16,34 +16,37 @@
#include "session_manager.hpp"
#include "http_cache.hpp"
namespace
cinatra
{
class
response
{
public:
response
()
{
}
class
response
{
public:
response
()
{}
std
::
string
&
response_str
(){
return
rep_str_
;
}
std
::
string
&
response_str
()
{
return
rep_str_
;
}
void
enable_response_time
(
bool
enable
)
{
need_response_time_
=
enable
;
if
(
need_response_time_
)
{
char
mbstr
[
50
];
std
::
time_t
tm
=
std
::
chrono
::
system_clock
::
to_time_t
(
last_time_
);
std
::
strftime
(
mbstr
,
sizeof
(
mbstr
),
"%a, %d %b %Y %T GMT"
,
std
::
localtime
(
&
tm
));
std
::
strftime
(
mbstr
,
sizeof
(
mbstr
),
"%a, %d %b %Y %T GMT"
,
std
::
localtime
(
&
tm
));
last_date_str_
=
mbstr
;
}
}
template
<
status_type
status
,
req_content_type
content_type
,
size_t
N
>
constexpr
auto
set_status_and_content
(
const
char
(
&
content
)[
N
],
content_encoding
encoding
=
content_encoding
::
none
)
{
template
<
status_type
status
,
req_content_type
content_type
,
size_t
N
>
constexpr
auto
set_status_and_content
(
const
char
(
&
content
)[
N
],
content_encoding
encoding
=
content_encoding
::
none
)
{
constexpr
auto
status_str
=
to_rep_string
(
status
);
constexpr
auto
type_str
=
to_content_type_str
(
content_type
);
constexpr
auto
len_str
=
num_to_string
<
N
-
1
>::
value
;
constexpr
auto
len_str
=
num_to_string
<
N
-
1
>::
value
;
rep_str_
.
append
(
status_str
).
append
(
len_str
.
data
(),
len_str
.
size
()).
append
(
type_str
).
append
(
rep_server
);
rep_str_
.
append
(
status_str
)
.
append
(
len_str
.
data
(),
len_str
.
size
())
.
append
(
type_str
)
.
append
(
rep_server
);
if
(
need_response_time_
)
if
(
need_response_time_
)
append_date_time
();
else
rep_str_
.
append
(
"
\r\n
"
);
...
...
@@ -58,12 +61,12 @@ namespace cinatra {
if
(
t
-
last_time_
>
1s
)
{
char
mbstr
[
50
];
std
::
time_t
tm
=
std
::
chrono
::
system_clock
::
to_time_t
(
t
);
std
::
strftime
(
mbstr
,
sizeof
(
mbstr
),
"%a, %d %b %Y %T GMT"
,
std
::
localtime
(
&
tm
));
std
::
strftime
(
mbstr
,
sizeof
(
mbstr
),
"%a, %d %b %Y %T GMT"
,
std
::
localtime
(
&
tm
));
last_date_str_
=
mbstr
;
rep_str_
.
append
(
"Date: "
).
append
(
mbstr
).
append
(
"
\r\n\r\n
"
);
last_time_
=
t
;
}
else
{
}
else
{
rep_str_
.
append
(
"Date: "
).
append
(
last_date_str_
).
append
(
"
\r\n\r\n
"
);
}
}
...
...
@@ -71,16 +74,19 @@ namespace cinatra {
void
build_response_str
()
{
rep_str_
.
append
(
to_rep_string
(
status_
));
// if (keep_alive) {
// rep_str_.append("Connection: keep-alive\r\n");
// }
// else {
// rep_str_.append("Connection: close\r\n");
// }
// if (keep_alive) {
// rep_str_.append("Connection: keep-alive\r\n");
// }
// else {
// rep_str_.append("Connection: close\r\n");
// }
if
(
!
headers_
.
empty
())
{
for
(
auto
&
header
:
headers_
)
{
rep_str_
.
append
(
header
.
first
).
append
(
":"
).
append
(
header
.
second
).
append
(
"
\r\n
"
);
for
(
auto
&
header
:
headers_
)
{
rep_str_
.
append
(
header
.
first
)
.
append
(
":"
)
.
append
(
header
.
second
)
.
append
(
"
\r\n
"
);
}
headers_
.
clear
();
}
...
...
@@ -88,7 +94,7 @@ namespace cinatra {
char
temp
[
20
]
=
{};
itoa_fwd
((
int
)
content_
.
size
(),
temp
);
rep_str_
.
append
(
"Content-Length: "
).
append
(
temp
).
append
(
"
\r\n
"
);
if
(
res_type_
!=
req_content_type
::
none
)
{
if
(
res_type_
!=
req_content_type
::
none
)
{
rep_str_
.
append
(
get_content_type
(
res_type_
));
}
rep_str_
.
append
(
"Server: cinatra
\r\n
"
);
...
...
@@ -109,15 +115,14 @@ namespace cinatra {
std
::
vector
<
boost
::
asio
::
const_buffer
>
to_buffers
()
{
std
::
vector
<
boost
::
asio
::
const_buffer
>
buffers
;
add_header
(
"Host"
,
"cinatra"
);
if
(
session_
!=
nullptr
&&
session_
->
is_need_update
())
{
if
(
session_
!=
nullptr
&&
session_
->
is_need_update
())
{
auto
cookie_str
=
session_
->
get_cookie
().
to_string
();
add_header
(
"Set-Cookie"
,
cookie_str
.
c_str
());
add_header
(
"Set-Cookie"
,
cookie_str
.
c_str
());
session_
->
set_need_update
(
false
);
}
buffers
.
reserve
(
headers_
.
size
()
*
4
+
5
);
buffers
.
emplace_back
(
to_buffer
(
status_
));
for
(
auto
const
&
h
:
headers_
)
{
for
(
auto
const
&
h
:
headers_
)
{
buffers
.
emplace_back
(
boost
::
asio
::
buffer
(
h
.
first
));
buffers
.
emplace_back
(
boost
::
asio
::
buffer
(
name_value_separator
));
buffers
.
emplace_back
(
boost
::
asio
::
buffer
(
h
.
second
));
...
...
@@ -127,38 +132,33 @@ namespace cinatra {
buffers
.
push_back
(
boost
::
asio
::
buffer
(
crlf
));
if
(
body_type_
==
content_type
::
string
)
{
buffers
.
emplace_back
(
boost
::
asio
::
buffer
(
content_
.
data
(),
content_
.
size
()));
buffers
.
emplace_back
(
boost
::
asio
::
buffer
(
content_
.
data
(),
content_
.
size
()));
}
if
(
http_cache
::
get
().
need_cache
(
raw_url_
))
{
cache_data
.
clear
();
for
(
auto
&
buf
:
buffers
)
{
cache_data
.
push_back
(
std
::
string
(
boost
::
asio
::
buffer_cast
<
const
char
*>
(
buf
),
boost
::
asio
::
buffer_size
(
buf
)));
for
(
auto
&
buf
:
buffers
)
{
cache_data
.
push_back
(
std
::
string
(
boost
::
asio
::
buffer_cast
<
const
char
*>
(
buf
),
boost
::
asio
::
buffer_size
(
buf
)));
}
}
return
buffers
;
}
void
add_header
(
std
::
string
&&
key
,
std
::
string
&&
value
)
{
void
add_header
(
std
::
string
&&
key
,
std
::
string
&&
value
)
{
headers_
.
emplace_back
(
std
::
move
(
key
),
std
::
move
(
value
));
}
void
clear_headers
()
{
headers_
.
clear
();
}
void
clear_headers
()
{
headers_
.
clear
();
}
void
set_status
(
status_type
status
)
{
status_
=
status
;
}
void
set_status
(
status_type
status
)
{
status_
=
status
;
}
status_type
get_status
()
const
{
return
status_
;
}
status_type
get_status
()
const
{
return
status_
;
}
void
set_delay
(
bool
delay
)
{
delay_
=
delay
;
}
void
set_delay
(
bool
delay
)
{
delay_
=
delay
;
}
void
set_status_and_content
(
status_type
status
)
{
status_
=
status
;
...
...
@@ -166,29 +166,32 @@ namespace cinatra {
build_response_str
();
}
void
set_status_and_content
(
status_type
status
,
std
::
string
&&
content
,
req_content_type
res_type
=
req_content_type
::
none
,
content_encoding
encoding
=
content_encoding
::
none
)
{
void
set_status_and_content
(
status_type
status
,
std
::
string
&&
content
,
req_content_type
res_type
=
req_content_type
::
none
,
content_encoding
encoding
=
content_encoding
::
none
)
{
status_
=
status
;
res_type_
=
res_type
;
#ifdef CINATRA_ENABLE_GZIP
if
(
encoding
==
content_encoding
::
gzip
)
{
std
::
string
encode_str
;
bool
r
=
gzip_codec
::
compress
(
std
::
string_view
(
content
.
data
(),
content
.
length
()),
encode_str
,
true
);
bool
r
=
gzip_codec
::
compress
(
std
::
string_view
(
content
.
data
(),
content
.
length
()),
encode_str
,
true
);
if
(
!
r
)
{
set_status_and_content
(
status_type
::
internal_server_error
,
"gzip compress error"
);
}
else
{
set_status_and_content
(
status_type
::
internal_server_error
,
"gzip compress error"
);
}
else
{
add_header
(
"Content-Encoding"
,
"gzip"
);
set_content
(
std
::
move
(
encode_str
));
}
}
else
}
else
#endif
set_content
(
std
::
move
(
content
));
build_response_str
();
}
std
::
string_view
get_content_type
(
req_content_type
type
)
{
std
::
string_view
get_content_type
(
req_content_type
type
)
{
switch
(
type
)
{
case
cinatra
::
req_content_type
::
html
:
return
rep_html
;
...
...
@@ -204,12 +207,10 @@ namespace cinatra {
}
}
bool
need_delay
()
const
{
return
delay_
;
}
bool
need_delay
()
const
{
return
delay_
;
}
void
reset
()
{
if
(
headers_
.
empty
())
if
(
headers_
.
empty
())
rep_str_
.
clear
();
res_type_
=
req_content_type
::
none
;
status_
=
status_type
::
init
;
...
...
@@ -219,19 +220,15 @@ namespace cinatra {
content_
.
clear
();
session_
=
nullptr
;
if
(
cache_data
.
empty
())
if
(
cache_data
.
empty
())
cache_data
.
clear
();
}
void
set_continue
(
bool
con
)
{
proc_continue_
=
con
;
}
void
set_continue
(
bool
con
)
{
proc_continue_
=
con
;
}
bool
need_continue
()
const
{
return
proc_continue_
;
}
bool
need_continue
()
const
{
return
proc_continue_
;
}
void
set_content
(
std
::
string
&&
content
)
{
void
set_content
(
std
::
string
&&
content
)
{
body_type_
=
content_type
::
string
;
content_
=
std
::
move
(
content
);
}
...
...
@@ -241,7 +238,8 @@ namespace cinatra {
add_header
(
"Transfer-Encoding"
,
"chunked"
);
}
std
::
vector
<
boost
::
asio
::
const_buffer
>
to_chunked_buffers
(
const
char
*
chunk_data
,
size_t
length
,
bool
eof
)
{
std
::
vector
<
boost
::
asio
::
const_buffer
>
to_chunked_buffers
(
const
char
*
chunk_data
,
size_t
length
,
bool
eof
)
{
std
::
vector
<
boost
::
asio
::
const_buffer
>
buffers
;
if
(
length
>
0
)
{
...
...
@@ -255,7 +253,7 @@ namespace cinatra {
buffers
.
push_back
(
boost
::
asio
::
buffer
(
crlf
));
}
//
append last-chunk
//
append last-chunk
if
(
eof
)
{
buffers
.
push_back
(
boost
::
asio
::
buffer
(
last_chunk
));
buffers
.
push_back
(
boost
::
asio
::
buffer
(
crlf
));
...
...
@@ -264,14 +262,15 @@ namespace cinatra {
return
buffers
;
}
std
::
shared_ptr
<
cinatra
::
session
>
start_session
(
const
std
::
string
&
name
,
std
::
time_t
expire
=
-
1
,
std
::
string_view
domain
=
""
,
const
std
::
string
&
path
=
"/"
)
{
session_
=
session_manager
::
get
().
create_session
(
domain
,
name
,
expire
,
path
);
std
::
shared_ptr
<
cinatra
::
session
>
start_session
(
const
std
::
string
&
name
,
std
::
time_t
expire
=
-
1
,
std
::
string_view
domain
=
""
,
const
std
::
string
&
path
=
"/"
)
{
session_
=
session_manager
::
get
().
create_session
(
domain
,
name
,
expire
,
path
);
return
session_
;
}
std
::
shared_ptr
<
cinatra
::
session
>
start_session
()
{
std
::
shared_ptr
<
cinatra
::
session
>
start_session
()
{
if
(
domain_
.
empty
())
{
auto
host
=
get_header_value
(
"host"
);
if
(
!
host
.
empty
())
{
...
...
@@ -286,73 +285,59 @@ namespace cinatra {
return
session_
;
}
void
set_domain
(
std
::
string_view
domain
)
{
domain_
=
domain
;
}
void
set_domain
(
std
::
string_view
domain
)
{
domain_
=
domain
;
}
std
::
string_view
get_domain
()
{
return
domain_
;
}
std
::
string_view
get_domain
()
{
return
domain_
;
}
void
set_path
(
std
::
string_view
path
)
{
path_
=
path
;
}
void
set_path
(
std
::
string_view
path
)
{
path_
=
path
;
}
std
::
string_view
get_path
()
{
return
path_
;
}
std
::
string_view
get_path
()
{
return
path_
;
}
void
set_url
(
std
::
string_view
url
)
{
raw_url_
=
url
;
}
void
set_url
(
std
::
string_view
url
)
{
raw_url_
=
url
;
}
std
::
string_view
get_url
(
std
::
string_view
url
)
{
return
raw_url_
;
}
std
::
string_view
get_url
(
std
::
string_view
url
)
{
return
raw_url_
;
}
void
set_headers
(
std
::
pair
<
phr_header
*
,
size_t
>
headers
)
{
void
set_headers
(
std
::
pair
<
phr_header
*
,
size_t
>
headers
)
{
req_headers_
=
headers
;
}
void
render_string
(
std
::
string
&&
content
)
{
void
render_string
(
std
::
string
&&
content
)
{
#ifdef CINATRA_ENABLE_GZIP
set_status_and_content
(
status_type
::
ok
,
std
::
move
(
content
),
res_content_type
::
string
,
content_encoding
::
gzip
);
set_status_and_content
(
status_type
::
ok
,
std
::
move
(
content
),
res_content_type
::
string
,
content_encoding
::
gzip
);
#else
set_status_and_content
(
status_type
::
ok
,
std
::
move
(
content
),
req_content_type
::
string
,
content_encoding
::
none
);
set_status_and_content
(
status_type
::
ok
,
std
::
move
(
content
),
req_content_type
::
string
,
content_encoding
::
none
);
#endif
}
std
::
vector
<
std
::
string
>
raw_content
()
{
return
cache_data
;
}
std
::
vector
<
std
::
string
>
raw_content
()
{
return
cache_data
;
}
void
redirect
(
const
std
::
string
&
url
,
bool
is_forever
=
false
)
{
add_header
(
"Location"
,
url
.
c_str
());
is_forever
==
false
?
set_status_and_content
(
status_type
::
moved_temporarily
)
:
set_status_and_content
(
status_type
::
moved_permanently
);
void
redirect
(
const
std
::
string
&
url
,
bool
is_forever
=
false
)
{
add_header
(
"Location"
,
url
.
c_str
());
is_forever
==
false
?
set_status_and_content
(
status_type
::
moved_temporarily
)
:
set_status_and_content
(
status_type
::
moved_permanently
);
}
void
redirect_post
(
const
std
::
string
&
url
)
{
void
redirect_post
(
const
std
::
string
&
url
)
{
add_header
(
"Location"
,
url
.
c_str
());
set_status_and_content
(
status_type
::
temporary_redirect
);
}
void
set_session
(
std
::
weak_ptr
<
cinatra
::
session
>
sessionref
)
{
if
(
sessionref
.
lock
()){
void
set_session
(
std
::
weak_ptr
<
cinatra
::
session
>
sessionref
)
{
if
(
sessionref
.
lock
())
{
session_
=
sessionref
.
lock
();
}
}
private:
private:
std
::
string_view
get_header_value
(
std
::
string_view
key
)
const
{
phr_header
*
headers
=
req_headers_
.
first
;
phr_header
*
headers
=
req_headers_
.
first
;
size_t
num_headers
=
req_headers_
.
second
;
for
(
size_t
i
=
0
;
i
<
num_headers
;
i
++
)
{
if
(
iequal
(
headers
[
i
].
name
,
headers
[
i
].
name_len
,
key
.
data
(),
key
.
length
()))
if
(
iequal
(
headers
[
i
].
name
,
headers
[
i
].
name_len
,
key
.
data
(),
key
.
length
()))
return
std
::
string_view
(
headers
[
i
].
value
,
headers
[
i
].
value_len
);
}
...
...
@@ -370,15 +355,16 @@ namespace cinatra {
bool
delay_
=
false
;
std
::
pair
<
phr_header
*
,
size_t
>
req_headers_
;
std
::
pair
<
phr_header
*
,
size_t
>
req_headers_
;
std
::
string_view
domain_
;
std
::
string_view
path_
;
std
::
shared_ptr
<
cinatra
::
session
>
session_
=
nullptr
;
std
::
string
rep_str_
;
std
::
chrono
::
system_clock
::
time_point
last_time_
=
std
::
chrono
::
system_clock
::
now
();
std
::
chrono
::
system_clock
::
time_point
last_time_
=
std
::
chrono
::
system_clock
::
now
();
std
::
string
last_date_str_
;
req_content_type
res_type_
;
bool
need_response_time_
=
false
;
};
};
}
#endif //CINATRA_RESPONSE_HPP
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录