Skip to content

Commit 0aea327

Browse files
authored
[coro_http_client]fix async upload (#870)
1 parent fac0f5e commit 0aea327

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

include/ylt/metric/metric_manager.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,11 @@ class dynamic_metric_manager {
456456
private:
457457
void clean_label_expired() {
458458
executor_ = coro_io::create_io_context_pool(1);
459+
auto sp = executor_;
459460
timer_ = std::make_shared<coro_io::period_timer>(executor_->get_executor());
460461
check_label_expired(timer_)
461462
.via(executor_->get_executor())
462-
.start([](auto&&) {
463+
.start([sp](auto&&) {
463464
});
464465
}
465466

include/ylt/standalone/cinatra/coro_http_client.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
11011101
req_context<> ctx{content_type};
11021102
resp_data data{};
11031103

1104+
out_buf_ = {};
1105+
11041106
std::shared_ptr<void> guard(nullptr, [&, this](auto) {
11051107
if (!req_headers_.empty()) {
11061108
req_headers_.clear();

src/coro_http/tests/test_cinatra.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,85 @@ TEST_CASE("test pipeline") {
885885
}
886886
#endif
887887

888+
enum class upload_type { send_file, chunked, multipart };
889+
890+
TEST_CASE("test out buffer and async upload ") {
891+
coro_http_server server(1, 9000);
892+
server.set_http_handler<GET, POST>(
893+
"/write_chunked",
894+
[](coro_http_request &req,
895+
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
896+
resp.set_format_type(format_type::chunked);
897+
bool ok;
898+
if (ok = co_await resp.get_conn()->begin_chunked(); !ok) {
899+
co_return;
900+
}
901+
902+
std::vector<std::string> vec{"hello", " world", " ok"};
903+
904+
for (auto &str : vec) {
905+
if (ok = co_await resp.get_conn()->write_chunked(str); !ok) {
906+
co_return;
907+
}
908+
}
909+
910+
ok = co_await resp.get_conn()->end_chunked();
911+
});
912+
server.set_http_handler<GET, POST>(
913+
"/normal", [](coro_http_request &req, coro_http_response &resp) {
914+
resp.set_status_and_content(status_type::ok, "test");
915+
});
916+
server.set_http_handler<GET, POST>(
917+
"/more", [](coro_http_request &req, coro_http_response &resp) {
918+
resp.set_status_and_content(status_type::ok, "test more");
919+
});
920+
921+
server.async_start();
922+
923+
auto lazy = [](upload_type flag) -> async_simple::coro::Lazy<void> {
924+
coro_http_client client{};
925+
std::string uri = "http://127.0.0.1:9000/normal";
926+
std::vector<char> oubuf;
927+
oubuf.resize(10);
928+
req_context<> ctx{};
929+
auto result = co_await client.async_request(uri, http_method::GET,
930+
std::move(ctx), {}, oubuf);
931+
std::cout << oubuf.data() << "\n";
932+
933+
std::string_view out_view(oubuf.data(), result.resp_body.size());
934+
assert(out_view == "test");
935+
assert(out_view == result.resp_body);
936+
937+
auto ss = std::make_shared<std::stringstream>();
938+
*ss << "hello world";
939+
940+
if (flag == upload_type::send_file) {
941+
result = co_await client.async_upload("http://127.0.0.1:9000/more"sv,
942+
http_method::POST, ss);
943+
}
944+
else if (flag == upload_type::chunked) {
945+
result = co_await client.async_upload_chunked(
946+
"http://127.0.0.1:9000/more"sv, http_method::POST, ss);
947+
}
948+
else if (flag == upload_type::multipart) {
949+
client.add_str_part("test_key", "test_value");
950+
result =
951+
co_await client.async_upload_multipart("http://127.0.0.1:9000/more");
952+
}
953+
954+
std::cout << (int)flag << oubuf.data() << "\n";
955+
std::cout << result.resp_body << "\n";
956+
957+
std::string_view out_view1(oubuf.data(), out_view.size());
958+
assert(out_view == out_view1);
959+
assert(result.resp_body != out_view1);
960+
};
961+
962+
async_simple::coro::syncAwait(lazy(upload_type::send_file));
963+
async_simple::coro::syncAwait(lazy(upload_type::chunked));
964+
async_simple::coro::syncAwait(lazy(upload_type::multipart));
965+
}
966+
888967
TEST_CASE("test multipart and chunked return error") {
889968
coro_http_server server(1, 8090);
890969
server.set_http_handler<cinatra::PUT, cinatra::POST>(

0 commit comments

Comments
 (0)