From 683d2cb48c4f3620fc3be68ba97b2d3bb5a40e30 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 25 Apr 2020 18:36:54 +0200 Subject: Added statistics --- response.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'response.cpp') diff --git a/response.cpp b/response.cpp index e1b6c05..c5ba426 100644 --- a/response.cpp +++ b/response.cpp @@ -246,6 +246,30 @@ response_type HttpStatus(std::string status, std::string message, response_type& return res; } +// Do statistics at end of response generation, handle all exit paths via RAII +class StatisticsGuard +{ + request_type& mReq; + response_type& mRes; + Server& mServer; +public: + StatisticsGuard(request_type& req, response_type& res, Server& server) + : mReq(req) + , mRes(res) + , mServer(server) + { + } + + ~StatisticsGuard() + { + mServer.GetStatistics().count(mReq.body().size(), + mRes.body().size(), + mRes.result_int() == 200, + is_ipv6_address(mServer.GetSocket().address), + mServer.GetSocket().protocol == SocketProtocol::HTTPS); + } +}; + } // anonymous namespace response_type generate_response(request_type& req, Server& server) @@ -255,6 +279,8 @@ response_type generate_response(request_type& req, Server& server) res.set(http::field::content_type, mime_type(extend_index_html(std::string(req.target())))); res.keep_alive(req.keep_alive()); + StatisticsGuard statsGuard{req, res, server}; + try { RequestContext req_ctx{req, server}; // can throw std::out_of_range @@ -275,11 +301,8 @@ response_type generate_response(request_type& req, Server& server) std::string password{authorization.substr(pos + 1)}; auto it {auth.find(login)}; - if (it == auth.end()) + if (it == auth.end() || it->second != password) return HttpStatus("401", "Bad Authorization", res); - - if (it->second != password) - return HttpStatus("401", "Bad Authorization", res); // should be same message as previous one to prevent login guessing } plugin_type plugin{req_ctx.GetPlugin()}; -- cgit v1.2.3