locale independent http date header

master
Chunting Gu 5 years ago
parent 427c46a65a
commit 12e3569a83

@ -104,7 +104,7 @@ RequestBuilder& RequestBuilder::AuthToken(const std::string& token) {
RequestBuilder& RequestBuilder::Date() { RequestBuilder& RequestBuilder::Date() {
headers_.push_back(headers::kDate); headers_.push_back(headers::kDate);
headers_.push_back(utility::GetTimestamp()); headers_.push_back(utility::HttpDate());
return *this; return *this;
} }

@ -57,7 +57,7 @@ ResponseBuilder& ResponseBuilder::File(const std::filesystem::path& path,
ResponseBuilder& ResponseBuilder::Date() { ResponseBuilder& ResponseBuilder::Date() {
headers_.push_back(headers::kDate); headers_.push_back(headers::kDate);
headers_.push_back(utility::GetTimestamp()); headers_.push_back(utility::HttpDate());
return *this; return *this;
} }

@ -15,15 +15,38 @@ namespace webcc {
namespace utility { namespace utility {
const std::string& UserAgent() { const std::string& UserAgent() {
static auto s_user_agent = std::string("Webcc/") + WEBCC_VERSION; static const std::string s_user_agent = std::string("Webcc/") + WEBCC_VERSION;
return s_user_agent; return s_user_agent;
} }
std::string GetTimestamp() { std::string HttpDate() {
std::time_t t = std::time(nullptr); std::time_t t = std::time(nullptr);
std::stringstream ss; tm* gmt = std::gmtime(&t);
ss << std::put_time(std::gmtime(&t), "%a, %d %b %Y %H:%M:%S") << " GMT";
return ss.str(); // Either put_time() or strftime() could format the date as expected, but they
// are both locale dependent!
//
// std::stringstream ss;
// ss << std::put_time(gmt, "%a, %d %b %Y %H:%M:%S") << " GMT";
// return ss.str();
//
// char buf[26];
// std::strftime(buf, 26, "%a, %d %b %Y %H:%M:%S", gmt);
static const char* const kDays[7] = { "Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat" };
static const char* const kMonths[12] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
char buf[26];
std::snprintf(buf, 26, "%s, %.2i %s %i %.2i:%.2i:%.2i", kDays[gmt->tm_wday],
gmt->tm_mday, kMonths[gmt->tm_mon], gmt->tm_year + 1900,
gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
return std::string(buf) + " GMT";
} }
std::size_t TellSize(const std::filesystem::path& path) { std::size_t TellSize(const std::filesystem::path& path) {

@ -24,7 +24,7 @@ const std::string& UserAgent();
// Get the timestamp for HTTP Date header field. // Get the timestamp for HTTP Date header field.
// E.g., Wed, 21 Oct 2015 07:28:00 GMT // E.g., Wed, 21 Oct 2015 07:28:00 GMT
// See: https://tools.ietf.org/html/rfc7231#section-7.1.1.2 // See: https://tools.ietf.org/html/rfc7231#section-7.1.1.2
std::string GetTimestamp(); std::string HttpDate();
// Tell the size in bytes of the given file. // Tell the size in bytes of the given file.
// Return kInvalidLength (-1) on failure. // Return kInvalidLength (-1) on failure.

Loading…
Cancel
Save