Skip to content

Commit

Permalink
update (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Apr 17, 2024
1 parent f762d3a commit 58ec899
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ async_simple::coro::Lazy<void> test_websocket() {
std::cout << data.resp_body << std::endl;
});
bool r = co_await client.async_ws_connect("ws://localhost:8090/ws");
if (!r) {
auto r = co_await client.connect("ws://localhost:8090/ws");
if (r.net_err) {
co_return;
}
Expand Down
4 changes: 2 additions & 2 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ async_simple::coro::Lazy<void> use_websocket() {
std::this_thread::sleep_for(300ms); // wait for server start

coro_http_client client{};
bool r = co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo");
if (!r) {
auto r = co_await client.connect("ws://127.0.0.1:9001/ws_echo");
if (r.net_err) {
co_return;
}

Expand Down
41 changes: 15 additions & 26 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
{
auto time_out_guard =
timer_guard(this, conn_timeout_duration_, "connect timer");
if (u.is_websocket()) {
// build websocket http header
add_header("Upgrade", "websocket");
add_header("Connection", "Upgrade");
if (ws_sec_key_.empty()) {
ws_sec_key_ = "s//GYHa/XO7Hd2F2eOGfyA=="; // provide a random string.
}
add_header("Sec-WebSocket-Key", ws_sec_key_);
add_header("Sec-WebSocket-Version", "13");

req_context<> ctx{};
data = co_await async_request(std::move(uri), http_method::GET,
std::move(ctx));
co_return data;
}
data = co_await connect(u);
}
if (socket_->is_timeout_) {
Expand Down Expand Up @@ -319,31 +334,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

void set_ws_sec_key(std::string sec_key) { ws_sec_key_ = std::move(sec_key); }

async_simple::coro::Lazy<bool> async_ws_connect(std::string uri) {
resp_data data{};
auto [r, u] = handle_uri(data, uri);
if (!r) {
CINATRA_LOG_WARNING << "url error:";
co_return false;
}

req_context<> ctx{};
if (u.is_websocket()) {
// build websocket http header
add_header("Upgrade", "websocket");
add_header("Connection", "Upgrade");
if (ws_sec_key_.empty()) {
ws_sec_key_ = "s//GYHa/XO7Hd2F2eOGfyA=="; // provide a random string.
}
add_header("Sec-WebSocket-Key", ws_sec_key_);
add_header("Sec-WebSocket-Version", "13");
}

data = co_await async_request(std::move(uri), http_method::GET,
std::move(ctx));
co_return !data.net_err;
}

async_simple::coro::Lazy<resp_data> read_websocket() {
co_return co_await async_read_ws();
}
Expand Down Expand Up @@ -1779,7 +1769,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
co_return resp_data{{}, 200};
}

// this function must be called before async_ws_connect.
async_simple::coro::Lazy<resp_data> async_read_ws() {
resp_data data{};

Expand Down
4 changes: 2 additions & 2 deletions lang/coro_http_client_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ void on_ws_msg(std::function<void(resp_data)> on_ws_msg);
```
websocket 连接服务器接口:
```c++
async_simple::coro::Lazy<bool> async_ws_connect(std::string uri);
async_simple::coro::Lazy<resp_data> connect(std::string uri);
```
websocket 发送数据接口:
```c++
Expand Down Expand Up @@ -474,7 +474,7 @@ websocket 例子:
coro_http_client client;
// 连接websocket 服务器
async_simple::coro::syncAwait(
client.async_ws_connect("ws://localhost:8090"));
client.connect("ws://localhost:8090"));
std::string send_str(len, 'a');
// 发送websocket 数据
Expand Down
2 changes: 1 addition & 1 deletion lang/coroutine_based_http_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ client端:
cinatra::coro_http_client client{};
std::string message(100, 'x');
co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo");
co_await client.connect("ws://127.0.0.1:9001/ws_echo");
co_await client.write_websocket(std::string(message));
auto data = co_await client.read_websocket();
CHECK(data.resp_body == message);
Expand Down
4 changes: 2 additions & 2 deletions lang/english/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ async_simple::coro::Lazy<void> test_websocket() {
std::cout << data.resp_body << std::endl;
});

bool r = co_await client.async_ws_connect("ws://localhost:8090/ws");
if (!r) {
auto r = co_await client.connect("ws://localhost:8090/ws");
if (r.net_err) {
co_return;
}

Expand Down
18 changes: 8 additions & 10 deletions tests/test_cinatra_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ TEST_CASE("test wss client") {
"../../include/cinatra/server.crt");
REQUIRE_MESSAGE(ok == true, "init ssl fail, please check ssl config");

REQUIRE(async_simple::coro::syncAwait(
client.async_ws_connect("wss://localhost:9001")));
async_simple::coro::syncAwait(client.connect("wss://localhost:9001"));

async_simple::coro::syncAwait(client.write_websocket("hello"));
auto data = async_simple::coro::syncAwait(client.read_websocket());
Expand All @@ -57,8 +56,8 @@ TEST_CASE("test wss client") {
#endif

async_simple::coro::Lazy<void> test_websocket(coro_http_client &client) {
bool r = co_await client.async_ws_connect("ws://localhost:8090/ws");
if (!r) {
auto r = co_await client.connect("ws://localhost:8090/ws");
if (r.net_err) {
co_return;
}

Expand Down Expand Up @@ -133,7 +132,7 @@ void test_websocket_content(size_t len) {

auto lazy = [len]() -> async_simple::coro::Lazy<void> {
coro_http_client client{};
co_await client.async_ws_connect("ws://localhost:8090");
co_await client.connect("ws://localhost:8090");
std::string send_str(len, 'a');
co_await client.write_websocket(std::string(send_str));
auto data = co_await client.read_websocket();
Expand Down Expand Up @@ -170,8 +169,7 @@ TEST_CASE("test send after server stop") {
std::this_thread::sleep_for(std::chrono::milliseconds(300));

coro_http_client client{};
REQUIRE(async_simple::coro::syncAwait(
client.async_ws_connect("ws://127.0.0.1:8090")));
async_simple::coro::syncAwait(client.connect("ws://127.0.0.1:8090"));

server.stop();

Expand Down Expand Up @@ -222,7 +220,7 @@ TEST_CASE("test read write in different threads") {
});

auto lazy = [client, weak, &send_str]() -> async_simple::coro::Lazy<void> {
co_await client->async_ws_connect("ws://localhost:8090");
co_await client->connect("ws://localhost:8090");
for (int i = 0; i < 100; i++) {
auto data = co_await client->write_websocket(std::string(send_str));
if (data.net_err) {
Expand All @@ -240,8 +238,8 @@ TEST_CASE("test read write in different threads") {

async_simple::coro::Lazy<void> test_websocket() {
coro_http_client client{};
bool r = co_await client.async_ws_connect("ws://127.0.0.1:8089/ws_echo");
if (!r) {
auto r = co_await client.connect("ws://127.0.0.1:8089/ws_echo");
if (r.net_err) {
co_return;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ TEST_CASE("test websocket with chunked") {

coro_http_client client{};
async_simple::coro::syncAwait(
client.async_ws_connect("ws://127.0.0.1:9001/ws_source"));
client.connect("ws://127.0.0.1:9001/ws_source"));

std::string filename = "test.tmp";
create_file(filename);
Expand Down Expand Up @@ -911,7 +911,7 @@ TEST_CASE("test websocket") {

auto lazy = []() -> async_simple::coro::Lazy<void> {
coro_http_client client{};
co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo");
co_await client.connect("ws://127.0.0.1:9001/ws_echo");
co_await client.write_websocket("test2fdsaf", true, opcode::binary);
auto data = co_await client.read_websocket();
CHECK(data.resp_body == "test2fdsaf");
Expand Down Expand Up @@ -1027,23 +1027,23 @@ TEST_CASE("test websocket binary data") {

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

std::string short_str(127, 'A');
async_simple::coro::syncAwait(
client1->write_websocket(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"));
client2->connect("ws://127.0.0.1:9001/medium_binary"));

std::string medium_str(65535, 'A');
async_simple::coro::syncAwait(
client2->write_websocket(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"));
client3->connect("ws://127.0.0.1:9001/long_binary"));

std::string long_str(65536, 'A');
async_simple::coro::syncAwait(
Expand Down Expand Up @@ -1106,7 +1106,7 @@ TEST_CASE("test websocket with different message size") {

auto lazy = [](std::string &str) -> async_simple::coro::Lazy<void> {
coro_http_client client{};
co_await client.async_ws_connect("ws://127.0.0.1:9001/ws_echo1");
co_await client.connect("ws://127.0.0.1:9001/ws_echo1");
co_await client.write_websocket(str);
auto data = co_await client.read_websocket();
CHECK(data.resp_body.size() == str.size());
Expand Down

0 comments on commit 58ec899

Please sign in to comment.