Use std::tm instead of boost

This commit is contained in:
Vladislav Oleshko 2022-04-17 00:02:18 +03:00 committed by Farook Al-Sammarraie
parent 988926660e
commit a01e39cf5d
2 changed files with 8 additions and 16 deletions

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <boost/date_time.hpp> #include <iomanip>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
#include "crow/http_request.h" #include "crow/http_request.h"
@ -54,8 +54,7 @@ namespace crow
std::string format() const std::string format() const
{ {
const static std::string DIVIDER = "; "; const static std::string DIVIDER = "; ";
const static auto* HTTP_FACET = const static char* HTTP_DATE_FORMAT = "%a, %d %b %Y %H:%M:%S GMT";
new boost::posix_time::time_facet("%a, %d %b %Y %H:%M:%S GMT");
std::stringstream ss; std::stringstream ss;
ss << key_ << '='; ss << key_ << '=';
@ -63,8 +62,7 @@ namespace crow
if (expires_at_) if (expires_at_)
{ {
ss << DIVIDER << "Expires="; ss << DIVIDER << "Expires=";
ss.imbue(std::locale(std::locale::classic(), HTTP_FACET)); ss << std::put_time(expires_at_.get_ptr(), HTTP_DATE_FORMAT);
ss << *expires_at_;
} }
if (max_age_) ss << DIVIDER << "Max-Age=" << *max_age_; if (max_age_) ss << DIVIDER << "Max-Age=" << *max_age_;
if (!domain_.empty()) ss << DIVIDER << "Domain=" << domain_; if (!domain_.empty()) ss << DIVIDER << "Domain=" << domain_;
@ -91,22 +89,16 @@ namespace crow
} }
// Expires attribute // Expires attribute
Cookie& expires(const boost::posix_time::ptime& time) Cookie& expires(const std::tm& time)
{ {
expires_at_ = time; expires_at_ = time;
return *this; return *this;
} }
// Max-Age attribute // Max-Age attribute
Cookie& max_age(long long age) Cookie& max_age(long long seconds)
{ {
max_age_ = age; max_age_ = seconds;
return *this;
}
Cookie& max_age(const boost::posix_time::time_duration& dt)
{
max_age_ = dt.seconds();
return *this; return *this;
} }
@ -156,7 +148,7 @@ namespace crow
std::string path_ = ""; std::string path_ = "";
bool secure_ = false; bool secure_ = false;
bool httponly_ = false; bool httponly_ = false;
boost::optional<boost::posix_time::ptime> expires_at_{}; boost::optional<std::tm> expires_at_{};
boost::optional<SameSitePolicy> same_site_{}; boost::optional<SameSitePolicy> same_site_{};
}; };

View File

@ -1542,7 +1542,7 @@ TEST_CASE("middleware_cookieparser_format")
{ {
auto tp = boost::posix_time::time_from_string("2000-11-01 23:59:59.000"); auto tp = boost::posix_time::time_from_string("2000-11-01 23:59:59.000");
auto c = Cookie("key", "value") auto c = Cookie("key", "value")
.expires(tp); .expires(boost::posix_time::to_tm(tp));
auto s = c.format(); auto s = c.format();
CHECK(valid(s, 2)); CHECK(valid(s, 2));
CHECK(s.find("Expires=Wed, 01 Nov 2000 23:59:59 GMT") != std::string::npos); CHECK(s.find("Expires=Wed, 01 Nov 2000 23:59:59 GMT") != std::string::npos);