From 67e62a743bdbf67fc16ce3b13803ae651bf80297 Mon Sep 17 00:00:00 2001 From: antao Date: Tue, 11 Sep 2018 17:31:10 +0800 Subject: [PATCH] Modify date format(rfc2616-3.3.1) --- lib/inc/drogon/utils/Utilities.h | 3 +++ lib/src/Cookie.cc | 15 ++++++++------- lib/src/HttpResponseImpl.h | 9 ++------- lib/src/Utilities.cc | 7 +++++++ tests/CMakeLists.txt | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h index 9842455b..30bfb5f9 100755 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -14,9 +14,11 @@ #pragma once +#include #include #include #include + namespace drogon{ bool isInteger(const std::string &str); std::string genRandomString(int length); @@ -30,6 +32,7 @@ namespace drogon{ char *zdata, size_t *nzdata); int gzipDecompress(const char *zdata, const size_t nzdata, char *data, size_t *ndata); + std::string getHttpFullDate(const trantor::Date &date); } diff --git a/lib/src/Cookie.cc b/lib/src/Cookie.cc index cc5aa6e0..3af0224f 100755 --- a/lib/src/Cookie.cc +++ b/lib/src/Cookie.cc @@ -13,6 +13,7 @@ */ #include +#include using namespace drogon; const std::string Cookie::cookieString() const { @@ -21,14 +22,14 @@ const std::string Cookie::cookieString() const if(_expiresDate.microSecondsSinceEpoch()>0) { //add expiresDate string - struct tm tm1=_expiresDate.tmStruct(); - char timeBuf[64]; - asctime_r(&tm1,timeBuf); - std::string timeStr(timeBuf); - std::string::size_type len=timeStr.length(); - timeStr =timeStr.substr(0,len-1)+" GMT"; +// struct tm tm1=_expiresDate.tmStruct(); +// char timeBuf[64]; +// asctime_r(&tm1,timeBuf); +// std::string timeStr(timeBuf); +// std::string::size_type len=timeStr.length(); +// timeStr =timeStr.substr(0,len-1)+" GMT"; - ret.append("Expires= ").append(timeStr).append("; "); + ret.append("Expires= ").append(getHttpFullDate(_expiresDate)).append("; "); } if(!_domain.empty()) { diff --git a/lib/src/HttpResponseImpl.h b/lib/src/HttpResponseImpl.h index acec79e1..874dd4f5 100755 --- a/lib/src/HttpResponseImpl.h +++ b/lib/src/HttpResponseImpl.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -50,13 +51,7 @@ namespace drogon _left_body_length(0), _current_chunk_length(0) { - struct tm tm1=trantor::Date::date().tmStruct(); - char timeBuf[64]; - asctime_r(&tm1,timeBuf); - std::string timeStr(timeBuf); - std::string::size_type len=timeStr.length(); - timeStr =timeStr.substr(0,len-1)+" GMT"; - addHeader("Date",timeStr); + _headers["Date"]=getHttpFullDate(trantor::Date::date()); } virtual HttpStatusCode statusCode() override { diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 55abdcce..e4a80b7a 100755 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -303,5 +303,12 @@ namespace drogon{ *ndata = d_stream.total_out; return 0; } + std::string getHttpFullDate(const trantor::Date &date) + { + //rfc2616-3.3.1 + //Full Date format like this:Sun, 06 Nov 1994 08:49:37 GMT + // Wed, 12 Sep 2018 09:22:40 GMT + return date.toCustomedFormattedString("%a, %d %b %Y %T GMT"); + } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 18f91c34..afafff33 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,4 @@ -link_libraries(drogon trantor pthread) +link_libraries(drogon trantor uuid pthread jsoncpp dl z) add_executable(cache_map_test CacheMapTest.cc) add_executable(cookies_test CookiesTest.cc)