From 1c691874aed630b751a239bc8562bb326fac8fce Mon Sep 17 00:00:00 2001 From: hzq Date: Sat, 12 Oct 2024 09:01:57 +0800 Subject: [PATCH] fix httpclient bug --- common/autocontrolmethod.hpp | 3 ++ controller/include/testcowaitclient.h | 1 + controller/src/testcowaitclient.cpp | 43 +++++++++++++++++++++++++++ vendor/httpserver/src/httpclient.cpp | 20 ++++++++----- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/common/autocontrolmethod.hpp b/common/autocontrolmethod.hpp index 76a093dc..2f0e57ab 100755 --- a/common/autocontrolmethod.hpp +++ b/common/autocontrolmethod.hpp @@ -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; diff --git a/controller/include/testcowaitclient.h b/controller/include/testcowaitclient.h index d0b72d53..861fc1ca 100755 --- a/controller/include/testcowaitclient.h +++ b/controller/include/testcowaitclient.h @@ -13,4 +13,5 @@ namespace http std::string testhttpclient_cowait_post(std::shared_ptr peer); std::string testhttpclient_cowait_urls(std::shared_ptr peer); std::string testhttpclient_cowait_spawn(std::shared_ptr peer); + std::string testhttpclient_get_range(std::shared_ptr peer); } diff --git a/controller/src/testcowaitclient.cpp b/controller/src/testcowaitclient.cpp index 743083f3..0eaca641 100755 --- a/controller/src/testcowaitclient.cpp +++ b/controller/src/testcowaitclient.cpp @@ -169,4 +169,47 @@ std::string testhttpclient_cowait_spawn(std::shared_ptr peer) } return ""; } + +//@urlpath(null,testclientgetrange) +std::string testhttpclient_get_range(std::shared_ptr peer) +{ + httppeer &client = peer->get_peer(); + client << "hello world! test testhttpclient_cowait_body"; + + client_context &client_context = get_client_context_obj(); + std::vector 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 a = std::make_shared(); + a->get(urls[i]); + a->addheader("Range", "bytes=100-13919154"); + client << "

" << i << "

"; + a->onload = [](const std::string &respbody, std::shared_ptr a) -> void + { + std::cout << a->host << std::endl; + }; + // co_spawn( + // client_context.ioc, + // [](std::shared_ptr a) -> asio::awaitable + // { + // 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 \ No newline at end of file diff --git a/vendor/httpserver/src/httpclient.cpp b/vendor/httpserver/src/httpclient.cpp index ff496b58..f2a94fbe 100755 --- a/vendor/httpserver/src/httpclient.cpp +++ b/vendor/httpserver/src/httpclient.cpp @@ -303,6 +303,7 @@ asio::awaitable 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(executor); asio::error_code ec; constexpr auto tuple_awaitable = asio::as_tuple(asio::use_awaitable); @@ -320,12 +321,12 @@ asio::awaitable 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) { @@ -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";