Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix, improve and add ut #436

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/linux_llvm_cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ jobs:
sudo apt-get install libssl-dev
sudo apt-get install llvm

- name: Install newer Clang
run: |
wget https://apt.llvm.org/llvm.sh
chmod +x ./llvm.sh
sudo ./llvm.sh 17

- name: Run Coverage
run: |
mkdir build && cd build
CC=clang CXX=clang++ cmake .. -DCOVERAGE_TEST=ON -DCINATRA_ENABLE_SSL=ON
CC=clang-17 CXX=clang++-17 cmake .. -DCOVERAGE_TEST=ON -DCINATRA_ENABLE_SSL=ON
make -j test_cinatra test_corofile
export LLVM_PROFILE_FILE="test_cinatra-%m.profraw"
cd tests
Expand Down
2 changes: 1 addition & 1 deletion include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class coro_http_connection

while (true) {
const char *data_ptr = asio::buffer_cast<const char *>(head_buf_.data());
auto status = ws_.parse_header(data_ptr, SHORT_HEADER);
auto status = ws_.parse_header(data_ptr, ws_.len_bytes());
if (status == ws_header_status::complete) {
head_buf_.consume(head_buf_.size());

Expand Down
7 changes: 7 additions & 0 deletions include/cinatra/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,27 @@ class websocket {

left_header_len_ = 0;
if (length_field <= 125) {
len_bytes_ = SHORT_HEADER;
payload_length_ = length_field;
}
else if (length_field == 126) // msglen is 16bit!
{
len_bytes_ = MEDIUM_HEADER;
payload_length_ = ntohs(*(uint16_t *)&inp[2]); // (inp[2] << 8) + inp[3];
pos += 2;
left_header_len_ =
is_server ? MEDIUM_HEADER - size : CLIENT_MEDIUM_HEADER - size;
}
else if (length_field == 127) // msglen is 64bit!
{
len_bytes_ = LONG_HEADER;
payload_length_ = (size_t)be64toh(*(uint64_t *)&inp[2]);
pos += 8;
left_header_len_ =
is_server ? LONG_HEADER - size : CLIENT_LONG_HEADER - size;
}
else {
len_bytes_ = INVALID_HEADER;
return ws_header_status::error;
}

Expand All @@ -86,6 +90,8 @@ class websocket {
: ws_header_status::incomplete;
}

int len_bytes() const { return len_bytes_; }

ws_frame_type parse_payload(std::span<char> buf) {
// unmask data:
if (*(uint32_t *)mask_ != 0) {
Expand Down Expand Up @@ -302,6 +308,7 @@ class websocket {
unsigned char msg_fin_ = 0;

char msg_header_[10];
ws_head_len len_bytes_ = SHORT_HEADER;
};

} // namespace cinatra
148 changes: 140 additions & 8 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,29 @@ TEST_CASE("test websocket") {
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
CHECK(req.get_content_type() == content_type::websocket);
std::ofstream out_file("test.temp", std::ios::binary);
websocket_result result{};

while (true) {
result = co_await req.get_conn()->read_websocket();
if (result.ec) {
out_file.close();
break;
}

if (result.type == ws_frame_type::WS_CLOSE_FRAME) {
std::cout << "close frame\n";
CHECK(result.data.empty());
out_file.close();
break;
}

if (result.type == ws_frame_type::WS_TEXT_FRAME) {
CHECK(result.data == "test_ws");
CHECK(!result.data.empty());
std::cout << result.data << "\n";
}

if (result.type == ws_frame_type::WS_BINARY_FRAME) {
out_file << result.data;
}

auto ec = co_await req.get_conn()->write_websocket(result.data);
Expand All @@ -407,20 +415,143 @@ TEST_CASE("test websocket") {
}

std::cout << "ws msg len: " << data.resp_body.size() << std::endl;
CHECK(data.resp_body == "test_ws");
CHECK(!data.resp_body.empty());
});

async_simple::coro::syncAwait(
client->async_ws_connect("ws://127.0.0.1:9001/ws_echo"));
async_simple::coro::syncAwait(
client->async_send_ws("test2fdsaf", true, opcode::binary));
async_simple::coro::syncAwait(client->async_send_ws("test_ws"));

async_simple::coro::syncAwait(client->async_send_ws_close());
}

TEST_CASE("check small ws file") {
std::string filename = "test.temp";
std::error_code ec;
size_t file_size = std::filesystem::file_size(filename, ec);
if (ec) {
return;
}
std::ifstream file(filename, std::ios::binary);
if (!file) {
return;
}
std::string str;
str.resize(file_size);

file.read(str.data(), str.size());
CHECK(str == "test2fdsaf");
std::filesystem::remove(filename, ec);
}

TEST_CASE("test websocket binary data") {
cinatra::coro_http_server server(1, 9001);
server.set_http_handler<cinatra::GET>(
"/short_binary",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
CHECK(req.get_content_type() == content_type::websocket);
websocket_result result{};
while (true) {
result = co_await req.get_conn()->read_websocket();
if (result.ec) {
break;
}

if (result.type == ws_frame_type::WS_CLOSE_FRAME) {
std::cout << "close frame\n";
CHECK(result.data.empty());
break;
}

if (result.type == ws_frame_type::WS_BINARY_FRAME) {
CHECK(result.data.size() == 127);
}
}
});
server.set_http_handler<cinatra::GET>(
"/medium_binary",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
CHECK(req.get_content_type() == content_type::websocket);
websocket_result result{};
while (true) {
result = co_await req.get_conn()->read_websocket();
if (result.ec) {
break;
}

if (result.type == ws_frame_type::WS_CLOSE_FRAME) {
std::cout << "close frame\n";
CHECK(result.data.empty());
break;
}

if (result.type == ws_frame_type::WS_BINARY_FRAME) {
CHECK(result.data.size() == 65535);
}
}
});
server.set_http_handler<cinatra::GET>(
"/long_binary",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
CHECK(req.get_content_type() == content_type::websocket);
websocket_result result{};
while (true) {
result = co_await req.get_conn()->read_websocket();
if (result.ec) {
break;
}

if (result.type == ws_frame_type::WS_CLOSE_FRAME) {
std::cout << "close frame\n";
CHECK(result.data.empty());
break;
}

if (result.type == ws_frame_type::WS_BINARY_FRAME) {
CHECK(result.data.size() == 65536);
}
}
});
server.async_start();

auto client1 = std::make_shared<coro_http_client>();
async_simple::coro::syncAwait(
client1->async_ws_connect("ws://127.0.0.1:9001/short_binary"));

std::string short_str(127, 'A');
async_simple::coro::syncAwait(
client1->async_send_ws(std::move(short_str), true, opcode::binary));

auto client2 = std::make_shared<coro_http_client>();
async_simple::coro::syncAwait(
client2->async_ws_connect("ws://127.0.0.1:9001/medium_binary"));

std::string medium_str(65535, 'A');
async_simple::coro::syncAwait(
client2->async_send_ws(std::move(medium_str), true, opcode::binary));

auto client3 = std::make_shared<coro_http_client>();
async_simple::coro::syncAwait(
client3->async_ws_connect("ws://127.0.0.1:9001/long_binary"));

std::string long_str(65536, 'A');
async_simple::coro::syncAwait(
client3->async_send_ws(std::move(long_str), true, opcode::binary));

async_simple::coro::syncAwait(client1->async_send_ws_close());
async_simple::coro::syncAwait(client2->async_send_ws_close());
async_simple::coro::syncAwait(client3->async_send_ws_close());
}

