# HG changeset patch # User Kim Alvefur # Date 1522409743 -7200 # Node ID 678be8ea4d386598b2935dd37b47908fa6618e52 # Parent 557c976735e15ffb88f19ed5dcf6e508bed355bb mod_http_logging: Factor out body length calculation diff -r 557c976735e1 -r 678be8ea4d38 mod_http_logging/mod_http_logging.lua --- a/mod_http_logging/mod_http_logging.lua Fri Mar 30 13:34:15 2018 +0200 +++ b/mod_http_logging/mod_http_logging.lua Fri Mar 30 13:35:43 2018 +0200 @@ -14,14 +14,20 @@ local server = require "net.http.server"; +local function get_content_len(response, body) + local len = response.headers.content_length; + if len then return len; end + if not body then body = request.body; end + if body then return #tostring(body); end +end + local function log_response(response, body) - body = body or response.body; - local len = response.headers.content_length or (body and #body) or "-"; + local len = tostring(get_content_len(response, body) or "-"); local request = response.request; local ip = request.conn:ip(); local req = string.format("%s %s HTTP/%s", request.method, request.path, request.httpversion); local date = os.date("%d/%m/%Y:%H:%M:%S %z"); - module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, tostring(len)); + module:log("info", "%s - - [%s] \"%s\" %d %s", ip, date, req, response.status_code, len); end local send_response = server.send_response;