Add Server and Date fields to HTTP header of response.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
#include "webcc/http_response.h"
|
#include "webcc/http_response.h"
|
||||||
|
|
||||||
|
#include "webcc/utility.h"
|
||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
namespace status_strings {
|
namespace status_strings {
|
||||||
@@ -57,6 +59,11 @@ const std::string& ToString(int status) {
|
|||||||
|
|
||||||
void HttpResponse::Make() {
|
void HttpResponse::Make() {
|
||||||
start_line_ = status_strings::ToString(status_);
|
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) {
|
HttpResponse HttpResponse::Fault(HttpStatus::Enum status) {
|
||||||
|
|||||||
+10
-14
@@ -6,6 +6,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <iomanip> // for put_time
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -115,28 +116,23 @@ void LogInit(const std::string& dir, int modes) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static std::string GetTimestamp() {
|
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::stringstream ss;
|
||||||
std::time_t now_c = system_clock::to_time_t(now);
|
ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
|
||||||
std::tm* now_tm = std::localtime(&now_c);
|
|
||||||
|
|
||||||
char buf[20];
|
std::chrono::milliseconds milli_seconds =
|
||||||
std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", now_tm);
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
now.time_since_epoch());
|
||||||
std::string timestamp(buf);
|
|
||||||
|
|
||||||
milliseconds milli_seconds = duration_cast<milliseconds>(
|
|
||||||
now.time_since_epoch());
|
|
||||||
std::string micro_seconds_str = std::to_string(milli_seconds.count() % 1000);
|
std::string micro_seconds_str = std::to_string(milli_seconds.count() % 1000);
|
||||||
while (micro_seconds_str.size() < 3) {
|
while (micro_seconds_str.size() < 3) {
|
||||||
micro_seconds_str = "0" + micro_seconds_str;
|
micro_seconds_str = "0" + micro_seconds_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp.append(".");
|
ss << "." << micro_seconds_str;
|
||||||
timestamp.append(micro_seconds_str);
|
|
||||||
|
|
||||||
return timestamp;
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogWrite(int level, const char* file, int line, const char* format, ...) {
|
void LogWrite(int level, const char* file, int line, const char* format, ...) {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "webcc/utility.h"
|
#include "webcc/utility.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iomanip> // for put_time
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@@ -55,4 +57,11 @@ std::string EndpointToString(const boost::asio::ip::tcp::endpoint& endpoint) {
|
|||||||
return ss.str();
|
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
|
} // namespace webcc
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ void PrintEndpoints(
|
|||||||
|
|
||||||
std::string EndpointToString(const boost::asio::ip::tcp::endpoint& endpoint);
|
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
|
} // namespace webcc
|
||||||
|
|
||||||
#endif // WEBCC_UTILITY_H_
|
#endif // WEBCC_UTILITY_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user