Add Server and Date fields to HTTP header of response.

master
Chunting Gu 7 years ago
parent 364a37899d
commit e862153657

@ -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) {

@ -6,6 +6,7 @@
#include <chrono>
#include <cstdarg>
#include <ctime>
#include <iomanip> // for put_time
#include <mutex>
#include <sstream>
#include <string>
@ -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<milliseconds>(
now.time_since_epoch());
std::chrono::milliseconds milli_seconds =
std::chrono::duration_cast<std::chrono::milliseconds>(
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, ...) {

@ -1,6 +1,8 @@
#include "webcc/utility.h"
#include <algorithm>
#include <ctime>
#include <iomanip> // for put_time
#include <ostream>
#include <sstream>
@ -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

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

Loading…
Cancel
Save