From 38936bde81b36cc3e81d49ea903d9130cdb079b5 Mon Sep 17 00:00:00 2001 From: antao Date: Mon, 31 Dec 2018 19:59:57 +0800 Subject: [PATCH] Modify Date header strategy --- lib/inc/drogon/utils/Utilities.h | 2 +- lib/src/HttpResponseImpl.cc | 20 ++++++++++---------- lib/src/HttpResponseImpl.h | 3 +++ lib/src/Utilities.cc | 6 +----- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h index 959d6721..04c22da0 100755 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -71,7 +71,7 @@ int gzipDecompress(const char *zdata, const size_t nzdata, * like this:Sun, 06 Nov 1994 08:49:37 GMT * Wed, 12 Sep 2018 09:22:40 GMT */ -char *getHttpFullDate(const trantor::Date &date = trantor::Date::now(), bool *isChanged = nullptr); +char *getHttpFullDate(const trantor::Date &date = trantor::Date::now()); /// Get a formatted string std::string formattedString(const char *format, ...); diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index 34cd6339..94380489 100755 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -366,18 +366,18 @@ std::shared_ptr HttpResponseImpl::renderToString() const { if (_datePos != std::string::npos) { - bool isDateChanged = false; - auto newDate = getHttpFullDate(trantor::Date::now(), &isDateChanged); + std::lock_guard lock(*_httpStringMutex); + auto now = trantor::Date::now(); + bool isDateChanged = ((now.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC) != _httpStringDate); + assert(_httpString); + if (isDateChanged) { - std::lock_guard lock(*_httpStringMutex); - assert(_httpString); - if (isDateChanged) - { - _httpString = std::make_shared(*_httpString); - memcpy(_httpString->data() + _datePos, newDate, strlen(newDate)); - } - return _httpString; + _httpStringDate = now.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC; + auto newDate = getHttpFullDate(now); + _httpString = std::make_shared(*_httpString); + memcpy(_httpString->data() + _datePos, newDate, strlen(newDate)); } + return _httpString; } } auto httpString = std::make_shared(); diff --git a/lib/src/HttpResponseImpl.h b/lib/src/HttpResponseImpl.h index 1036c1cb..d99b5812 100755 --- a/lib/src/HttpResponseImpl.h +++ b/lib/src/HttpResponseImpl.h @@ -325,9 +325,12 @@ class HttpResponseImpl : public HttpResponse mutable std::shared_ptr _jsonPtr; std::shared_ptr _fullHeaderString; + mutable std::shared_ptr _httpString; mutable std::shared_ptr _httpStringMutex; mutable std::string::size_type _datePos = std::string::npos; + mutable uint64_t _httpStringDate = -1; + //trantor::Date receiveTime_; void setContentType(const std::string &contentType) diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index f1a713cd..59d4a674 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -404,19 +404,15 @@ int gzipDecompress(const char *zdata, const size_t nzdata, *ndata = d_stream.total_out; return 0; } -char *getHttpFullDate(const trantor::Date &date, bool *isChanged) +char *getHttpFullDate(const trantor::Date &date) { static __thread int64_t lastSecond = 0; static __thread char lastTimeString[128] = {0}; auto nowSecond = date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC; if (nowSecond == lastSecond) { - if(isChanged) - *isChanged = false; return lastTimeString; } - if(isChanged) - *isChanged = true; lastSecond = nowSecond; date.toCustomedFormattedString("%a, %d %b %Y %T GMT", lastTimeString, sizeof(lastTimeString)); return lastTimeString;