Skip to content

Commit

Permalink
add get_host, get_port (qicosmos#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Aug 15, 2023
1 parent 7fb8dfc commit ab4efd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 13 additions & 7 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ class coro_http_client {
co_return co_await connect(std::move(uri));
}

std::string_view get_host() { return host_; }

std::string_view get_port() { return port_; }

template <typename S, typename String>
async_simple::coro::Lazy<resp_data> async_upload_chunked(
S uri, http_method method, String filename,
Expand Down Expand Up @@ -800,10 +804,10 @@ class coro_http_client {
}
if (socket_->has_closed_) {
auto conn_future = start_timer(conn_timeout_duration_, "connect timer");
std::string host = proxy_host_.empty() ? u.get_host() : proxy_host_;
std::string port = proxy_port_.empty() ? u.get_port() : proxy_port_;
host_ = proxy_host_.empty() ? u.get_host() : proxy_host_;
port_ = proxy_port_.empty() ? u.get_port() : proxy_port_;
if (ec = co_await coro_io::async_connect(&executor_wrapper_,
socket_->impl_, host, port);
socket_->impl_, host_, port_);
ec) {
break;
}
Expand Down Expand Up @@ -1308,10 +1312,10 @@ class coro_http_client {

async_simple::coro::Lazy<resp_data> connect(const uri_t &u) {
if (socket_->has_closed_) {
std::string host = proxy_host_.empty() ? u.get_host() : proxy_host_;
std::string port = proxy_port_.empty() ? u.get_port() : proxy_port_;
if (auto ec = co_await coro_io::async_connect(&executor_wrapper_,
socket_->impl_, host, port);
host_ = proxy_host_.empty() ? u.get_host() : proxy_host_;
port_ = proxy_port_.empty() ? u.get_port() : proxy_port_;
if (auto ec = co_await coro_io::async_connect(
&executor_wrapper_, socket_->impl_, host_, port_);
ec) {
co_return resp_data{ec, 404};
}
Expand Down Expand Up @@ -1612,6 +1616,8 @@ class coro_http_client {
std::function<void(resp_data)> on_ws_msg_;
std::function<void(std::string_view)> on_ws_close_;
std::string ws_sec_key_;
std::string host_;
std::string port_;

#ifdef CINATRA_ENABLE_SSL
std::unique_ptr<asio::ssl::context> ssl_ctx_ = nullptr;
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ TEST_CASE("test coro_http_client connect/request timeout") {
auto r =
async_simple::coro::syncAwait(client.async_get("http://www.baidu.com"));
std::cout << r.net_err.value() << ", " << r.net_err.message() << "\n";
bool ret = (r.net_err == std::errc::timed_out ||
r.net_err == asio::error::operation_aborted);
CHECK(ret);
CHECK(r.net_err != std::errc{});
}

{
Expand All @@ -184,7 +182,7 @@ TEST_CASE("test coro_http_client connect/request timeout") {
auto r =
async_simple::coro::syncAwait(client.async_get("http://www.baidu.com"));
std::cout << r.net_err.message() << "\n";
CHECK(r.net_err == std::errc::timed_out);
CHECK(r.net_err != std::errc{});
}
}

Expand All @@ -205,6 +203,8 @@ TEST_CASE("test coro_http_client async_http_connect") {
CHECK(r.status != 200);

r = async_simple::coro::syncAwait(client1.reconnect("http://cn.bing.com"));
CHECK(client1.get_host() == "cn.bing.com");
CHECK(client1.get_port() == "http");
CHECK(r.status >= 200);

r = async_simple::coro::syncAwait(client1.reconnect("http://www.baidu.com"));
Expand Down

0 comments on commit ab4efd9

Please sign in to comment.