Modify Date header strategy

This commit is contained in:
antao 2018-12-31 19:59:57 +08:00
parent 7c15196bac
commit 38936bde81
4 changed files with 15 additions and 16 deletions

View File

@ -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, ...);

View File

@ -366,18 +366,18 @@ std::shared_ptr<std::string> HttpResponseImpl::renderToString() const
{
if (_datePos != std::string::npos)
{
bool isDateChanged = false;
auto newDate = getHttpFullDate(trantor::Date::now(), &isDateChanged);
std::lock_guard<std::mutex> lock(*_httpStringMutex);
auto now = trantor::Date::now();
bool isDateChanged = ((now.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC) != _httpStringDate);
assert(_httpString);
if (isDateChanged)
{
std::lock_guard<std::mutex> lock(*_httpStringMutex);
assert(_httpString);
if (isDateChanged)
{
_httpString = std::make_shared<std::string>(*_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<std::string>(*_httpString);
memcpy(_httpString->data() + _datePos, newDate, strlen(newDate));
}
return _httpString;
}
}
auto httpString = std::make_shared<std::string>();

View File

@ -325,9 +325,12 @@ class HttpResponseImpl : public HttpResponse
mutable std::shared_ptr<Json::Value> _jsonPtr;
std::shared_ptr<std::string> _fullHeaderString;
mutable std::shared_ptr<std::string> _httpString;
mutable std::shared_ptr<std::mutex> _httpStringMutex;
mutable std::string::size_type _datePos = std::string::npos;
mutable uint64_t _httpStringDate = -1;
//trantor::Date receiveTime_;
void setContentType(const std::string &contentType)

View File

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