Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
9498fc8a
Unverified
Commit
9498fc8a
authored
Jul 06, 2023
by
Dominik Charousset
Committed by
GitHub
Jul 06, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1470
Separate WebSocket control frames
parents
f3ccebc3
c5921b51
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
411 additions
and
75 deletions
+411
-75
libcaf_net/caf/net/web_socket/framing.hpp
libcaf_net/caf/net/web_socket/framing.hpp
+2
-1
libcaf_net/src/detail/rfc6455.cpp
libcaf_net/src/detail/rfc6455.cpp
+0
-3
libcaf_net/src/net/web_socket/framing.cpp
libcaf_net/src/net/web_socket/framing.cpp
+74
-64
libcaf_net/test/detail/rfc6455.cpp
libcaf_net/test/detail/rfc6455.cpp
+10
-0
libcaf_net/test/net-test.cpp
libcaf_net/test/net-test.cpp
+0
-1
libcaf_net/test/net/web_socket/framing.cpp
libcaf_net/test/net/web_socket/framing.cpp
+325
-6
No files found.
libcaf_net/caf/net/web_socket/framing.hpp
View file @
9498fc8a
...
...
@@ -132,7 +132,8 @@ private:
// nop
}
bool
handle
(
uint8_t
opcode
,
byte_span
payload
);
// Returns `frame_size` on success and -1 on error.
ptrdiff_t
handle
(
uint8_t
opcode
,
byte_span
payload
,
size_t
frame_size
);
void
ship_pong
(
byte_span
payload
);
...
...
libcaf_net/src/detail/rfc6455.cpp
View file @
9498fc8a
...
...
@@ -84,9 +84,6 @@ ptrdiff_t rfc6455::decode_header(const_byte_span data, header& hdr) {
bool
masked
=
(
byte2
&
0x80
)
!=
0
;
auto
len_field
=
byte2
&
0x7F
;
size_t
header_length
;
// control frames can only have payload up to 125 bytes
if
(
rfc6455
::
is_control_frame
(
hdr
.
opcode
)
&&
len_field
>
125
)
return
-
1
;
if
(
len_field
<
126
)
{
header_length
=
2
+
(
masked
?
4
:
0
);
hdr
.
payload_len
=
len_field
;
...
...
libcaf_net/src/net/web_socket/framing.cpp
View file @
9498fc8a
...
...
@@ -38,12 +38,30 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
// Wait for more input.
return
0
;
}
if
(
detail
::
rfc6455
::
is_control_frame
(
hdr
.
opcode
))
{
// control frames can only have payload up to 125 bytes
if
(
hdr
.
payload_len
>
125
)
{
CAF_LOG_DEBUG
(
"WebSocket control frame payload exceeds allowed size"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"WebSocket control frame payload "
"exceeds allowed size"
);
return
-
1
;
}
}
else
{
// Make sure the entire frame (including header) fits into max_frame_size.
if
(
hdr
.
payload_len
>=
(
max_frame_size
-
static_cast
<
size_t
>
(
hdr_bytes
)
))
{
if
(
hdr
.
payload_len
>=
max_frame_size
-
static_cast
<
size_t
>
(
hdr_bytes
))
{
CAF_LOG_DEBUG
(
"WebSocket frame too large"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"WebSocket frame too large"
);
return
-
1
;
}
// Reject any payload that exceeds max_frame_size. This covers assembled
// payloads as well by including payload_buf_.
if
(
payload_buf_
.
size
()
+
hdr
.
payload_len
>
max_frame_size
)
{
CAF_LOG_DEBUG
(
"fragmented WebSocket payload exceeds maximum size"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"fragmented WebSocket payload "
"exceeds maximum size"
);
return
-
1
;
}
}
// Wait for more data if necessary.
size_t
frame_size
=
hdr_bytes
+
hdr
.
payload_len
;
if
(
buffer
.
size
()
<
frame_size
)
{
...
...
@@ -56,48 +74,42 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
if
(
hdr
.
mask_key
!=
0
)
{
detail
::
rfc6455
::
mask_data
(
hdr
.
mask_key
,
payload
);
}
// Handle control frames first, since these may not me fragmented,
// and can arrive between regular message fragments.
if
(
detail
::
rfc6455
::
is_control_frame
(
hdr
.
opcode
)
&&
hdr
.
opcode
!=
detail
::
rfc6455
::
continuation_frame
)
{
if
(
!
hdr
.
fin
)
{
abort_and_shutdown
(
sec
::
protocol_error
,
"received a fragmented WebSocket control message"
);
return
-
1
;
}
return
handle
(
hdr
.
opcode
,
payload
,
frame_size
);
}
if
(
hdr
.
fin
)
{
if
(
opcode_
==
nil_code
)
{
// Call upper layer.
if
(
hdr
.
opcode
==
detail
::
rfc6455
::
connection_close
)
{
abort_and_shutdown
(
sec
::
connection_closed
);
return
-
1
;
}
else
if
(
!
handle
(
hdr
.
opcode
,
payload
))
{
return
-
1
;
return
handle
(
hdr
.
opcode
,
payload
,
frame_size
);
}
}
else
if
(
hdr
.
opcode
!=
detail
::
rfc6455
::
continuation_frame
)
{
if
(
hdr
.
opcode
!=
detail
::
rfc6455
::
continuation_frame
)
{
CAF_LOG_DEBUG
(
"expected a WebSocket continuation_frame"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"expected a WebSocket continuation_frame"
);
return
-
1
;
}
else
if
(
payload_buf_
.
size
()
+
payload_len
>
max_frame_size
)
{
CAF_LOG_DEBUG
(
"fragmented WebSocket payload exceeds maximum size"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"fragmented WebSocket payload "
"exceeds maximum size"
);
return
-
1
;
}
else
{
if
(
hdr
.
opcode
==
detail
::
rfc6455
::
connection_close
)
{
abort_and_shutdown
(
sec
::
connection_closed
);
return
-
1
;
}
else
{
}
// End of fragmented input.
payload_buf_
.
insert
(
payload_buf_
.
end
(),
payload
.
begin
(),
payload
.
end
());
if
(
!
handle
(
opcode_
,
payload_buf_
))
{
return
-
1
;
}
auto
result
=
handle
(
opcode_
,
payload_buf_
,
frame_size
);
opcode_
=
nil_code
;
payload_buf_
.
clear
();
return
result
;
}
}
}
else
{
// The first frame must not be a continuation frame. Any frame that is not
// the first frame must be a continuation frame.
if
(
opcode_
==
nil_code
)
{
if
(
hdr
.
opcode
==
detail
::
rfc6455
::
continuation_frame
)
{
CAF_LOG_DEBUG
(
"received WebSocket continuation "
"frame without prior opcode"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"received WebSocket continuation "
abort_and_shutdown
(
sec
::
protocol_error
,
"received WebSocket continuation "
"frame without prior opcode"
);
return
-
1
;
}
...
...
@@ -106,15 +118,8 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
CAF_LOG_DEBUG
(
"expected a continuation frame"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"expected a continuation frame"
);
return
-
1
;
}
else
if
(
payload_buf_
.
size
()
+
payload_len
>
max_frame_size
)
{
// Reject assembled payloads that exceed max_frame_size.
CAF_LOG_DEBUG
(
"fragmented WebSocket payload exceeds maximum size"
);
abort_and_shutdown
(
sec
::
protocol_error
,
"fragmented WebSocket payload "
"exceeds maximum size"
);
return
-
1
;
}
payload_buf_
.
insert
(
payload_buf_
.
end
(),
payload
.
begin
(),
payload
.
end
());
}
return
static_cast
<
ptrdiff_t
>
(
frame_size
);
}
...
...
@@ -186,27 +191,32 @@ bool framing::end_text_message() {
// -- implementation details ---------------------------------------------------
bool
framing
::
handle
(
uint8_t
opcode
,
byte_span
payload
)
{
// Code rfc6455::connection_close must be treated separately.
CAF_ASSERT
(
opcode
!=
detail
::
rfc6455
::
connection_close
);
ptrdiff_t
framing
::
handle
(
uint8_t
opcode
,
byte_span
payload
,
size_t
frame_size
)
{
// opcodes are checked for validity when decoding the header
switch
(
opcode
)
{
case
detail
:
:
rfc6455
::
connection_close
:
abort_and_shutdown
(
sec
::
connection_closed
);
return
-
1
;
case
detail
:
:
rfc6455
::
text_frame
:
{
std
::
string_view
text
{
reinterpret_cast
<
const
char
*>
(
payload
.
data
()),
payload
.
size
()};
return
up_
->
consume_text
(
text
)
>=
0
;
if
(
up_
->
consume_text
(
text
)
<
0
)
return
-
1
;
break
;
}
case
detail
:
:
rfc6455
::
binary_frame
:
return
up_
->
consume_binary
(
payload
)
>=
0
;
if
(
up_
->
consume_binary
(
payload
)
<
0
)
return
-
1
;
break
;
case
detail
:
:
rfc6455
::
ping
:
ship_pong
(
payload
);
return
true
;
case
detail
:
:
rfc6455
::
pong
:
break
;
default:
// detail::rfc6455::pong
// nop
return
true
;
default:
// error
return
false
;
break
;
}
return
static_cast
<
ptrdiff_t
>
(
frame_size
);
}
void
framing
::
ship_pong
(
byte_span
payload
)
{
...
...
libcaf_net/test/detail/rfc6455.cpp
View file @
9498fc8a
...
...
@@ -60,6 +60,16 @@ TEST_CASE("masking") {
CHECK_EQ
(
masked_data
,
data
);
}
TEST_CASE
(
"decoding a frame with RSV bits fails"
)
{
std
::
vector
<
uint8_t
>
data
;
byte_buffer
out
=
bytes
({
0xF2
,
// FIN + RSV + binary frame opcode
0x00
,
// data size = 0
});
impl
::
header
hdr
;
CHECK_EQ
(
impl
::
decode_header
(
out
,
hdr
),
-
1
);
}
TEST_CASE
(
"no mask key and no data"
)
{
std
::
vector
<
uint8_t
>
data
;
byte_buffer
out
;
...
...
libcaf_net/test/net-test.cpp
View file @
9498fc8a
...
...
@@ -160,7 +160,6 @@ mock_web_socket_app::accept(const caf::net::http::request_header& hdr) {
void
mock_web_socket_app
::
abort
(
const
caf
::
error
&
reason
)
{
abort_reason
=
reason
;
down
->
shutdown
(
reason
);
}
ptrdiff_t
mock_web_socket_app
::
consume_text
(
std
::
string_view
text
)
{
...
...
libcaf_net/test/net/web_socket/framing.cpp
View file @
9498fc8a
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment