summaryrefslogtreecommitdiffhomepage
path: root/response.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-25 18:36:54 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-25 18:36:54 +0200
commit683d2cb48c4f3620fc3be68ba97b2d3bb5a40e30 (patch)
tree8c0ae6438f6ec706a170eb41f683d6c91dc4a800 /response.cpp
parentb2c34d03399978a3d0838ee7ed92c760e7908721 (diff)
Added statistics
Diffstat (limited to 'response.cpp')
-rw-r--r--response.cpp31
1 files changed, 27 insertions, 4 deletions
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()};