TEST_CASE("test websocket with different message sizes") {
cinatra::coro_http_server server(1, 9001);
server.set_http_handler<cinatra::GET>(
"/ws_echo",
"/ws_echo1",
[](cinatra::coro_http_request &req,
cinatra::coro_http_response &resp) -> async_simple::coro::Lazy<void> {
REQUIRE(req.get_content_type() == cinatra::content_type::websocket);
Expand Down Expand Up @@ -451,18 +582,19 @@ TEST_CASE("test websocket with different message sizes") {
std::string medium_message(
65535, 'x'); // 65,535 'x' characters for the medium message test.

client->on_ws_msg([&medium_message](cinatra::resp_data data) {
client->on_ws_msg([medium_message](cinatra::resp_data data) {
if (data.net_err) {
std::cout << "ws_msg net error " << data.net_err.message() << "\n";
return;
}

size_t size = data.resp_body.size();
std::cout << "ws msg len: " << data.resp_body.size() << std::endl;
REQUIRE(data.resp_body == medium_message);
});

async_simple::coro::syncAwait(
client->async_ws_connect("ws://127.0.0.1:9001/ws_echo"));
client->async_ws_connect("ws://127.0.0.1:9001/ws_echo1"));
async_simple::coro::syncAwait(client->async_send_ws(medium_message));
async_simple::coro::syncAwait(client->async_send_ws_close());
}
Expand All @@ -471,7 +603,7 @@ TEST_CASE("test websocket with different message sizes") {
std::string large_message(
70000, 'x'); // 70,000 'x' characters for the large message test.

client->on_ws_msg([&large_message](cinatra::resp_data data) {
client->on_ws_msg([large_message](cinatra::resp_data data) {
if (data.net_err) {
std::cout << "ws_msg net error " << data.net_err.message() << "\n";
return;
Expand All @@ -482,7 +614,7 @@ TEST_CASE("test websocket with different message sizes") {
});

async_simple::coro::syncAwait(
client->async_ws_connect("ws://127.0.0.1:9001/ws_echo"));
client->async_ws_connect("ws://127.0.0.1:9001/ws_echo1"));
async_simple::coro::syncAwait(client->async_send_ws(large_message));
async_simple::coro::syncAwait(client->async_send_ws_close());
}
Expand Down
Loading