Skip to content

Commit

Permalink
fix httpclient bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hggq committed Oct 12, 2024
1 parent fdb7641 commit 1c69187
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
3 changes: 3 additions & 0 deletions common/autocontrolmethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ namespace http
temp.regfun = testhttpclient_cowait_spawn;
methodcallback.emplace("testcowaitclient3",temp);
temp.pre = nullptr;
temp.regfun = testhttpclient_get_range;
methodcallback.emplace("testclientgetrange",temp);
temp.pre = nullptr;
temp.regfun = testcors;
methodcallback.emplace("api/user/message",temp);
temp.pre = nullptr;
Expand Down
1 change: 1 addition & 0 deletions controller/include/testcowaitclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ namespace http
std::string testhttpclient_cowait_post(std::shared_ptr<httppeer> peer);
std::string testhttpclient_cowait_urls(std::shared_ptr<httppeer> peer);
std::string testhttpclient_cowait_spawn(std::shared_ptr<httppeer> peer);
std::string testhttpclient_get_range(std::shared_ptr<httppeer> peer);
}
43 changes: 43 additions & 0 deletions controller/src/testcowaitclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,47 @@ std::string testhttpclient_cowait_spawn(std::shared_ptr<httppeer> peer)
}
return "";
}

//@urlpath(null,testclientgetrange)
std::string testhttpclient_get_range(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
client << "hello world! test testhttpclient_cowait_body";

client_context &client_context = get_client_context_obj();
std::vector<std::string> urls = {"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz",
"https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-arm64-apple-macos11.tar.xz"};

for (unsigned int i = 0; i < urls.size(); i++)
{
std::shared_ptr<http::client> a = std::make_shared<http::client>();
a->get(urls[i]);
a->addheader("Range", "bytes=100-13919154");
client << "<p>" << i << "</p>";
a->onload = [](const std::string &respbody, std::shared_ptr<http::client> a) -> void
{
std::cout << a->host << std::endl;
};
// co_spawn(
// client_context.ioc,
// [](std::shared_ptr<http::client> a) -> asio::awaitable<void>
// {
// co_await a->co_send();
// }(a),
// asio::detached);
client_context.add_http_task(a);
}
std::this_thread::sleep_for(std::chrono::seconds(10));
return "";
}
}// namespace http
20 changes: 13 additions & 7 deletions vendor/httpserver/src/httpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ asio::awaitable<bool> client::co_init_http_sock()
asio::ip::tcp::resolver::iterator iter = co_await resolver.async_resolve(host, port, asio::use_awaitable);
asio::ip::tcp::resolver::iterator end;
asio::ip::tcp::endpoint endpoint;

sock = std::make_shared<asio::ip::tcp::socket>(executor);
asio::error_code ec;
constexpr auto tuple_awaitable = asio::as_tuple(asio::use_awaitable);
Expand All @@ -320,12 +321,12 @@ asio::awaitable<bool> client::co_init_http_sock()
}
}

if (iter == end)
{
error_msg = "resolver " + host + port;
DEBUG_LOG("%s", error_msg.c_str());
co_return false;
}
// if (iter == end)
// {
// error_msg = "resolver " + host + port;
// DEBUG_LOG("%s", error_msg.c_str());
// co_return false;
// }

if (ec)
{
Expand Down Expand Up @@ -408,9 +409,14 @@ bool client::init_http_sock()
{
endpoint = *iter++;
// at here maybe under code
sock->connect(endpoint, ec);
if (ec)
{
continue;
}
}
// asio::error_code ec;
sock->connect(endpoint, ec);

if (ec)
{
//error_msg = "Unable to connect";
Expand Down

0 comments on commit 1c69187

Please sign in to comment.