From a5f96d8cd8b43294779550e8c9294ff64abb655f Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 19 Oct 2023 14:29:56 +0800 Subject: [PATCH] do some update; add a check for client --- include/ylt/coro_http/coro_http_server.hpp | 16 +++- .../thirdparty/cinatra/coro_http_client.hpp | 86 ++++++++++++------- 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/include/ylt/coro_http/coro_http_server.hpp b/include/ylt/coro_http/coro_http_server.hpp index 84373040f..1f5b6cdb3 100644 --- a/include/ylt/coro_http/coro_http_server.hpp +++ b/include/ylt/coro_http/coro_http_server.hpp @@ -32,6 +32,18 @@ using coro_http_request = cinatra::coro_http_request; using coro_http_response = cinatra::coro_http_response; using status_type = cinatra::status_type; using http_method = cinatra::http_method; -constexpr auto GET = cinatra::GET; -constexpr auto POST = cinatra::POST; +using uri_t = cinatra::uri_t; +using req_content_type = cinatra::req_content_type; + +constexpr inline auto GET = cinatra::http_method::GET; +constexpr inline auto POST = cinatra::http_method::POST; +constexpr inline auto DEL = cinatra::http_method::DEL; +constexpr inline auto HEAD = cinatra::http_method::HEAD; +constexpr inline auto PUT = cinatra::http_method::PUT; +constexpr inline auto CONNECT = cinatra::http_method::CONNECT; +#ifdef TRACE +#undef TRACE +constexpr inline auto TRACE = cinatra::http_method::TRACE; +#endif +constexpr inline auto OPTIONS = cinatra::http_method::OPTIONS; } // namespace coro_http \ No newline at end of file diff --git a/include/ylt/thirdparty/cinatra/coro_http_client.hpp b/include/ylt/thirdparty/cinatra/coro_http_client.hpp index cd643d0d9..a82649dde 100644 --- a/include/ylt/thirdparty/cinatra/coro_http_client.hpp +++ b/include/ylt/thirdparty/cinatra/coro_http_client.hpp @@ -175,7 +175,7 @@ class coro_http_client { ~coro_http_client() { async_close(); } void async_close() { - if (socket_->has_closed_) + if (socket_ == nullptr || socket_->has_closed_) return; asio::dispatch(executor_wrapper_.get_asio_executor(), [socket = socket_] { @@ -365,33 +365,45 @@ class coro_http_client { void set_read_fix() { read_fix_ = 1; } #endif - async_simple::coro::Lazy async_patch(std::string uri) { + async_simple::coro::Lazy async_patch( + std::string uri, + std::unordered_map headers = {}) { return async_request(std::move(uri), cinatra::http_method::PATCH, - cinatra::req_context<>{}); + cinatra::req_context<>{}, std::move(headers)); } - async_simple::coro::Lazy async_options(std::string uri) { + async_simple::coro::Lazy async_options( + std::string uri, + std::unordered_map headers = {}) { return async_request(std::move(uri), cinatra::http_method::OPTIONS, - cinatra::req_context<>{}); + cinatra::req_context<>{}, std::move(headers)); } - async_simple::coro::Lazy async_trace(std::string uri) { + async_simple::coro::Lazy async_trace( + std::string uri, + std::unordered_map headers = {}) { return async_request(std::move(uri), cinatra::http_method::TRACE, - cinatra::req_context<>{}); + cinatra::req_context<>{}, std::move(headers)); } - async_simple::coro::Lazy async_head(std::string uri) { + async_simple::coro::Lazy async_head( + std::string uri, + std::unordered_map headers = {}) { return async_request(std::move(uri), cinatra::http_method::HEAD, - cinatra::req_context<>{}); + cinatra::req_context<>{}, std::move(headers)); } // CONNECT example.com HTTP/1.1 - async_simple::coro::Lazy async_http_connect(std::string uri) { + async_simple::coro::Lazy async_http_connect( + std::string uri, + std::unordered_map headers = {}) { return async_request(std::move(uri), cinatra::http_method::CONNECT, - cinatra::req_context<>{}); + cinatra::req_context<>{}, std::move(headers)); } - async_simple::coro::Lazy async_get(std::string uri) { + async_simple::coro::Lazy async_get( + std::string uri, + std::unordered_map headers = {}) { resp_data data{}; #ifdef BENCHMARK_TEST if (!req_str_.empty()) { @@ -462,7 +474,7 @@ class coro_http_client { req_context<> ctx{}; data = co_await async_request(std::move(uri), http_method::GET, - std::move(ctx)); + std::move(ctx), std::move(headers)); #ifdef BENCHMARK_TEST data.total = total_len_; #endif @@ -477,33 +489,41 @@ class coro_http_client { } } - resp_data get(std::string uri) { - return async_simple::coro::syncAwait(async_get(std::move(uri))); + resp_data get(std::string uri, + std::unordered_map headers = {}) { + return async_simple::coro::syncAwait( + async_get(std::move(uri), std::move(headers))); } resp_data post(std::string uri, std::string content, - req_content_type content_type) { - return async_simple::coro::syncAwait( - async_post(std::move(uri), std::move(content), content_type)); + req_content_type content_type, + std::unordered_map headers = {}) { + return async_simple::coro::syncAwait(async_post( + std::move(uri), std::move(content), content_type, std::move(headers))); } async_simple::coro::Lazy async_post( - std::string uri, std::string content, req_content_type content_type) { + std::string uri, std::string content, req_content_type content_type, + std::unordered_map headers = {}) { req_context<> ctx{content_type, "", std::move(content)}; - return async_request(std::move(uri), http_method::POST, std::move(ctx)); + return async_request(std::move(uri), http_method::POST, std::move(ctx), + std::move(headers)); } async_simple::coro::Lazy async_delete( - std::string uri, std::string content, req_content_type content_type) { + std::string uri, std::string content, req_content_type content_type, + std::unordered_map headers = {}) { req_context<> ctx{content_type, "", std::move(content)}; - return async_request(std::move(uri), http_method::DEL, std::move(ctx)); + return async_request(std::move(uri), http_method::DEL, std::move(ctx), + std::move(headers)); } - async_simple::coro::Lazy async_put(std::string uri, - std::string content, - req_content_type content_type) { + async_simple::coro::Lazy async_put( + std::string uri, std::string content, req_content_type content_type, + std::unordered_map headers = {}) { req_context<> ctx{content_type, "", std::move(content)}; - return async_request(std::move(uri), http_method::PUT, std::move(ctx)); + return async_request(std::move(uri), http_method::PUT, std::move(ctx), + std::move(headers)); } bool add_str_part(std::string name, std::string content) { @@ -1135,22 +1155,26 @@ class coro_http_client { req_str.append(ctx.req_str); size_t content_len = ctx.content.size(); - bool should_add = false; + bool should_add_len = false; if (content_len > 0) { - should_add = true; + should_add_len = true; } else { if ((method == http_method::POST || method == http_method::PUT) && ctx.content_type != req_content_type::multipart) { - should_add = true; + should_add_len = true; } } + if (req_headers_.find("Content-Length") != req_headers_.end()) { + should_add_len = false; + } + if (is_chunked) { - should_add = false; + should_add_len = false; } - if (should_add) { + if (should_add_len) { char buf[32]; auto [ptr, ec] = std::to_chars(buf, buf + 32, content_len); req_str.append("Content-Length: ")