From 2465027f486a0f1b76d4ac0988ac09c52d1ea62c Mon Sep 17 00:00:00 2001 From: huiyongchen Date: Tue, 20 May 2025 17:46:40 +0800 Subject: [PATCH 1/2] Bugfix: fix compilation failure of the Format function in string_helper.h under C++20 --- trpc/util/string/string_helper.h | 4 ++++ trpc/util/string/string_helper_test.cc | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/trpc/util/string/string_helper.h b/trpc/util/string/string_helper.h index 237382f7..2157ebb1 100644 --- a/trpc/util/string/string_helper.h +++ b/trpc/util/string/string_helper.h @@ -33,7 +33,11 @@ inline std::optional TryParse(const std::string_view& s, const Args&... args) /// @sa: `std::format` template std::string Format(const std::string_view& fmt, const Args&... args) { + #if FMT_VERSION >= 80000 + return fmt::format(fmt::runtime(fmt), args...); + #else return fmt::format(fmt, args...); + #endif } /// @brief `std::string(_view)::starts_with/ends_with` is not available until C++20, so diff --git a/trpc/util/string/string_helper_test.cc b/trpc/util/string/string_helper_test.cc index c9cbe12d..67a6782a 100644 --- a/trpc/util/string/string_helper_test.cc +++ b/trpc/util/string/string_helper_test.cc @@ -271,4 +271,9 @@ TEST(String, ToLower) { ASSERT_EQ("abcd", ToLower("aBCd")); } +TEST(String, FormatTest) { + std::string fmt_str = "this {} a {} project"; + ASSERT_EQ(Format(fmt_str, "is", "trpc-cpp"), "this is a trpc-cpp project"); +} + } // namespace trpc::testing From 9f93f60ef1e16a8573f350773d84cbe414154002 Mon Sep 17 00:00:00 2001 From: huiyongchen Date: Tue, 20 May 2025 19:35:13 +0800 Subject: [PATCH 2/2] BugFix: fix incorrect Content-Length header setting for HEAD method in fiber HTTP server streaming --- trpc/stream/http/http_stream.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/trpc/stream/http/http_stream.cc b/trpc/stream/http/http_stream.cc index c5e15fff..61196f33 100644 --- a/trpc/stream/http/http_stream.cc +++ b/trpc/stream/http/http_stream.cc @@ -93,6 +93,9 @@ Status HttpWriteStream::WriteHeader() { if (content_length_ == kChunked) { response_->SetHeader(http::kHeaderTransferEncoding, http::kTransferEncodingChunked); } + if (response_->IsHeaderOnly()) { + content_length_ = 0; + } response_->SerializeHeaderToString(builder); return ContextStatusToStreamStatus(context_->SendResponse(builder.DestructiveGet()), [&] { state_ |= kHeaderWritten;