From e8621536577d1bc1060e293a19beb77f443f7d9c Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Mon, 5 Nov 2018 13:34:18 +0800 Subject: [PATCH] Add Server and Date fields to HTTP header of response. --- webcc/http_response.cc | 7 +++++++ webcc/logger.cc | 24 ++++++++++-------------- webcc/utility.cc | 9 +++++++++ webcc/utility.h | 5 +++++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/webcc/http_response.cc b/webcc/http_response.cc index aec347a..71aecf3 100644 --- a/webcc/http_response.cc +++ b/webcc/http_response.cc @@ -1,5 +1,7 @@ #include "webcc/http_response.h" +#include "webcc/utility.h" + namespace webcc { namespace status_strings { @@ -57,6 +59,11 @@ const std::string& ToString(int status) { void HttpResponse::Make() { start_line_ = status_strings::ToString(status_); + + // NOTE: C++11 requires a space between literal and string macro. + SetHeader("Server", "Webcc/" WEBCC_VERSION); + + SetHeader("Date", GetHttpDateTimestamp()); } HttpResponse HttpResponse::Fault(HttpStatus::Enum status) { diff --git a/webcc/logger.cc b/webcc/logger.cc index d351e80..d5062dd 100644 --- a/webcc/logger.cc +++ b/webcc/logger.cc @@ -6,6 +6,7 @@ #include #include #include +#include // for put_time #include #include #include @@ -115,28 +116,23 @@ void LogInit(const std::string& dir, int modes) { } static std::string GetTimestamp() { - using namespace std::chrono; + auto now = std::chrono::system_clock::now(); + std::time_t t = std::chrono::system_clock::to_time_t(now); - auto now = system_clock::now(); - std::time_t now_c = system_clock::to_time_t(now); - std::tm* now_tm = std::localtime(&now_c); - - char buf[20]; - std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", now_tm); - - std::string timestamp(buf); + std::stringstream ss; + ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S"); - milliseconds milli_seconds = duration_cast( - now.time_since_epoch()); + std::chrono::milliseconds milli_seconds = + std::chrono::duration_cast( + now.time_since_epoch()); std::string micro_seconds_str = std::to_string(milli_seconds.count() % 1000); while (micro_seconds_str.size() < 3) { micro_seconds_str = "0" + micro_seconds_str; } - timestamp.append("."); - timestamp.append(micro_seconds_str); + ss << "." << micro_seconds_str; - return timestamp; + return ss.str(); } void LogWrite(int level, const char* file, int line, const char* format, ...) { diff --git a/webcc/utility.cc b/webcc/utility.cc index ae30880..1c8d676 100644 --- a/webcc/utility.cc +++ b/webcc/utility.cc @@ -1,6 +1,8 @@ #include "webcc/utility.h" #include +#include +#include // for put_time #include #include @@ -55,4 +57,11 @@ std::string EndpointToString(const boost::asio::ip::tcp::endpoint& endpoint) { return ss.str(); } +std::string GetHttpDateTimestamp() { + std::time_t t = std::time(nullptr); + std::stringstream ss; + ss << std::put_time(std::gmtime(&t), "%a, %d %b %Y %H:%M:%S") << " GMT"; + return ss.str(); +} + } // namespace webcc diff --git a/webcc/utility.h b/webcc/utility.h index 324090f..ec643e4 100644 --- a/webcc/utility.h +++ b/webcc/utility.h @@ -22,6 +22,11 @@ void PrintEndpoints( std::string EndpointToString(const boost::asio::ip::tcp::endpoint& endpoint); +// Get the timestamp for HTTP Date header field. +// E.g., Wed, 21 Oct 2015 07:28:00 GMT +// See: https://tools.ietf.org/html/rfc7231#section-7.1.1.2 +std::string GetHttpDateTimestamp(); + } // namespace webcc #endif // WEBCC_UTILITY_H_