Skip to content

Commit

Permalink
add available (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 19, 2024
1 parent 4ddbbb7 commit cae4ab2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/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/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 tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,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 @@ -891,6 +896,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
4 changes: 3 additions & 1 deletion 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 cae4ab2

Please sign in to comment.