mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
added HTTP Date header
This commit is contained in:
parent
de7a70de34
commit
bc6140a1eb
77
datetime.h
Normal file
77
datetime.h
Normal file
@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <boost/date_time/local_time/local_time.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace flask
|
||||
{
|
||||
// code from http://stackoverflow.com/questions/2838524/use-boost-date-time-to-parse-and-create-http-dates
|
||||
class DateTime
|
||||
{
|
||||
public:
|
||||
DateTime()
|
||||
: m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr()))
|
||||
{
|
||||
}
|
||||
DateTime(const std::string& path)
|
||||
: DateTime()
|
||||
{
|
||||
from_file(path);
|
||||
}
|
||||
|
||||
// return datetime string
|
||||
std::string str()
|
||||
{
|
||||
std::string result;
|
||||
boost::local_time::local_time_facet* lf(new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT"));
|
||||
try
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss.imbue(std::locale(ss.getloc(), lf));
|
||||
ss << m_dt;
|
||||
result = ss.str();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// update datetime from file mod date
|
||||
std::string from_file(const std::string& path)
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::filesystem::path p(path);
|
||||
boost::posix_time::ptime pt = boost::posix_time::from_time_t(
|
||||
boost::filesystem::last_write_time(p));
|
||||
m_dt = boost::local_time::local_date_time(pt, boost::local_time::time_zone_ptr());
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
}
|
||||
return str();
|
||||
}
|
||||
|
||||
// parse datetime string
|
||||
void parse(const std::string& dt)
|
||||
{
|
||||
boost::local_time::local_time_input_facet* lif(new boost::local_time::local_time_input_facet("%a, %d %b %Y %H:%M:%S GMT"));
|
||||
std::stringstream ss(dt);
|
||||
ss.imbue(std::locale(std::locale::classic(), lif));
|
||||
ss >> m_dt;
|
||||
}
|
||||
|
||||
// boolean equal operator
|
||||
friend bool operator==(const DateTime& left, const DateTime& right)
|
||||
{
|
||||
return (left.m_dt == right.m_dt);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::local_time::local_date_time m_dt;
|
||||
};
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include "parser.h"
|
||||
#include "http_response.h"
|
||||
#include "datetime.h"
|
||||
|
||||
namespace flask
|
||||
{
|
||||
@ -112,6 +113,12 @@ namespace flask
|
||||
}
|
||||
if (!has_date)
|
||||
{
|
||||
std::string date_str = DateTime().str();
|
||||
auto ret = res.headers.emplace("Date", date_str);
|
||||
buffers_.emplace_back(ret.first->first.data(), ret.first->first.size());
|
||||
buffers_.emplace_back(seperator.data(), seperator.size());
|
||||
buffers_.emplace_back(ret.first->second.data(), ret.first->second.size());
|
||||
buffers_.emplace_back(crlf.data(), crlf.size());
|
||||
}
|
||||
|
||||
buffers_.emplace_back(crlf.data(), crlf.size());
|
||||
|
Loading…
Reference in New Issue
Block a user