Skip to content

Commit

Permalink
[coro_http][feat]support available (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 24, 2024
1 parent 0aea327 commit da88211
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions include/ylt/standalone/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
max_http_body_len_ = max_size;
}

size_t available() {
std::error_code ec{};
return socket_->impl_.available(ec);
}

async_simple::coro::Lazy<resp_data> read_websocket() {
auto time_out_guard =
timer_guard(this, req_timeout_duration_, "websocket timer");
Expand Down
5 changes: 5 additions & 0 deletions include/ylt/standalone/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ class coro_http_connection
return remote_addr_;
}

size_t available() {
std::error_code ec{};
return socket_.available(ec);
}

void set_multi_buf(bool r) { multi_buf_ = r; }

void set_default_handler(
Expand Down
19 changes: 19 additions & 0 deletions src/coro_http/tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,11 @@ TEST_CASE("test pipeline") {
res.set_status_and_content(status_type::ok, "hello coro");
co_return;
});
server.set_http_handler<GET, POST>(
"/test_available", [](coro_http_request &req, coro_http_response &res) {
std::string str(1400, 'a');
res.set_status_and_content(status_type::ok, std::move(str));
});
server.async_start();

{
Expand Down Expand Up @@ -882,6 +887,20 @@ TEST_CASE("test pipeline") {
result.resp_body.size(), 0);
CHECK(parser.status() != 200);
}

{
coro_http_client client{};
std::string uri = "http://127.0.0.1:9001";
async_simple::coro::syncAwait(client.connect(uri));
auto ec = async_simple::coro::syncAwait(client.async_write_raw(
"GET /test_available HTTP/1.1\r\nHost: 127.0.0.1:8090\r\n\r\n"));
CHECK(!ec);

auto result =
async_simple::coro::syncAwait(client.async_read_raw(http_method::GET));
auto sz = client.available();
CHECK(sz > 0);
}
}
#endif

Expand Down
14 changes: 10 additions & 4 deletions src/coro_http/tests/test_cinatra_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,24 @@ TEST_CASE("test send after server stop") {

TEST_CASE("test read write in different threads") {
cinatra::coro_http_server server(1, 8090);
size_t count = 0;
std::promise<void> promise;
server.set_http_handler<cinatra::GET>(
"/",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
[&](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;
}

count++;
if (count == 100) {
promise.set_value();
break;
}
auto ec = co_await req.get_conn()->write_websocket(result.data);
if (ec) {
break;
Expand Down Expand Up @@ -326,7 +332,7 @@ TEST_CASE("test read write in different threads") {

async_simple::coro::syncAwait(lazy());

std::this_thread::sleep_for(std::chrono::milliseconds(300));
promise.get_future().wait_for(std::chrono::seconds(2));

server.stop();
}
Expand Down
4 changes: 3 additions & 1 deletion src/coro_http/tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,13 +751,15 @@ TEST_CASE("chunked request") {

while (true) {
result = co_await req.get_conn()->read_chunked();
auto size = req.get_conn()->available();
if (result.ec) {
co_return;
}
if (result.eof) {
CHECK(size == 0);
break;
}

CHECK(size >= 0);
content.append(result.data);
}

Expand Down

0 comments on commit da88211

Please sign in to comment.