asio: Improve date header field precision

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-07 00:20:53 +09:00
parent c3265de625
commit f8182333b4
4 changed files with 19 additions and 28 deletions

View File

@ -36,8 +36,6 @@
#include "asio_server.h" #include "asio_server.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include "asio_server_connection.h" #include "asio_server_connection.h"
#include "util.h" #include "util.h"
@ -46,9 +44,7 @@ namespace asio_http2 {
namespace server { namespace server {
server::server(std::size_t io_service_pool_size) server::server(std::size_t io_service_pool_size)
: io_service_pool_(io_service_pool_size), : io_service_pool_(io_service_pool_size) {}
tick_timer_(io_service_pool_.get_io_service(),
boost::posix_time::seconds(1)) {}
boost::system::error_code boost::system::error_code
server::listen_and_serve(boost::system::error_code &ec, server::listen_and_serve(boost::system::error_code &ec,
@ -69,8 +65,6 @@ server::listen_and_serve(boost::system::error_code &ec,
} }
} }
start_timer();
io_service_pool_.run(); io_service_pool_.run();
return ec; return ec;
@ -123,18 +117,6 @@ boost::system::error_code server::bind_and_listen(boost::system::error_code &ec,
return ec; return ec;
} }
std::shared_ptr<std::string> cached_date;
void server::start_timer() {
cached_date = std::make_shared<std::string>(util::http_date(time(nullptr)));
tick_timer_.async_wait([this](const boost::system::error_code &e) {
tick_timer_.expires_at(tick_timer_.expires_at() +
boost::posix_time::seconds(1));
start_timer();
});
}
void server::start_accept(boost::asio::ssl::context &tls_context, void server::start_accept(boost::asio::ssl::context &tls_context,
tcp::acceptor &acceptor, serve_mux &mux) { tcp::acceptor &acceptor, serve_mux &mux) {
auto new_connection = std::make_shared<connection<ssl_socket>>( auto new_connection = std::make_shared<connection<ssl_socket>>(

View File

@ -84,14 +84,10 @@ private:
const std::string &port, const std::string &port,
int backlog); int backlog);
void start_timer();
/// The pool of io_service objects used to perform asynchronous /// The pool of io_service objects used to perform asynchronous
/// operations. /// operations.
io_service_pool io_service_pool_; io_service_pool io_service_pool_;
boost::asio::deadline_timer tick_timer_;
/// Acceptor used to listen for incoming connections. /// Acceptor used to listen for incoming connections.
std::vector<tcp::acceptor> acceptors_; std::vector<tcp::acceptor> acceptors_;

View File

@ -41,8 +41,6 @@ namespace asio_http2 {
namespace server { namespace server {
extern std::shared_ptr<std::string> cached_date;
namespace { namespace {
int stream_error(nghttp2_session *session, int32_t stream_id, int stream_error(nghttp2_session *session, int32_t stream_id,
uint32_t error_code) { uint32_t error_code) {
@ -229,10 +227,21 @@ int on_frame_not_send_callback(nghttp2_session *session,
http2_handler::http2_handler(boost::asio::io_service &io_service, http2_handler::http2_handler(boost::asio::io_service &io_service,
connection_write writefun, serve_mux &mux) connection_write writefun, serve_mux &mux)
: writefun_(writefun), mux_(mux), io_service_(io_service), : writefun_(writefun), mux_(mux), io_service_(io_service),
session_(nullptr), buf_(nullptr), buflen_(0), inside_callback_(false) {} session_(nullptr), buf_(nullptr), buflen_(0), inside_callback_(false),
tstamp_cached_(time(nullptr)),
formatted_date_(util::http_date(tstamp_cached_)) {}
http2_handler::~http2_handler() { nghttp2_session_del(session_); } http2_handler::~http2_handler() { nghttp2_session_del(session_); }
const std::string &http2_handler::http_date() {
auto t = time(nullptr);
if (t != tstamp_cached_) {
tstamp_cached_ = t;
formatted_date_ = util::http_date(t);
}
return formatted_date_;
}
int http2_handler::start() { int http2_handler::start() {
int rv; int rv;
@ -317,9 +326,9 @@ int http2_handler::start_response(stream &strm) {
auto nva = std::vector<nghttp2_nv>(); auto nva = std::vector<nghttp2_nv>();
nva.reserve(2 + header.size()); nva.reserve(2 + header.size());
auto status = util::utos(res.status_code()); auto status = util::utos(res.status_code());
auto date = cached_date; auto date = http_date();
nva.push_back(nghttp2::http2::make_nv_ls(":status", status)); nva.push_back(nghttp2::http2::make_nv_ls(":status", status));
nva.push_back(nghttp2::http2::make_nv_ls("date", *date)); nva.push_back(nghttp2::http2::make_nv_ls("date", date));
for (auto &hd : header) { for (auto &hd : header) {
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value, nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
hd.second.sensitive)); hd.second.sensitive));

View File

@ -87,6 +87,8 @@ public:
boost::asio::io_service &io_service(); boost::asio::io_service &io_service();
const std::string &http_date();
template <size_t N> template <size_t N>
int on_read(const boost::array<uint8_t, N> &buffer, std::size_t len) { int on_read(const boost::array<uint8_t, N> &buffer, std::size_t len) {
callback_guard cg(*this); callback_guard cg(*this);
@ -152,6 +154,8 @@ private:
const uint8_t *buf_; const uint8_t *buf_;
std::size_t buflen_; std::size_t buflen_;
bool inside_callback_; bool inside_callback_;
time_t tstamp_cached_;
std::string formatted_date_;
}; };
} // namespace server } // namespace server