Browse Source

Log the response length for static files in the docker server

The access log took $body_bytes_sent from res.body, which stays empty
for a static file because it is sent by a content provider. Every file
was logged as 0 bytes. Use the Content-Length of the response instead,
and 0 for HEAD.
yhirose 1 day ago
parent
commit
f6aee694e2
1 changed files with 3 additions and 1 deletions
  1. 3 1
      docker/main.cc

+ 3 - 1
docker/main.cc

@@ -76,7 +76,9 @@ void nginx_access_logger(const Request &req, const Response &res) {
   // the percent-decoded req.path.
   auto request = std::format("{} {} {}", req.method, req.target, req.version);
   auto status = res.status;
-  auto body_bytes_sent = res.body.size();
+  // A static file is sent by a content provider, which leaves res.body empty.
+  auto body_bytes_sent =
+      req.method == "HEAD" ? 0 : res.get_header_value_u64("Content-Length");
   auto http_referer = req.get_header_value("Referer");
   if (http_referer.empty()) http_referer = "-";
   auto http_user_agent = req.get_header_value("User-Agent");