Skip to content

Commit

Permalink
rpcdaemon: add Date header in HTTP response (#1779)
Browse files Browse the repository at this point in the history
Co-authored-by: canepat <[email protected]>
  • Loading branch information
lupin012 and canepat committed Jan 29, 2024
1 parent d97dac5 commit ef30fcd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions silkworm/rpc/http/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/asio/use_awaitable.hpp>
#include <boost/asio/write.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <jwt-cpp/jwt.h>
#include <jwt-cpp/traits/nlohmann-json/defaults.h>

Expand Down Expand Up @@ -126,6 +127,7 @@ Task<void> Connection::open_stream() {
try {
boost::beast::http::response<boost::beast::http::empty_body> rsp{boost::beast::http::status::ok, request_http_version_};
rsp.set(boost::beast::http::field::content_type, "application/json");
rsp.set(boost::beast::http::field::date, get_date_time());
rsp.chunked(true);

set_cors<boost::beast::http::empty_body>(rsp);
Expand Down Expand Up @@ -165,6 +167,8 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
SILK_TRACE << "Connection::do_write response: " << content;
boost::beast::http::response<boost::beast::http::string_body> res{http_status, request_http_version_};
res.set(boost::beast::http::field::content_type, "application/json");
res.set(boost::beast::http::field::date, get_date_time());
res.erase(boost::beast::http::field::host);
res.keep_alive(request_keep_alive_);
res.content_length(content.size());
res.body() = content;
Expand Down Expand Up @@ -241,4 +245,13 @@ void Connection::set_cors(boost::beast::http::response<Body>& res) {
res.set("Access-Control-Max-Age", "600");
}

std::string Connection::get_date_time() {
static const absl::TimeZone tz{absl::LocalTimeZone()};
const absl::Time now{absl::Now()};

std::stringstream ss;
ss << absl::FormatTime("%a, %d %b %E4Y %H:%M:%S ", now, tz) << tz.name();
return ss.str();
}

} // namespace silkworm::rpc::http
2 changes: 2 additions & 0 deletions silkworm/rpc/http/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Connection : public Channel {
//! Perform an asynchronous write operation.
Task<void> do_write(const std::string& content, boost::beast::http::status http_status = boost::beast::http::status::ok);

static std::string get_date_time();

//! Socket for the connection.
boost::asio::ip::tcp::socket socket_;

Expand Down

0 comments on commit ef30fcd

Please sign in to comment.