Skip to content

Commit da88211

Browse files
authored
[coro_http][feat]support available (#860)
1 parent 0aea327 commit da88211

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

include/ylt/standalone/cinatra/coro_http_client.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
369369
max_http_body_len_ = max_size;
370370
}
371371

372+
size_t available() {
373+
std::error_code ec{};
374+
return socket_->impl_.available(ec);
375+
}
376+
372377
async_simple::coro::Lazy<resp_data> read_websocket() {
373378
auto time_out_guard =
374379
timer_guard(this, req_timeout_duration_, "websocket timer");

include/ylt/standalone/cinatra/coro_http_connection.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ class coro_http_connection
445445
return remote_addr_;
446446
}
447447

448+
size_t available() {
449+
std::error_code ec{};
450+
return socket_.available(ec);
451+
}
452+
448453
void set_multi_buf(bool r) { multi_buf_ = r; }
449454

450455
void set_default_handler(

src/coro_http/tests/test_cinatra.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,11 @@ TEST_CASE("test pipeline") {
759759
res.set_status_and_content(status_type::ok, "hello coro");
760760
co_return;
761761
});
762+
server.set_http_handler<GET, POST>(
763+
"/test_available", [](coro_http_request &req, coro_http_response &res) {
764+
std::string str(1400, 'a');
765+
res.set_status_and_content(status_type::ok, std::move(str));
766+
});
762767
server.async_start();
763768

764769
{
@@ -882,6 +887,20 @@ TEST_CASE("test pipeline") {
882887
result.resp_body.size(), 0);
883888
CHECK(parser.status() != 200);
884889
}
890+
891+
{
892+
coro_http_client client{};
893+
std::string uri = "http://127.0.0.1:9001";
894+
async_simple::coro::syncAwait(client.connect(uri));
895+
auto ec = async_simple::coro::syncAwait(client.async_write_raw(
896+
"GET /test_available HTTP/1.1\r\nHost: 127.0.0.1:8090\r\n\r\n"));
897+
CHECK(!ec);
898+
899+
auto result =
900+
async_simple::coro::syncAwait(client.async_read_raw(http_method::GET));
901+
auto sz = client.available();
902+
CHECK(sz > 0);
903+
}
885904
}
886905
#endif
887906

src/coro_http/tests/test_cinatra_websocket.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,24 @@ TEST_CASE("test send after server stop") {
277277

278278
TEST_CASE("test read write in different threads") {
279279
cinatra::coro_http_server server(1, 8090);
280+
size_t count = 0;
281+
std::promise<void> promise;
280282
server.set_http_handler<cinatra::GET>(
281283
"/",
282-
[](coro_http_request &req,
283-
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
284+
[&](coro_http_request &req,
285+
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
284286
CHECK(req.get_content_type() == content_type::websocket);
285287
websocket_result result{};
286288
while (true) {
287289
result = co_await req.get_conn()->read_websocket();
288290
if (result.ec) {
289291
break;
290292
}
291-
293+
count++;
294+
if (count == 100) {
295+
promise.set_value();
296+
break;
297+
}
292298
auto ec = co_await req.get_conn()->write_websocket(result.data);
293299
if (ec) {
294300
break;
@@ -326,7 +332,7 @@ TEST_CASE("test read write in different threads") {
326332

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

329-
std::this_thread::sleep_for(std::chrono::milliseconds(300));
335+
promise.get_future().wait_for(std::chrono::seconds(2));
330336

331337
server.stop();
332338
}

src/coro_http/tests/test_coro_http_server.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,15 @@ TEST_CASE("chunked request") {
751751

752752
while (true) {
753753
result = co_await req.get_conn()->read_chunked();
754+
auto size = req.get_conn()->available();
754755
if (result.ec) {
755756
co_return;
756757
}
757758
if (result.eof) {
759+
CHECK(size == 0);
758760
break;
759761
}
760-
762+
CHECK(size >= 0);
761763
content.append(result.data);
762764
}
763765

0 commit comments

Comments
 (0)