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; 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